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