libkafs: derivation from non-DES key (rxkad-kdf)
[heimdal.git] / lib / roken / getxxyyy.c
blob5beed69df6de80ae9c9e68bb32db2f7d6872f2f1
1 /*
2 * Copyright (c) 2011 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include <config.h>
36 #include "roken.h"
38 #ifdef TEST_GETXXYYY
39 #undef rk_getpwnam_r
41 ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
42 rk_getpwnam_r(const char *, struct passwd *, char *, size_t, struct passwd **);
43 #endif
45 #if !defined(POSIX_GETPWNAM_R) || defined(TEST_GETXXYYY)
48 * At least limit the race between threads
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)
55 struct passwd *p;
56 size_t slen, n = 0;
58 *result = NULL;
60 p = getpwnam(name);
61 if(p == NULL)
62 return (errno = ENOENT);
64 memset(pwd, 0, sizeof(*pwd));
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)
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);
85 *result = pwd;
87 return 0;
90 #endif /* POSIX_GETPWNAM_R */
92 #ifdef TEST_GETXXYYY
94 #include <err.h>
96 int verbose_flag = 0;
98 static void
99 print_result(struct passwd *p)
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);
110 main(int argc, char **argv)
112 struct passwd pwd, *result;
113 char buf[1024];
114 int ret;
115 const char *user;
117 user = getenv("USER");
118 if (!user)
119 user = "root";
121 ret = rk_getpwnam_r(user, &pwd, buf, sizeof(buf), &result);
122 if (ret)
123 errx(1, "rk_getpwnam_r");
124 print_result(result);
126 ret = rk_getpwnam_r(user, &pwd, buf, 1, &result);
127 if (ret == 0)
128 errx(1, "rk_getpwnam_r too small buf");
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");
134 return 0;
137 #endif