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
25 #include "user.h" /* for user_checkaction */
30 static void set_var(nut_ctype_t
*client
, const char *upsname
, const char *var
,
37 char cmd
[SMALLBUF
], esc
[SMALLBUF
];
39 ups
= get_ups_ptr(upsname
);
42 send_err(client
, NUT_ERR_UNKNOWN_UPS
);
46 if (!ups_available(ups
, client
))
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
);
55 val
= sstate_getinfo(ups
, var
);
58 send_err(client
, NUT_ERR_VAR_NOT_SUPPORTED
);
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
);
68 /* see if the new value is allowed for this variable */
70 if (sstate_getflags(ups
, var
) & ST_FLAG_STRING
) {
73 aux
= sstate_getaux(ups
, var
);
75 /* check for insanity from the driver */
77 upslogx(LOG_WARNING
, "UPS [%s]: auxdata for %s is invalid",
80 send_err(client
, NUT_ERR_SET_FAILED
);
84 if (aux
< (int) strlen(newval
)) {
85 send_err(client
, NUT_ERR_TOO_LONG
);
90 /* see if it's enumerated */
92 etmp
= sstate_getenumlist(ups
, var
);
98 if (!strcmp(etmp
->val
, newval
)) {
107 send_err(client
, NUT_ERR_INVALID_VALUE
);
112 /* or if it's within a range */
114 rtmp
= sstate_getrangelist(ups
, var
);
118 int inewval
= atoi(newval
);
121 if ((inewval
>= rtmp
->min
) && (inewval
<= rtmp
->max
)) {
130 send_err(client
, NUT_ERR_INVALID_VALUE
);
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
);
149 sendback(client
, "OK\n");
152 void net_set(nut_ctype_t
*client
, int numarg
, const char **arg
)
155 send_err(client
, NUT_ERR_INVALID_ARGUMENT
);
159 /* SET VAR UPS VARNAME VALUE */
160 if (!strcasecmp(arg
[0], "VAR")) {
161 set_var(client
, arg
[1], arg
[2], arg
[3]);
165 send_err(client
, NUT_ERR_INVALID_ARGUMENT
);