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