Mention upsrw output change.
[networkupstools/kirr.git] / server / netset.c
blobb88bb40f7dabba581e56c023e64c1a2db7f56498
1 /* netset.c - SET handler for upsd
3 Copyright (C) 2003 Russell Kroll <rkroll@exploits.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "common.h"
22 #include "upsd.h"
23 #include "sstate.h"
24 #include "state.h"
25 #include "user.h" /* for user_checkaction */
26 #include "neterr.h"
28 #include "netset.h"
30 static void set_var(nut_ctype_t *client, const char *upsname, const char *var,
31 const char *newval)
33 upstype_t *ups;
34 const char *val;
35 const enum_t *etmp;
36 const range_t *rtmp;
37 char cmd[SMALLBUF], esc[SMALLBUF];
39 ups = get_ups_ptr(upsname);
41 if (!ups) {
42 send_err(client, NUT_ERR_UNKNOWN_UPS);
43 return;
46 if (!ups_available(ups, client))
47 return;
49 /* make sure this user is allowed to do SET */
50 if (!user_checkaction(client->username, client->password, "SET")) {
51 send_err(client, NUT_ERR_ACCESS_DENIED);
52 return;
55 val = sstate_getinfo(ups, var);
57 if (!val) {
58 send_err(client, NUT_ERR_VAR_NOT_SUPPORTED);
59 return;
62 /* make sure this variable is writable (RW) */
63 if ((sstate_getflags(ups, var) & ST_FLAG_RW) == 0) {
64 send_err(client, NUT_ERR_READONLY);
65 return;
68 /* see if the new value is allowed for this variable */
70 if (sstate_getflags(ups, var) & ST_FLAG_STRING) {
71 int aux;
73 aux = sstate_getaux(ups, var);
75 /* check for insanity from the driver */
76 if (aux < 1) {
77 upslogx(LOG_WARNING, "UPS [%s]: auxdata for %s is invalid",
78 ups->name, var);
80 send_err(client, NUT_ERR_SET_FAILED);
81 return;
84 if (aux < (int) strlen(newval)) {
85 send_err(client, NUT_ERR_TOO_LONG);
86 return;
90 /* see if it's enumerated */
92 etmp = sstate_getenumlist(ups, var);
94 if (etmp) {
95 int found = 0;
97 while (etmp) {
98 if (!strcmp(etmp->val, newval)) {
99 found = 1;
100 break;
103 etmp = etmp->next;
106 if (!found) {
107 send_err(client, NUT_ERR_INVALID_VALUE);
108 return;
112 /* or if it's within a range */
114 rtmp = sstate_getrangelist(ups, var);
116 if (rtmp) {
117 int found = 0;
118 int inewval = atoi(newval);
120 while (rtmp) {
121 if ((inewval >= rtmp->min) && (inewval <= rtmp->max)) {
122 found = 1;
123 break;
126 rtmp = rtmp->next;
129 if (!found) {
130 send_err(client, NUT_ERR_INVALID_VALUE);
131 return;
135 /* must be OK now */
137 upslogx(LOG_INFO, "Set variable: %s@%s set %s on %s to %s",
138 client->username, client->addr, var, ups->name, newval);
140 snprintf(cmd, sizeof(cmd), "SET %s \"%s\"\n",
141 var, pconf_encode(newval, esc, sizeof(esc)));
143 if (!sstate_sendline(ups, cmd)) {
144 upslogx(LOG_INFO, "Set command send failed");
145 send_err(client, NUT_ERR_SET_FAILED);
146 return;
149 sendback(client, "OK\n");
152 void net_set(nut_ctype_t *client, int numarg, const char **arg)
154 if (numarg < 4) {
155 send_err(client, NUT_ERR_INVALID_ARGUMENT);
156 return;
159 /* SET VAR UPS VARNAME VALUE */
160 if (!strcasecmp(arg[0], "VAR")) {
161 set_var(client, arg[1], arg[2], arg[3]);
162 return;
165 send_err(client, NUT_ERR_INVALID_ARGUMENT);
166 return;