summaryrefslogtreecommitdiff
path: root/usr/src/cmd/lp/cmd/lpsched/lpfsck.c
blob: 8b569227d32592f8279bd1a9984afc3feb7dc806 (plain)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

#include "stdarg.h"
#include "stdlib.h"
#include "fcntl.h"
#include <sys/param.h>
#include "lpsched.h"


static void check_link();

/**
 ** lpfsck()
 **/

#define	F	0
#define D	1
#define P	2
#define S	3

static void		proto (int, int, ...);
static int		va_makepath(va_list *, char **);
static void		_rename (char *, char *, ...);

void
lpfsck(void)
{
	struct stat		stbuf;
	int			real_am_in_background = am_in_background;


	/*
	 * Force log messages to go into the log file instead of stdout.
	 */
	am_in_background = 1;

	/*
	 * Most of these lines repeat the prototype file from the
	 * packaging and should match those items exactly.
	 * (In fact, they probably ought to be generated from that file,
	 * but that work is for a rainy day...)
	 */

	/*
	 * DIRECTORIES:
	 */
proto (D, 0,  Lp_A, NULL,			    0775, Lp_Uid, Lp_Gid);
proto (D, 1,  Lp_A_Classes, NULL,		    0775, Lp_Uid, Lp_Gid);
proto (D, 1,  Lp_A_Forms, NULL,			    0775, Lp_Uid, Lp_Gid);
proto (D, 1,  Lp_A_Interfaces, NULL,		    0775, Lp_Uid, Lp_Gid);
proto (D, 1,  Lp_A_Printers, NULL,		    0775, Lp_Uid, Lp_Gid);
proto (D, 1,  Lp_A_PrintWheels, NULL,		    0775, Lp_Uid, Lp_Gid);
proto (D, 0,  "/var/lp", NULL,			    0775, Lp_Uid, Lp_Gid);
proto (D, 1,  Lp_Logs, NULL,			    0775, Lp_Uid, Lp_Gid);
proto (D, 1,  Lp_Spooldir, NULL,		    0775, Lp_Uid, Lp_Gid);
proto (D, 1,  Lp_Admins, NULL,			    0775, Lp_Uid, Lp_Gid);
proto (D, 1,  Lp_Requests, NULL,		    0775, Lp_Uid, Lp_Gid);
proto (D, 1,  Lp_Requests, Local_System, NULL,	    0770, Lp_Uid, Lp_Gid);
proto (D, 1,  Lp_System, NULL,			    0775, Lp_Uid, Lp_Gid);
proto (D, 1,  Lp_Tmp, NULL,			    0771, Lp_Uid, Lp_Gid);
proto (D, 1,  Lp_Tmp, Local_System, NULL,	    0775, Lp_Uid, Lp_Gid);

	/*
	 * DIRECTORIES: not described in the packaging
	 */
proto (D, 0,  Lp_Spooldir, FIFOSDIR, NULL,	    0775, Lp_Uid, Lp_Gid);

	/*
	 * THE MAIN FIFO:
	 */
proto (P, 1,  Lp_FIFO, NULL,			    0666, Lp_Uid, Lp_Gid);

	/*
	 * SYMBOLIC LINKS:
	 * Watch out! These names are given in the reverse
	 * order found in the prototype file (sorry!)
	 */
proto (S, 1,  Lp_Model, NULL,			"/etc/lp/model", NULL);
proto (S, 1,  Lp_Logs, NULL,			"/etc/lp/logs", NULL);
/*     S, 1,  Lp_Tmp, Local_System, ...    DONE BELOW */
proto (S, 1,  Lp_Bin, NULL,			Lp_Spooldir, "bin", NULL);
proto (S, 1,  Lp_A, NULL,			Lp_Admins, "lp", NULL);

	/*
	 * OTHER FILES:
	 */

	/*
	 * SPECIAL CASE:
	 * If the "temp" symbolic link already exists,
	 * but is not correct, assume the machine's nodename changed.
	 * Rename directories that include the nodename, if possible,
	 * so that unprinted requests are saved. Then change the
	 * symbolic link.
	 * Watch out for a ``symbolic link'' that isn't!
	 */
	if (Lstat(Lp_Temp, &stbuf) == 0)
	    switch (stbuf.st_mode & S_IFMT) {

	    default:
		Unlink (Lp_Temp);
		break;

	    case S_IFDIR:
		Rmdir (Lp_Temp);
		break;

	    case S_IFLNK:
		check_link();
		break;
	    }

	proto(S, 1, Lp_Tmp, Local_System, NULL,	Lp_Temp, NULL);

	am_in_background = real_am_in_background;
	return;
}

static void
check_link()
{
	int len;
	char symbolic[MAXPATHLEN + 1];
	char *real_dir;
	char *old_system;

	if ((len = Readlink(Lp_Temp, symbolic, MAXPATHLEN)) <= 0) {
		Unlink(Lp_Temp);
		return;
	}

	/*
	 * If the symbolic link contained trailing slashes, remove
	 * them.
	 */
	while ((len > 1) && (symbolic[len - 1] == '/')) {
		len--;
	}
	symbolic[len] = 0;

	/* check that symlink points into /var/spool/lp/tmp */
	if (strncmp(Lp_Tmp, symbolic, strlen(Lp_Tmp)) != 0) {
		Unlink(Lp_Temp);
		return;
	}

	/*
	 * Check that symlink points to something.
	 * There should be at least 2 characters
	 * after the string '/var/spool/lp/tmp':
	 * a '/' and another character.
	 */
	if (len <= strlen(Lp_Tmp) + 1) {
		Unlink(Lp_Temp);
		return;
	}

	real_dir = makepath(Lp_Tmp, Local_System, NULL);
	if (!STREQU(real_dir, symbolic)) {
		if (!(old_system = strrchr(symbolic, '/')))
			old_system = symbolic;
		else
			old_system++;

		/*
		 * The "rename()" system call (buried
		 * inside the "_rename()" routine) should
		 * succeed, even though we blindly created
		 * the new directory earlier, as the only
		 * directory entries should be . and ..
		 * (although if someone already created
		 * them, we'll note the fact).
		 */
		_rename(old_system, Local_System, Lp_Tmp, NULL);
		_rename(old_system, Local_System, Lp_Requests, NULL);

		Unlink(Lp_Temp);
	}
	Free(real_dir);
}


/**
 ** proto()
 **/

static void
proto(int type, int rm_ok, ...)
{
	va_list			ap;

	char			*path,
				*symbolic;

	int			exist,
				err;

	mode_t			mode;

	uid_t			uid;

	gid_t			gid;

	struct stat		stbuf;


	va_start(ap, rm_ok);

	if ((err = va_makepath(&ap, &path)) < 0)
		fail ("\"%s\" is a truncated name!\n", path);

	exist = (stat(path, &stbuf) == 0);

	switch (type) {

	case S:
		if (!exist)
			fail ("%s is missing!\n", path);
		if ((err = va_makepath(&ap, &symbolic)) < 0)
			fail ("\"%s\" is a truncated name!\n", symbolic);
		Symlink (path, symbolic);
		Free (symbolic);
		Free (path);
		return;

	case D:
		if (exist && !S_ISDIR(stbuf.st_mode)) {
			if (!rm_ok)
				fail ("%s is not a directory!\n", path);
			else {
				Unlink (path);
				exist = 0;
			}
		}
		if (!exist)
			Mkdir (path, 0);
		break;

	case F:
		if (exist && !S_ISREG(stbuf.st_mode)) {
			if (!rm_ok)
				fail ("%s is not a file!\n", path);
			else {
				Unlink (path);
				exist = 0;
			}
		}
		if (!exist)
			Close(Creat(path, 0));
		break;

	case P:
		/*
		 * Either a pipe or a file.
		 */
		if (exist &&
		    !S_ISREG(stbuf.st_mode) && !S_ISFIFO(stbuf.st_mode)) {
			if (!rm_ok)
				fail ("%s is not a file or pipe!\n", path);
			else {
				Unlink (path);
				exist = 0;
			}
		}
		if (!exist)
			Close(Creat(path, 0));
		break;

	}

	mode = va_arg(ap, mode_t);
	uid = va_arg(ap, uid_t);
	gid = va_arg(ap, gid_t);
	(void) chownmod(path, uid, gid, mode);

	Free (path);
	return;
}

/*
 * va_makepath()
 *
 * Takes a variable length list of path components and attempts to string them
 * together into a path.  It returns a heap-allocated string via the output
 * parameter 'ret', and returns an integer success value: < 0 indicates failure,
 * 0 indicates success.  Note that 'ret' will never be NULL (unless the system
 * is so overloaded that it can't allocate a single byte), and should always be
 * free()d.
 */
static int
va_makepath (va_list *pap, char **ret)
{
	char			*component;
	char 			buf[MAXPATHLEN];
	int			buflen;

	memset(buf, 0, sizeof (buf));
	while ((component = va_arg((*pap), char *)) != NULL) {
		if (strlcat(buf, component, sizeof (buf)) >= sizeof (buf) ||
			strlcat(buf, "/", sizeof (buf)) >= sizeof (buf)) {
			if ((*ret = strdup(buf)) == NULL)
				*ret = strdup("");
			return (-1);
		}
	}

	/* remove the trailing slash */
	buflen = strlen(buf);
	if ((buflen > 1) && (buf[buflen - 1] == '/')) {
		buf[buflen - 1] = '\0';
	}

	if ((*ret = strdup(buf)) == NULL) {
		*ret = strdup("");
		return (-1);
	}
	return (0);
}

/**
 ** _rename()
 **/

static void
_rename(char *old_system, char *new_system, ...)
{
	va_list			ap;

	char *			prefix;
	char *			old;
	char *			new;
	int			err;


	va_start (ap, new_system);
	if ((err = va_makepath(&ap, &prefix)) < 0)
		fail (
			"Rename failed; prefix \"%s\" is a truncated name.\n",
			prefix
		);
	va_end (ap);

	old = makepath(prefix, old_system, (char *)0);
	new = makepath(prefix, new_system, (char *)0);

	if (Rename(old, new) == 0)
		note ("Renamed %s to %s.\n", old, new);
	else if (errno == EEXIST)
		note (
			"Rename of %s to %s failed because %s exists.\n",
			old,
			new,
			new
		);
	else
		fail (
			"Rename of %s to %s failed (%s).\n",
			old,
			new,
			PERROR
		);

	Free (new);
	Free (old);
	Free (prefix);

	return;
}