efa7a97b |
1 | /* -*-c-*- |
2 | * |
194363d4 |
3 | * $Id: chkpath.c,v 1.4 2004/04/08 01:36:22 mdw Exp $ |
efa7a97b |
4 | * |
5 | * Check a user's file search path |
6 | * |
7 | * (c) 1999 Mark Wooding |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of chkpath. |
13 | * |
14 | * chkpath is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by |
16 | * the Free Software Foundation; either version 2 of the License, or |
17 | * (at your option) any later version. |
18 | * |
19 | * chkpath is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
25 | * along with chkpath; if not, write to the Free Software Foundation, |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
27 | */ |
28 | |
efa7a97b |
29 | /*----- Header files ------------------------------------------------------*/ |
30 | |
31 | #include <errno.h> |
32 | #include <limits.h> |
33 | #include <stdio.h> |
34 | #include <stdlib.h> |
35 | #include <string.h> |
36 | |
37 | #include <mLib/alloc.h> |
38 | #include <mLib/mdwopt.h> |
39 | #include <mLib/quis.h> |
40 | #include <mLib/report.h> |
41 | |
d7b5ee0c |
42 | #include "checkpath.h" |
efa7a97b |
43 | |
44 | /*----- Main code ---------------------------------------------------------*/ |
45 | |
d7b5ee0c |
46 | static void report(unsigned what, int verbose, |
efa7a97b |
47 | const char *p, const char *msg, |
48 | void *arg) |
49 | { |
50 | moan("%s", msg); |
51 | } |
52 | |
53 | /* --- @usage@ --- */ |
54 | |
55 | static void usage(FILE *fp) |
56 | { |
57 | fprintf(fp, "Usage: %s [-vqstp] [PATH...]\n", QUIS); |
58 | } |
59 | |
60 | /* --- @version@ --- */ |
61 | |
62 | static void version(FILE *fp) |
63 | { |
64 | fprintf(fp, "%s version %s\n", QUIS, VERSION); |
65 | } |
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\ |
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; |
d7b5ee0c |
114 | cp.cp_what = CP_PROBLEMS | CP_REPORT | CP_SYMLINK; |
efa7a97b |
115 | cp.cp_report = report; |
116 | cp.cp_arg = 0; |
d7b5ee0c |
117 | checkpath_setids(&cp); |
efa7a97b |
118 | |
119 | /* --- Parse the options --- */ |
120 | |
121 | for (;;) { |
122 | static struct option opts[] = { |
123 | { "help", 0, 0, 'h' }, |
124 | { "version", 0, 0, 'V' }, |
125 | { "usage", 0, 0, 'u' }, |
126 | { "verbose", 0, 0, 'v' }, |
127 | { "quiet", 0, 0, 'q' }, |
128 | { "sticky", 0, 0, 's' }, |
129 | { "trust-group", 0, 0, 't' }, |
130 | { "print", 0, 0, 'p' }, |
131 | { 0, 0, 0, 0 } |
132 | }; |
133 | int i = mdwopt(argc, argv, "hVu vqstp", opts, 0, 0, 0); |
134 | |
135 | if (i < 0) |
136 | break; |
137 | switch (i) { |
138 | case 'h': |
139 | help(stdout); |
140 | exit(0); |
141 | case 'V': |
142 | version(stdout); |
143 | exit(0); |
144 | case 'u': |
145 | usage(stdout); |
146 | exit(0); |
147 | case 'v': |
148 | cp.cp_verbose++; |
149 | break; |
150 | case 'q': |
151 | if (cp.cp_verbose) |
152 | cp.cp_verbose--; |
153 | break; |
154 | case 's': |
155 | cp.cp_what |= CP_STICKYOK; |
156 | break; |
157 | case 't': |
158 | cp.cp_what = (cp.cp_what & ~CP_WRGRP) | CP_WROTHGRP; |
159 | break; |
160 | case 'p': |
161 | f |= f_print; |
162 | break; |
163 | default: |
164 | bad = 1; |
165 | break; |
166 | } |
167 | } |
168 | |
169 | if (bad) { |
170 | usage(stderr); |
171 | exit(1); |
172 | } |
173 | |
174 | /* --- Sort out what needs doing --- */ |
175 | |
176 | if (optind == argc) { |
177 | path = getenv("PATH"); |
178 | argv = &path; |
179 | argc = 1; |
180 | optind = 0; |
181 | } |
182 | |
183 | for (i = optind; i < argc; i++) { |
184 | p = xstrdup(argv[i]); |
185 | q = strtok(p, ":"); |
186 | while (q) { |
d7b5ee0c |
187 | unsigned b = checkpath(q, &cp); |
efa7a97b |
188 | if (!b && (f & f_print)) { |
189 | if (f & f_colon) |
190 | putchar(':'); |
191 | fputs(q, stdout); |
192 | f |= f_colon; |
193 | } |
194 | bad |= b; |
195 | q = strtok(0, ":"); |
196 | } |
197 | free(p); |
198 | } |
199 | |
200 | if (f & f_colon) |
201 | putchar('\n'); |
202 | |
203 | return (bad); |
204 | } |
205 | |
206 | /*----- That's all, folks -------------------------------------------------*/ |