chiark / gitweb /
a7158d3c7efb3978fc7813e0f9dd957b630c3e03
[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 [-pqstv] [-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 -g, --group NAME        Consider members of group NAME trustworthy.\n\
87 -p, --print             Write the secure path elements to standard output.\n\
88 -q, --quiet             Be quiet about the search progress (cumulative).\n\
89 -s, --sticky            Consider sticky directories secure against\n\
90                         modification by world and group (not recommended).\n\
91 -t, --trust-groups      Consider other members of your group trustworthy.\n\
92 -v, --verbose           Be verbose about the search progress (cumulative).\n\
93 ",
94         fp);
95 }
96
97 int main(int argc, char *argv[])
98 {
99   unsigned bad = 0;
100   int i;
101   char *p, *q, *path;
102   struct checkpath cp;
103   int f = 0;
104
105 #define f_print 1u
106 #define f_colon 2u
107
108   /* --- Initialize the world --- */
109
110   ego(argv[0]);
111
112   /* --- Set up path scanning defaults --- */
113
114   cp.cp_verbose = 1;
115   cp.cp_what = (CP_PROBLEMS | CP_REPORT | CP_SYMLINK) & ~CP_WRGRP;
116   cp.cp_report = report;
117   cp.cp_arg = 0;
118   cp.cp_gids = 0;
119   checkpath_setuid(&cp);
120
121   /* --- Parse the options --- */
122
123   for (;;) {
124     static struct option opts[] = {
125       { "help",         0,              0,      'h' },
126       { "version",      0,              0,      'V' },
127       { "usage",        0,              0,      'u' },
128       { "group",        OPTF_ARGREQ,    0,      'g' },
129       { "print",        0,              0,      'p' },
130       { "quiet",        0,              0,      'q' },
131       { "sticky",       0,              0,      's' },
132       { "trust-groups", 0,              0,      't' },
133       { "verbose",      0,              0,      'v' },
134       { 0,              0,              0,      0 }
135     };
136
137     i = mdwopt(argc, argv, "hVu" "g:pqstv", opts, 0, 0, 0);
138     if (i < 0)
139       break;
140     switch (i) {
141       case 'h':
142         help(stdout);
143         exit(0);
144       case 'V':
145         version(stdout);
146         exit(0);
147       case 'u':
148         usage(stdout);
149         exit(0);
150       case 'g':
151         allowgroup(&cp, optarg);
152         break;
153       case 'p':
154         f |= f_print;
155         break;
156       case 'q':
157         if (cp.cp_verbose)
158           cp.cp_verbose--;
159         break;
160       case 's':
161         cp.cp_what |= CP_STICKYOK;
162         break;
163       case 't':
164         if (checkpath_setgid(&cp) || checkpath_setgroups(&cp))
165           die(1, "too many groups");
166         break;
167       case 'v':
168         cp.cp_verbose++;
169         break;
170       default:
171         bad = 1;
172         break;
173     }
174   }
175
176   if (bad) {
177     usage(stderr);
178     exit(1);
179   }
180
181   /* --- Sort out what needs doing --- */
182
183   if (optind == argc) {
184     path = getenv("PATH");
185     argv = &path;
186     argc = 1;
187     optind = 0;
188   }
189
190   for (i = optind; i < argc; i++) {
191     p = xstrdup(argv[i]);
192     q = strtok(p, ":");
193     while (q) {
194       unsigned b = checkpath(q, &cp);
195       if (!b && (f & f_print)) {
196         if (f & f_colon)
197           putchar(':');
198         fputs(q, stdout);
199         f |= f_colon;
200       }
201       bad |= b;
202       q = strtok(0, ":");
203     }
204     free(p);
205   }
206
207   if (f & f_colon)
208     putchar('\n');
209
210   return (bad);
211 }
212
213 /*----- That's all, folks -------------------------------------------------*/