2 * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
42 static int passwd_marshal_func(struct passwd *, char *, size_t *);
43 static int passwd_lookup_func(const char *, size_t, char **, size_t *);
44 static void *passwd_mp_init_func(void);
45 static int passwd_mp_lookup_func(char **, size_t *, void *);
46 static void passwd_mp_destroy_func(void *mdata);
49 passwd_marshal_func(struct passwd *pwd, char *buffer, size_t *buffer_size)
52 struct passwd new_pwd;
53 size_t desired_size, size;
55 TRACE_IN(passwd_marshal_func);
56 desired_size = sizeof(struct passwd) + sizeof(char *) +
57 strlen(pwd->pw_name) + 1;
58 if (pwd->pw_passwd != NULL)
59 desired_size += strlen(pwd->pw_passwd) + 1;
60 if (pwd->pw_class != NULL)
61 desired_size += strlen(pwd->pw_class) + 1;
62 if (pwd->pw_gecos != NULL)
63 desired_size += strlen(pwd->pw_gecos) + 1;
64 if (pwd->pw_dir != NULL)
65 desired_size += strlen(pwd->pw_dir) + 1;
66 if (pwd->pw_shell != NULL)
67 desired_size += strlen(pwd->pw_shell) + 1;
69 if ((*buffer_size < desired_size) || (buffer == NULL)) {
70 *buffer_size = desired_size;
71 TRACE_OUT(passwd_marshal_func);
75 memcpy(&new_pwd, pwd, sizeof(struct passwd));
76 memset(buffer, 0, desired_size);
78 *buffer_size = desired_size;
79 p = buffer + sizeof(struct passwd) + sizeof(char *);
80 memcpy(buffer + sizeof(struct passwd), &p, sizeof(char *));
82 if (new_pwd.pw_name != NULL) {
83 size = strlen(new_pwd.pw_name);
84 memcpy(p, new_pwd.pw_name, size);
89 if (new_pwd.pw_passwd != NULL) {
90 size = strlen(new_pwd.pw_passwd);
91 memcpy(p, new_pwd.pw_passwd, size);
92 new_pwd.pw_passwd = p;
96 if (new_pwd.pw_class != NULL) {
97 size = strlen(new_pwd.pw_class);
98 memcpy(p, new_pwd.pw_class, size);
103 if (new_pwd.pw_gecos != NULL) {
104 size = strlen(new_pwd.pw_gecos);
105 memcpy(p, new_pwd.pw_gecos, size);
106 new_pwd.pw_gecos = p;
110 if (new_pwd.pw_dir != NULL) {
111 size = strlen(new_pwd.pw_dir);
112 memcpy(p, new_pwd.pw_dir, size);
117 if (new_pwd.pw_shell != NULL) {
118 size = strlen(new_pwd.pw_shell);
119 memcpy(p, new_pwd.pw_shell, size);
120 new_pwd.pw_shell = p;
124 memcpy(buffer, &new_pwd, sizeof(struct passwd));
125 TRACE_OUT(passwd_marshal_func);
130 passwd_lookup_func(const char *key, size_t key_size, char **buffer,
133 enum nss_lookup_type lookup_type;
138 struct passwd *result;
140 TRACE_IN(passwd_lookup_func);
141 assert(buffer != NULL);
142 assert(buffer_size != NULL);
144 if (key_size < sizeof(enum nss_lookup_type)) {
145 TRACE_OUT(passwd_lookup_func);
148 memcpy(&lookup_type, key, sizeof(enum nss_lookup_type));
150 switch (lookup_type) {
152 size = key_size - sizeof(enum nss_lookup_type) + 1;
153 login = calloc(1, size);
154 assert(login != NULL);
155 memcpy(login, key + sizeof(enum nss_lookup_type), size - 1);
158 if (key_size < sizeof(enum nss_lookup_type) +
160 TRACE_OUT(passwd_lookup_func);
164 memcpy(&uid, key + sizeof(enum nss_lookup_type), sizeof(uid_t));
167 TRACE_OUT(passwd_lookup_func);
171 switch (lookup_type) {
173 result = getpwnam(login);
177 result = getpwuid(uid);
180 /* SHOULD NOT BE REACHED */
184 if (result != NULL) {
185 passwd_marshal_func(result, NULL, buffer_size);
186 *buffer = malloc(*buffer_size);
187 assert(*buffer != NULL);
188 passwd_marshal_func(result, *buffer, buffer_size);
191 TRACE_OUT(passwd_lookup_func);
192 return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
196 passwd_mp_init_func(void)
198 TRACE_IN(passwd_mp_init_func);
200 TRACE_OUT(passwd_mp_init_func);
206 passwd_mp_lookup_func(char **buffer, size_t *buffer_size, void *mdata)
208 struct passwd *result;
210 TRACE_IN(passwd_mp_lookup_func);
212 if (result != NULL) {
213 passwd_marshal_func(result, NULL, buffer_size);
214 *buffer = malloc(*buffer_size);
215 assert(*buffer != NULL);
216 passwd_marshal_func(result, *buffer, buffer_size);
219 TRACE_OUT(passwd_mp_lookup_func);
220 return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
224 passwd_mp_destroy_func(void *mdata)
226 TRACE_IN(passwd_mp_destroy_func);
227 TRACE_OUT(passwd_mp_destroy_func);
231 init_passwd_agent(void)
233 struct common_agent *retval;
235 TRACE_IN(init_passwd_agent);
236 retval = calloc(1, sizeof(*retval));
237 assert(retval != NULL);
239 retval->parent.name = strdup("passwd");
240 assert(retval->parent.name != NULL);
242 retval->parent.type = COMMON_AGENT;
243 retval->lookup_func = passwd_lookup_func;
245 TRACE_OUT(init_passwd_agent);
246 return ((struct agent *)retval);
250 init_passwd_mp_agent(void)
252 struct multipart_agent *retval;
254 TRACE_IN(init_passwd_mp_agent);
257 assert(retval != NULL);
259 retval->parent.name = strdup("passwd");
260 retval->parent.type = MULTIPART_AGENT;
261 retval->mp_init_func = passwd_mp_init_func;
262 retval->mp_lookup_func = passwd_mp_lookup_func;
263 retval->mp_destroy_func = passwd_mp_destroy_func;
264 assert(retval->parent.name != NULL);
266 TRACE_OUT(init_passwd_mp_agent);
267 return ((struct agent *)retval);