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$"); 30158115Sume 31158115Sume#include <sys/types.h> 32194093Sdes 33158115Sume#include <assert.h> 34158115Sume#include <nsswitch.h> 35158115Sume#include <pwd.h> 36194093Sdes#include <stdlib.h> 37158115Sume#include <string.h> 38194093Sdes 39158115Sume#include "../debug.h" 40158115Sume#include "passwd.h" 41158115Sume 42158115Sumestatic int passwd_marshal_func(struct passwd *, char *, size_t *); 43158115Sumestatic int passwd_lookup_func(const char *, size_t, char **, size_t *); 44194087Sdesstatic void *passwd_mp_init_func(void); 45158115Sumestatic int passwd_mp_lookup_func(char **, size_t *, void *); 46158115Sumestatic void passwd_mp_destroy_func(void *mdata); 47158115Sume 48158115Sumestatic int 49158115Sumepasswd_marshal_func(struct passwd *pwd, char *buffer, size_t *buffer_size) 50158115Sume{ 51158115Sume char *p; 52158115Sume struct passwd new_pwd; 53158115Sume size_t desired_size, size; 54158115Sume 55158115Sume TRACE_IN(passwd_marshal_func); 56158115Sume desired_size = sizeof(struct passwd) + sizeof(char *) + 57158115Sume strlen(pwd->pw_name) + 1; 58158115Sume if (pwd->pw_passwd != NULL) 59158115Sume desired_size += strlen(pwd->pw_passwd) + 1; 60158115Sume if (pwd->pw_class != NULL) 61158115Sume desired_size += strlen(pwd->pw_class) + 1; 62158115Sume if (pwd->pw_gecos != NULL) 63158115Sume desired_size += strlen(pwd->pw_gecos) + 1; 64158115Sume if (pwd->pw_dir != NULL) 65158115Sume desired_size += strlen(pwd->pw_dir) + 1; 66158115Sume if (pwd->pw_shell != NULL) 67158115Sume desired_size += strlen(pwd->pw_shell) + 1; 68158115Sume 69158115Sume if ((*buffer_size < desired_size) || (buffer == NULL)) { 70158115Sume *buffer_size = desired_size; 71158115Sume TRACE_OUT(passwd_marshal_func); 72158115Sume return (NS_RETURN); 73158115Sume } 74158115Sume 75158115Sume memcpy(&new_pwd, pwd, sizeof(struct passwd)); 76158115Sume memset(buffer, 0, desired_size); 77158115Sume 78158115Sume *buffer_size = desired_size; 79158115Sume p = buffer + sizeof(struct passwd) + sizeof(char *); 80158115Sume memcpy(buffer + sizeof(struct passwd), &p, sizeof(char *)); 81158115Sume 82158115Sume if (new_pwd.pw_name != NULL) { 83158115Sume size = strlen(new_pwd.pw_name); 84158115Sume memcpy(p, new_pwd.pw_name, size); 85158115Sume new_pwd.pw_name = p; 86158115Sume p += size + 1; 87158115Sume } 88158115Sume 89158115Sume if (new_pwd.pw_passwd != NULL) { 90158115Sume size = strlen(new_pwd.pw_passwd); 91158115Sume memcpy(p, new_pwd.pw_passwd, size); 92158115Sume new_pwd.pw_passwd = p; 93158115Sume p += size + 1; 94158115Sume } 95158115Sume 96158115Sume if (new_pwd.pw_class != NULL) { 97158115Sume size = strlen(new_pwd.pw_class); 98158115Sume memcpy(p, new_pwd.pw_class, size); 99158115Sume new_pwd.pw_class = p; 100158115Sume p += size + 1; 101158115Sume } 102158115Sume 103158115Sume if (new_pwd.pw_gecos != NULL) { 104158115Sume size = strlen(new_pwd.pw_gecos); 105158115Sume memcpy(p, new_pwd.pw_gecos, size); 106158115Sume new_pwd.pw_gecos = p; 107158115Sume p += size + 1; 108158115Sume } 109158115Sume 110158115Sume if (new_pwd.pw_dir != NULL) { 111158115Sume size = strlen(new_pwd.pw_dir); 112158115Sume memcpy(p, new_pwd.pw_dir, size); 113158115Sume new_pwd.pw_dir = p; 114158115Sume p += size + 1; 115158115Sume } 116158115Sume 117158115Sume if (new_pwd.pw_shell != NULL) { 118158115Sume size = strlen(new_pwd.pw_shell); 119158115Sume memcpy(p, new_pwd.pw_shell, size); 120158115Sume new_pwd.pw_shell = p; 121158115Sume p += size + 1; 122158115Sume } 123158115Sume 124158115Sume memcpy(buffer, &new_pwd, sizeof(struct passwd)); 125158115Sume TRACE_OUT(passwd_marshal_func); 126158115Sume return (NS_SUCCESS); 127158115Sume} 128158115Sume 129158115Sumestatic int 130158115Sumepasswd_lookup_func(const char *key, size_t key_size, char **buffer, 131158115Sume size_t *buffer_size) 132158115Sume{ 133158115Sume enum nss_lookup_type lookup_type; 134158115Sume char *login; 135158115Sume size_t size; 136158115Sume uid_t uid; 137158115Sume 138158115Sume struct passwd *result; 139158115Sume 140158115Sume TRACE_IN(passwd_lookup_func); 141158115Sume assert(buffer != NULL); 142158115Sume assert(buffer_size != NULL); 143158115Sume 144158115Sume if (key_size < sizeof(enum nss_lookup_type)) { 145158115Sume TRACE_OUT(passwd_lookup_func); 146158115Sume return (NS_UNAVAIL); 147158115Sume } 148158115Sume memcpy(&lookup_type, key, sizeof(enum nss_lookup_type)); 149158115Sume 150158115Sume switch (lookup_type) { 151158115Sume case nss_lt_name: 152158115Sume size = key_size - sizeof(enum nss_lookup_type) + 1; 153194104Sdes login = calloc(1, size); 154158115Sume assert(login != NULL); 155158115Sume memcpy(login, key + sizeof(enum nss_lookup_type), size - 1); 156158115Sume break; 157158115Sume case nss_lt_id: 158158115Sume if (key_size < sizeof(enum nss_lookup_type) + 159158115Sume sizeof(uid_t)) { 160158115Sume TRACE_OUT(passwd_lookup_func); 161158115Sume return (NS_UNAVAIL); 162158115Sume } 163158115Sume 164158115Sume memcpy(&uid, key + sizeof(enum nss_lookup_type), sizeof(uid_t)); 165158115Sume break; 166158115Sume default: 167158115Sume TRACE_OUT(passwd_lookup_func); 168158115Sume return (NS_UNAVAIL); 169158115Sume } 170158115Sume 171158115Sume switch (lookup_type) { 172158115Sume case nss_lt_name: 173158115Sume result = getpwnam(login); 174158115Sume free(login); 175158115Sume break; 176158115Sume case nss_lt_id: 177158115Sume result = getpwuid(uid); 178158115Sume break; 179158115Sume default: 180158115Sume /* SHOULD NOT BE REACHED */ 181158115Sume break; 182158115Sume } 183158115Sume 184158115Sume if (result != NULL) { 185158115Sume passwd_marshal_func(result, NULL, buffer_size); 186194104Sdes *buffer = malloc(*buffer_size); 187158115Sume assert(*buffer != NULL); 188158115Sume passwd_marshal_func(result, *buffer, buffer_size); 189158115Sume } 190158115Sume 191158115Sume TRACE_OUT(passwd_lookup_func); 192158115Sume return (result == NULL ? NS_NOTFOUND : NS_SUCCESS); 193158115Sume} 194158115Sume 195158115Sumestatic void * 196194087Sdespasswd_mp_init_func(void) 197158115Sume{ 198158115Sume TRACE_IN(passwd_mp_init_func); 199158115Sume setpwent(); 200158115Sume TRACE_OUT(passwd_mp_init_func); 201158115Sume 202158115Sume return (NULL); 203158115Sume} 204158115Sume 205158115Sumestatic int 206158115Sumepasswd_mp_lookup_func(char **buffer, size_t *buffer_size, void *mdata) 207158115Sume{ 208158115Sume struct passwd *result; 209158115Sume 210158115Sume TRACE_IN(passwd_mp_lookup_func); 211158115Sume result = getpwent(); 212158115Sume if (result != NULL) { 213158115Sume passwd_marshal_func(result, NULL, buffer_size); 214194104Sdes *buffer = malloc(*buffer_size); 215158115Sume assert(*buffer != NULL); 216158115Sume passwd_marshal_func(result, *buffer, buffer_size); 217158115Sume } 218158115Sume 219158115Sume TRACE_OUT(passwd_mp_lookup_func); 220158115Sume return (result == NULL ? NS_NOTFOUND : NS_SUCCESS); 221158115Sume} 222158115Sume 223158115Sumestatic void 224158115Sumepasswd_mp_destroy_func(void *mdata) 225158115Sume{ 226158115Sume TRACE_IN(passwd_mp_destroy_func); 227158115Sume TRACE_OUT(passwd_mp_destroy_func); 228158115Sume} 229158115Sume 230158115Sumestruct agent * 231194087Sdesinit_passwd_agent(void) 232158115Sume{ 233158115Sume struct common_agent *retval; 234158115Sume 235158115Sume TRACE_IN(init_passwd_agent); 236194104Sdes retval = calloc(1, sizeof(*retval)); 237158115Sume assert(retval != NULL); 238158115Sume 239158115Sume retval->parent.name = strdup("passwd"); 240158115Sume assert(retval->parent.name != NULL); 241158115Sume 242158115Sume retval->parent.type = COMMON_AGENT; 243158115Sume retval->lookup_func = passwd_lookup_func; 244158115Sume 245158115Sume TRACE_OUT(init_passwd_agent); 246158115Sume return ((struct agent *)retval); 247158115Sume} 248158115Sume 249158115Sumestruct agent * 250194087Sdesinit_passwd_mp_agent(void) 251158115Sume{ 252158115Sume struct multipart_agent *retval; 253158115Sume 254158115Sume TRACE_IN(init_passwd_mp_agent); 255194104Sdes retval = calloc(1, 256194104Sdes sizeof(*retval)); 257158115Sume assert(retval != NULL); 258158115Sume 259158115Sume retval->parent.name = strdup("passwd"); 260158115Sume retval->parent.type = MULTIPART_AGENT; 261158115Sume retval->mp_init_func = passwd_mp_init_func; 262158115Sume retval->mp_lookup_func = passwd_mp_lookup_func; 263158115Sume retval->mp_destroy_func = passwd_mp_destroy_func; 264158115Sume assert(retval->parent.name != NULL); 265158115Sume 266158115Sume TRACE_OUT(init_passwd_mp_agent); 267158115Sume return ((struct agent *)retval); 268158115Sume} 269