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>
40 static int passwd_marshal_func(struct passwd *, char *, size_t *);
41 static int passwd_lookup_func(const char *, size_t, char **, size_t *);
42 static void *passwd_mp_init_func();
43 static int passwd_mp_lookup_func(char **, size_t *, void *);
44 static void passwd_mp_destroy_func(void *mdata);
47 passwd_marshal_func(struct passwd *pwd, char *buffer, size_t *buffer_size)
50 struct passwd new_pwd;
51 size_t desired_size, size;
53 TRACE_IN(passwd_marshal_func);
54 desired_size = sizeof(struct passwd) + sizeof(char *) +
55 strlen(pwd->pw_name) + 1;
56 if (pwd->pw_passwd != NULL)
57 desired_size += strlen(pwd->pw_passwd) + 1;
58 if (pwd->pw_class != NULL)
59 desired_size += strlen(pwd->pw_class) + 1;
60 if (pwd->pw_gecos != NULL)
61 desired_size += strlen(pwd->pw_gecos) + 1;
62 if (pwd->pw_dir != NULL)
63 desired_size += strlen(pwd->pw_dir) + 1;
64 if (pwd->pw_shell != NULL)
65 desired_size += strlen(pwd->pw_shell) + 1;
67 if ((*buffer_size < desired_size) || (buffer == NULL)) {
68 *buffer_size = desired_size;
69 TRACE_OUT(passwd_marshal_func);
73 memcpy(&new_pwd, pwd, sizeof(struct passwd));
74 memset(buffer, 0, desired_size);
76 *buffer_size = desired_size;
77 p = buffer + sizeof(struct passwd) + sizeof(char *);
78 memcpy(buffer + sizeof(struct passwd), &p, sizeof(char *));
80 if (new_pwd.pw_name != NULL) {
81 size = strlen(new_pwd.pw_name);
82 memcpy(p, new_pwd.pw_name, size);
87 if (new_pwd.pw_passwd != NULL) {
88 size = strlen(new_pwd.pw_passwd);
89 memcpy(p, new_pwd.pw_passwd, size);
90 new_pwd.pw_passwd = p;
94 if (new_pwd.pw_class != NULL) {
95 size = strlen(new_pwd.pw_class);
96 memcpy(p, new_pwd.pw_class, size);
101 if (new_pwd.pw_gecos != NULL) {
102 size = strlen(new_pwd.pw_gecos);
103 memcpy(p, new_pwd.pw_gecos, size);
104 new_pwd.pw_gecos = p;
108 if (new_pwd.pw_dir != NULL) {
109 size = strlen(new_pwd.pw_dir);
110 memcpy(p, new_pwd.pw_dir, size);
115 if (new_pwd.pw_shell != NULL) {
116 size = strlen(new_pwd.pw_shell);
117 memcpy(p, new_pwd.pw_shell, size);
118 new_pwd.pw_shell = p;
122 memcpy(buffer, &new_pwd, sizeof(struct passwd));
123 TRACE_OUT(passwd_marshal_func);
128 passwd_lookup_func(const char *key, size_t key_size, char **buffer,
131 enum nss_lookup_type lookup_type;
136 struct passwd *result;
138 TRACE_IN(passwd_lookup_func);
139 assert(buffer != NULL);
140 assert(buffer_size != NULL);
142 if (key_size < sizeof(enum nss_lookup_type)) {
143 TRACE_OUT(passwd_lookup_func);
146 memcpy(&lookup_type, key, sizeof(enum nss_lookup_type));
148 switch (lookup_type) {
150 size = key_size - sizeof(enum nss_lookup_type) + 1;
151 login = (char *)malloc(size);
152 assert(login != NULL);
153 memset(login, 0, size);
154 memcpy(login, key + sizeof(enum nss_lookup_type), size - 1);
157 if (key_size < sizeof(enum nss_lookup_type) +
159 TRACE_OUT(passwd_lookup_func);
163 memcpy(&uid, key + sizeof(enum nss_lookup_type), sizeof(uid_t));
166 TRACE_OUT(passwd_lookup_func);
170 switch (lookup_type) {
172 result = getpwnam(login);
176 result = getpwuid(uid);
179 /* SHOULD NOT BE REACHED */
183 if (result != NULL) {
184 passwd_marshal_func(result, NULL, buffer_size);
185 *buffer = (char *)malloc(*buffer_size);
186 assert(*buffer != NULL);
187 passwd_marshal_func(result, *buffer, buffer_size);
190 TRACE_OUT(passwd_lookup_func);
191 return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
195 passwd_mp_init_func()
197 TRACE_IN(passwd_mp_init_func);
199 TRACE_OUT(passwd_mp_init_func);
205 passwd_mp_lookup_func(char **buffer, size_t *buffer_size, void *mdata)
207 struct passwd *result;
209 TRACE_IN(passwd_mp_lookup_func);
211 if (result != NULL) {
212 passwd_marshal_func(result, NULL, buffer_size);
213 *buffer = (char *)malloc(*buffer_size);
214 assert(*buffer != NULL);
215 passwd_marshal_func(result, *buffer, buffer_size);
218 TRACE_OUT(passwd_mp_lookup_func);
219 return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
223 passwd_mp_destroy_func(void *mdata)
225 TRACE_IN(passwd_mp_destroy_func);
226 TRACE_OUT(passwd_mp_destroy_func);
232 struct common_agent *retval;
234 TRACE_IN(init_passwd_agent);
235 retval = (struct common_agent *)malloc(sizeof(struct common_agent));
236 assert(retval != NULL);
237 memset(retval, 0, sizeof(struct common_agent));
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()
252 struct multipart_agent *retval;
254 TRACE_IN(init_passwd_mp_agent);
255 retval = (struct multipart_agent *)malloc(
256 sizeof(struct multipart_agent));
257 assert(retval != NULL);
258 memset(retval, 0, sizeof(struct multipart_agent));
260 retval->parent.name = strdup("passwd");
261 retval->parent.type = MULTIPART_AGENT;
262 retval->mp_init_func = passwd_mp_init_func;
263 retval->mp_lookup_func = passwd_mp_lookup_func;
264 retval->mp_destroy_func = passwd_mp_destroy_func;
265 assert(retval->parent.name != NULL);
267 TRACE_OUT(init_passwd_mp_agent);
268 return ((struct agent *)retval);