Merge pull request #2680 from masterwishx/work2471-eco_addon
[networkupstools.git] / server / netuser.c
blob6f4c677688c858dd2dcd2a2b2e511ffde68fc794
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
20 #include "common.h"
22 #include "upsd.h"
23 #include "sstate.h"
24 #include "state.h"
25 #include "neterr.h"
26 #include "user.h" /* for user_checkaction */
28 #include "netuser.h"
30 /* LOGIN <ups> */
31 void net_login(nut_ctype_t *client, size_t numarg, const char **arg)
33 upstype_t *ups;
35 if (numarg != 1) {
36 send_err(client, NUT_ERR_INVALID_ARGUMENT);
37 return;
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);
43 return;
46 /* make sure we got a valid UPS name */
47 ups = get_ups_ptr(arg[0]);
49 if (!ups) {
50 send_err(client, NUT_ERR_UNKNOWN_UPS);
51 return;
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);
59 return;
62 ups->numlogins++;
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);
73 if (numarg != 0) {
74 send_err(client, NUT_ERR_INVALID_ARGUMENT);
75 return;
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)
93 upstype_t *ups;
95 if (numarg != 1) {
96 send_err(client, NUT_ERR_INVALID_ARGUMENT);
97 return -1;
100 ups = get_ups_ptr(arg[0]);
102 if (!ups) {
103 send_err(client, NUT_ERR_UNKNOWN_UPS);
104 return -1;
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);
112 return -1;
115 /* this is just an access level check */
116 /* sendback() will be worded by caller below */
117 return 0;
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 */
123 upsdebugx(1,
124 "WARNING: Client %s@%s "
125 "requested MASTER level for device %s - "
126 "which is deprecated in favor of PRIMARY "
127 "since NUT 2.8.0",
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)
147 if (numarg != 1) {
148 send_err(client, NUT_ERR_INVALID_ARGUMENT);
149 return;
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);
157 return;
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)
167 if (numarg != 1) {
168 send_err(client, NUT_ERR_INVALID_ARGUMENT);
169 return;
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);
176 else
177 upslogx(LOG_INFO, "Client on %s tried to set a password twice",
178 client->addr);
180 send_err(client, NUT_ERR_ALREADY_SET_PASSWORD);
181 return;
184 client->password = xstrdup(arg[0]);
185 sendback(client, "OK\n");