| 1 |
/* |
| 2 |
* Copyright (c) 2011 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the Institute nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this software |
| 19 |
* without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 22 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 25 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 |
* SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <config.h> |
| 35 |
|
| 36 |
#include "roken.h" |
| 37 |
|
| 38 |
#ifdef TEST_GETXXYYY |
| 39 |
#undef rk_getpwnam_r |
| 40 |
|
| 41 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 42 |
rk_getpwnam_r(const char *, struct passwd *, char *, size_t, struct passwd **); |
| 43 |
#endif |
| 44 |
|
| 45 |
#if !defined(POSIX_GETPWNAM_R) || defined(TEST_GETXXYYY) |
| 46 |
|
| 47 |
/* |
| 48 |
* At least limit the race between threads |
| 49 |
*/ |
| 50 |
|
| 51 |
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL |
| 52 |
rk_getpwnam_r(const char *name, struct passwd *pwd, char *buffer, |
| 53 |
size_t bufsize, struct passwd **result) |
| 54 |
{ |
| 55 |
struct passwd *p; |
| 56 |
size_t slen; |
| 57 |
|
| 58 |
*result = NULL; |
| 59 |
|
| 60 |
p = getpwnam(name); |
| 61 |
if(p == NULL) |
| 62 |
return (errno = ENOENT); |
| 63 |
|
| 64 |
memset(pwd, 0, sizeof(*pwd)); |
| 65 |
|
| 66 |
#define APPEND(el) \ |
| 67 |
do { \ |
| 68 |
slen = strlen(p->el) + 1; \ |
| 69 |
if (slen > bufsize) return (errno = ENOMEM); \ |
| 70 |
memcpy(buffer, p->el, slen); \ |
| 71 |
pwd->el = buffer; \ |
| 72 |
buffer += slen; \ |
| 73 |
bufsize -= slen; \ |
| 74 |
} while(0) |
| 75 |
|
| 76 |
APPEND(pw_name); |
| 77 |
if (p->pw_passwd) |
| 78 |
APPEND(pw_name); |
| 79 |
pwd->pw_uid = p->pw_uid; |
| 80 |
pwd->pw_gid = p->pw_gid; |
| 81 |
APPEND(pw_gecos); |
| 82 |
APPEND(pw_dir); |
| 83 |
APPEND(pw_shell); |
| 84 |
|
| 85 |
*result = pwd; |
| 86 |
|
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
#endif /* POSIX_GETPWNAM_R */ |
| 91 |
|
| 92 |
#ifdef TEST_GETXXYYY |
| 93 |
|
| 94 |
#include <err.h> |
| 95 |
|
| 96 |
int verbose_flag = 0; |
| 97 |
|
| 98 |
static void |
| 99 |
print_result(struct passwd *p) |
| 100 |
{ |
| 101 |
if (!verbose_flag) |
| 102 |
return; |
| 103 |
printf("%s\n", p->pw_name); |
| 104 |
printf("%d\n", (int)p->pw_uid); |
| 105 |
printf("%s\n", p->pw_shell); |
| 106 |
printf("%s\n", p->pw_dir); |
| 107 |
} |
| 108 |
|
| 109 |
int |
| 110 |
main(int argc, char **argv) |
| 111 |
{ |
| 112 |
struct passwd pwd, *result; |
| 113 |
char buf[1024]; |
| 114 |
int ret; |
| 115 |
const char *user; |
| 116 |
|
| 117 |
user = getenv("USER"); |
| 118 |
if (!user) |
| 119 |
user = "root"; |
| 120 |
|
| 121 |
ret = rk_getpwnam_r(user, &pwd, buf, sizeof(buf), &result); |
| 122 |
if (ret) |
| 123 |
errx(1, "rk_getpwnam_r"); |
| 124 |
print_result(result); |
| 125 |
|
| 126 |
ret = rk_getpwnam_r(user, &pwd, buf, 1, &result); |
| 127 |
if (ret == 0) |
| 128 |
errx(1, "rk_getpwnam_r too small buf"); |
| 129 |
|
| 130 |
ret = rk_getpwnam_r("no-user-here-promise", &pwd, buf, sizeof(buf), &result); |
| 131 |
if (ret == 0) |
| 132 |
errx(1, "rk_getpwnam_r no user"); |
| 133 |
|
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
#endif |