On Fri, 15 Apr 2022 at 18:54:49 -0600, Todd C. Miller wrote:
> On Fri, 15 Apr 2022 13:28:33 -0500, joshua stein wrote:
> 
> > Anyone want to bikeshed this language?
> 
> I think it is more helpful to refer to glob(7) than glob(3).
> Perhaps something like this.

I like it.  Here's the full diff:


Index: lib/libutil/login_fbtab.c
===================================================================
RCS file: /cvs/src/lib/libutil/login_fbtab.c,v
retrieving revision 1.16
diff -u -p -u -p -r1.16 login_fbtab.c
--- lib/libutil/login_fbtab.c   27 Nov 2015 01:57:59 -0000      1.16
+++ lib/libutil/login_fbtab.c   20 Apr 2022 14:17:08 -0000
@@ -61,8 +61,8 @@
 #include <sys/stat.h>
 
 #include <errno.h>
-#include <dirent.h>
 #include <limits.h>
+#include <glob.h>
 #include <paths.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -134,49 +134,31 @@ login_fbtab(const char *tty, uid_t uid, 
 static void
 login_protect(const char *path, mode_t mask, uid_t uid, gid_t gid)
 {
-       char    buf[PATH_MAX];
-       size_t  pathlen = strlen(path);
-       DIR     *dir;
-       struct  dirent *ent;
+       glob_t  g;
+       size_t  n;
+       char    *gpath;
 
-       if (pathlen >= sizeof(buf)) {
+       if (strlen(path) >= PATH_MAX) {
                errno = ENAMETOOLONG;
                syslog(LOG_ERR, "%s: %s: %m", _PATH_FBTAB, path);
                return;
        }
 
-       if (strcmp("/*", path + pathlen - 2) != 0) {
-               if (chmod(path, mask) && errno != ENOENT)
-                       syslog(LOG_ERR, "%s: chmod(%s): %m", _PATH_FBTAB, path);
-               if (chown(path, uid, gid) && errno != ENOENT)
-                       syslog(LOG_ERR, "%s: chown(%s): %m", _PATH_FBTAB, path);
-       } else {
-               /*
-                * This is a wildcard directory (/path/to/whatever/ * ).
-                * Make a copy of path without the trailing '*' (but leave
-                * the trailing '/' so we can append directory entries.)
-                */
-               memcpy(buf, path, pathlen - 1);
-               buf[pathlen - 1] = '\0';
-               if ((dir = opendir(buf)) == NULL) {
-                       syslog(LOG_ERR, "%s: opendir(%s): %m", _PATH_FBTAB,
-                           path);
-                       return;
-               }
+       if (glob(path, GLOB_NOSORT, NULL, &g) != 0) {
+               if (errno != ENOENT)
+                       syslog(LOG_ERR, "%s: glob(%s): %m", _PATH_FBTAB, path);
+               globfree(&g);
+               return;
+       }
 
-               while ((ent = readdir(dir)) != NULL) {
-                       if (strcmp(ent->d_name, ".")  != 0 &&
-                           strcmp(ent->d_name, "..") != 0) {
-                               buf[pathlen - 1] = '\0';
-                               if (strlcat(buf, ent->d_name, sizeof(buf))
-                                   >= sizeof(buf)) {
-                                       errno = ENAMETOOLONG;
-                                       syslog(LOG_ERR, "%s: %s: %m",
-                                           _PATH_FBTAB, path);
-                               } else
-                                       login_protect(buf, mask, uid, gid);
-                       }
-               }
-               closedir(dir);
+       for (n = 0; n < g.gl_matchc; n++) {
+               gpath = g.gl_pathv[n];
+
+               if (chmod(gpath, mask) && errno != ENOENT)
+                       syslog(LOG_ERR, "%s: chmod(%s): %m", _PATH_FBTAB, 
gpath);
+               if (chown(gpath, uid, gid) && errno != ENOENT)
+                       syslog(LOG_ERR, "%s: chown(%s): %m", _PATH_FBTAB, 
gpath);
        }
+
+       globfree(&g);
 }
Index: share/man/man5/fbtab.5
===================================================================
RCS file: /cvs/src/share/man/man5/fbtab.5,v
retrieving revision 1.14
diff -u -p -u -p -r1.14 fbtab.5
--- share/man/man5/fbtab.5      8 Sep 2014 01:27:55 -0000       1.14
+++ share/man/man5/fbtab.5      20 Apr 2022 14:17:08 -0000
@@ -51,15 +51,11 @@ An octal permission number (0600), as us
 .It Other devices
 The final field is a colon
 .Pq Ql \&:
-delimited list of devices (e.g.,
-.Dq /dev/console:/dev/fd0a ) .
-All device names are absolute paths.
-A path that ends in
-.Dq /\&*
-refers to all directory entries except
-.Dq \&.
-and
-.Dq \&.\&. .
+delimited list of device paths (e.g.,
+.Dq /dev/console:/dev/fd0a:/dev/wskbd* ) .
+Device paths may include shell-style globbing patterns (see
+.Xr glob 7 ) ,
+potentially matching multiple devices.
 .El
 .Pp
 The
@@ -84,5 +80,6 @@ the files once again belonging to root.
 .Xr login 1 ,
 .Xr login_fbtab 3 ,
 .Xr init 8
+.Xr glob 7
 .Sh AUTHORS
 .An Guido van Rooij

Reply via email to