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 | |
Denis Vlasenko | becd8c5 | 2006-12-01 21:34:20 +0000 | [diff] [blame] | 15 | /* |
| 16 | * if bufsize is > 0 char *buffer cannot be set to NULL. |
| 17 | * If idname is not NULL it is written on the static |
| 18 | * allocated buffer (and a pointer to it is returned). |
| 19 | * if idname is NULL, id as string is written to the static |
| 20 | * allocated buffer and NULL is returned. |
| 21 | * if bufsize is = 0 char *buffer can be set to NULL. |
| 22 | * If idname exists a pointer to it is returned, |
| 23 | * else NULL is returned. |
| 24 | * if bufsize is < 0 char *buffer can be set to NULL. |
| 25 | * If idname exists a pointer to it is returned, |
| 26 | * else an error message is printed and the program exits. |
| 27 | */ |
| 28 | |
| 29 | /* internal function for bb_getpwuid and bb_getgrgid */ |
| 30 | static char * bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix) |
| 31 | { |
| 32 | if (bufsize > 0 ) { |
| 33 | assert(buffer!=NULL); |
| 34 | if(idname) { |
| 35 | return safe_strncpy(buffer, idname, bufsize); |
| 36 | } |
| 37 | snprintf(buffer, bufsize, "%ld", id); |
| 38 | } else if (bufsize < 0 && !idname) { |
| 39 | bb_error_msg_and_die("unknown %cid %ld", prefix, id); |
| 40 | } |
| 41 | return idname; |
| 42 | } |
| 43 | |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 44 | /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more |
| 45 | * flexible : |
| 46 | * |
| 47 | * if bufsize is > 0 char *group cannot be set to NULL. |
| 48 | * On success groupname is written on static allocated buffer |
| 49 | * group (and a pointer to it is returned). |
| 50 | * On failure gid as string is written to static allocated |
| 51 | * buffer group and NULL is returned. |
| 52 | * if bufsize is = 0 char *group can be set to NULL. |
| 53 | * On success groupname is returned. |
| 54 | * On failure NULL is returned. |
| 55 | * if bufsize is < 0 char *group can be set to NULL. |
| 56 | * On success groupname is returned. |
| 57 | * On failure an error message is printed and |
| 58 | * the program exits. |
| 59 | */ |
| 60 | |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 61 | /* gets a groupname given a gid */ |
| 62 | char * bb_getgrgid(char *group, long gid, int bufsize) |
| 63 | { |
| 64 | struct group *mygroup = getgrgid(gid); |
| 65 | |
Denis Vlasenko | cf94446 | 2006-10-03 19:02:20 +0000 | [diff] [blame] | 66 | return bb_getug(group, (mygroup) ? |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 67 | mygroup->gr_name : (char *)mygroup, gid, bufsize, 'g'); |
| 68 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 69 | |
| 70 | /* returns a gid given a group name */ |
| 71 | long bb_xgetgrnam(const char *name) |
| 72 | { |
| 73 | struct group *mygroup; |
| 74 | |
| 75 | mygroup = getgrnam(name); |
| 76 | if (mygroup==NULL) |
| 77 | bb_error_msg_and_die("unknown group name: %s", name); |
| 78 | |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 79 | return mygroup->gr_gid; |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 80 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 81 | |
| 82 | /* returns a uid given a username */ |
| 83 | long bb_xgetpwnam(const char *name) |
| 84 | { |
| 85 | struct passwd *myuser; |
| 86 | |
| 87 | myuser = getpwnam(name); |
| 88 | if (myuser==NULL) |
| 89 | bb_error_msg_and_die("unknown user name: %s", name); |
| 90 | |
| 91 | return myuser->pw_uid; |
| 92 | } |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 93 | |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 94 | /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more |
| 95 | * flexible : |
| 96 | * |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 97 | * if bufsize is > 0 char *name cannot be set to NULL. |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 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 | |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 111 | /* gets a username given a uid */ |
| 112 | char * bb_getpwuid(char *name, long uid, int bufsize) |
| 113 | { |
| 114 | struct passwd *myuser = getpwuid(uid); |
| 115 | |
Denis Vlasenko | becd8c5 | 2006-12-01 21:34:20 +0000 | [diff] [blame] | 116 | return bb_getug(name, myuser ? myuser->pw_name : (char *)myuser, |
| 117 | uid, bufsize, 'u'); |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 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; |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 124 | |
Denis Vlasenko | becd8c5 | 2006-12-01 21:34:20 +0000 | [diff] [blame] | 125 | r = bb_strtoul(s, NULL, 10); |
| 126 | if (errno) |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 127 | r = __bb_getxxnam(s); |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 128 | |
| 129 | return r; |
| 130 | } |