Subject: bin/37482: mtree does not check for getlogin() returning null in spec create
To: None <gnats-admin@netbsd.org, netbsd-bugs@netbsd.org>
From: None <netbsd.org@mjch.net>
List: netbsd-bugs
Date: 12/05/2007 10:20:00
>Number:         37482
>Category:       bin
>Synopsis:       mtree does not check for getlogin() returning null in spec create
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Dec 05 10:20:00 +0000 2007
>Originator:     Malcolm Herbert
>Release:        3.0
>Organization:
>Environment:
NetBSD mamers.internal 3.0 NetBSD 3.0 (GENERIC) #0: Mon Dec 19 01:04:02 UTC 2005  builds@works.netbsd.org:/home/builds/ab/netbsd-3-0-RELEASE/i386/200512182024Z-obj/home/builds/ab/netbsd-3-0-RELEASE/src/sys/arch/i386/compile/GENERIC i386
>Description:
Discovered initially under Solaris, getlogin() can return null which is not checked for before the result is used in a printf() statement.  Under Solaris this bombs mtree when running 

mtree -c
>How-To-Repeat:
mtree -c
>Fix:
--- create.c.orig       Wed Aug  1 08:28:28 2007
+++ create.c    Wed Aug  1 08:27:28 2007
@@ -132,6 +132,8 @@
        time_t clocktime;
        char host[MAXHOSTNAMELEN + 1];
        char *argv[2];
+       char *uname;
+       struct passwd *passwd;
        char  dot[] = ".";
        argv[0] = dot;
        argv[1] = NULL;
@@ -139,9 +141,20 @@
        time(&clocktime);
        gethostname(host, sizeof(host));
        host[sizeof(host) - 1] = '\0';
+       uname = getlogin();
+       if (!uname) {
+               uid = getuid();
+               passwd = getpwuid(uid);
+               if (passwd) {
+                       uname = passwd->pw_name;
+               }
+       }
+       if (!uname) {
+               uname = "<unknown>";
+       }
        printf(
            "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
-           getlogin(), host, fullpath, ctime(&clocktime));
+           uname, host, fullpath, ctime(&clocktime));

        if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
                mtree_err("fts_open: %s", strerror(errno));