File: class.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 (298 lines) | stat: -rw-r--r-- 5,309 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
/*
 * Resource class handling
 *
 * Copyright (C) 2001-2002, Olaf Kirch <okir@lst.de>
 */

#include <sys/stat.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include "resmgrd.h"

res_class_t *		res_classes;


static int		lock_stale(res_device_t *);

res_class_t *
res_class_get(const char *name)
{
	res_class_t	*cls;

	for (cls = res_classes; cls; cls = cls->next) {
		if (!strcmp(cls->name, name))
			return cls;
	}
	return NULL;
}

res_class_t *
res_class_create(const char *name)
{
	res_class_t	*cls;

	cls = (res_class_t *) calloc(1, sizeof(*cls));
	cls->name = strdup(name);
	cls->refcnt = 1;

	cls->next = res_classes;
	res_classes = cls;

	return cls;
}

res_device_t *
res_class_add(res_class_t *cls, const char *devname, int flags)
{
	res_device_t	*dev, **p;

	for (p = &cls->devices; (dev = *p) != NULL; p = &dev->next)  {
		if (!strcmp(dev->name, devname)) {
			/* existing device: just update flags */
			dev->flags = flags;
			return dev;
		}
	}

	*p = dev = res_device_create(devname, flags);
	return dev;
}

void
res_class_remove(res_class_t *cls, const char *devname)
{
	res_device_t	*dev, **p;

	p = &cls->devices;
	while ((dev = *p) != NULL) {
		if (!strcmp(dev->name, devname)) {
			*p = dev->next;
			res_device_free(dev);
		} else {
			p = &dev->next;
		}
	}
}

res_device_t *
res_class_get_device(res_class_t *cls, res_name_t *name)
{
	res_device_t	*dev;

	for (dev = cls->devices; dev != NULL; dev = dev->next)  {
		if (res_name_match(name, dev))
			return dev;
	}
	return NULL;
}

void
res_class_revoke(res_class_t *cls, res_user_t *user)
{
	/* XXX to be implemented */
}

void
res_class_free(res_class_t *cls)
{
	res_device_t	*dev;

	cls->refcnt--;
	if (cls->refcnt == 0) {
		res_acl_free(cls->acl);
		while ((dev = cls->devices) != NULL) {
			cls->devices = dev->next;
			res_device_free(dev);
		}
		free(cls->name);
		memset(cls, 0, sizeof(*cls));
		free(cls);
	}
}

/*
 * Check whether a given path name is a sane device name
 * XXX This should go to a per-family routine
 */
int
res_device_sane(const char *path)
{
	char		resolved_path[PATH_MAX+1];
	struct stat	stb;
	int		is_usb;


	is_usb = !strncmp(path,
			_PATH_PROC_BUS_USB,
			sizeof(_PATH_PROC_BUS_USB)-1);

	/* It must start with /dev/ */
	if (!is_usb && strncmp(path, "/dev/", 5))
		return 0;

	/* It must not contain .. */
	if (strstr(path, ".."))
		return 0;

	/* It does not exist - give it the benefit of doubt?
	 * Bad idea in a security service. */
	if (lstat(path, &stb) < 0)
		return 0;

	/* If it's a symlink, make sure the resolved path
	 * points to a proper device */
	if (S_ISLNK(stb.st_mode)) {
		if (!realpath(path, resolved_path))
			return 0;
		return res_device_sane(resolved_path);
	}

	if (is_usb)
		return S_ISREG(stb.st_mode);

	/* Must be a device */
	if (!S_ISBLK(stb.st_mode) && !S_ISCHR(stb.st_mode))
		return 0;

	return 1;
}

/*
 * Create device objects
 */
res_device_t *
res_device_create(const char *name, int flags)
{
	res_device_t	*dev;
	char		path[4096], *s;

	dev = (res_device_t *) calloc(1, sizeof(*dev));
	dev->name = strdup(name);
	dev->flags = flags;

	snprintf(path, sizeof(path), "%s/LCK.", res_config_lock_dir);
	if (!strncmp(name, "/dev/", 5))
		name += 4;

	s = path + strlen(path);
	while (*name && s < path+sizeof(path)-1) {
		if (*name == '/') {
			*s++ = '.';
			while (*name == '/')
				name++;
		} else {
			*s++ = *name++;
		}
	}
	*s++ = '\0';
	dev->lock = strdup(path);

	return dev;
}

/*
 * Lock/unlock a device
 */
int
res_device_lock(res_device_t *dev, uid_t uid, pid_t pid)
{
	char	locktemp[4096], pidbuf[64];
	int	fd;

	sprintf(pidbuf, "%u\n", pid);

	/* First create a temp lock file */
	snprintf(locktemp, sizeof(locktemp),
			"%s.XXXXXX", dev->lock);
	if ((fd = mkstemp(locktemp)) < 0) {
		log("Unable to create lock fencepost: %m");
		return -1;
	}

	write(fd, pidbuf, strlen(pidbuf));
	close(fd);

	if (link(locktemp, dev->lock) >= 0) {
		chmod(dev->lock, 0644);
		chown(dev->lock, uid, 0);
		unlink(locktemp);
		return 0;
	}

	if (errno != EEXIST) {
		unlink(locktemp);
		return -1;
	}
	unlink(locktemp);

	/* There's a conflicting lock - see if it still exists */
	if (!(pid = lock_stale(dev)))
		return -1;

	/* Stale lock file. Return the holder's pid */
	return pid;
}

int
res_device_unlock(res_device_t *dev, uid_t uid)
{
	struct stat	stb;

	if (stat(dev->lock, &stb) < 0)
		return -1;
	if (stb.st_uid != uid && !lock_stale(dev))
		return -1;
	unlink(dev->lock);
	return 0;
}

int
lock_stale(res_device_t *dev)
{
	char	pidbuf[64];
	int	n, fd, locker;

	/* Now try to read the locker's pid from the file */
	fd = open(dev->lock, O_RDONLY);
	if (fd < 0)
		return 0;

	n = read(fd, pidbuf, sizeof(pidbuf)-1);
	close(fd);

	if (n <= 0)
		return 0;
	pidbuf[n] = '\0';

	/* Transparently deal with binary lock files - nobody
	 * should use that locking style anymore, but better be
	 * safe than sorry */
	if (n == sizeof(int)) {
		memcpy(&locker, pidbuf, sizeof(int));
	} else {
		locker = strtol(pidbuf, NULL, 0);
	}
	if (locker <= 0)
		return 0;

	if (kill(locker, 0) < 0 && errno == ESRCH)
		return locker;

	return 0;
}

/*
 * Delete a device object
 */
void
res_device_free(res_device_t *dev)
{
	free(dev->name);
	free(dev->lock);
	memset(dev, 0, sizeof(*dev));
	free(dev);
}