Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * password utility routines. |
| 4 | * |
| 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
| 6 | * |
Bernhard Reutner-Fischer | 7fee0c4 | 2006-09-13 16:39:19 +0000 | [diff] [blame] | 7 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denis Vlasenko | 7d219aa | 2006-10-05 10:17:08 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
| 11 | #include <string.h> |
| 12 | #include <assert.h> |
| 13 | #include "libbb.h" |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 14 | |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 15 | /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more |
| 16 | * flexible : |
| 17 | * |
| 18 | * if bufsize is > 0 char *group cannot be set to NULL. |
| 19 | * On success groupname is written on static allocated buffer |
| 20 | * group (and a pointer to it is returned). |
| 21 | * On failure gid as string is written to static allocated |
| 22 | * buffer group and NULL is returned. |
| 23 | * if bufsize is = 0 char *group can be set to NULL. |
| 24 | * On success groupname is returned. |
| 25 | * On failure NULL is returned. |
| 26 | * if bufsize is < 0 char *group can be set to NULL. |
| 27 | * On success groupname is returned. |
| 28 | * On failure an error message is printed and |
| 29 | * the program exits. |
| 30 | */ |
| 31 | |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 32 | /* gets a groupname given a gid */ |
| 33 | char * bb_getgrgid(char *group, long gid, int bufsize) |
| 34 | { |
| 35 | struct group *mygroup = getgrgid(gid); |
| 36 | |
Denis Vlasenko | cf94446 | 2006-10-03 19:02:20 +0000 | [diff] [blame] | 37 | return bb_getug(group, (mygroup) ? |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 38 | mygroup->gr_name : (char *)mygroup, gid, bufsize, 'g'); |
| 39 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 40 | |
| 41 | /* returns a gid given a group name */ |
| 42 | long bb_xgetgrnam(const char *name) |
| 43 | { |
| 44 | struct group *mygroup; |
| 45 | |
| 46 | mygroup = getgrnam(name); |
| 47 | if (mygroup==NULL) |
| 48 | bb_error_msg_and_die("unknown group name: %s", name); |
| 49 | |
| 50 | return (mygroup->gr_gid); |
| 51 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 52 | |
| 53 | /* returns a uid given a username */ |
| 54 | long bb_xgetpwnam(const char *name) |
| 55 | { |
| 56 | struct passwd *myuser; |
| 57 | |
| 58 | myuser = getpwnam(name); |
| 59 | if (myuser==NULL) |
| 60 | bb_error_msg_and_die("unknown user name: %s", name); |
| 61 | |
| 62 | return myuser->pw_uid; |
| 63 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 64 | |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 65 | /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more |
| 66 | * flexible : |
| 67 | * |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame^] | 68 | * if bufsize is > 0 char *name cannot be set to NULL. |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 69 | * On success username is written on the static allocated |
| 70 | * buffer name (and a pointer to it is returned). |
| 71 | * On failure uid as string is written to the static |
| 72 | * allocated buffer name and NULL is returned. |
| 73 | * if bufsize is = 0 char *name can be set to NULL. |
| 74 | * On success username is returned. |
| 75 | * On failure NULL is returned. |
| 76 | * if bufsize is < 0 char *name can be set to NULL |
| 77 | * On success username is returned. |
| 78 | * On failure an error message is printed and |
| 79 | * the program exits. |
| 80 | */ |
| 81 | |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 82 | /* gets a username given a uid */ |
| 83 | char * bb_getpwuid(char *name, long uid, int bufsize) |
| 84 | { |
| 85 | struct passwd *myuser = getpwuid(uid); |
| 86 | |
| 87 | return bb_getug(name, (myuser) ? |
| 88 | myuser->pw_name : (char *)myuser , uid, bufsize, 'u'); |
| 89 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 90 | |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 91 | /* |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame^] | 92 | * if bufsize is > 0 char *buffer cannot be set to NULL. |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 93 | * If idname is not NULL it is written on the static |
| 94 | * allocated buffer (and a pointer to it is returned). |
| 95 | * if idname is NULL, id as string is written to the static |
| 96 | * allocated buffer and NULL is returned. |
| 97 | * if bufsize is = 0 char *buffer can be set to NULL. |
| 98 | * If idname exists a pointer to it is returned, |
| 99 | * else NULL is returned. |
| 100 | * if bufsize is < 0 char *buffer can be set to NULL. |
| 101 | * If idname exists a pointer to it is returned, |
| 102 | * else an error message is printed and the program exits. |
| 103 | */ |
| 104 | |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 105 | /* internal function for bb_getpwuid and bb_getgrgid */ |
| 106 | char * bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix) |
| 107 | { |
| 108 | if(bufsize > 0 ) { |
| 109 | assert(buffer!=NULL); |
| 110 | if(idname) { |
| 111 | return safe_strncpy(buffer, idname, bufsize); |
| 112 | } |
| 113 | snprintf(buffer, bufsize, "%ld", id); |
| 114 | } else if(bufsize < 0 && !idname) { |
| 115 | bb_error_msg_and_die("unknown %cid %ld", prefix, id); |
| 116 | } |
| 117 | return idname; |
| 118 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 119 | |
Rob Landley | ca08771 | 2006-03-29 16:52:56 +0000 | [diff] [blame] | 120 | unsigned long get_ug_id(const char *s, |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 121 | long (*__bb_getxxnam)(const char *)) |
| 122 | { |
| 123 | unsigned long r; |
| 124 | char *p; |
| 125 | |
| 126 | r = strtoul(s, &p, 10); |
| 127 | if (*p || (s == p)) { |
| 128 | r = __bb_getxxnam(s); |
| 129 | } |
| 130 | |
| 131 | return r; |
| 132 | } |