aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/catman.c
blob: 8767e5e3f6dbcb9530d965441ae1347e178f8a1a (plain) (blame)
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*	$Id: catman.c,v 1.11.2.2 2013/10/11 00:06:48 schwarze Exp $ */
/*
 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/param.h>
#include <sys/stat.h>
#include <sys/wait.h>

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#if defined(__linux__) || defined(__sun)
# include <db_185.h>
#else
# include <db.h>
#endif

#include "manpath.h"
#include "mandocdb.h"

#define	xstrlcpy(_dst, _src, _sz) \
	do if (strlcpy((_dst), (_src), (_sz)) >= (_sz)) { \
		fprintf(stderr, "%s: Path too long", (_dst)); \
		exit(EXIT_FAILURE); \
	} while (/* CONSTCOND */0)

#define	xstrlcat(_dst, _src, _sz) \
	do if (strlcat((_dst), (_src), (_sz)) >= (_sz)) { \
		fprintf(stderr, "%s: Path too long", (_dst)); \
		exit(EXIT_FAILURE); \
	} while (/* CONSTCOND */0)

static	int		 indexhtml(char *, size_t, char *, size_t);
static	int		 manup(const struct manpaths *, char *);
static	int		 mkpath(char *, mode_t, mode_t);
static	int		 treecpy(char *, char *);
static	int		 update(char *, char *);
static	void		 usage(void);

static	const char	*progname;
static	int		 verbose;
static	int		 force;

int
main(int argc, char *argv[])
{
	int		 ch;
	char		*aux, *base, *conf_file;
	struct manpaths	 dirs;
	char		 buf[MAXPATHLEN];
	extern char	*optarg;
	extern int	 optind;

	progname = strrchr(argv[0], '/');
	if (progname == NULL)
		progname = argv[0];
	else
		++progname;

	aux = base = conf_file = NULL;
	xstrlcpy(buf, "/var/www/cache/man.cgi", MAXPATHLEN);

	while (-1 != (ch = getopt(argc, argv, "C:fm:M:o:v")))
		switch (ch) {
		case ('C'):
			conf_file = optarg;
			break;
		case ('f'):
			force = 1;
			break;
		case ('m'):
			aux = optarg;
			break;
		case ('M'):
			base = optarg;
			break;
		case ('o'):
			xstrlcpy(buf, optarg, MAXPATHLEN);
			break;
		case ('v'):
			verbose++;
			break;
		default:
			usage();
			return(EXIT_FAILURE);
		}

	argc -= optind;
	argv += optind;

	if (argc > 0) {
		usage();
		return(EXIT_FAILURE);
	}

	memset(&dirs, 0, sizeof(struct manpaths));
	manpath_parse(&dirs, conf_file, base, aux);
	ch = manup(&dirs, buf);
	manpath_free(&dirs);
	return(ch ? EXIT_SUCCESS : EXIT_FAILURE);
}

static void
usage(void)
{
	
	fprintf(stderr, "usage: %s "
			"[-fv] "
			"[-C file] "
			"[-o path] "
			"[-m manpath] "
			"[-M manpath]\n",
			progname);
}

/*
 * If "src" file doesn't exist (errors out), return -1.  Otherwise,
 * return 1 if "src" is newer (which also happens "dst" doesn't exist)
 * and 0 otherwise.
 */
static int
isnewer(const char *dst, const char *src)
{
	struct stat	 s1, s2;

	if (-1 == stat(src, &s1))
		return(-1);
	if (force)
		return(1);

	return(-1 == stat(dst, &s2) ? 1 : s1.st_mtime > s2.st_mtime);
}

/*
 * Copy the contents of one file into another.
 * Returns 0 on failure, 1 on success.
 */
static int
filecpy(const char *dst, const char *src)
{
	char		 buf[BUFSIZ];
	int		 sfd, dfd, rc;
	ssize_t		 rsz, wsz;

	sfd = dfd = -1;
	rc = 0;

	if (-1 == (dfd = open(dst, O_CREAT|O_TRUNC|O_WRONLY, 0644))) {
		perror(dst);
		goto out;
	} else if (-1 == (sfd = open(src, O_RDONLY, 0))) {
		perror(src);
		goto out;
	} 

	while ((rsz = read(sfd, buf, BUFSIZ)) > 0)
		if (-1 == (wsz = write(dfd, buf, (size_t)rsz))) {
			perror(dst);
			goto out;
		} else if (wsz < rsz) {
			fprintf(stderr, "%s: Short write\n", dst);
			goto out;
		}
	
	if (rsz < 0)
		perror(src);
	else
		rc = 1;
out:
	if (-1 != sfd)
		close(sfd);
	if (-1 != dfd)
		close(dfd);

	return(rc);
}

/*
 * Pass over the recno database and re-create HTML pages if they're
 * found to be out of date.
 * Returns -1 on fatal error, 1 on success.
 */
static int
indexhtml(char *src, size_t ssz, char *dst, size_t dsz)
{
	DB		*idx;
	DBT		 key, val;
	int		 c, rc;
	unsigned int	 fl;
	const char	*f;
	char		*d;
	char		 fname[MAXPATHLEN];

	xstrlcpy(fname, dst, MAXPATHLEN);
	xstrlcat(fname, "/", MAXPATHLEN);
	xstrlcat(fname, MANDOC_IDX, MAXPATHLEN);

	idx = dbopen(fname, O_RDONLY, 0, DB_RECNO, NULL);
	if (NULL == idx) {
		perror(fname);
		return(-1);
	}

	fl = R_FIRST;
	while (0 == (c = (*idx->seq)(idx, &key, &val, fl))) {
		fl = R_NEXT;
		/*
		 * If the record is zero-length, then it's unassigned.
		 * Skip past these.
		 */
		if (0 == val.size)
			continue;

		f = (const char *)val.data + 1;
		if (NULL == memchr(f, '\0', val.size - 1))
			break;

		src[(int)ssz] = dst[(int)dsz] = '\0';

		xstrlcat(dst, "/", MAXPATHLEN);
		xstrlcat(dst, f, MAXPATHLEN);

		xstrlcat(src, "/", MAXPATHLEN);
		xstrlcat(src, f, MAXPATHLEN);

		if (-1 == (rc = isnewer(dst, src))) {
			fprintf(stderr, "%s: File missing\n", f);
			break;
		} else if (0 == rc)
			continue;

		d = strrchr(dst, '/');
		assert(NULL != d);
		*d = '\0';

		if (-1 == mkpath(dst, 0755, 0755)) {
			perror(dst);
			break;
		}

		*d = '/';

		if ( ! filecpy(dst, src))
			break;
		if (verbose)
			printf("%s\n", dst);
	}

	(*idx->close)(idx);

	if (c < 0)
		perror(fname);
	else if (0 == c) 
		fprintf(stderr, "%s: Corrupt index\n", fname);

	return(1 == c ? 1 : -1);
}

/*
 * Copy both recno and btree databases into the destination.
 * Call in to begin recreating HTML files.
 * Return -1 on fatal error and 1 if the update went well.
 */
static int
update(char *dst, char *src)
{
	size_t		 dsz, ssz;

	dsz = strlen(dst);
	ssz = strlen(src);

	xstrlcat(src, "/", MAXPATHLEN);
	xstrlcat(dst, "/", MAXPATHLEN);

	xstrlcat(src, MANDOC_DB, MAXPATHLEN);
	xstrlcat(dst, MANDOC_DB, MAXPATHLEN);

	if ( ! filecpy(dst, src))
		return(-1);
	if (verbose)
		printf("%s\n", dst);

	dst[(int)dsz] = src[(int)ssz] = '\0';

	xstrlcat(src, "/", MAXPATHLEN);
	xstrlcat(dst, "/", MAXPATHLEN);

	xstrlcat(src, MANDOC_IDX, MAXPATHLEN);
	xstrlcat(dst, MANDOC_IDX, MAXPATHLEN);

	if ( ! filecpy(dst, src))
		return(-1);
	if (verbose)
		printf("%s\n", dst);

	dst[(int)dsz] = src[(int)ssz] = '\0';

	return(indexhtml(src, ssz, dst, dsz));
}

/*
 * See if btree or recno databases in the destination are out of date
 * with respect to a single manpath component.
 * Return -1 on fatal error, 0 if the source is no longer valid (and
 * shouldn't be listed), and 1 if the update went well.
 */
static int
treecpy(char *dst, char *src)
{
	size_t		 dsz, ssz;
	int		 rc;

	dsz = strlen(dst);
	ssz = strlen(src);

	xstrlcat(src, "/", MAXPATHLEN);
	xstrlcat(dst, "/", MAXPATHLEN);

	xstrlcat(src, MANDOC_IDX, MAXPATHLEN);
	xstrlcat(dst, MANDOC_IDX, MAXPATHLEN);

	if (-1 == (rc = isnewer(dst, src)))
		return(0);

	dst[(int)dsz] = src[(int)ssz] = '\0';

	if (1 == rc)
		return(update(dst, src));

	xstrlcat(src, "/", MAXPATHLEN);
	xstrlcat(dst, "/", MAXPATHLEN);

	xstrlcat(src, MANDOC_DB, MAXPATHLEN);
	xstrlcat(dst, MANDOC_DB, MAXPATHLEN);

	if (-1 == (rc = isnewer(dst, src)))
		return(0);
	else if (rc == 0)
		return(1);

	dst[(int)dsz] = src[(int)ssz] = '\0';

	return(update(dst, src));
}

/*
 * Update the destination's file-tree with respect to changes in the
 * source manpath components.
 * "Change" is defined by an updated index or btree database.
 * Returns 1 on success, 0 on failure.
 */
static int
manup(const struct manpaths *dirs, char *base)
{
	char		 dst[MAXPATHLEN],
			 src[MAXPATHLEN];
	const char	*path;
	size_t		 i;
	int		 c;
	size_t		 sz;
	FILE		*f;

	/* Create the path and file for the catman.conf file. */

	sz = strlen(base);
	xstrlcpy(dst, base, MAXPATHLEN);
	xstrlcat(dst, "/etc", MAXPATHLEN);
	if (-1 == mkpath(dst, 0755, 0755)) {
		perror(dst);
		return(0);
	}

	xstrlcat(dst, "/catman.conf", MAXPATHLEN);
	if (NULL == (f = fopen(dst, "w"))) {
		perror(dst);
		return(0);
	} else if (verbose)
		printf("%s\n", dst);

	for (i = 0; i < dirs->sz; i++) {
		path = dirs->paths[i];
		dst[(int)sz] = '\0';
		xstrlcat(dst, path, MAXPATHLEN);
		if (-1 == mkpath(dst, 0755, 0755)) {
			perror(dst);
			break;
		}

		xstrlcpy(src, path, MAXPATHLEN);
		if (-1 == (c = treecpy(dst, src)))
			break;
		else if (0 == c)
			continue;

		/*
		 * We want to use a relative path here because manpath.h
		 * will realpath() when invoked with man.cgi, and we'll
		 * make sure to chdir() into the cache directory before.
		 *
		 * This allows the cache directory to be in an arbitrary
		 * place, working in both chroot() and non-chroot()
		 * "safe" modes.
		 */
		assert('/' == path[0]);
		fprintf(f, "_whatdb %s/whatis.db\n", path + 1);
	}

	fclose(f);
	return(i == dirs->sz);
}

/*
 * Copyright (c) 1983, 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
static int
mkpath(char *path, mode_t mode, mode_t dir_mode)
{
	struct stat sb;
	char *slash;
	int done, exists;

	slash = path;

	for (;;) {
		/* LINTED */
		slash += strspn(slash, "/");
		/* LINTED */
		slash += strcspn(slash, "/");

		done = (*slash == '\0');
		*slash = '\0';

		/* skip existing path components */
		exists = !stat(path, &sb);
		if (!done && exists && S_ISDIR(sb.st_mode)) {
			*slash = '/';
			continue;
		}

		if (mkdir(path, done ? mode : dir_mode) == 0) {
			if (mode > 0777 && chmod(path, mode) < 0)
				return (-1);
		} else {
			if (!exists) {
				/* Not there */
				return (-1);
			}
			if (!S_ISDIR(sb.st_mode)) {
				/* Is there, but isn't a directory */
				errno = ENOTDIR;
				return (-1);
			}
		}

		if (done)
			break;

		*slash = '/';
	}

	return (0);
}