nlookup.9 - document nlookup_init_root
[dragonfly.git] / usr.sbin / nscd / agents / passwd.c
blob631f655e067cba504085cebd121d49470eebf3b3
1 /*-
2 * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
26 * $FreeBSD: src/usr.sbin/nscd/agents/passwd.c,v 1.3 2008/10/23 00:15:00 delphij Exp $
29 #include <sys/types.h>
30 #include <assert.h>
31 #include <nsswitch.h>
32 #include <pwd.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include "../debug.h"
36 #include "passwd.h"
38 static int passwd_marshal_func(struct passwd *, char *, size_t *);
39 static int passwd_lookup_func(const char *, size_t, char **, size_t *);
40 static void *passwd_mp_init_func(void);
41 static int passwd_mp_lookup_func(char **, size_t *, void *);
42 static void passwd_mp_destroy_func(void *mdata);
44 static int
45 passwd_marshal_func(struct passwd *pwd, char *buffer, size_t *buffer_size)
47 char *p;
48 struct passwd new_pwd;
49 size_t desired_size, size;
51 TRACE_IN(passwd_marshal_func);
52 desired_size = sizeof(struct passwd) + sizeof(char *) +
53 strlen(pwd->pw_name) + 1;
54 if (pwd->pw_passwd != NULL)
55 desired_size += strlen(pwd->pw_passwd) + 1;
56 if (pwd->pw_class != NULL)
57 desired_size += strlen(pwd->pw_class) + 1;
58 if (pwd->pw_gecos != NULL)
59 desired_size += strlen(pwd->pw_gecos) + 1;
60 if (pwd->pw_dir != NULL)
61 desired_size += strlen(pwd->pw_dir) + 1;
62 if (pwd->pw_shell != NULL)
63 desired_size += strlen(pwd->pw_shell) + 1;
65 if ((*buffer_size < desired_size) || (buffer == NULL)) {
66 *buffer_size = desired_size;
67 TRACE_OUT(passwd_marshal_func);
68 return (NS_RETURN);
71 memcpy(&new_pwd, pwd, sizeof(struct passwd));
72 memset(buffer, 0, desired_size);
74 *buffer_size = desired_size;
75 p = buffer + sizeof(struct passwd) + sizeof(char *);
76 memcpy(buffer + sizeof(struct passwd), &p, sizeof(char *));
78 if (new_pwd.pw_name != NULL) {
79 size = strlen(new_pwd.pw_name);
80 memcpy(p, new_pwd.pw_name, size);
81 new_pwd.pw_name = p;
82 p += size + 1;
85 if (new_pwd.pw_passwd != NULL) {
86 size = strlen(new_pwd.pw_passwd);
87 memcpy(p, new_pwd.pw_passwd, size);
88 new_pwd.pw_passwd = p;
89 p += size + 1;
92 if (new_pwd.pw_class != NULL) {
93 size = strlen(new_pwd.pw_class);
94 memcpy(p, new_pwd.pw_class, size);
95 new_pwd.pw_class = p;
96 p += size + 1;
99 if (new_pwd.pw_gecos != NULL) {
100 size = strlen(new_pwd.pw_gecos);
101 memcpy(p, new_pwd.pw_gecos, size);
102 new_pwd.pw_gecos = p;
103 p += size + 1;
106 if (new_pwd.pw_dir != NULL) {
107 size = strlen(new_pwd.pw_dir);
108 memcpy(p, new_pwd.pw_dir, size);
109 new_pwd.pw_dir = p;
110 p += size + 1;
113 if (new_pwd.pw_shell != NULL) {
114 size = strlen(new_pwd.pw_shell);
115 memcpy(p, new_pwd.pw_shell, size);
116 new_pwd.pw_shell = p;
117 p += size + 1;
120 memcpy(buffer, &new_pwd, sizeof(struct passwd));
121 TRACE_OUT(passwd_marshal_func);
122 return (NS_SUCCESS);
125 static int
126 passwd_lookup_func(const char *key, size_t key_size, char **buffer,
127 size_t *buffer_size)
129 enum nss_lookup_type lookup_type;
130 char *login;
131 size_t size;
132 uid_t uid;
134 struct passwd *result;
136 TRACE_IN(passwd_lookup_func);
137 assert(buffer != NULL);
138 assert(buffer_size != NULL);
140 if (key_size < sizeof(enum nss_lookup_type)) {
141 TRACE_OUT(passwd_lookup_func);
142 return (NS_UNAVAIL);
144 memcpy(&lookup_type, key, sizeof(enum nss_lookup_type));
146 switch (lookup_type) {
147 case nss_lt_name:
148 size = key_size - sizeof(enum nss_lookup_type) + 1;
149 login = (char *)calloc(1, size);
150 assert(login != NULL);
151 memcpy(login, key + sizeof(enum nss_lookup_type), size - 1);
152 break;
153 case nss_lt_id:
154 if (key_size < sizeof(enum nss_lookup_type) +
155 sizeof(uid_t)) {
156 TRACE_OUT(passwd_lookup_func);
157 return (NS_UNAVAIL);
160 memcpy(&uid, key + sizeof(enum nss_lookup_type), sizeof(uid_t));
161 break;
162 default:
163 TRACE_OUT(passwd_lookup_func);
164 return (NS_UNAVAIL);
167 switch (lookup_type) {
168 case nss_lt_name:
169 result = getpwnam(login);
170 free(login);
171 break;
172 case nss_lt_id:
173 result = getpwuid(uid);
174 break;
175 default:
176 /* SHOULD NOT BE REACHED */
177 break;
180 if (result != NULL) {
181 passwd_marshal_func(result, NULL, buffer_size);
182 *buffer = (char *)malloc(*buffer_size);
183 assert(*buffer != NULL);
184 passwd_marshal_func(result, *buffer, buffer_size);
187 TRACE_OUT(passwd_lookup_func);
188 return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
191 static void *
192 passwd_mp_init_func(void)
194 TRACE_IN(passwd_mp_init_func);
195 setpwent();
196 TRACE_OUT(passwd_mp_init_func);
198 return (NULL);
201 static int
202 passwd_mp_lookup_func(char **buffer, size_t *buffer_size, void *mdata)
204 struct passwd *result;
206 TRACE_IN(passwd_mp_lookup_func);
207 result = getpwent();
208 if (result != NULL) {
209 passwd_marshal_func(result, NULL, buffer_size);
210 *buffer = (char *)malloc(*buffer_size);
211 assert(*buffer != NULL);
212 passwd_marshal_func(result, *buffer, buffer_size);
215 TRACE_OUT(passwd_mp_lookup_func);
216 return (result == NULL ? NS_NOTFOUND : NS_SUCCESS);
219 static void
220 passwd_mp_destroy_func(void *mdata)
222 TRACE_IN(passwd_mp_destroy_func);
223 TRACE_OUT(passwd_mp_destroy_func);
226 struct agent *
227 init_passwd_agent(void)
229 struct common_agent *retval;
231 TRACE_IN(init_passwd_agent);
232 retval = (struct common_agent *)calloc(1, sizeof(struct common_agent));
233 assert(retval != NULL);
235 retval->parent.name = strdup("passwd");
236 assert(retval->parent.name != NULL);
238 retval->parent.type = COMMON_AGENT;
239 retval->lookup_func = passwd_lookup_func;
241 TRACE_OUT(init_passwd_agent);
242 return ((struct agent *)retval);
245 struct agent *
246 init_passwd_mp_agent(void)
248 struct multipart_agent *retval;
250 TRACE_IN(init_passwd_mp_agent);
251 retval = (struct multipart_agent *)calloc(1,
252 sizeof(struct multipart_agent));
253 assert(retval != NULL);
255 retval->parent.name = strdup("passwd");
256 retval->parent.type = MULTIPART_AGENT;
257 retval->mp_init_func = passwd_mp_init_func;
258 retval->mp_lookup_func = passwd_mp_lookup_func;
259 retval->mp_destroy_func = passwd_mp_destroy_func;
260 assert(retval->parent.name != NULL);
262 TRACE_OUT(init_passwd_mp_agent);
263 return ((struct agent *)retval);