plist.c revision 228990
1181847Sjkim/*
2182393Sjkim * FreeBSD install - a package for the installation and maintenance
3181847Sjkim * of non-core utilities.
4181847Sjkim *
5181847Sjkim * Redistribution and use in source and binary forms, with or without
6181847Sjkim * modification, are permitted provided that the following conditions
7181847Sjkim * are met:
8181847Sjkim * 1. Redistributions of source code must retain the above copyright
9182393Sjkim *    notice, this list of conditions and the following disclaimer.
10182393Sjkim * 2. Redistributions in binary form must reproduce the above copyright
11182393Sjkim *    notice, this list of conditions and the following disclaimer in the
12181847Sjkim *    documentation and/or other materials provided with the distribution.
13181847Sjkim *
14181847Sjkim * Jordan K. Hubbard
15181847Sjkim * 18 July 1993
16181847Sjkim *
17181847Sjkim * General packing list routines.
18181847Sjkim *
19181847Sjkim */
20181847Sjkim
21181847Sjkim#include <sys/cdefs.h>
22181847Sjkim__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/lib/plist.c 228990 2011-12-30 10:58:14Z uqs $");
23181847Sjkim
24181847Sjkim#include "lib.h"
25181847Sjkim#include <err.h>
26181847Sjkim#include <md5.h>
27181847Sjkim
28181847Sjkim/* Add an item to a packing list */
29181847Sjkimvoid
30181847Sjkimadd_plist(Package *p, plist_t type, const char *arg)
31182393Sjkim{
32181847Sjkim    PackingList tmp;
33
34    tmp = new_plist_entry();
35    tmp->name = copy_string(arg);
36    tmp->type = type;
37
38    if (!p->head)
39	p->head = p->tail = tmp;
40    else {
41	tmp->prev = p->tail;
42	p->tail->next = tmp;
43	p->tail = tmp;
44    }
45    switch (type) {
46    case PLIST_NAME:
47	p->name = tmp->name;
48	break;
49
50    case PLIST_ORIGIN:
51	p->origin = tmp->name;
52	break;
53
54    default:
55	break;
56    }
57}
58
59void
60add_plist_top(Package *p, plist_t type, const char *arg)
61{
62    PackingList tmp;
63
64    tmp = new_plist_entry();
65    tmp->name = copy_string(arg);
66    tmp->type = type;
67
68    if (!p->head)
69	p->head = p->tail = tmp;
70    else {
71	tmp->next = p->head;
72	p->head->prev = tmp;
73	p->head = tmp;
74    }
75}
76
77/* Return the last (most recent) entry in a packing list */
78PackingList
79last_plist(Package *p)
80{
81    return p->tail;
82}
83
84/* Mark all items in a packing list to prevent iteration over them */
85void
86mark_plist(Package *pkg)
87{
88    PackingList p = pkg->head;
89
90    while (p) {
91	p->marked = TRUE;
92	p = p->next;
93    }
94}
95
96/* Find a given item in a packing list and, if so, return it (else NULL) */
97PackingList
98find_plist(Package *pkg, plist_t type)
99{
100    PackingList p = pkg->head;
101
102    while (p) {
103	if (p->type == type)
104	    return p;
105	p = p->next;
106    }
107    return NULL;
108}
109
110/* Look for a specific boolean option argument in the list */
111char *
112find_plist_option(Package *pkg, const char *name)
113{
114    PackingList p = pkg->head;
115
116    while (p) {
117	if (p->type == PLIST_OPTION && !strcmp(p->name, name))
118	    return p->name;
119	p = p->next;
120    }
121    return NULL;
122}
123
124/*
125 * Delete plist item 'type' in the list (if 'name' is non-null, match it
126 * too.)  If 'all' is set, delete all items, not just the first occurrance.
127 */
128void
129delete_plist(Package *pkg, Boolean all, plist_t type, const char *name)
130{
131    PackingList p = pkg->head;
132
133    while (p) {
134	PackingList pnext = p->next;
135
136	if (p->type == type && (!name || !strcmp(name, p->name))) {
137	    free(p->name);
138	    if (p->prev)
139		p->prev->next = pnext;
140	    else
141		pkg->head = pnext;
142	    if (pnext)
143		pnext->prev = p->prev;
144	    else
145		pkg->tail = p->prev;
146	    free(p);
147	    if (!all)
148		return;
149	    p = pnext;
150	}
151	else
152	    p = p->next;
153    }
154}
155
156/* Allocate a new packing list entry */
157PackingList
158new_plist_entry(void)
159{
160    PackingList ret;
161
162    ret = (PackingList)malloc(sizeof(struct _plist));
163    bzero(ret, sizeof(struct _plist));
164    return ret;
165}
166
167/* Free an entire packing list */
168void
169free_plist(Package *pkg)
170{
171    PackingList p = pkg->head;
172
173    while (p) {
174	PackingList p1 = p->next;
175
176	free(p->name);
177	free(p);
178	p = p1;
179    }
180    pkg->head = pkg->tail = NULL;
181}
182
183/*
184 * For an ascii string denoting a plist command, return its code and
185 * optionally its argument(s)
186 */
187int
188plist_cmd(const char *s, char **arg)
189{
190    char cmd[FILENAME_MAX + 20];	/* 20 == fudge for max cmd len */
191    char *cp;
192    const char *sp;
193
194    strcpy(cmd, s);
195    str_lowercase(cmd);
196    cp = cmd;
197    sp = s;
198    while (*cp) {
199	if (isspace(*cp)) {
200	    *cp = '\0';
201	    while (isspace(*sp)) /* Never sure if macro, increment later */
202		++sp;
203	    break;
204	}
205	++cp, ++sp;
206    }
207    if (arg)
208	*arg = (char *)sp;
209    if (!strcmp(cmd, "cwd"))
210	return PLIST_CWD;
211    else if (!strcmp(cmd, "srcdir"))
212	return PLIST_SRC;
213    else if (!strcmp(cmd, "cd"))
214	return PLIST_CWD;
215    else if (!strcmp(cmd, "exec"))
216	return PLIST_CMD;
217    else if (!strcmp(cmd, "unexec"))
218	return PLIST_UNEXEC;
219    else if (!strcmp(cmd, "mode"))
220	return PLIST_CHMOD;
221    else if (!strcmp(cmd, "owner"))
222	return PLIST_CHOWN;
223    else if (!strcmp(cmd, "group"))
224	return PLIST_CHGRP;
225    else if (!strcmp(cmd, "noinst"))
226	return PLIST_NOINST;
227    else if (!strcmp(cmd, "comment")) {
228	if (!strncmp(*arg, "ORIGIN:", 7)) {
229	    *arg += 7;
230	    return PLIST_ORIGIN;
231	} else if (!strncmp(*arg, "DEPORIGIN:", 10)) {
232	    *arg += 10;
233	    return PLIST_DEPORIGIN;
234	}
235	return PLIST_COMMENT;
236    } else if (!strcmp(cmd, "ignore"))
237	return PLIST_IGNORE;
238    else if (!strcmp(cmd, "ignore_inst"))
239	return PLIST_IGNORE_INST;
240    else if (!strcmp(cmd, "name"))
241	return PLIST_NAME;
242    else if (!strcmp(cmd, "display"))
243	return PLIST_DISPLAY;
244    else if (!strcmp(cmd, "pkgdep"))
245	return PLIST_PKGDEP;
246    else if (!strcmp(cmd, "conflicts"))
247	return PLIST_CONFLICTS;
248    else if (!strcmp(cmd, "mtree"))
249	return PLIST_MTREE;
250    else if (!strcmp(cmd, "dirrm"))
251	return PLIST_DIR_RM;
252    else if (!strcmp(cmd, "option"))
253	return PLIST_OPTION;
254    else
255	return FAIL;
256}
257
258/* Read a packing list from a file */
259void
260read_plist(Package *pkg, FILE *fp)
261{
262    char *cp, pline[FILENAME_MAX];
263    int cmd, major, minor;
264
265    pkg->fmtver_maj = 1;
266    pkg->fmtver_mnr = 0;
267    pkg->origin = NULL;
268    while (fgets(pline, FILENAME_MAX, fp)) {
269	int len = strlen(pline);
270
271	while (len && isspace(pline[len - 1]))
272	    pline[--len] = '\0';
273	if (!len)
274	    continue;
275	cp = pline;
276	if (pline[0] != CMD_CHAR) {
277	    cmd = PLIST_FILE;
278	    goto bottom;
279	}
280	cmd = plist_cmd(pline + 1, &cp);
281	if (cmd == FAIL) {
282	    warnx("%s: unknown command '%s' (package tools out of date?)",
283		__func__, pline);
284	    goto bottom;
285	}
286	if (*cp == '\0') {
287	    cp = NULL;
288	    if (cmd == PLIST_PKGDEP) {
289		warnx("corrupted record (pkgdep line without argument), ignoring");
290		cmd = FAIL;
291	    }
292	    goto bottom;
293	}
294	if (cmd == PLIST_COMMENT && sscanf(cp, "PKG_FORMAT_REVISION:%d.%d\n",
295					   &major, &minor) == 2) {
296	    pkg->fmtver_maj = major;
297	    pkg->fmtver_mnr = minor;
298	    if (verscmp(pkg, PLIST_FMT_VER_MAJOR, PLIST_FMT_VER_MINOR) <= 0)
299		goto bottom;
300
301	    warnx("plist format revision (%d.%d) is higher than supported"
302		  "(%d.%d)", pkg->fmtver_maj, pkg->fmtver_mnr,
303		  PLIST_FMT_VER_MAJOR, PLIST_FMT_VER_MINOR);
304	    if (pkg->fmtver_maj > PLIST_FMT_VER_MAJOR) {
305		cleanup(0);
306		exit(2);
307	    }
308	}
309bottom:
310	add_plist(pkg, cmd, cp);
311    }
312}
313
314/* Write a packing list to a file, converting commands to ascii equivs */
315void
316write_plist(Package *pkg, FILE *fp)
317{
318    PackingList plist = pkg->head;
319
320    while (plist) {
321	switch(plist->type) {
322	case PLIST_FILE:
323	    fprintf(fp, "%s\n", plist->name);
324	    break;
325
326	case PLIST_CWD:
327	    fprintf(fp, "%ccwd %s\n", CMD_CHAR, (plist->name == NULL) ? "" : plist->name);
328	    break;
329
330	case PLIST_SRC:
331	    fprintf(fp, "%csrcdir %s\n", CMD_CHAR, plist->name);
332	    break;
333
334	case PLIST_CMD:
335	    fprintf(fp, "%cexec %s\n", CMD_CHAR, plist->name);
336	    break;
337
338	case PLIST_UNEXEC:
339	    fprintf(fp, "%cunexec %s\n", CMD_CHAR, plist->name);
340	    break;
341
342	case PLIST_CHMOD:
343	    fprintf(fp, "%cmode %s\n", CMD_CHAR, plist->name ? plist->name : "");
344	    break;
345
346	case PLIST_CHOWN:
347	    fprintf(fp, "%cowner %s\n", CMD_CHAR, plist->name ? plist->name : "");
348	    break;
349
350	case PLIST_CHGRP:
351	    fprintf(fp, "%cgroup %s\n", CMD_CHAR, plist->name ? plist->name : "");
352	    break;
353
354	case PLIST_COMMENT:
355	    fprintf(fp, "%ccomment %s\n", CMD_CHAR, plist->name);
356	    break;
357
358	case PLIST_NOINST:
359	    fprintf(fp, "%cnoinst %s\n", CMD_CHAR, plist->name);
360	    break;
361
362	case PLIST_IGNORE:
363	case PLIST_IGNORE_INST:		/* a one-time non-ignored file */
364	    fprintf(fp, "%cignore\n", CMD_CHAR);
365	    break;
366
367	case PLIST_NAME:
368	    fprintf(fp, "%cname %s\n", CMD_CHAR, plist->name);
369	    break;
370
371	case PLIST_DISPLAY:
372	    fprintf(fp, "%cdisplay %s\n", CMD_CHAR, plist->name);
373	    break;
374
375	case PLIST_PKGDEP:
376	    fprintf(fp, "%cpkgdep %s\n", CMD_CHAR, plist->name);
377	    break;
378
379	case PLIST_CONFLICTS:
380	    fprintf(fp, "%cconflicts %s\n", CMD_CHAR, plist->name);
381	    break;
382
383	case PLIST_MTREE:
384	    fprintf(fp, "%cmtree %s\n", CMD_CHAR, plist->name);
385	    break;
386
387	case PLIST_DIR_RM:
388	    fprintf(fp, "%cdirrm %s\n", CMD_CHAR, plist->name);
389	    break;
390
391	case PLIST_OPTION:
392	    fprintf(fp, "%coption %s\n", CMD_CHAR, plist->name);
393	    break;
394
395	case PLIST_ORIGIN:
396	    fprintf(fp, "%ccomment ORIGIN:%s\n", CMD_CHAR, plist->name);
397	    break;
398
399	case PLIST_DEPORIGIN:
400	    fprintf(fp, "%ccomment DEPORIGIN:%s\n", CMD_CHAR, plist->name);
401	    break;
402
403	default:
404	    cleanup(0);
405	    errx(2, "%s: unknown command type %d (%s)", __func__,
406		plist->type, plist->name);
407	    break;
408	}
409	plist = plist->next;
410    }
411}
412
413/*
414 * Delete the results of a package installation.
415 *
416 * This is here rather than in the pkg_delete code because pkg_add needs to
417 * run it too in cases of failure.
418 */
419int
420delete_package(Boolean ign_err, Boolean nukedirs, Package *pkg)
421{
422    PackingList p;
423    const char *Where = ".", *last_file = "";
424    Boolean fail = SUCCESS;
425    Boolean preserve;
426    char tmp[FILENAME_MAX], *name = NULL;
427    char *prefix = NULL;
428
429    preserve = find_plist_option(pkg, "preserve") ? TRUE : FALSE;
430    for (p = pkg->head; p; p = p->next) {
431	switch (p->type)  {
432	case PLIST_NAME:
433	    name = p->name;
434	    break;
435
436	case PLIST_IGNORE:
437	    p = p->next;
438	    break;
439
440	case PLIST_CWD:
441	    if (!prefix)
442		prefix = p->name;
443	    Where = (p->name == NULL) ? prefix : p->name;
444	    if (Verbose)
445		printf("Change working directory to %s\n", Where);
446	    break;
447
448	case PLIST_UNEXEC:
449	    format_cmd(tmp, FILENAME_MAX, p->name, Where, last_file);
450	    if (Verbose)
451		printf("Execute '%s'\n", tmp);
452	    if (!Fake && system(tmp)) {
453		warnx("unexec command for '%s' failed", tmp);
454		fail = FAIL;
455	    }
456	    break;
457
458	case PLIST_FILE:
459	    last_file = p->name;
460	    sprintf(tmp, "%s/%s", Where, p->name);
461	    if (isdir(tmp) && fexists(tmp) && !issymlink(tmp)) {
462		warnx("cannot delete specified file '%s' - it is a directory!\n"
463	   "this packing list is incorrect - ignoring delete request", tmp);
464	    }
465	    else {
466		if (p->next && p->next->type == PLIST_COMMENT && !strncmp(p->next->name, "MD5:", 4)) {
467		    char *cp = NULL, buf[33];
468
469		    /*
470		     * For packing lists whose version is 1.1 or greater, the md5
471		     * hash for a symlink is calculated on the string returned
472		     * by readlink().
473		     */
474		    if (issymlink(tmp) && verscmp(pkg, 1, 0) > 0) {
475			int len;
476			char linkbuf[FILENAME_MAX];
477
478			if ((len = readlink(tmp, linkbuf, FILENAME_MAX)) > 0)
479			     cp = MD5Data((unsigned char *)linkbuf, len, buf);
480		    } else if (isfile(tmp) || verscmp(pkg, 1, 1) < 0)
481			cp = MD5File(tmp, buf);
482
483		    if (cp != NULL) {
484			/* Mismatch? */
485			if (strcmp(cp, p->next->name + 4)) {
486			    warnx("'%s' fails original MD5 checksum - %s",
487				  tmp, Force ? "deleted anyway." : "not deleted.");
488			    if (!Force) {
489				fail = FAIL;
490				continue;
491			    }
492			}
493		    }
494		}
495		if (Verbose)
496		    printf("Delete file %s\n", tmp);
497		if (!Fake) {
498		    if (delete_hierarchy(tmp, ign_err, nukedirs))
499			fail = FAIL;
500		    if (preserve && name) {
501			char tmp2[FILENAME_MAX];
502
503			if (make_preserve_name(tmp2, FILENAME_MAX, name, tmp)) {
504			    if (fexists(tmp2)) {
505				if (rename(tmp2, tmp))
506				   warn("preserve: unable to restore %s as %s",
507					tmp2, tmp);
508			    }
509			}
510		    }
511		}
512	    }
513	    break;
514
515	case PLIST_DIR_RM:
516	    sprintf(tmp, "%s/%s", Where, p->name);
517	    if (!isdir(tmp) && fexists(tmp)) {
518		warnx("cannot delete specified directory '%s' - it is a file!\n"
519	"this packing list is incorrect - ignoring delete request", tmp);
520	    }
521	    else {
522		if (Verbose)
523		    printf("Delete directory %s\n", tmp);
524		if (!Fake && delete_hierarchy(tmp, ign_err, FALSE)) {
525		    warnx("unable to completely remove directory '%s'", tmp);
526		    fail = FAIL;
527		}
528	    }
529	    last_file = p->name;
530	    break;
531
532	default:
533	    break;
534	}
535    }
536    return fail;
537}
538
539#ifdef DEBUG
540#define RMDIR(dir) vsystem("%s %s", RMDIR_CMD, dir)
541#define REMOVE(dir,ie) vsystem("%s %s%s", REMOVE_CMD, (ie ? "-f " : ""), dir)
542#else
543#define RMDIR rmdir
544#define	REMOVE(file,ie) (remove(file) && !(ie))
545#endif
546
547/* Selectively delete a hierarchy */
548int
549delete_hierarchy(const char *dir, Boolean ign_err, Boolean nukedirs)
550{
551    char *cp1, *cp2;
552
553    cp1 = cp2 = strdup(dir);
554    if (!fexists(dir) && !issymlink(dir)) {
555	if (!ign_err)
556	    warnx("%s '%s' doesn't exist",
557		isdir(dir) ? "directory" : "file", dir);
558	return !ign_err;
559    }
560    else if (nukedirs) {
561	if (vsystem("%s -r%s %s", REMOVE_CMD, (ign_err ? "f" : ""), dir))
562	    return 1;
563    }
564    else if (isdir(dir) && !issymlink(dir)) {
565	if (RMDIR(dir) && !ign_err)
566	    return 1;
567    }
568    else {
569	if (REMOVE(dir, ign_err))
570	    return 1;
571    }
572
573    if (!nukedirs)
574	return 0;
575    while (cp2) {
576	if ((cp2 = strrchr(cp1, '/')) != NULL)
577	    *cp2 = '\0';
578	if (!isemptydir(dir))
579	    return 0;
580	if (RMDIR(dir) && !ign_err) {
581	    if (!fexists(dir))
582		warnx("directory '%s' doesn't exist", dir);
583	    else
584		return 1;
585	}
586	/* back up the pathname one component */
587	if (cp2) {
588	    cp1 = strdup(dir);
589	}
590    }
591    return 0;
592}
593