chiark / gitweb /
chkpath.c, tmpdir.c, utils.c: Add option to trust private groups.
[checkpath] / chkpath.c
1 /* -*-c-*-
2  *
3  * Check a user's file search path
4  *
5  * (c) 1999 Mark Wooding
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of chkpath.
11  *
12  * chkpath is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * chkpath is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with chkpath; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include "config.h"
30
31 #include <errno.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <pwd.h>
38 #include <grp.h>
39
40 #include <mLib/alloc.h>
41 #include <mLib/mdwopt.h>
42 #include <mLib/quis.h>
43 #include <mLib/report.h>
44
45 #include "checkpath.h"
46 #include "utils.h"
47
48 /*----- Main code ---------------------------------------------------------*/
49
50 /* --- @report@ --- */
51
52 static void report(unsigned what, int verbose,
53                    const char *p, const char *msg,
54                    void *arg)
55   { moan("%s", msg); }
56
57 /* --- @usage@ --- */
58
59 static void usage(FILE *fp)
60   { fprintf(fp, "Usage: %s [-Tpqstv] [-g NAME] [PATH...]\n", QUIS); }
61
62 /* --- @version@ --- */
63
64 static void version(FILE *fp)
65   { fprintf(fp, "%s version %s\n", QUIS, VERSION); }
66
67 /* --- @help@ --- */
68
69 static void help(FILE *fp)
70 {
71   version(fp);
72   putc('\n', fp);
73   usage(fp);
74   fputs("\n\
75 Checks a path string (by default the PATH variable) for security.  It\n\
76 ensures that only `root' or the calling user can write to all the parent\n\
77 directories of the path elements, so nobody can maliciously replace the\n\
78 binaries unexpectedly.\n\
79 \n\
80 Options provided are:\n\
81 \n\
82 -h, --help              Display this help message.\n\
83 -V, --version           Display the program's version number.\n\
84 -u, --usage             Show a terse usage summary.\n\
85 \n\
86 -T, --private-group     Accept paths writable by primary group if it has\n\
87                         no other members.\n\
88 -g, --group NAME        Consider members of group NAME trustworthy.\n\
89 -p, --print             Write the secure path elements to standard output.\n\
90 -q, --quiet             Be quiet about the search progress (cumulative).\n\
91 -s, --sticky            Consider sticky directories secure against\n\
92                         modification by world and group (not recommended).\n\
93 -t, --trust-groups      Consider other members of your group trustworthy.\n\
94 -v, --verbose           Be verbose about the search progress (cumulative).\n\
95 ",
96         fp);
97 }
98
99 int main(int argc, char *argv[])
100 {
101   unsigned bad = 0;
102   int i;
103   char *p, *q, *path;
104   struct checkpath cp;
105   gid_t gid;
106   int f = 0;
107
108 #define f_print 1u
109 #define f_colon 2u
110
111   /* --- Initialize the world --- */
112
113   ego(argv[0]);
114
115   /* --- Set up path scanning defaults --- */
116
117   cp.cp_verbose = 1;
118   cp.cp_what = (CP_PROBLEMS | CP_REPORT | CP_SYMLINK) & ~CP_WRGRP;
119   cp.cp_report = report;
120   cp.cp_arg = 0;
121   cp.cp_gids = 0;
122   checkpath_setuid(&cp);
123
124   /* --- Parse the options --- */
125
126   for (;;) {
127     static struct option opts[] = {
128       { "help",         0,              0,      'h' },
129       { "version",      0,              0,      'V' },
130       { "usage",        0,              0,      'u' },
131       { "private-group", 0,             0,      'T' },
132       { "group",        OPTF_ARGREQ,    0,      'g' },
133       { "print",        0,              0,      'p' },
134       { "quiet",        0,              0,      'q' },
135       { "sticky",       0,              0,      's' },
136       { "trust-groups", 0,              0,      't' },
137       { "verbose",      0,              0,      'v' },
138       { 0,              0,              0,      0 }
139     };
140
141     i = mdwopt(argc, argv, "hVu" "Tg:pqstv", opts, 0, 0, 0);
142     if (i < 0)
143       break;
144     switch (i) {
145       case 'h':
146         help(stdout);
147         exit(0);
148       case 'V':
149         version(stdout);
150         exit(0);
151       case 'u':
152         usage(stdout);
153         exit(0);
154       case 'T':
155         if (!private_group(&gid, cp.cp_verbose) &&
156             checkpath_addgid(&cp, gid))
157           die(1, "too many groups");
158         break;
159       case 'g':
160         allowgroup(&cp, optarg);
161         break;
162       case 'p':
163         f |= f_print;
164         break;
165       case 'q':
166         if (cp.cp_verbose)
167           cp.cp_verbose--;
168         break;
169       case 's':
170         cp.cp_what |= CP_STICKYOK;
171         break;
172       case 't':
173         if (checkpath_setgid(&cp) || checkpath_setgroups(&cp))
174           die(1, "too many groups");
175         break;
176       case 'v':
177         cp.cp_verbose++;
178         break;
179       default:
180         bad = 1;
181         break;
182     }
183   }
184
185   if (bad) {
186     usage(stderr);
187     exit(1);
188   }
189
190   /* --- Sort out what needs doing --- */
191
192   if (optind == argc) {
193     path = getenv("PATH");
194     argv = &path;
195     argc = 1;
196     optind = 0;
197   }
198
199   for (i = optind; i < argc; i++) {
200     p = xstrdup(argv[i]);
201     q = strtok(p, ":");
202     while (q) {
203       unsigned b = checkpath(q, &cp);
204       if (!b && (f & f_print)) {
205         if (f & f_colon)
206           putchar(':');
207         fputs(q, stdout);
208         f |= f_colon;
209       }
210       bad |= b;
211       q = strtok(0, ":");
212     }
213     free(p);
214   }
215
216   if (f & f_colon)
217     putchar('\n');
218
219   return (bad);
220 }
221
222 /*----- That's all, folks -------------------------------------------------*/