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 | * |
| 7 | * Licensed under the GPL v2, see the file LICENSE in this tarball. |
| 8 | */ |
| 9 | |
"Vladimir N. Oleynik" | 86a1073 | 2005-10-12 15:21:32 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 11 | |
| 12 | #ifdef L_bb_getgrgid |
| 13 | /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more |
| 14 | * flexible : |
| 15 | * |
| 16 | * if bufsize is > 0 char *group cannot be set to NULL. |
| 17 | * On success groupname is written on static allocated buffer |
| 18 | * group (and a pointer to it is returned). |
| 19 | * On failure gid as string is written to static allocated |
| 20 | * buffer group and NULL is returned. |
| 21 | * if bufsize is = 0 char *group can be set to NULL. |
| 22 | * On success groupname is returned. |
| 23 | * On failure NULL is returned. |
| 24 | * if bufsize is < 0 char *group can be set to NULL. |
| 25 | * On success groupname is returned. |
| 26 | * On failure an error message is printed and |
| 27 | * the program exits. |
| 28 | */ |
| 29 | |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 30 | #include "grp_.h" |
| 31 | |
| 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 | |
| 37 | return bb_getug(group, (mygroup) ? |
| 38 | mygroup->gr_name : (char *)mygroup, gid, bufsize, 'g'); |
| 39 | } |
| 40 | #endif /* L_bb_getgrgid */ |
| 41 | |
| 42 | #ifdef L_bb_xgetgrnam |
| 43 | #include <stdio.h> |
| 44 | #include <string.h> |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 45 | #include "pwd_.h" |
| 46 | #include "grp_.h" |
| 47 | |
| 48 | |
| 49 | /* returns a gid given a group name */ |
| 50 | long bb_xgetgrnam(const char *name) |
| 51 | { |
| 52 | struct group *mygroup; |
| 53 | |
| 54 | mygroup = getgrnam(name); |
| 55 | if (mygroup==NULL) |
| 56 | bb_error_msg_and_die("unknown group name: %s", name); |
| 57 | |
| 58 | return (mygroup->gr_gid); |
| 59 | } |
| 60 | #endif /* L_bb_xgetgrnam */ |
| 61 | |
| 62 | #ifdef L_bb_xgetpwnam |
| 63 | #include <stdio.h> |
| 64 | #include <string.h> |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 65 | #include "pwd_.h" |
| 66 | #include "grp_.h" |
| 67 | |
| 68 | |
| 69 | /* returns a uid given a username */ |
| 70 | long bb_xgetpwnam(const char *name) |
| 71 | { |
| 72 | struct passwd *myuser; |
| 73 | |
| 74 | myuser = getpwnam(name); |
| 75 | if (myuser==NULL) |
| 76 | bb_error_msg_and_die("unknown user name: %s", name); |
| 77 | |
| 78 | return myuser->pw_uid; |
| 79 | } |
| 80 | #endif /* L_bb_xgetpwnam */ |
| 81 | |
| 82 | #ifdef L_bb_getpwuid |
| 83 | /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more |
| 84 | * flexible : |
| 85 | * |
| 86 | * if bufsize is > 0 char *name can not be set to NULL. |
| 87 | * On success username is written on the static allocated |
| 88 | * buffer name (and a pointer to it is returned). |
| 89 | * On failure uid as string is written to the static |
| 90 | * allocated buffer name and NULL is returned. |
| 91 | * if bufsize is = 0 char *name can be set to NULL. |
| 92 | * On success username is returned. |
| 93 | * On failure NULL is returned. |
| 94 | * if bufsize is < 0 char *name can be set to NULL |
| 95 | * On success username is returned. |
| 96 | * On failure an error message is printed and |
| 97 | * the program exits. |
| 98 | */ |
| 99 | |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 100 | #include "pwd_.h" |
| 101 | |
| 102 | /* gets a username given a uid */ |
| 103 | char * bb_getpwuid(char *name, long uid, int bufsize) |
| 104 | { |
| 105 | struct passwd *myuser = getpwuid(uid); |
| 106 | |
| 107 | return bb_getug(name, (myuser) ? |
| 108 | myuser->pw_name : (char *)myuser , uid, bufsize, 'u'); |
| 109 | } |
| 110 | #endif /* L_bb_getpwuid */ |
| 111 | |
| 112 | #ifdef L_bb_getug |
| 113 | /* |
| 114 | * if bufsize is > 0 char *buffer can not be set to NULL. |
| 115 | * If idname is not NULL it is written on the static |
| 116 | * allocated buffer (and a pointer to it is returned). |
| 117 | * if idname is NULL, id as string is written to the static |
| 118 | * allocated buffer and NULL is returned. |
| 119 | * if bufsize is = 0 char *buffer can be set to NULL. |
| 120 | * If idname exists a pointer to it is returned, |
| 121 | * else NULL is returned. |
| 122 | * if bufsize is < 0 char *buffer can be set to NULL. |
| 123 | * If idname exists a pointer to it is returned, |
| 124 | * else an error message is printed and the program exits. |
| 125 | */ |
| 126 | |
| 127 | #include <stdio.h> |
| 128 | #include <assert.h> |
Bernhard Reutner-Fischer | c5280e8 | 2005-09-20 21:09:31 +0000 | [diff] [blame] | 129 | |
| 130 | |
| 131 | /* internal function for bb_getpwuid and bb_getgrgid */ |
| 132 | char * bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix) |
| 133 | { |
| 134 | if(bufsize > 0 ) { |
| 135 | assert(buffer!=NULL); |
| 136 | if(idname) { |
| 137 | return safe_strncpy(buffer, idname, bufsize); |
| 138 | } |
| 139 | snprintf(buffer, bufsize, "%ld", id); |
| 140 | } else if(bufsize < 0 && !idname) { |
| 141 | bb_error_msg_and_die("unknown %cid %ld", prefix, id); |
| 142 | } |
| 143 | return idname; |
| 144 | } |
| 145 | #endif /* L_bb_getug */ |
| 146 | |
| 147 | |
| 148 | #ifdef L_get_ug_id |
| 149 | /* indirect dispatcher for pwd helpers. */ |
| 150 | #include <stdlib.h> |
| 151 | |
| 152 | extern unsigned long get_ug_id(const char *s, |
| 153 | long (*__bb_getxxnam)(const char *)) |
| 154 | { |
| 155 | unsigned long r; |
| 156 | char *p; |
| 157 | |
| 158 | r = strtoul(s, &p, 10); |
| 159 | if (*p || (s == p)) { |
| 160 | r = __bb_getxxnam(s); |
| 161 | } |
| 162 | |
| 163 | return r; |
| 164 | } |
| 165 | #endif /* L_get_ug_id */ |
| 166 | |
| 167 | /* END CODE */ |
| 168 | /* |
| 169 | Local Variables: |
| 170 | c-file-style: "linux" |
| 171 | c-basic-offset: 4 |
| 172 | tab-width: 4 |
| 173 | End: |
| 174 | */ |
| 175 | |