passwd.c revision 194087
1158115Sume/*- 2158115Sume * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru> 3158115Sume * All rights reserved. 4158115Sume * 5158115Sume * Redistribution and use in source and binary forms, with or without 6158115Sume * modification, are permitted provided that the following conditions 7158115Sume * are met: 8158115Sume * 1. Redistributions of source code must retain the above copyright 9158115Sume * notice, this list of conditions and the following disclaimer. 10158115Sume * 2. Redistributions in binary form must reproduce the above copyright 11158115Sume * notice, this list of conditions and the following disclaimer in the 12158115Sume * documentation and/or other materials provided with the distribution. 13158115Sume * 14158115Sume * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15158115Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16158115Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17158115Sume * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18158115Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19158115Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20158115Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21158115Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22158115Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23158115Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24158115Sume * SUCH DAMAGE. 25158115Sume * 26158115Sume */ 27158115Sume 28158115Sume#include <sys/cdefs.h> 29158115Sume__FBSDID("$FreeBSD: head/usr.sbin/nscd/agents/passwd.c 194087 2009-06-12 23:39:05Z des $"); 30158115Sume 31158115Sume#include <sys/types.h> 32158115Sume#include <assert.h> 33158115Sume#include <nsswitch.h> 34158115Sume#include <pwd.h> 35158115Sume#include <string.h> 36158115Sume#include <stdlib.h> 37158115Sume#include "../debug.h" 38158115Sume#include "passwd.h" 39158115Sume 40158115Sumestatic int passwd_marshal_func(struct passwd *, char *, size_t *); 41158115Sumestatic int passwd_lookup_func(const char *, size_t, char **, size_t *); 42194087Sdesstatic void *passwd_mp_init_func(void); 43158115Sumestatic int passwd_mp_lookup_func(char **, size_t *, void *); 44158115Sumestatic void passwd_mp_destroy_func(void *mdata); 45158115Sume 46158115Sumestatic int 47158115Sumepasswd_marshal_func(struct passwd *pwd, char *buffer, size_t *buffer_size) 48158115Sume{ 49158115Sume char *p; 50158115Sume struct passwd new_pwd; 51158115Sume size_t desired_size, size; 52158115Sume 53158115Sume TRACE_IN(passwd_marshal_func); 54158115Sume desired_size = sizeof(struct passwd) + sizeof(char *) + 55158115Sume strlen(pwd->pw_name) + 1; 56158115Sume if (pwd->pw_passwd != NULL) 57158115Sume desired_size += strlen(pwd->pw_passwd) + 1; 58158115Sume if (pwd->pw_class != NULL) 59158115Sume desired_size += strlen(pwd->pw_class) + 1; 60158115Sume if (pwd->pw_gecos != NULL) 61158115Sume desired_size += strlen(pwd->pw_gecos) + 1; 62158115Sume if (pwd->pw_dir != NULL) 63158115Sume desired_size += strlen(pwd->pw_dir) + 1; 64158115Sume if (pwd->pw_shell != NULL) 65158115Sume desired_size += strlen(pwd->pw_shell) + 1; 66158115Sume 67158115Sume if ((*buffer_size < desired_size) || (buffer == NULL)) { 68158115Sume *buffer_size = desired_size; 69158115Sume TRACE_OUT(passwd_marshal_func); 70158115Sume return (NS_RETURN); 71158115Sume } 72158115Sume 73158115Sume memcpy(&new_pwd, pwd, sizeof(struct passwd)); 74158115Sume memset(buffer, 0, desired_size); 75158115Sume 76158115Sume *buffer_size = desired_size; 77158115Sume p = buffer + sizeof(struct passwd) + sizeof(char *); 78158115Sume memcpy(buffer + sizeof(struct passwd), &p, sizeof(char *)); 79158115Sume 80158115Sume if (new_pwd.pw_name != NULL) { 81158115Sume size = strlen(new_pwd.pw_name); 82158115Sume memcpy(p, new_pwd.pw_name, size); 83158115Sume new_pwd.pw_name = p; 84158115Sume p += size + 1; 85158115Sume } 86158115Sume 87158115Sume if (new_pwd.pw_passwd != NULL) { 88158115Sume size = strlen(new_pwd.pw_passwd); 89158115Sume memcpy(p, new_pwd.pw_passwd, size); 90158115Sume new_pwd.pw_passwd = p; 91158115Sume p += size + 1; 92158115Sume } 93158115Sume 94158115Sume if (new_pwd.pw_class != NULL) { 95158115Sume size = strlen(new_pwd.pw_class); 96158115Sume memcpy(p, new_pwd.pw_class, size); 97158115Sume new_pwd.pw_class = p; 98158115Sume p += size + 1; 99158115Sume } 100158115Sume 101158115Sume if (new_pwd.pw_gecos != NULL) { 102158115Sume size = strlen(new_pwd.pw_gecos); 103158115Sume memcpy(p, new_pwd.pw_gecos, size); 104158115Sume new_pwd.pw_gecos = p; 105158115Sume p += size + 1; 106158115Sume } 107158115Sume 108158115Sume if (new_pwd.pw_dir != NULL) { 109158115Sume size = strlen(new_pwd.pw_dir); 110158115Sume memcpy(p, new_pwd.pw_dir, size); 111158115Sume new_pwd.pw_dir = p; 112158115Sume p += size + 1; 113158115Sume } 114158115Sume 115158115Sume if (new_pwd.pw_shell != NULL) { 116158115Sume size = strlen(new_pwd.pw_shell); 117158115Sume memcpy(p, new_pwd.pw_shell, size); 118158115Sume new_pwd.pw_shell = p; 119158115Sume p += size + 1; 120158115Sume } 121158115Sume 122158115Sume memcpy(buffer, &new_pwd, sizeof(struct passwd)); 123158115Sume TRACE_OUT(passwd_marshal_func); 124158115Sume return (NS_SUCCESS); 125158115Sume} 126158115Sume 127158115Sumestatic int 128158115Sumepasswd_lookup_func(const char *key, size_t key_size, char **buffer, 129158115Sume size_t *buffer_size) 130158115Sume{ 131158115Sume enum nss_lookup_type lookup_type; 132158115Sume char *login; 133158115Sume size_t size; 134158115Sume uid_t uid; 135158115Sume 136158115Sume struct passwd *result; 137158115Sume 138158115Sume TRACE_IN(passwd_lookup_func); 139158115Sume assert(buffer != NULL); 140158115Sume assert(buffer_size != NULL); 141158115Sume 142158115Sume if (key_size < sizeof(enum nss_lookup_type)) { 143158115Sume TRACE_OUT(passwd_lookup_func); 144158115Sume return (NS_UNAVAIL); 145158115Sume } 146158115Sume memcpy(&lookup_type, key, sizeof(enum nss_lookup_type)); 147158115Sume 148158115Sume switch (lookup_type) { 149158115Sume case nss_lt_name: 150158115Sume size = key_size - sizeof(enum nss_lookup_type) + 1; 151184186Sdelphij login = (char *)calloc(1, size); 152158115Sume assert(login != NULL); 153158115Sume memcpy(login, key + sizeof(enum nss_lookup_type), size - 1); 154158115Sume break; 155158115Sume case nss_lt_id: 156158115Sume if (key_size < sizeof(enum nss_lookup_type) + 157158115Sume sizeof(uid_t)) { 158158115Sume TRACE_OUT(passwd_lookup_func); 159158115Sume return (NS_UNAVAIL); 160158115Sume } 161158115Sume 162158115Sume memcpy(&uid, key + sizeof(enum nss_lookup_type), sizeof(uid_t)); 163158115Sume break; 164158115Sume default: 165158115Sume TRACE_OUT(passwd_lookup_func); 166158115Sume return (NS_UNAVAIL); 167158115Sume } 168158115Sume 169158115Sume switch (lookup_type) { 170158115Sume case nss_lt_name: 171158115Sume result = getpwnam(login); 172158115Sume free(login); 173158115Sume break; 174158115Sume case nss_lt_id: 175158115Sume result = getpwuid(uid); 176158115Sume break; 177158115Sume default: 178158115Sume /* SHOULD NOT BE REACHED */ 179158115Sume break; 180158115Sume } 181158115Sume 182158115Sume if (result != NULL) { 183158115Sume passwd_marshal_func(result, NULL, buffer_size); 184158115Sume *buffer = (char *)malloc(*buffer_size); 185158115Sume assert(*buffer != NULL); 186158115Sume passwd_marshal_func(result, *buffer, buffer_size); 187158115Sume } 188158115Sume 189158115Sume TRACE_OUT(passwd_lookup_func); 190158115Sume return (result == NULL ? NS_NOTFOUND : NS_SUCCESS); 191158115Sume} 192158115Sume 193158115Sumestatic void * 194194087Sdespasswd_mp_init_func(void) 195158115Sume{ 196158115Sume TRACE_IN(passwd_mp_init_func); 197158115Sume setpwent(); 198158115Sume TRACE_OUT(passwd_mp_init_func); 199158115Sume 200158115Sume return (NULL); 201158115Sume} 202158115Sume 203158115Sumestatic int 204158115Sumepasswd_mp_lookup_func(char **buffer, size_t *buffer_size, void *mdata) 205158115Sume{ 206158115Sume struct passwd *result; 207158115Sume 208158115Sume TRACE_IN(passwd_mp_lookup_func); 209158115Sume result = getpwent(); 210158115Sume if (result != NULL) { 211158115Sume passwd_marshal_func(result, NULL, buffer_size); 212158115Sume *buffer = (char *)malloc(*buffer_size); 213158115Sume assert(*buffer != NULL); 214158115Sume passwd_marshal_func(result, *buffer, buffer_size); 215158115Sume } 216158115Sume 217158115Sume TRACE_OUT(passwd_mp_lookup_func); 218158115Sume return (result == NULL ? NS_NOTFOUND : NS_SUCCESS); 219158115Sume} 220158115Sume 221158115Sumestatic void 222158115Sumepasswd_mp_destroy_func(void *mdata) 223158115Sume{ 224158115Sume TRACE_IN(passwd_mp_destroy_func); 225158115Sume TRACE_OUT(passwd_mp_destroy_func); 226158115Sume} 227158115Sume 228158115Sumestruct agent * 229194087Sdesinit_passwd_agent(void) 230158115Sume{ 231158115Sume struct common_agent *retval; 232158115Sume 233158115Sume TRACE_IN(init_passwd_agent); 234184186Sdelphij retval = (struct common_agent *)calloc(1, sizeof(struct common_agent)); 235158115Sume assert(retval != NULL); 236158115Sume 237158115Sume retval->parent.name = strdup("passwd"); 238158115Sume assert(retval->parent.name != NULL); 239158115Sume 240158115Sume retval->parent.type = COMMON_AGENT; 241158115Sume retval->lookup_func = passwd_lookup_func; 242158115Sume 243158115Sume TRACE_OUT(init_passwd_agent); 244158115Sume return ((struct agent *)retval); 245158115Sume} 246158115Sume 247158115Sumestruct agent * 248194087Sdesinit_passwd_mp_agent(void) 249158115Sume{ 250158115Sume struct multipart_agent *retval; 251158115Sume 252158115Sume TRACE_IN(init_passwd_mp_agent); 253184186Sdelphij retval = (struct multipart_agent *)calloc(1, 254158115Sume sizeof(struct multipart_agent)); 255158115Sume assert(retval != NULL); 256158115Sume 257158115Sume retval->parent.name = strdup("passwd"); 258158115Sume retval->parent.type = MULTIPART_AGENT; 259158115Sume retval->mp_init_func = passwd_mp_init_func; 260158115Sume retval->mp_lookup_func = passwd_mp_lookup_func; 261158115Sume retval->mp_destroy_func = passwd_mp_destroy_func; 262158115Sume assert(retval->parent.name != NULL); 263158115Sume 264158115Sume TRACE_OUT(init_passwd_mp_agent); 265158115Sume return ((struct agent *)retval); 266158115Sume} 267