aboutsummaryrefslogtreecommitdiff
path: root/utility.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-01-27 06:01:43 +0000
committerEric Andersen <andersen@codepoet.org>2001-01-27 06:01:43 +0000
commitab050f5522e843bf08994685134adaaac7ffd392 (patch)
treee3f2fbeb43186bbe44fb58ba5a7f6a3c4843c1a5 /utility.c
parent3654ca56fae6e3bdcb7369bc21d2b41d460ef732 (diff)
downloadbusybox-w32-ab050f5522e843bf08994685134adaaac7ffd392.tar.gz
busybox-w32-ab050f5522e843bf08994685134adaaac7ffd392.tar.bz2
busybox-w32-ab050f5522e843bf08994685134adaaac7ffd392.zip
Add in a patch to make busybox use the normal pwd.h and grp.h
functions. Add in simple implementations of these functions, which can, optionally, be used instead of the system versions. -Erik
Diffstat (limited to 'utility.c')
-rw-r--r--utility.c129
1 files changed, 43 insertions, 86 deletions
diff --git a/utility.c b/utility.c
index 81542e94f..13b8065d3 100644
--- a/utility.c
+++ b/utility.c
@@ -52,6 +52,8 @@
52#include <ctype.h> 52#include <ctype.h>
53#include <sys/ioctl.h> 53#include <sys/ioctl.h>
54#include <sys/utsname.h> /* for uname(2) */ 54#include <sys/utsname.h> /* for uname(2) */
55#include "pwd_grp/pwd.h"
56#include "pwd_grp/grp.h"
55 57
56/* Busybox mount uses either /proc/filesystems or /dev/mtab to get the 58/* Busybox mount uses either /proc/filesystems or /dev/mtab to get the
57 * list of available filesystems used for the -t auto option */ 59 * list of available filesystems used for the -t auto option */
@@ -856,117 +858,72 @@ extern int parse_mode(const char *s, mode_t * theMode)
856#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \ 858#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS \
857 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \ 859 || defined BB_TAR || defined BB_ID || defined BB_LOGGER \
858 || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH 860 || defined BB_LOGNAME || defined BB_WHOAMI || defined BB_SH
859
860/* This parses entries in /etc/passwd and /etc/group. This is desirable
861 * for BusyBox, since we want to avoid using the glibc NSS stuff, which
862 * increases target size and is often not needed or wanted for embedded
863 * systems.
864 *
865 * /etc/passwd entries look like this:
866 * root:x:0:0:root:/root:/bin/bash
867 * and /etc/group entries look like this:
868 * root:x:0:
869 *
870 * This uses buf as storage to hold things.
871 *
872 */
873unsigned long my_getid(const char *filename, char *name, long id, long *gid)
874{
875 FILE *file;
876 char *rname, *start, *end, buf[128];
877 long rid;
878 long rgid = 0;
879
880 file = fopen(filename, "r");
881 if (file == NULL) {
882 /* Do not complain. It is ok for /etc/passwd and
883 * friends to be missing... */
884 return (-1);
885 }
886
887 while (fgets(buf, 128, file) != NULL) {
888 if (buf[0] == '#')
889 continue;
890
891 /* username/group name */
892 start = buf;
893 end = strchr(start, ':');
894 if (end == NULL)
895 continue;
896 *end = '\0';
897 rname = start;
898
899 /* password */
900 start = end + 1;
901 end = strchr(start, ':');
902 if (end == NULL)
903 continue;
904
905 /* uid in passwd, gid in group */
906 start = end + 1;
907 rid = (unsigned long) strtol(start, &end, 10);
908 if (end == start)
909 continue;
910
911 /* gid in passwd */
912 start = end + 1;
913 rgid = (unsigned long) strtol(start, &end, 10);
914
915 if (name) {
916 if (0 == strcmp(rname, name)) {
917 if (gid) *gid = rgid;
918 fclose(file);
919 return (rid);
920 }
921 }
922 if (id != -1 && id == rid) {
923 strncpy(name, rname, 8);
924 name[8]='\0';
925 if (gid) *gid = rgid;
926 fclose(file);
927 return (TRUE);
928 }
929 }
930 fclose(file);
931 return (-1);
932}
933
934/* returns a uid given a username */ 861/* returns a uid given a username */
935long my_getpwnam(char *name) 862long my_getpwnam(char *name)
936{ 863{
937 return my_getid("/etc/passwd", name, -1, NULL); 864 struct passwd *myuser;
865
866 myuser = getpwnam(name);
867 if (myuser==NULL)
868 error_msg_and_die( "unknown username: %s\n", name);
869
870 return myuser->pw_uid;
938} 871}
939 872
940/* returns a gid given a group name */ 873/* returns a gid given a group name */
941long my_getgrnam(char *name) 874long my_getgrnam(char *name)
942{ 875{
943 return my_getid("/etc/group", name, -1, NULL); 876 struct group *mygroup;
877
878 mygroup = getgrnam(name);
879 if (mygroup==NULL)
880 error_msg_and_die( "unknown group: %s\n", name);
881
882 return (mygroup->gr_gid);
944} 883}
945 884
946/* gets a username given a uid */ 885/* gets a username given a uid */
947void my_getpwuid(char *name, long uid) 886void my_getpwuid(char *name, long uid)
948{ 887{
949 name[0] = '\0'; 888 struct passwd *myuser;
950 my_getid("/etc/passwd", name, uid, NULL); 889
890 myuser = getpwuid(uid);
891 if (myuser==NULL)
892 error_msg_and_die( "unknown uid %ld\n", (long)uid);
893
894 strcpy(name, myuser->pw_name);
951} 895}
952 896
953/* gets a groupname given a gid */ 897/* gets a groupname given a gid */
954void my_getgrgid(char *group, long gid) 898void my_getgrgid(char *group, long gid)
955{ 899{
956 group[0] = '\0'; 900 struct group *mygroup;
957 my_getid("/etc/group", group, gid, NULL); 901
902 mygroup = getgrgid(gid);
903 if (mygroup==NULL)
904 error_msg_and_die( "unknown gid %ld\n", (long)gid);
905
906 strcpy(group, mygroup->gr_name);
958} 907}
959 908
960#if defined BB_ID 909#if defined BB_ID
961/* gets a gid given a user name */ 910/* gets a gid given a user name */
962long my_getpwnamegid(char *name) 911long my_getpwnamegid(char *name)
963{ 912{
964 long gid; 913 struct group *mygroup;
965 my_getid("/etc/passwd", name, -1, &gid); 914 struct passwd *myuser;
966 return gid; 915
967} 916 myuser=getpwnam(name);
968#endif 917 if (myuser==NULL)
918 error_msg_and_die( "unknown user name: %s\n", name);
969 919
920 mygroup = getgrgid(myuser->pw_gid);
921 if (mygroup==NULL)
922 error_msg_and_die( "unknown gid %ld\n", (long)myuser->pw_gid);
923
924 return mygroup->gr_gid;
925}
926#endif /* BB_ID */
970#endif 927#endif
971 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \ 928 /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR \
972 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */ 929 || BB_ID || BB_LOGGER || BB_LOGNAME || BB_WHOAMI */