Add in a patch to make busybox use the normal pwd.h and grp.h
authorEric Andersen <andersen@codepoet.org>
Sat, 27 Jan 2001 06:01:43 +0000 (06:01 -0000)
committerEric Andersen <andersen@codepoet.org>
Sat, 27 Jan 2001 06:01:43 +0000 (06:01 -0000)
functions.  Add in simple implementations of these functions,
which can, optionally, be used instead of the system versions.
 -Erik

15 files changed:
Makefile
chmod_chown_chgrp.c
cmdedit.c
coreutils/id.c
coreutils/uudecode.c
coreutils/uuencode.c
coreutils/whoami.c
id.c
include/grp.h [new file with mode: 0644]
include/pwd.h [new file with mode: 0644]
shell/cmdedit.c
utility.c
uudecode.c
uuencode.c
whoami.c

index 285f3ab66620871919ded3fd9c3875372c7376f6..d5587ae4fe42d8151e33209322c954e3f1ae2b6b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -42,6 +42,17 @@ DOSTATIC = false
 # Do not enable this for production builds...
 DODEBUG = false
 
+# Setting this to `true' will cause busybox to directly use the system's
+# password and group functions.  Assuming you use GNU libc, when this is
+# `true', you will need to install the /etc/nsswitch.conf configuration file
+# and the required libnss_* libraries. This generally makes your embedded
+# system quite a bit larger... If you leave this off, busybox will directly
+# use the /etc/password, /etc/group files (and your system will be smaller, and
+# I will get fewer emails asking about how glibc NSS works).  Enabling this adds
+# just 1.4k to the binary size (which is a _lot_ less then glibc NSS costs),
+# Most people will want to leave this set to false.
+USE_SYSTEM_PWD_GRP = false
+
 # This enables compiling with dmalloc ( http://dmalloc.com/ )
 # which is an excellent public domain mem leak and malloc problem
 # detector.  To enable dmalloc, before running busybox you will
@@ -158,10 +169,20 @@ ifdef BB_INIT_SCRIPT
     CFLAGS += -DINIT_SCRIPT='"$(BB_INIT_SCRIPT)"'
 endif
 
+ifneq ($(USE_SYSTEM_PWD_GRP),true)
+    PWD_LIB   = pwd_grp/libpwd.a
+    LIBRARIES += $(PWD_LIB)
+else
+    CFLAGS    += -DUSE_SYSTEM_PWD_GRP
+endif
+
+
 # Put user-supplied flags at the end, where they
 # have a chance of winning.
 CFLAGS += $(CFLAGS_EXTRA)
 
+.EXPORT_ALL_VARIABLES:
+
 all: busybox busybox.links doc
 
 doc: olddoc
@@ -220,15 +241,19 @@ docs/busybox/busyboxdocumentation.html: docs/busybox.sgml
 
 
 
-busybox: $(OBJECTS) 
+busybox: $(PWD_LIB) $(OBJECTS) 
        $(CC) $(LDFLAGS) -o $@ $^ $(LIBRARIES)
        $(STRIP)
 
+$(PWD_LIB):
+       $(MAKE) -eC pwd_grp
+
 busybox.links: Config.h applets.h
        - $(BB_SRC_DIR)/busybox.mkll $(CONFIG_H) $(BB_SRC_DIR)/applets.h >$@
 
 nfsmount.o cmdedit.o: %.o: %.h
 $(OBJECTS): %.o: %.c Config.h busybox.h applets.h Makefile
+       $(CC) $(CFLAGS) -c $*.c -o $*.o
 
 utility.o: loop.h
 
@@ -240,6 +265,7 @@ test tests:
 
 clean:
        - cd tests && $(MAKE) clean
+       - cd pwd_grp && $(MAKE) clean
        - rm -f docs/BusyBox.txt docs/BusyBox.1 docs/BusyBox.html \
            docs/busybox.lineo.com/BusyBox.html
        - rm -f docs/busybox.txt docs/busybox.dvi docs/busybox.ps \
index 530c45658b5a2c26680a1799ddc6cf202a7f5a9e..f6ecfd6f86879143039528bdf4f5d207feb34b35 100644 (file)
@@ -29,8 +29,6 @@
 #include "messages.c"
 
 #include <stdio.h>
-#include <grp.h>
-#include <pwd.h>
 
 
 static long uid = -1;
index c2b4123dbc7fa437465ff7307630b28bc38ddda6..e043d5f5227d9cb4d3e7d97f3300ecf7e72ca4c8 100644 (file)
--- a/cmdedit.c
+++ b/cmdedit.c
 #include <sys/stat.h>
 #endif
 
-#ifdef BB_FEATURE_USERNAME_COMPLETION
-#include <pwd.h>
-#endif
-
 static const int MAX_HISTORY = 15;             /* Maximum length of the linked list for the command line history */
 
 enum {
@@ -354,10 +350,10 @@ static char** username_tab_completion(char *ud, int *num_matches)
     char                 *temp;
     int                   nm = 0;
 
-    setpwent ();
+    bb_setpwent ();
     userlen = strlen (ud + 1);
 
-    while ((entry = getpwent ()) != NULL) {
+    while ((entry = bb_getpwent ()) != NULL) {
        /* Null usernames should result in all users as possible completions. */
        if (!userlen || !strncmp (ud + 1, entry->pw_name, userlen)) {
 
@@ -369,7 +365,7 @@ static char** username_tab_completion(char *ud, int *num_matches)
        }
     }
 
-    endpwent ();
+    bb_endpwent ();
     (*num_matches) = nm;
        return (matches);
 }
index 59cfafa0a6019ca1a84b2a4657c21922f3c3b70d..d50de4775151087f263c753f5a7e7e7b7e62f396 100644 (file)
@@ -23,8 +23,6 @@
 #include "busybox.h"
 #include <stdio.h>
 #include <unistd.h>
-#include <pwd.h>
-#include <grp.h>
 #include <getopt.h>
 #include <sys/types.h>
 
index 279b9d6ce46919f3f8ee1d269d70acf02f92d4cb..7b26d2dad78b99545beb66495d7aaacf3e2d81a0 100644 (file)
@@ -27,7 +27,6 @@
 #include <stdio.h>
 #include <errno.h>
 #include <getopt.h>
-#include <pwd.h>
 
 /*struct passwd *getpwnam();*/
 
index 36bc4970f0a5bdaa1e7804a0dc174b4427b8e0c1..24aabd3730e1309a84e081841ce7888f297427d7 100644 (file)
@@ -27,7 +27,6 @@
 #include <stdio.h>
 #include <errno.h>
 #include <getopt.h>
-#include <pwd.h>
 
 #define        RW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
 
index 38a2b30787f02c3a321306fcc2dd8b29320d9738..d7f0a177c99769702c87668fac7fc7d6bc153cbb 100644 (file)
@@ -22,7 +22,6 @@
 
 #include "busybox.h"
 #include <stdio.h>
-#include <pwd.h>
 
 extern int whoami_main(int argc, char **argv)
 {
diff --git a/id.c b/id.c
index 59cfafa0a6019ca1a84b2a4657c21922f3c3b70d..d50de4775151087f263c753f5a7e7e7b7e62f396 100644 (file)
--- a/id.c
+++ b/id.c
@@ -23,8 +23,6 @@
 #include "busybox.h"
 #include <stdio.h>
 #include <unistd.h>
-#include <pwd.h>
-#include <grp.h>
 #include <getopt.h>
 #include <sys/types.h>
 
diff --git a/include/grp.h b/include/grp.h
new file mode 100644 (file)
index 0000000..f27c466
--- /dev/null
@@ -0,0 +1,47 @@
+#ifndef        __BB_GRP_H
+#define        __BB_GRP_H
+
+#if defined USE_SYSTEM_PWD_GRP
+#include <grp.h>
+#else
+
+#define bb_setgrent setgrent
+#define bb_endgrent endgrent
+#define bb_getgrent getgrent
+#define bb_getgrgid getgrgid
+#define bb_getgrnam getgrnam
+#define bb_fgetgrent fgetgrent
+#define bb_setgroups setgroups
+#define bb_initgroups initgroups
+#define __bb_getgrent __getgrent
+
+#include <sys/types.h>
+#include <features.h>
+#include <stdio.h>
+
+/* The group structure */
+struct group
+{
+  char *gr_name;               /* Group name.  */
+  char *gr_passwd;             /* Password.    */
+  gid_t gr_gid;                        /* Group ID.    */
+  char **gr_mem;               /* Member list. */
+};
+
+extern void bb_setgrent __P ((void));
+extern void bb_endgrent __P ((void));
+extern struct group * bb_getgrent __P ((void));
+
+extern struct group * bb_getgrgid __P ((__const gid_t gid));
+extern struct group * bb_getgrnam __P ((__const char * name));
+
+extern struct group * bb_fgetgrent __P ((FILE * file));
+
+extern int bb_setgroups __P ((size_t n, __const gid_t * groups));
+extern int bb_initgroups __P ((__const char * user, gid_t gid));
+
+extern struct group * __bb_getgrent __P ((int grp_fd));
+
+#endif /* USE_SYSTEM_PWD_GRP */
+#endif /* __BB_GRP_H */
+
diff --git a/include/pwd.h b/include/pwd.h
new file mode 100644 (file)
index 0000000..82743f5
--- /dev/null
@@ -0,0 +1,51 @@
+#ifndef        __BB_PWD_H
+#define        __BB_PWD_H
+
+#if defined USE_SYSTEM_PWD_GRP
+#include <pwd.h>
+#else
+
+#define bb_setpwent setpwent
+#define bb_endpwent endpwent
+#define bb_getpwent getpwent
+#define bb_putpwent putpwent
+#define bb_getpw getpw
+#define bb_fgetpwent fgetpwent
+#define bb_getpwuid getpwuid
+#define bb_getpwnam getpwnam
+#define __bb_getpwent __bb_getpwent
+
+
+#include <sys/types.h>
+#include <features.h>
+#include <stdio.h>
+
+/* The passwd structure.  */
+struct passwd
+{
+  char *pw_name;               /* Username.  */
+  char *pw_passwd;             /* Password.  */
+  uid_t pw_uid;                        /* User ID.  */
+  gid_t pw_gid;                        /* Group ID.  */
+  char *pw_gecos;              /* Real name.  */
+  char *pw_dir;                        /* Home directory.  */
+  char *pw_shell;              /* Shell program.  */
+};
+
+extern void bb_setpwent __P ((void));
+extern void bb_endpwent __P ((void));
+extern struct passwd * bb_getpwent __P ((void));
+
+extern int bb_putpwent __P ((__const struct passwd * __p, FILE * __f));
+extern int bb_getpw __P ((uid_t uid, char *buf));
+
+extern struct passwd * bb_fgetpwent __P ((FILE * file));
+
+extern struct passwd * bb_getpwuid __P ((__const uid_t));
+extern struct passwd * bb_getpwnam __P ((__const char *));
+
+extern struct passwd * __bb_getpwent __P ((__const int passwd_fd));
+
+#endif /* USE_SYSTEM_PWD_GRP */
+#endif /* __BB_PWD_H  */
+
index c2b4123dbc7fa437465ff7307630b28bc38ddda6..e043d5f5227d9cb4d3e7d97f3300ecf7e72ca4c8 100644 (file)
 #include <sys/stat.h>
 #endif
 
-#ifdef BB_FEATURE_USERNAME_COMPLETION
-#include <pwd.h>
-#endif
-
 static const int MAX_HISTORY = 15;             /* Maximum length of the linked list for the command line history */
 
 enum {
@@ -354,10 +350,10 @@ static char** username_tab_completion(char *ud, int *num_matches)
     char                 *temp;
     int                   nm = 0;
 
-    setpwent ();
+    bb_setpwent ();
     userlen = strlen (ud + 1);
 
-    while ((entry = getpwent ()) != NULL) {
+    while ((entry = bb_getpwent ()) != NULL) {
        /* Null usernames should result in all users as possible completions. */
        if (!userlen || !strncmp (ud + 1, entry->pw_name, userlen)) {
 
@@ -369,7 +365,7 @@ static char** username_tab_completion(char *ud, int *num_matches)
        }
     }
 
-    endpwent ();
+    bb_endpwent ();
     (*num_matches) = nm;
        return (matches);
 }
index 81542e94f0c912c62d1b59fe072a2fbbd5ef2ff8..13b8065d3945d63410a9fd99b68c86d5ba2d5110 100644 (file)
--- a/utility.c
+++ b/utility.c
@@ -52,6 +52,8 @@
 #include <ctype.h>
 #include <sys/ioctl.h>
 #include <sys/utsname.h>               /* for uname(2) */
+#include "pwd_grp/pwd.h"
+#include "pwd_grp/grp.h"
 
 /* Busybox mount uses either /proc/filesystems or /dev/mtab to get the 
  * list of available filesystems used for the -t auto option */ 
@@ -856,117 +858,72 @@ extern int parse_mode(const char *s, mode_t * theMode)
 #if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
  || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
  || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH
-
-/* This parses entries in /etc/passwd and /etc/group.  This is desirable
- * for BusyBox, since we want to avoid using the glibc NSS stuff, which
- * increases target size and is often not needed or wanted for embedded
- * systems.
- *
- * /etc/passwd entries look like this: 
- *             root:x:0:0:root:/root:/bin/bash
- * and /etc/group entries look like this: 
- *             root:x:0:
- *
- * This uses buf as storage to hold things.
- * 
- */
-unsigned long my_getid(const char *filename, char *name, long id, long *gid)
-{
-       FILE *file;
-       char *rname, *start, *end, buf[128];
-       long rid;
-       long rgid = 0;
-
-       file = fopen(filename, "r");
-       if (file == NULL) {
-               /* Do not complain.  It is ok for /etc/passwd and
-                * friends to be missing... */
-               return (-1);
-       }
-
-       while (fgets(buf, 128, file) != NULL) {
-               if (buf[0] == '#')
-                       continue;
-
-               /* username/group name */
-               start = buf;
-               end = strchr(start, ':');
-               if (end == NULL)
-                       continue;
-               *end = '\0';
-               rname = start;
-
-               /* password */
-               start = end + 1;
-               end = strchr(start, ':');
-               if (end == NULL)
-                       continue;
-
-               /* uid in passwd, gid in group */
-               start = end + 1;
-               rid = (unsigned long) strtol(start, &end, 10);
-               if (end == start)
-                       continue;
-
-               /* gid in passwd */
-               start = end + 1;
-               rgid = (unsigned long) strtol(start, &end, 10);
-               
-               if (name) {
-                       if (0 == strcmp(rname, name)) {
-                           if (gid) *gid = rgid;
-                               fclose(file);
-                               return (rid);
-                       }
-               }
-               if (id != -1 && id == rid) {
-                       strncpy(name, rname, 8);
-                       name[8]='\0';
-                       if (gid) *gid = rgid;
-                       fclose(file);
-                       return (TRUE);
-               }
-       }
-       fclose(file);
-       return (-1);
-}
-
 /* returns a uid given a username */
 long my_getpwnam(char *name)
 {
-       return my_getid("/etc/passwd", name, -1, NULL);
+       struct passwd *myuser;
+
+       myuser  = getpwnam(name);
+       if (myuser==NULL)
+               error_msg_and_die( "unknown username: %s\n", name);
+
+       return myuser->pw_uid;
 }
 
 /* returns a gid given a group name */
 long my_getgrnam(char *name)
 {
-       return my_getid("/etc/group", name, -1, NULL);
+       struct group *mygroup;
+
+       mygroup  = getgrnam(name);
+       if (mygroup==NULL)
+               error_msg_and_die( "unknown group: %s\n", name);
+
+       return (mygroup->gr_gid);
 }
 
 /* gets a username given a uid */
 void my_getpwuid(char *name, long uid)
 {
-       name[0] = '\0';
-       my_getid("/etc/passwd", name, uid, NULL);
+       struct passwd *myuser;
+
+       myuser  = getpwuid(uid);
+       if (myuser==NULL)
+               error_msg_and_die( "unknown uid %ld\n", (long)uid);
+
+       strcpy(name, myuser->pw_name);
 }
 
 /* gets a groupname given a gid */
 void my_getgrgid(char *group, long gid)
 {
-       group[0] = '\0';
-       my_getid("/etc/group", group, gid, NULL);
+       struct group *mygroup;
+
+       mygroup  = getgrgid(gid);
+       if (mygroup==NULL)
+               error_msg_and_die( "unknown gid %ld\n", (long)gid);
+
+       strcpy(group, mygroup->gr_name);
 }
 
 #if defined BB_ID
 /* gets a gid given a user name */
 long my_getpwnamegid(char *name)
 {
-       long gid;
-       my_getid("/etc/passwd", name, -1, &gid);
-       return gid;
-}
-#endif
+       struct group *mygroup;
+       struct passwd *myuser;
+
+       myuser=getpwnam(name);
+       if (myuser==NULL)
+               error_msg_and_die( "unknown user name: %s\n", name);
 
+       mygroup  = getgrgid(myuser->pw_gid);
+       if (mygroup==NULL)
+               error_msg_and_die( "unknown gid %ld\n", (long)myuser->pw_gid);
+
+       return mygroup->gr_gid;
+}
+#endif /* BB_ID */
 #endif
  /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
  || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */
index 279b9d6ce46919f3f8ee1d269d70acf02f92d4cb..7b26d2dad78b99545beb66495d7aaacf3e2d81a0 100644 (file)
@@ -27,7 +27,6 @@
 #include <stdio.h>
 #include <errno.h>
 #include <getopt.h>
-#include <pwd.h>
 
 /*struct passwd *getpwnam();*/
 
index 36bc4970f0a5bdaa1e7804a0dc174b4427b8e0c1..24aabd3730e1309a84e081841ce7888f297427d7 100644 (file)
@@ -27,7 +27,6 @@
 #include <stdio.h>
 #include <errno.h>
 #include <getopt.h>
-#include <pwd.h>
 
 #define        RW (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
 
index 38a2b30787f02c3a321306fcc2dd8b29320d9738..d7f0a177c99769702c87668fac7fc7d6bc153cbb 100644 (file)
--- a/whoami.c
+++ b/whoami.c
@@ -22,7 +22,6 @@
 
 #include "busybox.h"
 #include <stdio.h>
-#include <pwd.h>
 
 extern int whoami_main(int argc, char **argv)
 {