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) |
5dd99356 |
49 | { moan("%s", msg); } |
efa7a97b |
50 | |
51 | /* --- @usage@ --- */ |
52 | |
53 | static void usage(FILE *fp) |
5dd99356 |
54 | { fprintf(fp, "Usage: %s [-vqstp] [PATH...]\n", QUIS); } |
efa7a97b |
55 | |
56 | /* --- @version@ --- */ |
57 | |
58 | static void version(FILE *fp) |
5dd99356 |
59 | { fprintf(fp, "%s version %s\n", QUIS, VERSION); } |
efa7a97b |
60 | |
61 | /* --- @help@ --- */ |
62 | |
63 | static void help(FILE *fp) |
64 | { |
65 | version(fp); |
66 | putc('\n', fp); |
67 | usage(fp); |
68 | fputs("\n\ |
69 | Checks a path string (by default the PATH variable) for security. It\n\ |
70 | ensures that only `root' or the calling user can write to all the parent\n\ |
71 | directories of the path elements, so nobody can maliciously replace the\n\ |
72 | binaries unexpectedly.\n\ |
73 | \n\ |
74 | Options 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 | |
90 | int 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 | }; |
5dd99356 |
127 | int i = mdwopt(argc, argv, "hVu" "vqstp", opts, 0, 0, 0); |
efa7a97b |
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 -------------------------------------------------*/ |