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

« back to all changes in this revision

Viewing changes to server/netuser.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2005-07-20 19:48:50 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050720194850-oo61wjr33rrx2mre
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* netuser.c - LOGIN/LOGOUT/USERNAME/PASSWORD/MASTER handlers 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 "upsd.h"
 
23
#include "sstate.h"
 
24
#include "state.h"
 
25
#include "neterr.h"
 
26
#include "user.h"               /* for user_checkaction */
 
27
 
 
28
#include "netuser.h"
 
29
 
 
30
/* LOGIN <ups> */
 
31
void net_login(ctype *client, int numarg, const char **arg)
 
32
{
 
33
        upstype *ups;
 
34
 
 
35
        if (numarg != 1) {
 
36
                send_err(client, NUT_ERR_INVALID_ARGUMENT);
 
37
                return;
 
38
        }
 
39
 
 
40
        if (client->loginups != NULL) {
 
41
 
 
42
                if (client->username)
 
43
                        upslogx(LOG_INFO, "Client %s@%s tried to login twice",
 
44
                                client->username, client->addr);
 
45
                else
 
46
                        upslogx(LOG_INFO, "Client on %s tried to login twice",
 
47
                                client->addr);
 
48
 
 
49
                send_err(client, NUT_ERR_ALREADY_LOGGED_IN);
 
50
                return;
 
51
        }
 
52
 
 
53
        /* make sure we got a valid UPS name */
 
54
        ups = get_ups_ptr(arg[0]);
 
55
 
 
56
        if (!ups) {
 
57
                send_err(client, NUT_ERR_UNKNOWN_UPS);
 
58
                return;
 
59
        }
 
60
 
 
61
        ups->numlogins++;
 
62
        client->loginups = xstrdup(ups->name);
 
63
 
 
64
        upslogx(LOG_INFO, "Client %s@%s logged into UPS [%s]%s",
 
65
                client->username, client->addr, client->loginups,
 
66
                client->ssl ? " (SSL)" : "");
 
67
 
 
68
        sendback(client, "OK\n");
 
69
}
 
70
 
 
71
void net_logout(ctype *client, int numarg, const char **arg)
 
72
{
 
73
        if (numarg != 0) {
 
74
                send_err(client, NUT_ERR_INVALID_ARGUMENT);
 
75
                return;
 
76
        }
 
77
 
 
78
        if (client->username)
 
79
                upslogx(LOG_INFO, "Client %s@%s logged out",
 
80
                        client->username, client->addr);
 
81
        else
 
82
                upslogx(LOG_INFO, "Client on %s logged out", client->addr);
 
83
 
 
84
        sendback(client, "OK Goodbye\n");
 
85
        client->delete = 1;
 
86
}
 
87
 
 
88
/* MASTER <upsname> */
 
89
void net_master(ctype *client, int numarg, const char **arg)
 
90
{
 
91
        upstype *ups;
 
92
 
 
93
        if (numarg != 1) {
 
94
                send_err(client, NUT_ERR_INVALID_ARGUMENT);
 
95
                return;
 
96
        }
 
97
        
 
98
        ups = get_ups_ptr(arg[0]);
 
99
 
 
100
        if (!ups) {
 
101
                send_err(client, NUT_ERR_UNKNOWN_UPS);
 
102
                return;
 
103
        }
 
104
 
 
105
        /* make sure this user is allowed to do MASTER */
 
106
        if (!user_checkaction(&client->sock, client->username,
 
107
                client->password, "MASTER")) {
 
108
 
 
109
                send_err(client, NUT_ERR_ACCESS_DENIED);
 
110
                return;
 
111
        }               
 
112
 
 
113
        /* this is just an access level check */
 
114
        sendback(client, "OK MASTER-GRANTED\n");
 
115
}
 
116
 
 
117
/* USERNAME <username> */
 
118
void net_username(ctype *client, int numarg, const char **arg)
 
119
{
 
120
        if (numarg != 1) {
 
121
                send_err(client, NUT_ERR_INVALID_ARGUMENT);
 
122
                return;
 
123
        }
 
124
        
 
125
        if (client->username != NULL) {
 
126
                upslogx(LOG_INFO, "Client %s@%s tried to set a username twice",
 
127
                        client->username, client->addr);
 
128
 
 
129
                send_err(client, NUT_ERR_ALREADY_SET_USERNAME);
 
130
                return;
 
131
        }
 
132
 
 
133
        client->username = xstrdup(arg[0]);
 
134
        sendback(client, "OK\n");
 
135
}
 
136
 
 
137
/* PASSWORD <password> */
 
138
void net_password(ctype *client, int numarg, const char **arg)
 
139
{
 
140
        if (numarg != 1) {
 
141
                send_err(client, NUT_ERR_INVALID_ARGUMENT);
 
142
                return;
 
143
        }
 
144
 
 
145
        if (client->password != NULL) {
 
146
                if (client->username)
 
147
                        upslogx(LOG_INFO, "Client %s@%s tried to set a password twice",
 
148
                                client->username, client->addr);
 
149
                else
 
150
                        upslogx(LOG_INFO, "Client on %s tried to set a password twice",
 
151
                                client->addr);
 
152
 
 
153
                send_err(client, NUT_ERR_ALREADY_SET_PASSWORD);
 
154
                return;
 
155
        }
 
156
 
 
157
        client->password = xstrdup(arg[0]);
 
158
        sendback(client, "OK\n");
 
159
}