diff options
Diffstat (limited to 'src/ftee/main.c')
-rw-r--r-- | src/ftee/main.c | 36 |
1 files changed, 16 insertions, 20 deletions
diff --git a/src/ftee/main.c b/src/ftee/main.c index 8daea8487..2b27baa5a 100644 --- a/src/ftee/main.c +++ b/src/ftee/main.c | |||
@@ -179,10 +179,6 @@ static int is_link(const char *fname) { | |||
179 | return 0; | 179 | return 0; |
180 | } | 180 | } |
181 | 181 | ||
182 | |||
183 | |||
184 | |||
185 | |||
186 | static void usage(void) { | 182 | static void usage(void) { |
187 | printf("Usage: ftee filename\n"); | 183 | printf("Usage: ftee filename\n"); |
188 | } | 184 | } |
@@ -193,37 +189,33 @@ int main(int argc, char **argv) { | |||
193 | usage(); | 189 | usage(); |
194 | exit(1); | 190 | exit(1); |
195 | } | 191 | } |
192 | if (strcmp(argv[1], "--help") == 0) { | ||
193 | usage(); | ||
194 | return 0; | ||
195 | } | ||
196 | char *fname = argv[1]; | 196 | char *fname = argv[1]; |
197 | 197 | ||
198 | 198 | ||
199 | // do not accept directories, links, and files with ".." | 199 | // do not accept directories, links, and files with ".." |
200 | if (strstr(fname, "..") || is_link(fname) || is_dir(fname)) { | 200 | if (strstr(fname, "..") || is_link(fname) || is_dir(fname)) |
201 | fprintf(stderr, "Error: invalid output file. Links, directories and files with \"..\" are not allowed.\n"); | 201 | goto errexit; |
202 | exit(1); | ||
203 | } | ||
204 | 202 | ||
205 | struct stat s; | 203 | struct stat s; |
206 | if (stat(fname, &s) == 0) { | 204 | if (stat(fname, &s) == 0) { |
207 | // check permissions | 205 | // check permissions |
208 | if (s.st_uid != getuid() || s.st_gid != getgid()) { | 206 | if (s.st_uid != getuid() || s.st_gid != getgid()) |
209 | fprintf(stderr, "Error: the output file needs to be owned by the current user.\n"); | 207 | goto errexit; |
210 | exit(1); | ||
211 | } | ||
212 | 208 | ||
213 | // check hard links | 209 | // check hard links |
214 | if (s.st_nlink != 1) { | 210 | if (s.st_nlink != 1) |
215 | fprintf(stderr, "Error: no hard links allowed.\n"); | 211 | goto errexit; |
216 | exit(1); | ||
217 | } | ||
218 | } | 212 | } |
219 | 213 | ||
220 | // check if we can append to this file | 214 | // check if we can append to this file |
221 | /* coverity[toctou] */ | 215 | /* coverity[toctou] */ |
222 | FILE *fp = fopen(fname, "a"); | 216 | FILE *fp = fopen(fname, "a"); |
223 | if (!fp) { | 217 | if (!fp) |
224 | fprintf(stderr, "Error: cannot open output file %s\n", fname); | 218 | goto errexit; |
225 | exit(1); | ||
226 | } | ||
227 | fclose(fp); | 219 | fclose(fp); |
228 | 220 | ||
229 | 221 | ||
@@ -244,4 +236,8 @@ int main(int argc, char **argv) { | |||
244 | 236 | ||
245 | log_close(); | 237 | log_close(); |
246 | return 0; | 238 | return 0; |
239 | |||
240 | errexit: | ||
241 | fprintf(stderr, "Error ftee: invalid output file.\n"); | ||
242 | return 1; | ||
247 | } | 243 | } |