File: netuser.c

package info (click to toggle)
nut 2.2.2-6.5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,240 kB
  • ctags: 4,918
  • sloc: ansic: 42,528; sh: 10,284; makefile: 860
file content (159 lines) | stat: -rw-r--r-- 3,811 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* netuser.c - LOGIN/LOGOUT/USERNAME/PASSWORD/MASTER handlers 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 "neterr.h"
#include "user.h"		/* for user_checkaction */

#include "netuser.h"

/* LOGIN <ups> */
void net_login(ctype_t *client, int numarg, const char **arg)
{
	upstype_t	*ups;

	if (numarg != 1) {
		send_err(client, NUT_ERR_INVALID_ARGUMENT);
		return;
	}

	if (client->loginups != NULL) {

		if (client->username)
			upslogx(LOG_INFO, "Client %s@%s tried to login twice",
				client->username, client->addr);
		else
			upslogx(LOG_INFO, "Client on %s tried to login twice",
				client->addr);

		send_err(client, NUT_ERR_ALREADY_LOGGED_IN);
		return;
	}

	/* make sure we got a valid UPS name */
	ups = get_ups_ptr(arg[0]);

	if (!ups) {
		send_err(client, NUT_ERR_UNKNOWN_UPS);
		return;
	}

	ups->numlogins++;
	client->loginups = xstrdup(ups->name);

	upslogx(LOG_INFO, "Client %s@%s logged into UPS [%s]%s",
		client->username, client->addr, client->loginups,
		client->ssl ? " (SSL)" : "");

	sendback(client, "OK\n");
}

void net_logout(ctype_t *client, int numarg, const char **arg)
{
	if (numarg != 0) {
		send_err(client, NUT_ERR_INVALID_ARGUMENT);
		return;
	}

	if (client->username)
		upslogx(LOG_DEBUG, "Client %s@%s logged out",
			client->username, client->addr);
	else
		upslogx(LOG_DEBUG, "Client on %s logged out", client->addr);

	sendback(client, "OK Goodbye\n");
	client->delete = 1;
}

/* MASTER <upsname> */
void net_master(ctype_t *client, int numarg, const char **arg)
{
	upstype_t	*ups;

	if (numarg != 1) {
		send_err(client, NUT_ERR_INVALID_ARGUMENT);
		return;
	}
	
	ups = get_ups_ptr(arg[0]);

	if (!ups) {
		send_err(client, NUT_ERR_UNKNOWN_UPS);
		return;
	}

	/* make sure this user is allowed to do MASTER */
	if (!user_checkaction(&client->sock, client->username,
		client->password, "MASTER")) {

		send_err(client, NUT_ERR_ACCESS_DENIED);
		return;
	}		

	/* this is just an access level check */
	sendback(client, "OK MASTER-GRANTED\n");
}

/* USERNAME <username> */
void net_username(ctype_t *client, int numarg, const char **arg)
{
	if (numarg != 1) {
		send_err(client, NUT_ERR_INVALID_ARGUMENT);
		return;
	}
	
	if (client->username != NULL) {
		upslogx(LOG_INFO, "Client %s@%s tried to set a username twice",
			client->username, client->addr);

		send_err(client, NUT_ERR_ALREADY_SET_USERNAME);
		return;
	}

	client->username = xstrdup(arg[0]);
	sendback(client, "OK\n");
}

/* PASSWORD <password> */
void net_password(ctype_t *client, int numarg, const char **arg)
{
	if (numarg != 1) {
		send_err(client, NUT_ERR_INVALID_ARGUMENT);
		return;
	}

	if (client->password != NULL) {
		if (client->username)
			upslogx(LOG_INFO, "Client %s@%s tried to set a password twice",
				client->username, client->addr);
		else
			upslogx(LOG_INFO, "Client on %s tried to set a password twice",
				client->addr);

		send_err(client, NUT_ERR_ALREADY_SET_PASSWORD);
		return;
	}

	client->password = xstrdup(arg[0]);
	sendback(client, "OK\n");
}