aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-07-27 11:20:10 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-07-27 11:20:10 +0000
commit3734b946bfef55c8f63d367422da5c7aa7b972db (patch)
tree1bdd3e14523002dc8276d167b17f900e40fa6dff
parent661f6fad77b672a5f6648b01275eb9ff19c59139 (diff)
downloadbusybox-w32-3734b946bfef55c8f63d367422da5c7aa7b972db.tar.gz
busybox-w32-3734b946bfef55c8f63d367422da5c7aa7b972db.tar.bz2
busybox-w32-3734b946bfef55c8f63d367422da5c7aa7b972db.zip
bb_getpwuid, bb_getgrgid: change order of arguments to more intuitive one;
comment thoroughly when they die and when they dont.
-rw-r--r--coreutils/id.c17
-rw-r--r--coreutils/whoami.c3
-rw-r--r--include/libbb.h13
-rw-r--r--libbb/bb_pwd.c90
-rw-r--r--libbb/procps.c5
-rw-r--r--loginutils/passwd.c25
-rw-r--r--sysklogd/logger.c2
7 files changed, 68 insertions, 87 deletions
diff --git a/coreutils/id.c b/coreutils/id.c
index 27fb26e77..614d6d064 100644
--- a/coreutils/id.c
+++ b/coreutils/id.c
@@ -20,13 +20,13 @@
20#define JUST_USER 4 20#define JUST_USER 4
21#define JUST_GROUP 8 21#define JUST_GROUP 8
22#if ENABLE_SELINUX 22#if ENABLE_SELINUX
23#define JUST_CONTEXT 16 23#define JUST_CONTEXT 16
24#endif 24#endif
25 25
26static short printf_full(unsigned int id, const char *arg, const char prefix) 26static int printf_full(unsigned int id, const char *arg, const char prefix)
27{ 27{
28 const char *fmt = "%cid=%u"; 28 const char *fmt = "%cid=%u";
29 short status = EXIT_FAILURE; 29 int status = EXIT_FAILURE;
30 30
31 if (arg) { 31 if (arg) {
32 fmt = "%cid=%u(%s)"; 32 fmt = "%cid=%u(%s)";
@@ -71,8 +71,8 @@ int id_main(int argc, char **argv)
71 if (flags & (JUST_GROUP | JUST_USER USE_SELINUX(| JUST_CONTEXT))) { 71 if (flags & (JUST_GROUP | JUST_USER USE_SELINUX(| JUST_CONTEXT))) {
72 /* JUST_GROUP and JUST_USER are mutually exclusive */ 72 /* JUST_GROUP and JUST_USER are mutually exclusive */
73 if (flags & NAME_NOT_NUMBER) { 73 if (flags & NAME_NOT_NUMBER) {
74 /* bb_getpwuid and bb_getgrgid exit on failure so puts cannot segfault */ 74 /* bb_getXXXid(-1) exit on failure, puts cannot segfault */
75 puts((flags & JUST_USER) ? bb_getpwuid(NULL, uid, -1 ) : bb_getgrgid(NULL, gid, -1 )); 75 puts((flags & JUST_USER) ? bb_getpwuid(NULL, -1, uid) : bb_getgrgid(NULL, -1, gid));
76 } else { 76 } else {
77 if (flags & JUST_USER) { 77 if (flags & JUST_USER) {
78 printf("%u\n", uid); 78 printf("%u\n", uid);
@@ -100,11 +100,10 @@ int id_main(int argc, char **argv)
100 } 100 }
101 101
102 /* Print full info like GNU id */ 102 /* Print full info like GNU id */
103 /* bb_getpwuid doesn't exit on failure here */ 103 /* bb_getpwuid(0) doesn't exit on failure (returns NULL) */
104 status = printf_full(uid, bb_getpwuid(NULL, uid, 0), 'u'); 104 status = printf_full(uid, bb_getpwuid(NULL, 0, uid), 'u');
105 putchar(' '); 105 putchar(' ');
106 /* bb_getgrgid doesn't exit on failure here */ 106 status |= printf_full(gid, bb_getgrgid(NULL, 0, gid), 'g');
107 status |= printf_full(gid, bb_getgrgid(NULL, gid, 0), 'g');
108 107
109#if ENABLE_SELINUX 108#if ENABLE_SELINUX
110 if (is_selinux_enabled()) { 109 if (is_selinux_enabled()) {
diff --git a/coreutils/whoami.c b/coreutils/whoami.c
index 156516fa7..3718358d5 100644
--- a/coreutils/whoami.c
+++ b/coreutils/whoami.c
@@ -19,7 +19,8 @@ int whoami_main(int argc, char **argv)
19 if (argc > 1) 19 if (argc > 1)
20 bb_show_usage(); 20 bb_show_usage();
21 21
22 puts(bb_getpwuid(NULL, geteuid(), -1)); 22 /* Will complain and die if username not found */
23 puts(bb_getpwuid(NULL, -1, geteuid()));
23 24
24 return fflush(stdout); 25 return fflush(stdout);
25} 26}
diff --git a/include/libbb.h b/include/libbb.h
index 8fb5520eb..82cee380b 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -481,10 +481,14 @@ struct bb_uidgid_t {
481int get_uidgid(struct bb_uidgid_t*, const char*, int numeric_ok); 481int get_uidgid(struct bb_uidgid_t*, const char*, int numeric_ok);
482/* chown-like handling of "user[:[group]" */ 482/* chown-like handling of "user[:[group]" */
483void parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group); 483void parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group);
484/* what is this? */ 484/* bb_getpwuid, bb_getgrgid:
485/*extern char *bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix);*/ 485bb_getXXXid(buf, bufsz, id) - copy user/group name or id
486char *bb_getpwuid(char *name, long uid, int bufsize); 486 as a string to buf, return user/group name or NULL
487char *bb_getgrgid(char *group, long gid, int bufsize); 487bb_getXXXid(NULL, 0, id) - return user/group name or NULL
488bb_getXXXid(NULL, -1, id) - return user/group name or exit
489*/
490char *bb_getpwuid(char *name, int bufsize, long uid);
491char *bb_getgrgid(char *group, int bufsize, long gid);
488/* versions which cache results (useful for ps, ls etc) */ 492/* versions which cache results (useful for ps, ls etc) */
489const char* get_cached_username(uid_t uid); 493const char* get_cached_username(uid_t uid);
490const char* get_cached_groupname(gid_t gid); 494const char* get_cached_groupname(gid_t gid);
@@ -596,6 +600,7 @@ extern const char *opt_complementary;
596extern const char *applet_long_options; 600extern const char *applet_long_options;
597#endif 601#endif
598extern uint32_t option_mask32; 602extern uint32_t option_mask32;
603/* TODO: don't pass argc, determine it by looking at argv */
599extern uint32_t getopt32(int argc, char **argv, const char *applet_opts, ...); 604extern uint32_t getopt32(int argc, char **argv, const char *applet_opts, ...);
600 605
601 606
diff --git a/libbb/bb_pwd.c b/libbb/bb_pwd.c
index 3af1994d5..2bdb66230 100644
--- a/libbb/bb_pwd.c
+++ b/libbb/bb_pwd.c
@@ -11,22 +11,20 @@
11 11
12#define assert(x) ((void)0) 12#define assert(x) ((void)0)
13 13
14/*
15 * if bufsize is > 0 char *buffer cannot be set to NULL.
16 * If idname is not NULL it is written on the static
17 * allocated buffer (and a pointer to it is returned).
18 * if idname is NULL, id as string is written to the static
19 * allocated buffer and NULL is returned.
20 * if bufsize is = 0 char *buffer can be set to NULL.
21 * If idname exists a pointer to it is returned,
22 * else NULL is returned.
23 * if bufsize is < 0 char *buffer can be set to NULL.
24 * If idname exists a pointer to it is returned,
25 * else an error message is printed and the program exits.
26 */
27
28/* internal function for bb_getpwuid and bb_getgrgid */ 14/* internal function for bb_getpwuid and bb_getgrgid */
29static char* bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix) 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 */
27static char* bb_getug(char *buffer, int bufsize, char *idname, long id, char prefix)
30{ 28{
31 if (bufsize > 0) { 29 if (bufsize > 0) {
32 assert(buffer != NULL); 30 assert(buffer != NULL);
@@ -40,31 +38,29 @@ static char* bb_getug(char *buffer, char *idname, long id, int bufsize, char pre
40 return idname; 38 return idname;
41} 39}
42 40
43/* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more 41/* bb_getpwuid, bb_getgrgid:
44 * flexible : 42 * bb_getXXXid(buf, bufsz, id) - copy user/group name or id
45 * 43 * as a string to buf, return user/group name or NULL
46 * if bufsize is > 0 char *group cannot be set to NULL. 44 * bb_getXXXid(NULL, 0, id) - return user/group name or NULL
47 * On success groupname is written on static allocated buffer 45 * bb_getXXXid(NULL, -1, id) - return user/group name or exit
48 * group (and a pointer to it is returned).
49 * On failure gid as string is written to static allocated
50 * buffer group and NULL is returned.
51 * if bufsize is = 0 char *group can be set to NULL.
52 * On success groupname is returned.
53 * On failure NULL is returned.
54 * if bufsize is < 0 char *group can be set to NULL.
55 * On success groupname is returned.
56 * On failure an error message is printed and
57 * the program exits.
58 */ 46 */
47/* gets a username given a uid */
48char* bb_getpwuid(char *name, int bufsize, long uid)
49{
50 struct passwd *myuser = getpwuid(uid);
59 51
52 return bb_getug(name, bufsize,
53 (myuser ? myuser->pw_name : (char*)myuser),
54 uid, 'u');
55}
60/* gets a groupname given a gid */ 56/* gets a groupname given a gid */
61char* bb_getgrgid(char *group, long gid, int bufsize) 57char* bb_getgrgid(char *group, int bufsize, long gid)
62{ 58{
63 struct group *mygroup = getgrgid(gid); 59 struct group *mygroup = getgrgid(gid);
64 60
65 return bb_getug(group, 61 return bb_getug(group, bufsize,
66 mygroup ? mygroup->gr_name : (char *)mygroup, 62 (mygroup ? mygroup->gr_name : (char*)mygroup),
67 gid, bufsize, 'g'); 63 gid, 'g');
68} 64}
69 65
70/* returns a gid given a group name */ 66/* returns a gid given a group name */
@@ -91,32 +87,6 @@ long xuname2uid(const char *name)
91 return myuser->pw_uid; 87 return myuser->pw_uid;
92} 88}
93 89
94/* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
95 * flexible :
96 *
97 * if bufsize is > 0 char *name cannot be set to NULL.
98 * On success username is written on the static allocated
99 * buffer name (and a pointer to it is returned).
100 * On failure uid as string is written to the static
101 * allocated buffer name and NULL is returned.
102 * if bufsize is = 0 char *name can be set to NULL.
103 * On success username is returned.
104 * On failure NULL is returned.
105 * if bufsize is < 0 char *name can be set to NULL
106 * On success username is returned.
107 * On failure an error message is printed and
108 * the program exits.
109 */
110
111/* gets a username given a uid */
112char* bb_getpwuid(char *name, long uid, int bufsize)
113{
114 struct passwd *myuser = getpwuid(uid);
115
116 return bb_getug(name, myuser ? myuser->pw_name : (char *)myuser,
117 uid, bufsize, 'u');
118}
119
120unsigned long get_ug_id(const char *s, 90unsigned long get_ug_id(const char *s,
121 long (*xname2id)(const char *)) 91 long (*xname2id)(const char *))
122{ 92{
diff --git a/libbb/procps.c b/libbb/procps.c
index 37593700a..aa207af6f 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -52,7 +52,7 @@ static int get_cached(cache_t *cp, unsigned id)
52} 52}
53#endif 53#endif
54 54
55typedef char* ug_func(char *name, long uid, int bufsize); 55typedef char* ug_func(char *name, int bufsize, long uid);
56static char* get_cached(cache_t *cp, unsigned id, ug_func* fp) 56static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
57{ 57{
58 int i; 58 int i;
@@ -62,7 +62,8 @@ static char* get_cached(cache_t *cp, unsigned id, ug_func* fp)
62 i = cp->size++; 62 i = cp->size++;
63 cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache)); 63 cp->cache = xrealloc(cp->cache, cp->size * sizeof(*cp->cache));
64 cp->cache[i].id = id; 64 cp->cache[i].id = id;
65 fp(cp->cache[i].name, id, sizeof(cp->cache[i].name)); 65 /* Never fails. Generates numeric string if name isn't found */
66 fp(cp->cache[i].name, sizeof(cp->cache[i].name), id);
66 return cp->cache[i].name; 67 return cp->cache[i].name;
67} 68}
68const char* get_cached_username(uid_t uid) 69const char* get_cached_username(uid_t uid)
diff --git a/loginutils/passwd.c b/loginutils/passwd.c
index cd98d4101..a293ee926 100644
--- a/loginutils/passwd.c
+++ b/loginutils/passwd.c
@@ -51,13 +51,13 @@ static char* new_password(const struct passwd *pw, uid_t myuid, int algo)
51 goto err_ret; 51 goto err_ret;
52 } 52 }
53 53
54 /*memset(salt, 0, sizeof(salt)); - why?*/
55 crypt_make_salt(salt, 1, 0); /* des */ 54 crypt_make_salt(salt, 1, 0); /* des */
56 if (algo) { /* MD5 */ 55 if (algo) { /* MD5 */
57 strcpy(salt, "$1$"); 56 strcpy(salt, "$1$");
58 crypt_make_salt(salt + 3, 4, 0); 57 crypt_make_salt(salt + 3, 4, 0);
59 } 58 }
60 ret = xstrdup(pw_encrypt(newp, salt)); /* returns ptr to static */ 59 /* pw_encrypt returns ptr to static */
60 ret = xstrdup(pw_encrypt(newp, salt));
61 /* whee, success! */ 61 /* whee, success! */
62 62
63 err_ret: 63 err_ret:
@@ -80,7 +80,7 @@ int passwd_main(int argc, char **argv)
80 OPT_delete = 0x8, /* -d - delete password */ 80 OPT_delete = 0x8, /* -d - delete password */
81 OPT_lud = 0xe, 81 OPT_lud = 0xe,
82 STATE_ALGO_md5 = 0x10, 82 STATE_ALGO_md5 = 0x10,
83 /*STATE_ALGO_des = 0x20, not needed yet */ 83 //STATE_ALGO_des = 0x20, not needed yet
84 }; 84 };
85 unsigned opt; 85 unsigned opt;
86 int rc; 86 int rc;
@@ -104,7 +104,7 @@ int passwd_main(int argc, char **argv)
104 logmode = LOGMODE_BOTH; 104 logmode = LOGMODE_BOTH;
105 openlog(applet_name, LOG_NOWAIT, LOG_AUTH); 105 openlog(applet_name, LOG_NOWAIT, LOG_AUTH);
106 opt = getopt32(argc, argv, "a:lud", &opt_a); 106 opt = getopt32(argc, argv, "a:lud", &opt_a);
107 argc -= optind; 107 //argc -= optind;
108 argv += optind; 108 argv += optind;
109 109
110 if (strcasecmp(opt_a, "des") != 0) /* -a */ 110 if (strcasecmp(opt_a, "des") != 0) /* -a */
@@ -112,11 +112,13 @@ int passwd_main(int argc, char **argv)
112 //else 112 //else
113 // opt |= STATE_ALGO_des; 113 // opt |= STATE_ALGO_des;
114 myuid = getuid(); 114 myuid = getuid();
115 if ((opt & OPT_lud) && (!argc || myuid)) 115 /* -l, -u, -d require root priv and username argument */
116 if ((opt & OPT_lud) && (myuid || !argv[0]))
116 bb_show_usage(); 117 bb_show_usage();
117 118
118 myname = xstrdup(bb_getpwuid(NULL, myuid, -1)); 119 /* Will complain and die if username not found */
119 name = argc ? argv[0] : myname; 120 myname = xstrdup(bb_getpwuid(NULL, -1, myuid));
121 name = argv[0] ? argv[0] : myname;
120 122
121 pw = getpwnam(name); 123 pw = getpwnam(name);
122 if (!pw) bb_error_msg_and_die("unknown user %s", name); 124 if (!pw) bb_error_msg_and_die("unknown user %s", name);
@@ -158,9 +160,12 @@ int passwd_main(int argc, char **argv)
158 newp = xasprintf("!%s", pw->pw_passwd); 160 newp = xasprintf("!%s", pw->pw_passwd);
159 } else if (opt & OPT_unlock) { 161 } else if (opt & OPT_unlock) {
160 if (c) goto skip; /* not '!' */ 162 if (c) goto skip; /* not '!' */
163 /* pw->pw_passwd pints to static storage,
164 * strdup'ing to avoid nasty surprizes */
161 newp = xstrdup(&pw->pw_passwd[1]); 165 newp = xstrdup(&pw->pw_passwd[1]);
162 } else if (opt & OPT_delete) { 166 } else if (opt & OPT_delete) {
163 newp = xstrdup(""); 167 //newp = xstrdup("");
168 newp = (char*)"";
164 } 169 }
165 170
166 rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * 30000; 171 rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * 30000;
@@ -177,8 +182,8 @@ int passwd_main(int argc, char **argv)
177 filename); 182 filename);
178 bb_info_msg("Password for %s changed by %s", name, myname); 183 bb_info_msg("Password for %s changed by %s", name, myname);
179 184
180 if (ENABLE_FEATURE_CLEAN_UP) free(newp); 185 //if (ENABLE_FEATURE_CLEAN_UP) free(newp);
181skip: 186 skip:
182 if (!newp) { 187 if (!newp) {
183 bb_error_msg_and_die("password for %s is already %slocked", 188 bb_error_msg_and_die("password for %s is already %slocked",
184 name, (opt & OPT_unlock) ? "un" : ""); 189 name, (opt & OPT_unlock) ? "un" : "");
diff --git a/sysklogd/logger.c b/sysklogd/logger.c
index d9c3e5048..3f67aff5c 100644
--- a/sysklogd/logger.c
+++ b/sysklogd/logger.c
@@ -89,7 +89,7 @@ int logger_main(int argc, char **argv)
89 char name[80]; 89 char name[80];
90 90
91 /* Fill out the name string early (may be overwritten later) */ 91 /* Fill out the name string early (may be overwritten later) */
92 bb_getpwuid(name, geteuid(), sizeof(name)); 92 bb_getpwuid(name, sizeof(name), geteuid());
93 str_t = name; 93 str_t = name;
94 94
95 /* Parse any options */ 95 /* Parse any options */