tmpdir: Allow trusting of particular groups.
[checkpath] / chkpath.c
CommitLineData
efa7a97b 1/* -*-c-*-
2 *
efa7a97b 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
efa7a97b 27/*----- Header files ------------------------------------------------------*/
28
29#include <errno.h>
30#include <limits.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35#include <mLib/alloc.h>
36#include <mLib/mdwopt.h>
37#include <mLib/quis.h>
38#include <mLib/report.h>
39
d7b5ee0c 40#include "checkpath.h"
efa7a97b 41
42/*----- Main code ---------------------------------------------------------*/
43
d8e11317
MW
44/* --- @report@ --- */
45
d7b5ee0c 46static void report(unsigned what, int verbose,
efa7a97b 47 const char *p, const char *msg,
48 void *arg)
d8e11317 49 { moan("%s", msg); }
efa7a97b 50
51/* --- @usage@ --- */
52
53static void usage(FILE *fp)
d8e11317 54 { fprintf(fp, "Usage: %s [-vqstp] [PATH...]\n", QUIS); }
efa7a97b 55
56/* --- @version@ --- */
57
58static void version(FILE *fp)
d8e11317 59 { fprintf(fp, "%s version %s\n", QUIS, VERSION); }
efa7a97b 60
61/* --- @help@ --- */
62
63static void help(FILE *fp)
64{
65 version(fp);
66 putc('\n', fp);
67 usage(fp);
68 fputs("\n\
69Checks a path string (by default the PATH variable) for security. It\n\
70ensures that only `root' or the calling user can write to all the parent\n\
71directories of the path elements, so nobody can maliciously replace the\n\
72binaries unexpectedly.\n\
73\n\
74Options provided are:\n\
75\n\
76-h, --help Display this help message.\n\
77-V, --version Display the program's version number.\n\
78-u, --usage Show a terse usage summary.\n\
79\n\
80-v, --verbose Be verbose about the search progress (cumulative).\n\
81-q, --quiet Be quiet about the search progress (cumulative).\n\
82-s, --sticky Consider sticky directories secure against\n\
83 modification by world and group (not recommended).\n\
84-t, --trust-group Consider other members of your group trustworthy.\n\
85-p, --print Write the secure path elements to standard output.\n\
86",
87 fp);
88}
89
90int main(int argc, char *argv[])
91{
d7b5ee0c 92 unsigned bad = 0;
efa7a97b 93 int i;
94 char *p, *q, *path;
d7b5ee0c 95 struct checkpath cp;
efa7a97b 96 int f = 0;
97
7868d789 98#define f_print 1u
99#define f_colon 2u
efa7a97b 100
101 /* --- Initialize the world --- */
102
103 ego(argv[0]);
104
105 /* --- Set up path scanning defaults --- */
106
107 cp.cp_verbose = 1;
d7b5ee0c 108 cp.cp_what = CP_PROBLEMS | CP_REPORT | CP_SYMLINK;
efa7a97b 109 cp.cp_report = report;
110 cp.cp_arg = 0;
d7b5ee0c 111 checkpath_setids(&cp);
efa7a97b 112
113 /* --- Parse the options --- */
114
115 for (;;) {
116 static struct option opts[] = {
117 { "help", 0, 0, 'h' },
118 { "version", 0, 0, 'V' },
119 { "usage", 0, 0, 'u' },
120 { "verbose", 0, 0, 'v' },
121 { "quiet", 0, 0, 'q' },
122 { "sticky", 0, 0, 's' },
123 { "trust-group", 0, 0, 't' },
124 { "print", 0, 0, 'p' },
125 { 0, 0, 0, 0 }
126 };
127 int i = mdwopt(argc, argv, "hVu vqstp", opts, 0, 0, 0);
128
129 if (i < 0)
130 break;
131 switch (i) {
132 case 'h':
133 help(stdout);
134 exit(0);
135 case 'V':
136 version(stdout);
137 exit(0);
138 case 'u':
139 usage(stdout);
140 exit(0);
141 case 'v':
142 cp.cp_verbose++;
143 break;
144 case 'q':
145 if (cp.cp_verbose)
146 cp.cp_verbose--;
147 break;
148 case 's':
149 cp.cp_what |= CP_STICKYOK;
150 break;
151 case 't':
152 cp.cp_what = (cp.cp_what & ~CP_WRGRP) | CP_WROTHGRP;
153 break;
154 case 'p':
155 f |= f_print;
156 break;
157 default:
158 bad = 1;
159 break;
160 }
161 }
162
163 if (bad) {
164 usage(stderr);
165 exit(1);
166 }
167
168 /* --- Sort out what needs doing --- */
169
170 if (optind == argc) {
171 path = getenv("PATH");
172 argv = &path;
173 argc = 1;
174 optind = 0;
175 }
176
177 for (i = optind; i < argc; i++) {
178 p = xstrdup(argv[i]);
179 q = strtok(p, ":");
180 while (q) {
d7b5ee0c 181 unsigned b = checkpath(q, &cp);
efa7a97b 182 if (!b && (f & f_print)) {
183 if (f & f_colon)
184 putchar(':');
185 fputs(q, stdout);
186 f |= f_colon;
187 }
188 bad |= b;
189 q = strtok(0, ":");
190 }
191 free(p);
192 }
193
194 if (f & f_colon)
195 putchar('\n');
196
197 return (bad);
198}
199
200/*----- That's all, folks -------------------------------------------------*/