File: client.c

package info (click to toggle)
resmgr 1.0-2sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 308 kB
  • ctags: 313
  • sloc: ansic: 3,165; sh: 556; makefile: 109
file content (300 lines) | stat: -rw-r--r-- 5,564 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * Resource manager client functions
 *
 * Copyright (C) 2001-2002, Olaf Kirch <okir@lst.de>
 */

#include <string.h>
#include <syslog.h>
#include <stdarg.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdlib.h>
#include "protocol.h"
#include "resmgr.h"

static int	sane(const char *);
static int	wildmatch(const char *, const char *);


static int
rsm_command(int *code, const char *fmt, ...)
{
	struct conn	*conn;
	va_list		ap;
	int		fd = -1, lcode;

	if (!code)
		code = &lcode;
	*code = 0;

	va_start(ap, fmt);
	if (!(conn = rsm_connect(_PATH_RESMGR_SOCKET))
	 || rsm_vprintf(conn, fmt, ap) < 0
	 || (*code = rsm_recv_response(conn)) < 0) {
		syslog(LOG_NOTICE, "resmgr: communication failure: %m");
		goto out;
	}
	if (*code != MSG_OKAY) {
		syslog(LOG_DEBUG, "resmgr: server response code %d", *code);
		goto out;
	}
	if ((fd = conn->passfd) >= 0)
		conn->passfd = -1;
	else
		fd = 0;

out:	rsm_close(conn);
	va_end(ap);
	return fd;
}

char **
rsm_list_devices(const char *wildcard)
{
	struct conn	*conn;
	char		family[128], *s;
	char		buffer[512], *sp, *np, **result;
	int		count = 0, code;

	result = NULL;

	family[0] = '\0';
	if (wildcard && wildcard[0] != '/') {
		strncpy(family, wildcard, sizeof(family)-1);
		family[sizeof(family)-1] = '\0';
		if ((s = strchr(family, ':')) != NULL)
			*s++ = '\0';
		wildcard = s;
	}

	if (!(conn = rsm_connect(_PATH_RESMGR_SOCKET))
	 || rsm_printf(conn, "list %s", family) < 0)
		goto error;

	do {
		if (rsm_recv(conn, buffer, sizeof(buffer)) < 0)
			goto error;
		if (!isdigit(buffer[0])) {
			errno = EINVAL;
			goto error;
		}
		code = strtoul(buffer, &sp, 10);

		/* Skip the flags, get the path name */
		if (code == MSG_OKAY && strtok(sp, " \t\r\n")
		 && (np = strtok(NULL, " \t\r\n"))
		 && wildmatch(wildcard, np)) {
			result = (char **) realloc(result,
					(count+2) * sizeof(char *));
			result[count++] = strdup(np);
			result[count] = NULL;
		}
	} while (*sp == '-');

	if (code != MSG_OKAY) {
		syslog(LOG_DEBUG, "resmgr: server response code %d", code);
		goto error1;
	}

	goto out;

error:	syslog(LOG_NOTICE, "resmgr: communication failure: %m");
error1: while (count--)
		free(result[count]);
	if (result)
		free(result);

out:	rsm_close(conn);
	return result;
}

int
rsm_open_device_as(const char *family, const char *name, int flags)
{
	int	fd, ro = 1;

	/* Are we from a sane family? */
	if (!sane(name) || (family && !sane(family))) {
		errno = EINVAL;
		return -1;
	}

	/* If the name already starts with the family prefix,
	 * drop the family name.
	 */
	if (family) {
		int	n = strlen(family);
		
		if (!strncmp(name, family, n) && name[n] == ':')
			family = NULL;
	}

	if ((flags & O_ACCMODE) == O_RDWR
	 || (flags & O_ACCMODE) == O_WRONLY)
		ro = 0;

	fd = rsm_command(NULL, "open %s %s%s%s",
				ro? "-ro" : "",
				family? family : "",
				family? ":" : "",
				name);

	if (fd >= 0) {
		fcntl(fd, F_SETFL, flags);
	} else {
		errno = EACCES;
	}

	return fd;
}

int
rsm_open_device(const char *pathname, int flags)
{
	return rsm_open_device_as(NULL, pathname, flags);
}


int
rsm_lock_device(const char *pathname)
{
	int	code, res;

	if (pathname[0] != '/' || !sane(pathname)) {
		errno = EINVAL;
		return -1;
	}
	if (rsm_command(&code, "lock %s", pathname) >= 0)
		return 0;
	
	/* Is this a stale lock? */
	if (code == MSG_STALELOCK
	 && rsm_unlock_device(pathname) >= 0
	 && rsm_command(NULL, "lock %s", pathname) >= 0)
	 	return 0;

	errno = EACCES;
	return res;
}

int
rsm_unlock_device(const char *pathname)
{
	int	res;

	if (pathname[0] != '/' || !sane(pathname)) {
		errno = EINVAL;
		return -1;
	}
	if ((res = rsm_command(NULL, "unlock %s", pathname)) < 0)
		errno = EACCES;
	return res;
}

int
rsm_login(const char *user, const char *tty)
{
	if (!sane(user) || !sane(tty)) {
		errno = EINVAL;
		return -1;
	}

	return rsm_command(NULL, "login %s %s", user, tty);
}

int
rsm_logout(const char *tty)
{
	if (!sane(tty)) {
		errno = EINVAL;
		return -1;
	}

	return rsm_command(NULL, "logout %s", tty);
}

int
rsm_grant(const char *user, const char *classname)
{
	if (!sane(user) || !sane(classname)) {
		errno = EINVAL;
		return -1;
	}

	return rsm_command(NULL, "grant %s %s", user, classname);
}

int
rsm_revoke(const char *user, const char *classname)
{
	if (!sane(user) || (classname && !sane(classname))) {
		errno = EINVAL;
		return -1;
	}

	if (classname)
		return rsm_command(NULL, "revoke %s %s", user, classname);
	return rsm_command(NULL, "revoke %s %s", user);
}

int
rsm_add_device(const char *device, const char *classname)
{
	if (!sane(device) || !sane(classname)) {
		errno = EINVAL;
		return -1;
	}

	return rsm_command(NULL, "add %s %s", device, classname);
}

int
rsm_remove_device(const char *device, const char *classname)
{
	if (!sane(device) || (classname && !sane(classname))) {
		errno = EINVAL;
		return -1;
	}

	if (classname)
		return rsm_command(NULL, "remove %s %s", device, classname);
	return rsm_command(NULL, "remove %s", device);
}

/*
 * Utility function - check whether argument is reasonably sane
 */
int
sane(const char *arg)
{
	int	n;

	n = strcspn(arg, " \t\r\n");
	return (arg[n] == '\0');
}

/*
 * Simplified glob matching
 */
int
wildmatch(const char *pattern, const char *name)
{
	const char	*colon;

	if (pattern == NULL)
		return 1;

	/* Skip the resource family if present */
	if ((colon = strchr(name, ':')) != NULL)
		name = colon + 1;

	while ((colon = strchr(name, ':')) != NULL) {
		if (rsm_glob(pattern, name, colon))
			return 1;
		name = colon + 1;
	}

	return rsm_glob(pattern, name, NULL);
}