~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to server/netinstcmd.c

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Quette
  • Date: 2004-05-28 13:10:01 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040528131001-yj2m9qcez4ya2w14
Tags: upstream-1.4.2
ImportĀ upstreamĀ versionĀ 1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* netinstcmd.c - INSTCMD handler for upsd
 
2
 
 
3
   Copyright (C) 2003  Russell Kroll <rkroll@exploits.org>
 
4
 
 
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.
 
9
 
 
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.
 
14
 
 
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
 
18
*/
 
19
 
 
20
#include "common.h"
 
21
 
 
22
#include "upstype.h"
 
23
#include "upsd.h"
 
24
#include "sstate.h"
 
25
#include "state.h"
 
26
 
 
27
#include "user.h"                       /* for user_checkinstcmd */
 
28
#include "neterr.h"
 
29
 
 
30
static void send_instcmd(ctype *client, const char *upsname, 
 
31
        const char *cmdname)
 
32
{
 
33
        int     found;
 
34
        upstype *ups;
 
35
        const   struct  cmdlist_t  *ctmp;
 
36
        char    sockcmd[SMALLBUF];
 
37
 
 
38
        ups = get_ups_ptr(upsname);
 
39
 
 
40
        if (!ups) {
 
41
                send_err(client, NUT_ERR_UNKNOWN_UPS);
 
42
                return;
 
43
        }
 
44
 
 
45
        if (!ups_available(ups, client))
 
46
                return;
 
47
 
 
48
        ctmp = sstate_getcmdlist(ups);
 
49
 
 
50
        found = 0;
 
51
 
 
52
        while (ctmp) {
 
53
                if (!strcasecmp(ctmp->name, cmdname)) {
 
54
                        found = 1;
 
55
                        break;
 
56
                }
 
57
 
 
58
                ctmp = ctmp->next;
 
59
        }
 
60
 
 
61
        if (!found) {
 
62
                send_err(client, NUT_ERR_CMD_NOT_SUPPORTED);
 
63
                return;
 
64
        }
 
65
 
 
66
        /* see if this user is allowed to do this command */
 
67
        if (!user_checkinstcmd(&client->sock, client->username, 
 
68
                client->password, cmdname)) {
 
69
 
 
70
                send_err(client, NUT_ERR_ACCESS_DENIED);
 
71
                return;
 
72
        }
 
73
 
 
74
        upslogx(LOG_INFO, "Instant command: %s@%s did %s on %s",
 
75
               client->username, client->addr, cmdname, 
 
76
               ups->name);
 
77
 
 
78
        snprintf(sockcmd, sizeof(sockcmd), "INSTCMD %s\n", cmdname);
 
79
 
 
80
        if (!sstate_sendline(ups, sockcmd)) {
 
81
                upslogx(LOG_INFO, "Set command send failed");
 
82
                send_err(client, NUT_ERR_INSTCMD_FAILED);
 
83
                return;
 
84
        }
 
85
 
 
86
        sendback(client, "OK\n");
 
87
}
 
88
 
 
89
int net_instcmd(ctype *client, int numarg, char **arg)
 
90
{
 
91
        if (numarg < 2) {
 
92
 
 
93
                /* can't do this until 2.0 - backwards compatibilty */
 
94
                /* send_err(client, NUT_ERR_INVALID_ARGUMENT); */
 
95
 
 
96
                return 0;       /* not handled - fall through */
 
97
        }
 
98
 
 
99
        /* the old way only had one arg, so 2 args is for us */
 
100
 
 
101
        send_instcmd(client, arg[0], arg[1]);
 
102
        return 1;       /* handled */
 
103
}