[hackers] [sbase] Audit ln(1) || FRIGN

From: <git_AT_suckless.org>
Date: Thu, 5 Mar 2015 22:22:48 +0100 (CET)

commit bbf0c147ffb50217f8a22463cd05c9cd42729b4a
Author: FRIGN <dev_AT_frign.de>
Date: Thu Mar 5 21:14:43 2015 +0100

    Audit ln(1)
    
    1) Clarify behaviour when the f-flag is given and a target is in its
       own way.
    2) Fix usage()-style.
    3) Group local variable declarations.
    4) reorder args
    5) argc style, other boolean style changes
    6) improve error messages
    7) set argv[argc - 1] to NULL to allow argv-centric loop later
    8) BUGFIX: POSIX specifies that when with the f-flag there's a
       situation where a file stands in its own way for linking it
       should be ignored.
    9) Add weprintf() where possible, so we don't pussy out when there's
       a small issue. This is sbase ffs!

diff --git a/README b/README
index d4ecd58..4e9f925 100644
--- a/README
+++ b/README
_AT_@ -39,7 +39,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
 =*| hostname non-posix none
 =* kill yes none
 =*| link yes none
-=* ln yes none
+=*| ln yes none
 =* logger yes none
 =* logname yes none
 = ls no (-C), -S, -f, -m, -s, -x
diff --git a/ln.1 b/ln.1
index 19f012a..2de6b67 100644
--- a/ln.1
+++ b/ln.1
_AT_@ -1,4 +1,4 @@
-.Dd January 26, 2015
+.Dd March 5, 2015
 .Dt LN 1
 .Os sbase
 .Sh NAME
_AT_@ -37,7 +37,9 @@ hardlinks them in the existing
 .It Fl f
 If
 .Ar name
-exists, remove it to allow the link.
+exists and is not a
+.Ar target ,
+remove it to allow the link.
 .It Fl L | Fl P
 If
 .Ar target
diff --git a/ln.c b/ln.c
index 8843a49..b63e75c 100644
--- a/ln.c
+++ b/ln.c
_AT_@ -1,6 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 #include <sys/stat.h>
 
+#include <errno.h>
 #include <fcntl.h>
 #include <libgen.h>
 #include <unistd.h>
_AT_@ -11,64 +12,73 @@ static void
 usage(void)
 {
         eprintf("usage: %s [-f] [-L | -P | -s] target [name]\n"
- " %s [-f] [-L | -P | -s] target ... directory\n",
- argv0, argv0);
+ " %s [-f] [-L | -P | -s] target ... dir\n", argv0, argv0);
 }
 
 int
 main(int argc, char *argv[])
 {
         char *fname, *to;
- int sflag = 0;
- int fflag = 0;
- int hasto = 0;
- int dirfd = AT_FDCWD;
- int flags = AT_SYMLINK_FOLLOW;
- struct stat st;
+ int sflag = 0, fflag = 0, hasto = 0, dirfd = AT_FDCWD, flags = AT_SYMLINK_FOLLOW;
+ struct stat st, tost;
 
         ARGBEGIN {
         case 'f':
                 fflag = 1;
                 break;
- case 's':
- sflag = 1;
- break;
         case 'L':
                 flags |= AT_SYMLINK_FOLLOW;
                 break;
         case 'P':
                 flags &= ~AT_SYMLINK_FOLLOW;
                 break;
+ case 's':
+ sflag = 1;
+ break;
         default:
                 usage();
         } ARGEND;
 
- if (argc == 0)
+ if (!argc)
                 usage();
 
         fname = sflag ? "symlink" : "link";
 
         if (argc >= 2) {
- if (stat(argv[argc - 1], &st) == 0 && S_ISDIR(st.st_mode)) {
+ if (!stat(argv[argc - 1], &st) && S_ISDIR(st.st_mode)) {
                         if ((dirfd = open(argv[argc - 1], O_RDONLY)) < 0)
- eprintf("open:");
+ eprintf("open %s:", argv[argc - 1]);
                 } else if (argc == 2) {
- to = argv[1];
+ to = argv[argc - 1];
                         hasto = 1;
                 } else {
- eprintf("destination is not a directory\n");
+ eprintf("%s: not a directory\n", argv[argc - 1]);
                 }
+ argv[argc - 1] = NULL;
                 argc--;
         }
 
- for (; argc > 0; argc--, argv++) {
+ for (; *argv; argc--, argv++) {
                 if (!hasto)
- to = basename(argv[0]);
- if (fflag)
- unlinkat(dirfd, to, 0);
- if ((!sflag ? linkat(AT_FDCWD, argv[0], dirfd, to, flags)
- : symlinkat(argv[0], dirfd, to)) < 0) {
- eprintf("%s %s <- %s:", fname, argv[0], to);
+ to = basename(*argv);
+ if (fflag) {
+ if (stat(*argv, &st) < 0) {
+ weprintf("stat %s:", *argv);
+ continue;
+ } else if (fstatat(dirfd, to, &tost, AT_SYMLINK_NOFOLLOW) < 0) {
+ if (errno != ENOENT)
+ eprintf("stat %s:", to);
+ } else {
+ if (st.st_dev == tost.st_dev && st.st_ino == tost.st_ino) {
+ weprintf("%s and %s are the same file\n", *argv, *argv);
+ continue;
+ }
+ unlinkat(dirfd, to, 0);
+ }
+ }
+ if ((!sflag ? linkat(AT_FDCWD, *argv, dirfd, to, flags)
+ : symlinkat(*argv, dirfd, to)) < 0) {
+ weprintf("%s %s <- %s:", fname, *argv, to);
                 }
         }
 
Received on Thu Mar 05 2015 - 22:22:48 CET

This archive was generated by hypermail 2.3.0 : Thu Mar 05 2015 - 22:24:13 CET