diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-12-02 22:56:59 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-12-02 22:56:59 +0000 |
commit | 0c68a874e7d996c62c8d00b0c0289180bdce1590 (patch) | |
tree | 57a06bc9317dd24ccf417874c2ecfb10b056adb2 | |
parent | b6ddee242592ffd02c05ae3ed6127c1dd80d7580 (diff) | |
download | busybox-w32-0c68a874e7d996c62c8d00b0c0289180bdce1590.tar.gz busybox-w32-0c68a874e7d996c62c8d00b0c0289180bdce1590.tar.bz2 busybox-w32-0c68a874e7d996c62c8d00b0c0289180bdce1590.zip |
libbb: password/group function helpers rewritten by Tito:
function old new delta
xgetpwuid - 27 +27
uid2uname_utoa - 22 +22
gid2group_utoa - 22 +22
uid2uname - 18 +18
gid2group - 18 +18
buffer_fill_and_print 179 196 +17
print_user 12 24 +12
print_group 12 24 +12
get_cached 89 99 +10
...
print_common 134 120 -14
vlock_main 415 396 -19
logger_main 410 387 -23
crontab_main 642 609 -33
bb_getpwuid 42 - -42
bb_getgrgid 42 - -42
bb_getug 80 - -80
------------------------------------------------------------------------------
(add/remove: 6/3 grow/shrink: 9/11 up/down: 187/-296) Total: -109 bytes
-rw-r--r-- | coreutils/id.c | 10 | ||||
-rw-r--r-- | coreutils/whoami.c | 2 | ||||
-rw-r--r-- | include/libbb.h | 16 | ||||
-rw-r--r-- | libbb/bb_pwd.c | 106 | ||||
-rw-r--r-- | libbb/procps.c | 12 | ||||
-rw-r--r-- | loginutils/passwd.c | 2 | ||||
-rw-r--r-- | loginutils/vlock.c | 7 | ||||
-rw-r--r-- | mailutils/sendmail.c | 2 | ||||
-rw-r--r-- | miscutils/crontab.c | 6 | ||||
-rw-r--r-- | printutils/lpr.c | 2 | ||||
-rw-r--r-- | sysklogd/logger.c | 13 |
11 files changed, 84 insertions, 94 deletions
diff --git a/coreutils/id.c b/coreutils/id.c index 6ddb23666..33e06f427 100644 --- a/coreutils/id.c +++ b/coreutils/id.c | |||
@@ -36,12 +36,8 @@ enum { | |||
36 | #endif | 36 | #endif |
37 | }; | 37 | }; |
38 | 38 | ||
39 | static int print_common(unsigned id, | 39 | static int print_common(unsigned id, const char *name, const char *prefix) |
40 | char* FAST_FUNC bb_getXXXid(char *name, int bufsize, long uid), | ||
41 | const char *prefix) | ||
42 | { | 40 | { |
43 | const char *name = bb_getXXXid(NULL, 0, id); | ||
44 | |||
45 | if (prefix) { | 41 | if (prefix) { |
46 | printf("%s", prefix); | 42 | printf("%s", prefix); |
47 | } | 43 | } |
@@ -65,12 +61,12 @@ static int print_common(unsigned id, | |||
65 | 61 | ||
66 | static int print_group(gid_t id, const char *prefix) | 62 | static int print_group(gid_t id, const char *prefix) |
67 | { | 63 | { |
68 | return print_common(id, bb_getgrgid, prefix); | 64 | return print_common(id, gid2group(id), prefix); |
69 | } | 65 | } |
70 | 66 | ||
71 | static int print_user(uid_t id, const char *prefix) | 67 | static int print_user(uid_t id, const char *prefix) |
72 | { | 68 | { |
73 | return print_common(id, bb_getpwuid, prefix); | 69 | return print_common(id, uid2uname(id), prefix); |
74 | } | 70 | } |
75 | 71 | ||
76 | /* On error set *n < 0 and return >= 0 | 72 | /* On error set *n < 0 and return >= 0 |
diff --git a/coreutils/whoami.c b/coreutils/whoami.c index 6756d4ba9..0dbcba955 100644 --- a/coreutils/whoami.c +++ b/coreutils/whoami.c | |||
@@ -20,7 +20,7 @@ int whoami_main(int argc, char **argv UNUSED_PARAM) | |||
20 | bb_show_usage(); | 20 | bb_show_usage(); |
21 | 21 | ||
22 | /* Will complain and die if username not found */ | 22 | /* Will complain and die if username not found */ |
23 | puts(bb_getpwuid(NULL, -1, geteuid())); | 23 | puts(xuid2uname(geteuid())); |
24 | 24 | ||
25 | return fflush(stdout); | 25 | return fflush(stdout); |
26 | } | 26 | } |
diff --git a/include/libbb.h b/include/libbb.h index e4ccc7573..acae93a56 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -702,14 +702,14 @@ 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 | /* bb_getpwuid, bb_getgrgid: | 705 | struct passwd* xgetpwuid(uid_t uid) FAST_FUNC; |
706 | * bb_getXXXid(buf, bufsz, id) - copy user/group name or id | 706 | struct group* xgetgrgid(gid_t gid) FAST_FUNC; |
707 | * as a string to buf, return user/group name or NULL | 707 | char* xuid2uname(uid_t uid) FAST_FUNC; |
708 | * bb_getXXXid(NULL, 0, id) - return user/group name or NULL | 708 | char* xgid2group(gid_t gid) FAST_FUNC; |
709 | * bb_getXXXid(NULL, -1, id) - return user/group name or exit | 709 | char* uid2uname(uid_t uid) FAST_FUNC; |
710 | */ | 710 | char* gid2group(gid_t gid) FAST_FUNC; |
711 | char *bb_getpwuid(char *name, int bufsize, long uid) FAST_FUNC; | 711 | char* uid2uname_utoa(long uid) FAST_FUNC; |
712 | char *bb_getgrgid(char *group, int bufsize, long gid) FAST_FUNC; | 712 | char* gid2group_utoa(long gid) FAST_FUNC; |
713 | /* versions which cache results (useful for ps, ls etc) */ | 713 | /* versions which cache results (useful for ps, ls etc) */ |
714 | const char* get_cached_username(uid_t uid) FAST_FUNC; | 714 | const char* get_cached_username(uid_t uid) FAST_FUNC; |
715 | const char* get_cached_groupname(gid_t gid) FAST_FUNC; | 715 | const char* get_cached_groupname(gid_t gid) FAST_FUNC; |
diff --git a/libbb/bb_pwd.c b/libbb/bb_pwd.c index 65eb69200..5e44edc90 100644 --- a/libbb/bb_pwd.c +++ b/libbb/bb_pwd.c | |||
@@ -3,79 +3,72 @@ | |||
3 | * password utility routines. | 3 | * password utility routines. |
4 | * | 4 | * |
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
6 | * Copyright (C) 2008 by Tito Ragusa <farmatito@tiscali.it> | ||
6 | * | 7 | * |
7 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. | 8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
8 | */ | 9 | */ |
9 | 10 | ||
10 | #include "libbb.h" | 11 | #include "libbb.h" |
11 | 12 | ||
12 | #define assert(x) ((void)0) | 13 | /* TODO: maybe change API to return malloced data? |
13 | 14 | * This will allow to stop using libc functions returning | |
14 | /* internal function for bb_getpwuid and bb_getgrgid */ | 15 | * pointers to static data (getpwuid) |
15 | /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more | ||
16 | * flexible: | ||
17 | * | ||
18 | * bufsize > 0: If idname is not NULL it is copied to buffer, | ||
19 | * and buffer is returned. Else id as string is written | ||
20 | * to buffer, and NULL is returned. | ||
21 | * | ||
22 | * bufsize == 0: idname is returned. | ||
23 | * | ||
24 | * bufsize < 0: If idname is not NULL it is returned. | ||
25 | * Else an error message is printed and the program exits. | ||
26 | */ | 16 | */ |
27 | static char* bb_getug(char *buffer, int bufsize, char *idname, long id, char prefix) | 17 | |
18 | /* TODO: add xgetpwnam, this construct is used a lot */ | ||
19 | |||
20 | struct passwd* FAST_FUNC xgetpwuid(uid_t uid) | ||
28 | { | 21 | { |
29 | if (bufsize > 0) { | 22 | struct passwd *pw = getpwuid(uid); |
30 | assert(buffer != NULL); | 23 | if (!pw) |
31 | if (idname) { | 24 | bb_error_msg_and_die("unknown uid %u", (unsigned)uid); |
32 | return safe_strncpy(buffer, idname, bufsize); | 25 | return pw; |
33 | } | ||
34 | snprintf(buffer, bufsize, "%ld", id); | ||
35 | } else if (bufsize < 0 && !idname) { | ||
36 | bb_error_msg_and_die("unknown %cid %ld", prefix, id); | ||
37 | } | ||
38 | return idname; | ||
39 | } | 26 | } |
40 | 27 | ||
41 | /* bb_getpwuid, bb_getgrgid: | 28 | struct group* FAST_FUNC xgetgrgid(gid_t gid) |
42 | * bb_getXXXid(buf, bufsz, id) - copy user/group name or id | ||
43 | * as a string to buf, return user/group name or NULL | ||
44 | * bb_getXXXid(NULL, 0, id) - return user/group name or NULL | ||
45 | * bb_getXXXid(NULL, -1, id) - return user/group name or exit | ||
46 | */ | ||
47 | /* gets a username given a uid */ | ||
48 | char* FAST_FUNC bb_getpwuid(char *name, int bufsize, long uid) | ||
49 | { | 29 | { |
50 | struct passwd *myuser = getpwuid(uid); | 30 | struct group *gr = getgrgid(gid); |
31 | if (!gr) | ||
32 | bb_error_msg_and_die("unknown gid %u", (unsigned)gid); | ||
33 | return gr; | ||
34 | } | ||
51 | 35 | ||
52 | return bb_getug(name, bufsize, | 36 | char* FAST_FUNC xuid2uname(uid_t uid) |
53 | (myuser ? myuser->pw_name : (char*)myuser), | 37 | { |
54 | uid, 'u'); | 38 | struct passwd *pw = xgetpwuid(uid); |
39 | return pw->pw_name; | ||
55 | } | 40 | } |
56 | /* gets a groupname given a gid */ | 41 | |
57 | char* FAST_FUNC bb_getgrgid(char *group, int bufsize, long gid) | 42 | char* FAST_FUNC xgid2group(gid_t gid) |
58 | { | 43 | { |
59 | struct group *mygroup = getgrgid(gid); | 44 | struct group *gr = xgetgrgid(gid); |
45 | return gr->gr_name; | ||
46 | } | ||
60 | 47 | ||
61 | return bb_getug(group, bufsize, | 48 | char* FAST_FUNC uid2uname(uid_t uid) |
62 | (mygroup ? mygroup->gr_name : (char*)mygroup), | 49 | { |
63 | gid, 'g'); | 50 | struct passwd *pw = getpwuid(uid); |
51 | return (pw) ? pw->pw_name : NULL; | ||
64 | } | 52 | } |
65 | 53 | ||
66 | /* returns a gid given a group name */ | 54 | char* FAST_FUNC gid2group(gid_t gid) |
67 | long FAST_FUNC xgroup2gid(const char *name) | ||
68 | { | 55 | { |
69 | struct group *mygroup; | 56 | struct group *gr = getgrgid(gid); |
57 | return (gr) ? gr->gr_name : NULL; | ||
58 | } | ||
70 | 59 | ||
71 | mygroup = getgrnam(name); | 60 | char* FAST_FUNC uid2uname_utoa(long uid) |
72 | if (mygroup == NULL) | 61 | { |
73 | bb_error_msg_and_die("unknown group name: %s", name); | 62 | char *name = uid2uname(uid); |
63 | return (name) ? name : utoa(uid); | ||
64 | } | ||
74 | 65 | ||
75 | return mygroup->gr_gid; | 66 | char* FAST_FUNC gid2group_utoa(long gid) |
67 | { | ||
68 | char *name = gid2group(gid); | ||
69 | return (name) ? name : utoa(gid); | ||
76 | } | 70 | } |
77 | 71 | ||
78 | /* returns a uid given a username */ | ||
79 | long FAST_FUNC xuname2uid(const char *name) | 72 | long FAST_FUNC xuname2uid(const char *name) |
80 | { | 73 | { |
81 | struct passwd *myuser; | 74 | struct passwd *myuser; |
@@ -87,6 +80,17 @@ long FAST_FUNC xuname2uid(const char *name) | |||
87 | return myuser->pw_uid; | 80 | return myuser->pw_uid; |
88 | } | 81 | } |
89 | 82 | ||
83 | long FAST_FUNC xgroup2gid(const char *name) | ||
84 | { | ||
85 | struct group *mygroup; | ||
86 | |||
87 | mygroup = getgrnam(name); | ||
88 | if (mygroup == NULL) | ||
89 | bb_error_msg_and_die("unknown group %s", name); | ||
90 | |||
91 | return mygroup->gr_gid; | ||
92 | } | ||
93 | |||
90 | unsigned long FAST_FUNC get_ug_id(const char *s, | 94 | unsigned long FAST_FUNC get_ug_id(const char *s, |
91 | long FAST_FUNC (*xname2id)(const char *)) | 95 | long FAST_FUNC (*xname2id)(const char *)) |
92 | { | 96 | { |
diff --git a/libbb/procps.c b/libbb/procps.c index 4d9a95b4f..c5e40bf88 100644 --- a/libbb/procps.c +++ b/libbb/procps.c | |||
@@ -13,7 +13,7 @@ | |||
13 | 13 | ||
14 | 14 | ||
15 | typedef struct unsigned_to_name_map_t { | 15 | typedef struct unsigned_to_name_map_t { |
16 | unsigned id; | 16 | long id; |
17 | char name[USERNAME_MAX_SIZE]; | 17 | char name[USERNAME_MAX_SIZE]; |
18 | } unsigned_to_name_map_t; | 18 | } unsigned_to_name_map_t; |
19 | 19 | ||
@@ -52,8 +52,8 @@ static int get_cached(cache_t *cp, unsigned id) | |||
52 | } | 52 | } |
53 | #endif | 53 | #endif |
54 | 54 | ||
55 | typedef char* FAST_FUNC ug_func(char *name, int bufsize, long uid); | 55 | static char* get_cached(cache_t *cp, long id, |
56 | static char* get_cached(cache_t *cp, unsigned id, ug_func* fp) | 56 | char* FAST_FUNC x2x_utoa(long id)) |
57 | { | 57 | { |
58 | int i; | 58 | int i; |
59 | for (i = 0; i < cp->size; i++) | 59 | for (i = 0; i < cp->size; i++) |
@@ -63,16 +63,16 @@ static char* get_cached(cache_t *cp, unsigned id, ug_func* fp) | |||
63 | cp->cache = xrealloc_vector(cp->cache, 2, i); | 63 | cp->cache = xrealloc_vector(cp->cache, 2, i); |
64 | cp->cache[i].id = id; | 64 | cp->cache[i].id = id; |
65 | /* Never fails. Generates numeric string if name isn't found */ | 65 | /* Never fails. Generates numeric string if name isn't found */ |
66 | fp(cp->cache[i].name, sizeof(cp->cache[i].name), id); | 66 | safe_strncpy(cp->cache[i].name, x2x_utoa(id), sizeof(cp->cache[i].name)); |
67 | return cp->cache[i].name; | 67 | return cp->cache[i].name; |
68 | } | 68 | } |
69 | const char* FAST_FUNC get_cached_username(uid_t uid) | 69 | const char* FAST_FUNC get_cached_username(uid_t uid) |
70 | { | 70 | { |
71 | return get_cached(&username, uid, bb_getpwuid); | 71 | return get_cached(&username, uid, uid2uname_utoa); |
72 | } | 72 | } |
73 | const char* FAST_FUNC get_cached_groupname(gid_t gid) | 73 | const char* FAST_FUNC get_cached_groupname(gid_t gid) |
74 | { | 74 | { |
75 | return get_cached(&groupname, gid, bb_getgrgid); | 75 | return get_cached(&groupname, gid, gid2group_utoa); |
76 | } | 76 | } |
77 | 77 | ||
78 | 78 | ||
diff --git a/loginutils/passwd.c b/loginutils/passwd.c index 99fb76eca..b156ab5af 100644 --- a/loginutils/passwd.c +++ b/loginutils/passwd.c | |||
@@ -115,7 +115,7 @@ int passwd_main(int argc UNUSED_PARAM, char **argv) | |||
115 | bb_show_usage(); | 115 | bb_show_usage(); |
116 | 116 | ||
117 | /* Will complain and die if username not found */ | 117 | /* Will complain and die if username not found */ |
118 | myname = xstrdup(bb_getpwuid(NULL, -1, 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 = getpwnam(name); |
diff --git a/loginutils/vlock.c b/loginutils/vlock.c index 0262da5bd..85f489c22 100644 --- a/loginutils/vlock.c +++ b/loginutils/vlock.c | |||
@@ -38,14 +38,9 @@ int vlock_main(int argc UNUSED_PARAM, char **argv) | |||
38 | struct termios term; | 38 | struct termios term; |
39 | struct termios oterm; | 39 | struct termios oterm; |
40 | struct vt_mode ovtm; | 40 | struct vt_mode ovtm; |
41 | uid_t uid; | ||
42 | struct passwd *pw; | 41 | struct passwd *pw; |
43 | /* XXX: xgetpwuid */ | ||
44 | uid = getuid(); | ||
45 | pw = getpwuid(uid); | ||
46 | if (pw == NULL) | ||
47 | bb_error_msg_and_die("unknown uid %d", (int)uid); | ||
48 | 42 | ||
43 | pw = xgetpwuid(getuid()); | ||
49 | opt_complementary = "=0"; /* no params! */ | 44 | opt_complementary = "=0"; /* no params! */ |
50 | getopt32(argv, "a"); | 45 | getopt32(argv, "a"); |
51 | 46 | ||
diff --git a/mailutils/sendmail.c b/mailutils/sendmail.c index 55555c326..b027f94d2 100644 --- a/mailutils/sendmail.c +++ b/mailutils/sendmail.c | |||
@@ -172,7 +172,7 @@ int sendmail_main(int argc UNUSED_PARAM, char **argv) | |||
172 | // got no sender address? -> use system username as a resort | 172 | // got no sender address? -> use system username as a resort |
173 | if (!(opts & OPT_f)) { | 173 | if (!(opts & OPT_f)) { |
174 | // N.B. IMHO getenv("USER") can be way easily spoofed! | 174 | // N.B. IMHO getenv("USER") can be way easily spoofed! |
175 | G.user = bb_getpwuid(NULL, -1, getuid()); | 175 | G.user = xuid2uname(getuid()); |
176 | opt_from = xasprintf("%s@%s", G.user, domain); | 176 | opt_from = xasprintf("%s@%s", G.user, domain); |
177 | } | 177 | } |
178 | if (ENABLE_FEATURE_CLEAN_UP) | 178 | if (ENABLE_FEATURE_CLEAN_UP) |
diff --git a/miscutils/crontab.c b/miscutils/crontab.c index 673b558c3..902014963 100644 --- a/miscutils/crontab.c +++ b/miscutils/crontab.c | |||
@@ -130,11 +130,7 @@ int crontab_main(int argc UNUSED_PARAM, char **argv) | |||
130 | if (!pas) | 130 | if (!pas) |
131 | bb_error_msg_and_die("user %s is not known", user_name); | 131 | bb_error_msg_and_die("user %s is not known", user_name); |
132 | } else { | 132 | } else { |
133 | /* XXX: xgetpwuid */ | 133 | pas = xgetpwuid(getuid()); |
134 | uid_t my_uid = getuid(); | ||
135 | pas = getpwuid(my_uid); | ||
136 | if (!pas) | ||
137 | bb_perror_msg_and_die("unknown uid %d", (int)my_uid); | ||
138 | } | 134 | } |
139 | 135 | ||
140 | #define user_name DONT_USE_ME_BEYOND_THIS_POINT | 136 | #define user_name DONT_USE_ME_BEYOND_THIS_POINT |
diff --git a/printutils/lpr.c b/printutils/lpr.c index 25cbcbc5e..f21cffddf 100644 --- a/printutils/lpr.c +++ b/printutils/lpr.c | |||
@@ -65,7 +65,7 @@ int lpqr_main(int argc UNUSED_PARAM, char *argv[]) | |||
65 | const char *server = "localhost"; // server[:port] of printer queue | 65 | const char *server = "localhost"; // server[:port] of printer queue |
66 | char *hostname; | 66 | char *hostname; |
67 | // N.B. IMHO getenv("USER") can be way easily spoofed! | 67 | // N.B. IMHO getenv("USER") can be way easily spoofed! |
68 | const char *user = bb_getpwuid(NULL, -1, getuid()); | 68 | const char *user = xuid2uname(getuid()); |
69 | unsigned job; | 69 | unsigned job; |
70 | unsigned opts; | 70 | unsigned opts; |
71 | int fd; | 71 | int fd; |
diff --git a/sysklogd/logger.c b/sysklogd/logger.c index 625811356..759981c75 100644 --- a/sysklogd/logger.c +++ b/sysklogd/logger.c | |||
@@ -72,22 +72,21 @@ int logger_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | |||
72 | int logger_main(int argc, char **argv) | 72 | int logger_main(int argc, char **argv) |
73 | { | 73 | { |
74 | char *str_p, *str_t; | 74 | char *str_p, *str_t; |
75 | int opt; | ||
75 | int i = 0; | 76 | int i = 0; |
76 | char name[80]; | ||
77 | 77 | ||
78 | /* Fill out the name string early (may be overwritten later) */ | 78 | /* Fill out the name string early (may be overwritten later) */ |
79 | bb_getpwuid(name, sizeof(name), geteuid()); | 79 | str_t = uid2uname_utoa(geteuid()); |
80 | str_t = name; | ||
81 | 80 | ||
82 | /* Parse any options */ | 81 | /* Parse any options */ |
83 | getopt32(argv, "p:st:", &str_p, &str_t); | 82 | opt = getopt32(argv, "p:st:", &str_p, &str_t); |
84 | 83 | ||
85 | if (option_mask32 & 0x2) /* -s */ | 84 | if (opt & 0x2) /* -s */ |
86 | i |= LOG_PERROR; | 85 | i |= LOG_PERROR; |
87 | //if (option_mask32 & 0x4) /* -t */ | 86 | //if (opt & 0x4) /* -t */ |
88 | openlog(str_t, i, 0); | 87 | openlog(str_t, i, 0); |
89 | i = LOG_USER | LOG_NOTICE; | 88 | i = LOG_USER | LOG_NOTICE; |
90 | if (option_mask32 & 0x1) /* -p */ | 89 | if (opt & 0x1) /* -p */ |
91 | i = pencode(str_p); | 90 | i = pencode(str_p); |
92 | 91 | ||
93 | argc -= optind; | 92 | argc -= optind; |