nut/server/netinstcmd.c

139 lines
3.5 KiB
C
Raw Normal View History

2010-03-25 23:20:59 +00:00
/* netinstcmd.c - INSTCMD handler for upsd
Copyright (C) 2003 Russell Kroll <rkroll@exploits.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "common.h"
#include "upsd.h"
#include "sstate.h"
#include "state.h"
#include "user.h" /* for user_checkinstcmd */
#include "neterr.h"
#include "netinstcmd.h"
2022-06-29 10:37:36 +00:00
static void send_instcmd(nut_ctype_t *client, const char *upsname,
const char *cmdname, const char *value, const char *tracking_id)
2010-03-25 23:20:59 +00:00
{
2022-06-29 10:37:36 +00:00
int found, have_tracking_id = 0;
2010-03-25 23:20:59 +00:00
upstype_t *ups;
2011-01-26 09:35:08 +00:00
const cmdlist_t *ctmp;
2011-06-01 20:31:49 +00:00
char sockcmd[SMALLBUF], esc[SMALLBUF];
2010-03-25 23:20:59 +00:00
ups = get_ups_ptr(upsname);
if (!ups) {
send_err(client, NUT_ERR_UNKNOWN_UPS);
return;
}
if (!ups_available(ups, client))
return;
ctmp = sstate_getcmdlist(ups);
found = 0;
while (ctmp) {
if (!strcasecmp(ctmp->name, cmdname)) {
found = 1;
break;
}
ctmp = ctmp->next;
}
if (!found) {
send_err(client, NUT_ERR_CMD_NOT_SUPPORTED);
return;
}
/* see if this user is allowed to do this command */
if (!user_checkinstcmd(client->username, client->password, cmdname)) {
send_err(client, NUT_ERR_ACCESS_DENIED);
return;
}
2022-06-29 10:37:36 +00:00
/* Format the base command */
snprintf(sockcmd, sizeof(sockcmd), "INSTCMD %s", cmdname);
2010-03-25 23:20:59 +00:00
2022-06-29 10:37:36 +00:00
/* see if the user has also passed a value for this command */
if (value != NULL)
snprintfcat(sockcmd, sizeof(sockcmd), " %s", pconf_encode(value, esc, sizeof(esc)));
/* see if the user want execution tracking for this command */
if (tracking_id && *tracking_id) {
snprintfcat(sockcmd, sizeof(sockcmd), " TRACKING %s", tracking_id);
/* Add an entry in the tracking structure */
tracking_add(tracking_id);
have_tracking_id = 1;
2011-06-01 20:31:49 +00:00
}
2022-06-29 10:37:36 +00:00
/* add EOL */
snprintfcat(sockcmd, sizeof(sockcmd), "\n");
upslogx(LOG_INFO, "Instant command: %s@%s did %s%s%s on %s (tracking ID: %s)",
client->username, client->addr, cmdname,
(value != NULL)?" with value ":"",
(value != NULL)?value:"",
ups->name,
(have_tracking_id) ? tracking_id : "disabled");
2010-03-25 23:20:59 +00:00
if (!sstate_sendline(ups, sockcmd)) {
upslogx(LOG_INFO, "Set command send failed");
send_err(client, NUT_ERR_INSTCMD_FAILED);
return;
}
2022-06-29 10:37:36 +00:00
/* return the result, possibly including tracking_id */
if (have_tracking_id)
sendback(client, "OK TRACKING %s\n", tracking_id);
else
sendback(client, "OK\n");
2010-03-25 23:20:59 +00:00
}
2022-06-29 10:37:36 +00:00
void net_instcmd(nut_ctype_t *client, size_t numarg, const char **arg)
2010-03-25 23:20:59 +00:00
{
2022-06-29 10:37:36 +00:00
const char *devname = NULL;
const char *cmdname = NULL;
const char *cmdparam = NULL;
char tracking_id[UUID4_LEN] = "";
2010-03-25 23:20:59 +00:00
if (numarg < 2) {
send_err(client, NUT_ERR_INVALID_ARGUMENT);
return;
}
2022-06-29 10:37:36 +00:00
/* INSTCMD <ups> <cmdname> [cmdparam] */
/* Check arguments */
devname = arg[0];
cmdname = arg[1];
if (numarg == 3)
cmdparam = arg[2];
if (client->tracking) {
/* Generate a tracking ID, if client requested status tracking */
nut_uuid_v4(tracking_id);
}
send_instcmd(client, devname, cmdname, cmdparam, tracking_id);
2010-03-25 23:20:59 +00:00
return;
}