1 /* netuser.c - LOGIN/LOGOUT/USERNAME/PASSWORD/MASTER[PRIMARY] handlers 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
26 #include "user.h" /* for user_checkaction */
31 void net_login(nut_ctype_t
*client
, size_t numarg
, const char **arg
)
36 send_err(client
, NUT_ERR_INVALID_ARGUMENT
);
40 if (client
->loginups
!= NULL
) {
41 upslogx(LOG_INFO
, "Client %s@%s tried to login twice", client
->username
, client
->addr
);
42 send_err(client
, NUT_ERR_ALREADY_LOGGED_IN
);
46 /* make sure we got a valid UPS name */
47 ups
= get_ups_ptr(arg
[0]);
50 send_err(client
, NUT_ERR_UNKNOWN_UPS
);
54 /* make sure this is a valid user */
55 if (!user_checkaction(client
->username
, client
->password
, "LOGIN")) {
56 upsdebugx(3, "%s: not a valid user: %s",
57 __func__
, client
->username
);
58 send_err(client
, NUT_ERR_ACCESS_DENIED
);
63 client
->loginups
= xstrdup(ups
->name
);
65 upslogx(LOG_INFO
, "User %s@%s logged into UPS [%s]%s", client
->username
, client
->addr
,
66 client
->loginups
, client
->ssl
? " (SSL)" : "");
67 sendback(client
, "OK\n");
70 void net_logout(nut_ctype_t
*client
, size_t numarg
, const char **arg
)
72 NUT_UNUSED_VARIABLE(arg
);
74 send_err(client
, NUT_ERR_INVALID_ARGUMENT
);
78 if (client
->loginups
!= NULL
) {
79 upslogx(LOG_INFO
, "User %s@%s logged out from UPS [%s]%s", client
->username
, client
->addr
,
80 client
->loginups
, client
->ssl
? " (SSL)" : "");
83 sendback(client
, "OK Goodbye\n");
85 client
->last_heard
= 0;
88 /* NOTE: Protocol updated since NUT 2.8.0 to handle master/primary
89 * and API bumped, to rename/alias the routine.
91 static int do_net_primary(nut_ctype_t
*client
, size_t numarg
, const char **arg
)
96 send_err(client
, NUT_ERR_INVALID_ARGUMENT
);
100 ups
= get_ups_ptr(arg
[0]);
103 send_err(client
, NUT_ERR_UNKNOWN_UPS
);
107 /* make sure this user is allowed to do PRIMARY or MASTER */
108 if (!user_checkaction(client
->username
, client
->password
, "PRIMARY")
109 && !user_checkaction(client
->username
, client
->password
, "MASTER")
111 send_err(client
, NUT_ERR_ACCESS_DENIED
);
115 /* this is just an access level check */
116 /* sendback() will be worded by caller below */
120 /* MASTER <upsname> (deprecated) */
121 void net_master(nut_ctype_t
*client
, size_t numarg
, const char **arg
) {
122 /* Allow existing binaries linked against this file to still work */
124 "WARNING: Client %s@%s "
125 "requested MASTER level for device %s - "
126 "which is deprecated in favor of PRIMARY "
128 client
->username
, client
->addr
,
129 (numarg
> 0) ? arg
[0] : "<null>");
131 if (0 == do_net_primary(client
, numarg
, arg
)) {
132 sendback(client
, "OK MASTER-GRANTED\n");
136 /* PRIMARY <upsname> (since NUT 2.8.0) */
137 void net_primary(nut_ctype_t
*client
, size_t numarg
, const char **arg
)
139 if (0 == do_net_primary(client
, numarg
, arg
)) {
140 sendback(client
, "OK PRIMARY-GRANTED\n");
144 /* USERNAME <username> */
145 void net_username(nut_ctype_t
*client
, size_t numarg
, const char **arg
)
148 send_err(client
, NUT_ERR_INVALID_ARGUMENT
);
152 if (client
->username
!= NULL
) {
153 upslogx(LOG_INFO
, "Client %s@%s tried to set a username twice",
154 client
->username
, client
->addr
);
156 send_err(client
, NUT_ERR_ALREADY_SET_USERNAME
);
160 client
->username
= xstrdup(arg
[0]);
161 sendback(client
, "OK\n");
164 /* PASSWORD <password> */
165 void net_password(nut_ctype_t
*client
, size_t numarg
, const char **arg
)
168 send_err(client
, NUT_ERR_INVALID_ARGUMENT
);
172 if (client
->password
!= NULL
) {
173 if (client
->username
)
174 upslogx(LOG_INFO
, "Client %s@%s tried to set a password twice",
175 client
->username
, client
->addr
);
177 upslogx(LOG_INFO
, "Client on %s tried to set a password twice",
180 send_err(client
, NUT_ERR_ALREADY_SET_PASSWORD
);
184 client
->password
= xstrdup(arg
[0]);
185 sendback(client
, "OK\n");