]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/namei.c
misc-utils: verify writing to streams was successful
[thirdparty/util-linux.git] / misc-utils / namei.c
1 /*
2 * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
3 *
4 * This file is part of util-linux.
5 *
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This file is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * The original namei(1) was writtent by:
17 * Roger S. Southwick (May 2, 1990)
18 * Steve Tell (March 28, 1991)
19 * Arkadiusz Mikiewicz (1999-02-22)
20 * Li Zefan (2007-09-10).
21 */
22
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <string.h>
27 #include <strings.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/param.h>
33 #include <pwd.h>
34 #include <grp.h>
35
36 #include "c.h"
37 #include "xalloc.h"
38 #include "nls.h"
39 #include "widechar.h"
40 #include "strutils.h"
41 #include "closestream.h"
42
43 #ifndef MAXSYMLINKS
44 #define MAXSYMLINKS 256
45 #endif
46
47 #ifndef LOGIN_NAME_MAX
48 #define LOGIN_NAME_MAX 256
49 #endif
50
51 #define NAMEI_NOLINKS (1 << 1)
52 #define NAMEI_MODES (1 << 2)
53 #define NAMEI_MNTS (1 << 3)
54 #define NAMEI_OWNERS (1 << 4)
55 #define NAMEI_VERTICAL (1 << 5)
56
57
58 struct namei {
59 struct stat st; /* item lstat() */
60 char *name; /* item name */
61 char *abslink; /* absolute symlink path */
62 int relstart; /* offset of relative path in 'abslink' */
63 struct namei *next; /* next item */
64 int level;
65 int mountpoint; /* is mount point */
66 int noent; /* is this item not existing */
67 };
68
69 struct idcache {
70 unsigned long int id;
71 char *name;
72 struct idcache *next;
73 };
74
75 static int flags;
76 static int uwidth; /* maximal width of username */
77 static int gwidth; /* maximal width of groupname */
78 static struct idcache *gcache; /* groupnames */
79 static struct idcache *ucache; /* usernames */
80
81 static struct idcache *
82 get_id(struct idcache *ic, unsigned long int id)
83 {
84 while(ic) {
85 if (ic->id == id)
86 return ic;
87 ic = ic->next;
88 }
89 return NULL;
90 }
91
92 static void
93 free_idcache(struct idcache *ic)
94 {
95 while(ic) {
96 struct idcache *next = ic->next;
97 free(ic->name);
98 free(ic);
99 ic = next;
100 }
101 }
102
103 static void
104 add_id(struct idcache **ic, char *name, unsigned long int id, int *width)
105 {
106 struct idcache *nc, *x;
107 int w = 0;
108
109 nc = xcalloc(1, sizeof(*nc));
110 nc->id = id;
111
112 if (name) {
113 #ifdef HAVE_WIDECHAR
114 wchar_t wc[LOGIN_NAME_MAX + 1];
115
116 if (mbstowcs(wc, name, LOGIN_NAME_MAX) > 0) {
117 wc[LOGIN_NAME_MAX] = '\0';
118 w = wcswidth(wc, LOGIN_NAME_MAX);
119 }
120 else
121 #endif
122 w = strlen(name);
123 }
124 /* note, we ignore names with non-printable widechars */
125 if (w > 0)
126 nc->name = xstrdup(name);
127 else if (xasprintf(&nc->name, "%lu", id) == -1)
128 nc->name = NULL;
129
130 for (x = *ic; x && x->next; x = x->next);
131
132 /* add 'nc' at end of the 'ic' list */
133 if (x)
134 x->next = nc;
135 else
136 *ic = nc;
137 if (w <= 0)
138 w = nc->name ? strlen(nc->name) : 0;
139
140 *width = *width < w ? w : *width;
141 return;
142 }
143
144 static void
145 add_uid(unsigned long int id)
146 {
147 struct idcache *ic = get_id(ucache, id);
148
149 if (!ic) {
150 struct passwd *pw = getpwuid((uid_t) id);
151 add_id(&ucache, pw ? pw->pw_name : NULL, id, &uwidth);
152 }
153 }
154
155 static void
156 add_gid(unsigned long int id)
157 {
158 struct idcache *ic = get_id(gcache, id);
159
160 if (!ic) {
161 struct group *gr = getgrgid((gid_t) id);
162 add_id(&gcache, gr ? gr->gr_name : NULL, id, &gwidth);
163 }
164 }
165
166 static void
167 free_namei(struct namei *nm)
168 {
169 while (nm) {
170 struct namei *next = nm->next;
171 free(nm->name);
172 free(nm->abslink);
173 free(nm);
174 nm = next;
175 }
176 }
177
178 static void
179 readlink_to_namei(struct namei *nm, const char *path)
180 {
181 char sym[PATH_MAX];
182 ssize_t sz;
183
184 sz = readlink(path, sym, sizeof(sym));
185 if (sz < 1)
186 err(EXIT_FAILURE, _("failed to read symlink: %s"), path);
187 if (*sym != '/') {
188 char *p = strrchr(path, '/');
189
190 nm->relstart = p ? p - path : 0;
191 if (nm->relstart)
192 sz += nm->relstart + 1;
193 }
194 nm->abslink = xmalloc(sz + 1);
195
196 if (*sym != '/' && nm->relstart) {
197 /* create the absolute path from the relative symlink */
198 memcpy(nm->abslink, path, nm->relstart);
199 *(nm->abslink + nm->relstart) = '/';
200 nm->relstart++;
201 memcpy(nm->abslink + nm->relstart, sym, sz - nm->relstart);
202 } else
203 memcpy(nm->abslink, sym, sz);
204 nm->abslink[sz] = '\0';
205 }
206
207 static struct stat *
208 dotdot_stat(const char *dirname, struct stat *st)
209 {
210 char *path;
211 size_t len;
212
213 #define DOTDOTDIR "/.."
214
215 if (!dirname)
216 return NULL;
217
218 len = strlen(dirname);
219 path = xmalloc(len + sizeof(DOTDOTDIR));
220
221 memcpy(path, dirname, len);
222 memcpy(path + len, DOTDOTDIR, sizeof(DOTDOTDIR));
223
224 if (stat(path, st))
225 err(EXIT_FAILURE, _("could not stat '%s'"), path);
226 free(path);
227 return st;
228 }
229
230 static struct namei *
231 new_namei(struct namei *parent, const char *path, const char *fname, int lev)
232 {
233 struct namei *nm;
234
235 if (!fname)
236 return NULL;
237 nm = xcalloc(1, sizeof(*nm));
238 if (parent)
239 parent->next = nm;
240
241 nm->level = lev;
242 nm->name = xstrdup(fname);
243
244 nm->noent = (lstat(path, &nm->st) == -1);
245 if (nm->noent)
246 return nm;
247
248 if (S_ISLNK(nm->st.st_mode))
249 readlink_to_namei(nm, path);
250 if (flags & NAMEI_OWNERS) {
251 add_uid(nm->st.st_uid);
252 add_gid(nm->st.st_gid);
253 }
254
255 if ((flags & NAMEI_MNTS) && S_ISDIR(nm->st.st_mode)) {
256 struct stat stbuf, *sb = NULL;
257
258 if (parent && S_ISDIR(parent->st.st_mode))
259 sb = &parent->st;
260 else if (!parent || S_ISLNK(parent->st.st_mode))
261 sb = dotdot_stat(path, &stbuf);
262
263 if (sb && (sb->st_dev != nm->st.st_dev || /* different device */
264 sb->st_ino == nm->st.st_ino)) /* root directory */
265 nm->mountpoint = 1;
266 }
267
268 return nm;
269 }
270
271 static struct namei *
272 add_namei(struct namei *parent, const char *orgpath, int start, struct namei **last)
273 {
274 struct namei *nm = NULL, *first = NULL;
275 char *fname, *end, *path;
276 int level = 0;
277
278 if (!orgpath)
279 return NULL;
280 if (parent) {
281 nm = parent;
282 level = parent->level + 1;
283 }
284 path = xstrdup(orgpath);
285 fname = path + start;
286
287 /* root directory */
288 if (*fname == '/') {
289 while (*fname == '/')
290 fname++; /* eat extra '/' */
291 first = nm = new_namei(nm, "/", "/", level);
292 }
293
294 for (end = fname; fname && end; ) {
295 /* set end of filename */
296 if (*fname) {
297 end = strchr(fname, '/');
298 if (end)
299 *end = '\0';
300
301 /* create a new entry */
302 nm = new_namei(nm, path, fname, level);
303 } else
304 end = NULL;
305 if (!first)
306 first = nm;
307 /* set begin of the next filename */
308 if (end) {
309 *end++ = '/';
310 while (*end == '/')
311 end++; /* eat extra '/' */
312 }
313 fname = end;
314 }
315
316 if (last)
317 *last = nm;
318
319 free(path);
320
321 return first;
322 }
323
324 static int
325 follow_symlinks(struct namei *nm)
326 {
327 int symcount = 0;
328
329 for (; nm; nm = nm->next) {
330 struct namei *next, *last;
331
332 if (nm->noent)
333 continue;
334 if (!S_ISLNK(nm->st.st_mode))
335 continue;
336 if (++symcount > MAXSYMLINKS) {
337 /* drop the rest of the list */
338 free_namei(nm->next);
339 nm->next = NULL;
340 return -1;
341 }
342 next = nm->next;
343 nm->next = add_namei(nm, nm->abslink, nm->relstart, &last);
344 if (last)
345 last->next = next;
346 else
347 nm->next = next;
348 }
349 return 0;
350 }
351
352 static int
353 print_namei(struct namei *nm, char *path)
354 {
355 int i;
356
357 if (path)
358 printf("f: %s\n", path);
359
360 for (; nm; nm = nm->next) {
361 char md[11];
362
363 if (nm->noent) {
364 printf(_("%s - No such file or directory\n"), nm->name);
365 return -1;
366 }
367
368 strmode(nm->st.st_mode, md);
369
370 if (nm->mountpoint)
371 md[0] = 'D';
372
373 if (!(flags & NAMEI_VERTICAL)) {
374 for (i = 0; i < nm->level; i++)
375 fputs(" ", stdout);
376 fputc(' ', stdout);
377 }
378
379 if (flags & NAMEI_MODES)
380 printf("%s", md);
381 else
382 printf("%c", md[0]);
383
384 if (flags & NAMEI_OWNERS) {
385 printf(" %-*s", uwidth,
386 get_id(ucache, nm->st.st_uid)->name);
387 printf(" %-*s", gwidth,
388 get_id(gcache, nm->st.st_gid)->name);
389 }
390
391 if (flags & NAMEI_VERTICAL)
392 for (i = 0; i < nm->level; i++)
393 fputs(" ", stdout);
394
395 if (S_ISLNK(nm->st.st_mode))
396 printf(" %s -> %s\n", nm->name,
397 nm->abslink + nm->relstart);
398 else
399 printf(" %s\n", nm->name);
400 }
401 return 0;
402 }
403
404 static void usage(int rc)
405 {
406 const char *p = program_invocation_short_name;
407 FILE *out = rc == EXIT_FAILURE ? stderr : stdout;
408
409 if (!*p)
410 p = "namei";
411
412 fputs(_("\nUsage:\n"), out);
413 fprintf(out,
414 _(" %s [options] pathname [pathname ...]\n"), p);
415
416 fputs(_("\nOptions:\n"), out);
417 fputs(_(" -h, --help displays this help text\n"
418 " -V, --version output version information and exit\n"
419 " -x, --mountpoints show mount point directories with a 'D'\n"
420 " -m, --modes show the mode bits of each file\n"
421 " -o, --owners show owner and group name of each file\n"
422 " -l, --long use a long listing format (-m -o -v) \n"
423 " -n, --nosymlinks don't follow symlinks\n"
424 " -v, --vertical vertical align of modes and owners\n"), out);
425
426 fputs(_("\nFor more information see namei(1).\n"), out);
427 exit(rc);
428 }
429
430 static const struct option longopts[] =
431 {
432 { "help", 0, 0, 'h' },
433 { "version", 0, 0, 'V' },
434 { "mountpoints",0, 0, 'x' },
435 { "modes", 0, 0, 'm' },
436 { "owners", 0, 0, 'o' },
437 { "long", 0, 0, 'l' },
438 { "nolinks", 0, 0, 'n' },
439 { "vertical", 0, 0, 'v' },
440 { NULL, 0, 0, 0 },
441 };
442
443 int
444 main(int argc, char **argv)
445 {
446 int c;
447 int rc = EXIT_SUCCESS;
448
449 setlocale(LC_ALL, "");
450 bindtextdomain(PACKAGE, LOCALEDIR);
451 textdomain(PACKAGE);
452 atexit(close_stdout);
453
454 while ((c = getopt_long(argc, argv, "hVlmnovx", longopts, NULL)) != -1) {
455 switch(c) {
456 case 'h':
457 usage(EXIT_SUCCESS);
458 break;
459 case 'V':
460 printf(_("%s from %s\n"), program_invocation_short_name,
461 PACKAGE_STRING);
462 return EXIT_SUCCESS;
463 case 'l':
464 flags |= (NAMEI_OWNERS | NAMEI_MODES | NAMEI_VERTICAL);
465 break;
466 case 'm':
467 flags |= NAMEI_MODES;
468 break;
469 case 'n':
470 flags |= NAMEI_NOLINKS;
471 break;
472 case 'o':
473 flags |= NAMEI_OWNERS;
474 break;
475 case 'x':
476 flags |= NAMEI_MNTS;
477 break;
478 case 'v':
479 flags |= NAMEI_VERTICAL;
480 break;
481 default:
482 usage(EXIT_FAILURE);
483 }
484 }
485
486 if (optind == argc) {
487 warnx(_("pathname argument is missing"));
488 usage(EXIT_FAILURE);
489 }
490
491 for(; optind < argc; optind++) {
492 char *path = argv[optind];
493 struct namei *nm = NULL;
494 struct stat st;
495
496 if (stat(path, &st) != 0)
497 rc = EXIT_FAILURE;
498
499 nm = add_namei(NULL, path, 0, NULL);
500 if (nm) {
501 int sml = 0;
502 if (!(flags & NAMEI_NOLINKS))
503 sml = follow_symlinks(nm);
504 if (print_namei(nm, path)) {
505 rc = EXIT_FAILURE;
506 continue;
507 }
508 free_namei(nm);
509 if (sml == -1) {
510 rc = EXIT_FAILURE;
511 warnx(_("%s: exceeded limit of symlinks"), path);
512 continue;
513 }
514 }
515 }
516
517 free_idcache(ucache);
518 free_idcache(gcache);
519
520 return rc;
521 }
522