diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-12-03 19:05:55 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-12-03 19:05:55 +0000 |
commit | d7a805efafd83203e72c334089c3f88204231ac7 (patch) | |
tree | d69521a26d050432f89cf5713aa1516f655b136f | |
parent | ac1c96f6737799d1d7f85515237df80e9da71346 (diff) | |
download | busybox-w32-d7a805efafd83203e72c334089c3f88204231ac7.tar.gz busybox-w32-d7a805efafd83203e72c334089c3f88204231ac7.tar.bz2 busybox-w32-d7a805efafd83203e72c334089c3f88204231ac7.zip |
libbb: introduce and use xgetpwnam. ~ -150 bytes.
-rw-r--r-- | coreutils/id.c | 4 | ||||
-rw-r--r-- | include/libbb.h | 1 | ||||
-rw-r--r-- | libbb/bb_pwd.c | 15 | ||||
-rw-r--r-- | loginutils/addgroup.c | 1 | ||||
-rw-r--r-- | loginutils/passwd.c | 4 | ||||
-rw-r--r-- | loginutils/su.c | 4 | ||||
-rw-r--r-- | miscutils/crontab.c | 4 | ||||
-rw-r--r-- | networking/tftp.c | 4 |
8 files changed, 17 insertions, 20 deletions
diff --git a/coreutils/id.c b/coreutils/id.c index 33e06f427..43f403fa3 100644 --- a/coreutils/id.c +++ b/coreutils/id.c | |||
@@ -124,9 +124,7 @@ int id_main(int argc UNUSED_PARAM, char **argv) | |||
124 | 124 | ||
125 | username = argv[optind]; | 125 | username = argv[optind]; |
126 | if (username) { | 126 | if (username) { |
127 | struct passwd *p = getpwnam(username); | 127 | struct passwd *p = xgetpwnam(username); |
128 | if (!p) | ||
129 | bb_error_msg_and_die("unknown user %s", username); | ||
130 | euid = ruid = p->pw_uid; | 128 | euid = ruid = p->pw_uid; |
131 | egid = rgid = p->pw_gid; | 129 | egid = rgid = p->pw_gid; |
132 | } else { | 130 | } else { |
diff --git a/include/libbb.h b/include/libbb.h index a34e8a1f9..80311db2b 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -702,6 +702,7 @@ int get_uidgid(struct bb_uidgid_t*, const char*, int numeric_ok) FAST_FUNC; | |||
702 | void xget_uidgid(struct bb_uidgid_t*, const char*) FAST_FUNC; | 702 | void xget_uidgid(struct bb_uidgid_t*, const char*) FAST_FUNC; |
703 | /* chown-like handling of "user[:[group]" */ | 703 | /* chown-like handling of "user[:[group]" */ |
704 | void parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group) FAST_FUNC; | 704 | void parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group) FAST_FUNC; |
705 | struct passwd* xgetpwnam(const char *name) FAST_FUNC; | ||
705 | struct passwd* xgetpwuid(uid_t uid) FAST_FUNC; | 706 | struct passwd* xgetpwuid(uid_t uid) FAST_FUNC; |
706 | struct group* xgetgrgid(gid_t gid) FAST_FUNC; | 707 | struct group* xgetgrgid(gid_t gid) FAST_FUNC; |
707 | char* xuid2uname(uid_t uid) FAST_FUNC; | 708 | char* xuid2uname(uid_t uid) FAST_FUNC; |
diff --git a/libbb/bb_pwd.c b/libbb/bb_pwd.c index 5e44edc90..5dbc58d9f 100644 --- a/libbb/bb_pwd.c +++ b/libbb/bb_pwd.c | |||
@@ -15,7 +15,15 @@ | |||
15 | * pointers to static data (getpwuid) | 15 | * pointers to static data (getpwuid) |
16 | */ | 16 | */ |
17 | 17 | ||
18 | /* TODO: add xgetpwnam, this construct is used a lot */ | 18 | struct passwd* FAST_FUNC xgetpwnam(const char *name) |
19 | { | ||
20 | struct passwd *pw = getpwnam(name); | ||
21 | if (!pw) | ||
22 | bb_error_msg_and_die("unknown user %s", name); | ||
23 | return pw; | ||
24 | } | ||
25 | |||
26 | /* xgetgrnam too? */ | ||
19 | 27 | ||
20 | struct passwd* FAST_FUNC xgetpwuid(uid_t uid) | 28 | struct passwd* FAST_FUNC xgetpwuid(uid_t uid) |
21 | { | 29 | { |
@@ -73,10 +81,7 @@ long FAST_FUNC xuname2uid(const char *name) | |||
73 | { | 81 | { |
74 | struct passwd *myuser; | 82 | struct passwd *myuser; |
75 | 83 | ||
76 | myuser = getpwnam(name); | 84 | myuser = xgetpwnam(name); |
77 | if (myuser == NULL) | ||
78 | bb_error_msg_and_die("unknown user %s", name); | ||
79 | |||
80 | return myuser->pw_uid; | 85 | return myuser->pw_uid; |
81 | } | 86 | } |
82 | 87 | ||
diff --git a/loginutils/addgroup.c b/loginutils/addgroup.c index 89414d738..2a840d7c0 100644 --- a/loginutils/addgroup.c +++ b/loginutils/addgroup.c | |||
@@ -159,6 +159,7 @@ int addgroup_main(int argc UNUSED_PARAM, char **argv) | |||
159 | /* check if group and user exist */ | 159 | /* check if group and user exist */ |
160 | xuname2uid(argv[0]); /* unknown user: exit */ | 160 | xuname2uid(argv[0]); /* unknown user: exit */ |
161 | xgroup2gid(argv[1]); /* unknown group: exit */ | 161 | xgroup2gid(argv[1]); /* unknown group: exit */ |
162 | // race here! | ||
162 | /* check if user is already in this group */ | 163 | /* check if user is already in this group */ |
163 | gr = getgrnam(argv[1]); | 164 | gr = getgrnam(argv[1]); |
164 | for (; *(gr->gr_mem) != NULL; (gr->gr_mem)++) { | 165 | for (; *(gr->gr_mem) != NULL; (gr->gr_mem)++) { |
diff --git a/loginutils/passwd.c b/loginutils/passwd.c index e3e74bae7..aa89b87a7 100644 --- a/loginutils/passwd.c +++ b/loginutils/passwd.c | |||
@@ -118,9 +118,7 @@ int passwd_main(int argc UNUSED_PARAM, char **argv) | |||
118 | myname = xstrdup(xuid2uname(myuid)); | 118 | myname = xstrdup(xuid2uname(myuid)); |
119 | name = argv[0] ? argv[0] : myname; | 119 | name = argv[0] ? argv[0] : myname; |
120 | 120 | ||
121 | pw = getpwnam(name); | 121 | pw = xgetpwnam(name); |
122 | if (!pw) | ||
123 | bb_error_msg_and_die("unknown user %s", name); | ||
124 | if (myuid && pw->pw_uid != myuid) { | 122 | if (myuid && pw->pw_uid != myuid) { |
125 | /* LOGMODE_BOTH */ | 123 | /* LOGMODE_BOTH */ |
126 | bb_error_msg_and_die("%s can't change password for %s", myname, name); | 124 | bb_error_msg_and_die("%s can't change password for %s", myname, name); |
diff --git a/loginutils/su.c b/loginutils/su.c index 61039d823..e7e0001c7 100644 --- a/loginutils/su.c +++ b/loginutils/su.c | |||
@@ -48,9 +48,7 @@ int su_main(int argc UNUSED_PARAM, char **argv) | |||
48 | openlog(applet_name, 0, LOG_AUTH); | 48 | openlog(applet_name, 0, LOG_AUTH); |
49 | } | 49 | } |
50 | 50 | ||
51 | pw = getpwnam(opt_username); | 51 | pw = xgetpwnam(opt_username); |
52 | if (!pw) | ||
53 | bb_error_msg_and_die("unknown id: %s", opt_username); | ||
54 | 52 | ||
55 | /* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER | 53 | /* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER |
56 | is a username that is retrieved via NIS (YP), but that doesn't have | 54 | is a username that is retrieved via NIS (YP), but that doesn't have |
diff --git a/miscutils/crontab.c b/miscutils/crontab.c index 902014963..13dfd77ad 100644 --- a/miscutils/crontab.c +++ b/miscutils/crontab.c | |||
@@ -126,9 +126,7 @@ int crontab_main(int argc UNUSED_PARAM, char **argv) | |||
126 | } | 126 | } |
127 | 127 | ||
128 | if (opt_ler & OPT_u) { | 128 | if (opt_ler & OPT_u) { |
129 | pas = getpwnam(user_name); | 129 | pas = xgetpwnam(user_name); |
130 | if (!pas) | ||
131 | bb_error_msg_and_die("user %s is not known", user_name); | ||
132 | } else { | 130 | } else { |
133 | pas = xgetpwuid(getuid()); | 131 | pas = xgetpwuid(getuid()); |
134 | } | 132 | } |
diff --git a/networking/tftp.c b/networking/tftp.c index 1f706852a..799dd9903 100644 --- a/networking/tftp.c +++ b/networking/tftp.c | |||
@@ -223,9 +223,7 @@ static int tftp_protocol( | |||
223 | } | 223 | } |
224 | 224 | ||
225 | if (user_opt) { | 225 | if (user_opt) { |
226 | struct passwd *pw = getpwnam(user_opt); | 226 | struct passwd *pw = xgetpwnam(user_opt); |
227 | if (!pw) | ||
228 | bb_error_msg_and_die("unknown user %s", user_opt); | ||
229 | change_identity(pw); /* initgroups, setgid, setuid */ | 227 | change_identity(pw); /* initgroups, setgid, setuid */ |
230 | } | 228 | } |
231 | } | 229 | } |