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