libbb: introduce and use xgetpwnam. ~ -150 bytes.
authorDenis Vlasenko <vda.linux@googlemail.com>
Wed, 3 Dec 2008 19:05:55 +0000 (19:05 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Wed, 3 Dec 2008 19:05:55 +0000 (19:05 -0000)
coreutils/id.c
include/libbb.h
libbb/bb_pwd.c
loginutils/addgroup.c
loginutils/passwd.c
loginutils/su.c
miscutils/crontab.c
networking/tftp.c

index 33e06f4273d4dbb37c4a8d66774c70f8b264b1e6..43f403fa37b6600d32dfddc5ded285e426be8649 100644 (file)
@@ -124,9 +124,7 @@ int id_main(int argc UNUSED_PARAM, char **argv)
 
        username = argv[optind];
        if (username) {
-               struct passwd *p = getpwnam(username);
-               if (!p)
-                       bb_error_msg_and_die("unknown user %s", username);
+               struct passwd *p = xgetpwnam(username);
                euid = ruid = p->pw_uid;
                egid = rgid = p->pw_gid;
        } else {
index a34e8a1f9d2c21f2db6edbc46143840c871aa307..80311db2b2dbb671791bf18caa7a14834f1b6f12 100644 (file)
@@ -702,6 +702,7 @@ int get_uidgid(struct bb_uidgid_t*, const char*, int numeric_ok) FAST_FUNC;
 void xget_uidgid(struct bb_uidgid_t*, const char*) FAST_FUNC;
 /* chown-like handling of "user[:[group]" */
 void parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group) FAST_FUNC;
+struct passwd* xgetpwnam(const char *name) FAST_FUNC;
 struct passwd* xgetpwuid(uid_t uid) FAST_FUNC;
 struct group* xgetgrgid(gid_t gid) FAST_FUNC;
 char* xuid2uname(uid_t uid) FAST_FUNC;
index 5e44edc9056a63a6570a13f5ea2e20010a57aab5..5dbc58d9fd33d52349d11f6c9917bf5c4ce12ce7 100644 (file)
  * pointers to static data (getpwuid)
  */
 
-/* TODO: add xgetpwnam, this construct is used a lot */
+struct passwd* FAST_FUNC xgetpwnam(const char *name)
+{
+       struct passwd *pw = getpwnam(name);
+       if (!pw)
+               bb_error_msg_and_die("unknown user %s", name);
+       return pw;
+}
+
+/* xgetgrnam too? */
 
 struct passwd* FAST_FUNC xgetpwuid(uid_t uid)
 {
@@ -73,10 +81,7 @@ long FAST_FUNC xuname2uid(const char *name)
 {
        struct passwd *myuser;
 
-       myuser = getpwnam(name);
-       if (myuser == NULL)
-               bb_error_msg_and_die("unknown user %s", name);
-
+       myuser = xgetpwnam(name);
        return myuser->pw_uid;
 }
 
index 89414d7381fbfadedb165efe5fc61fae49c7b20f..2a840d7c03903b3f1e2e043ee2948d8c7f592d1c 100644 (file)
@@ -159,6 +159,7 @@ int addgroup_main(int argc UNUSED_PARAM, char **argv)
                /* check if group and user exist */
                xuname2uid(argv[0]); /* unknown user: exit */
                xgroup2gid(argv[1]); /* unknown group: exit */
+// race here!
                /* check if user is already in this group */
                gr = getgrnam(argv[1]);
                for (; *(gr->gr_mem) != NULL; (gr->gr_mem)++) {
index e3e74bae7b16d01d9dc1b8450336a5577b20e8da..aa89b87a755b14ce9f1ca6eba17ffd61ba0eab1d 100644 (file)
@@ -118,9 +118,7 @@ int passwd_main(int argc UNUSED_PARAM, char **argv)
        myname = xstrdup(xuid2uname(myuid));
        name = argv[0] ? argv[0] : myname;
 
-       pw = getpwnam(name);
-       if (!pw)
-               bb_error_msg_and_die("unknown user %s", name);
+       pw = xgetpwnam(name);
        if (myuid && pw->pw_uid != myuid) {
                /* LOGMODE_BOTH */
                bb_error_msg_and_die("%s can't change password for %s", myname, name);
index 61039d823f77240748eda605cd804b8a076b5450..e7e0001c73c996a2c2de4e8d8f704c8045bf1bfc 100644 (file)
@@ -48,9 +48,7 @@ int su_main(int argc UNUSED_PARAM, char **argv)
                openlog(applet_name, 0, LOG_AUTH);
        }
 
-       pw = getpwnam(opt_username);
-       if (!pw)
-               bb_error_msg_and_die("unknown id: %s", opt_username);
+       pw = xgetpwnam(opt_username);
 
        /* Make sure pw->pw_shell is non-NULL.  It may be NULL when NEW_USER
           is a username that is retrieved via NIS (YP), but that doesn't have
index 902014963dc951054a12068aad84fee0af3de217..13dfd77ad18ce42df0186d27fb3a2d6e2ca8846a 100644 (file)
@@ -126,9 +126,7 @@ int crontab_main(int argc UNUSED_PARAM, char **argv)
        }
 
        if (opt_ler & OPT_u) {
-               pas = getpwnam(user_name);
-               if (!pas)
-                       bb_error_msg_and_die("user %s is not known", user_name);
+               pas = xgetpwnam(user_name);
        } else {
                pas = xgetpwuid(getuid());
        }
index 1f706852aaec60ff12078a5fea62aa644c2ba9ba..799dd99039a5aded485d5b01c0882e956fe258a7 100644 (file)
@@ -223,9 +223,7 @@ static int tftp_protocol(
                }
 
                if (user_opt) {
-                       struct passwd *pw = getpwnam(user_opt);
-                       if (!pw)
-                               bb_error_msg_and_die("unknown user %s", user_opt);
+                       struct passwd *pw = xgetpwnam(user_opt);
                        change_identity(pw); /* initgroups, setgid, setuid */
                }
        }