Home | History | Annotate | Line # | Download | only in irs
      1      1.1  christos /*	$NetBSD: irp_pw.c,v 1.1.1.2 2012/09/09 16:07:51 christos Exp $	*/
      2      1.1  christos 
      3      1.1  christos /*
      4      1.1  christos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
      5      1.1  christos  * Portions Copyright (c) 1996 by Internet Software Consortium.
      6      1.1  christos  *
      7      1.1  christos  * Permission to use, copy, modify, and distribute this software for any
      8      1.1  christos  * purpose with or without fee is hereby granted, provided that the above
      9      1.1  christos  * copyright notice and this permission notice appear in all copies.
     10      1.1  christos  *
     11      1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     12      1.1  christos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13      1.1  christos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     14      1.1  christos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15      1.1  christos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16      1.1  christos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     17      1.1  christos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18      1.1  christos  */
     19      1.1  christos 
     20      1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     21  1.1.1.2  christos static const char rcsid[] = "Id: irp_pw.c,v 1.4 2005/04/27 04:56:29 sra Exp ";
     22      1.1  christos #endif /* LIBC_SCCS and not lint */
     23      1.1  christos 
     24      1.1  christos /* Extern */
     25      1.1  christos 
     26      1.1  christos #include "port_before.h"
     27      1.1  christos 
     28      1.1  christos #ifndef WANT_IRS_PW
     29      1.1  christos static int __bind_irs_pw_unneeded;
     30      1.1  christos #else
     31      1.1  christos 
     32      1.1  christos #include <syslog.h>
     33      1.1  christos #include <sys/param.h>
     34      1.1  christos 
     35      1.1  christos #include <db.h>
     36      1.1  christos #include <errno.h>
     37      1.1  christos #include <fcntl.h>
     38      1.1  christos #include <limits.h>
     39      1.1  christos #include <pwd.h>
     40      1.1  christos #include <stdlib.h>
     41      1.1  christos #include <string.h>
     42      1.1  christos #include <syslog.h>
     43      1.1  christos #include <utmp.h>
     44      1.1  christos #include <unistd.h>
     45      1.1  christos 
     46      1.1  christos #include <irs.h>
     47      1.1  christos #include <irp.h>
     48      1.1  christos #include <isc/memcluster.h>
     49      1.1  christos #include <isc/irpmarshall.h>
     50      1.1  christos 
     51      1.1  christos #include "port_after.h"
     52      1.1  christos 
     53      1.1  christos #include "irs_p.h"
     54      1.1  christos #include "irp_p.h"
     55      1.1  christos 
     56      1.1  christos 
     57      1.1  christos /* Types */
     58      1.1  christos 
     59      1.1  christos struct	pvt {
     60      1.1  christos 	struct irp_p   *girpdata; /*%< global IRP data */
     61      1.1  christos 	int		warned;
     62      1.1  christos 	struct passwd	passwd;		/*%< password structure */
     63      1.1  christos };
     64      1.1  christos 
     65      1.1  christos /* Forward */
     66      1.1  christos 
     67      1.1  christos static void			pw_close(struct irs_pw *);
     68      1.1  christos static struct passwd *		pw_next(struct irs_pw *);
     69      1.1  christos static struct passwd *		pw_byname(struct irs_pw *, const char *);
     70      1.1  christos static struct passwd *		pw_byuid(struct irs_pw *, uid_t);
     71      1.1  christos static void			pw_rewind(struct irs_pw *);
     72      1.1  christos static void			pw_minimize(struct irs_pw *);
     73      1.1  christos 
     74      1.1  christos static void			free_passwd(struct passwd *pw);
     75      1.1  christos 
     76      1.1  christos /* Public */
     77      1.1  christos struct irs_pw *
     78      1.1  christos irs_irp_pw(struct irs_acc *this) {
     79      1.1  christos 	struct irs_pw *pw;
     80      1.1  christos 	struct pvt *pvt;
     81      1.1  christos 
     82      1.1  christos 	if (!(pw = memget(sizeof *pw))) {
     83      1.1  christos 		errno = ENOMEM;
     84      1.1  christos 		return (NULL);
     85      1.1  christos 	}
     86      1.1  christos 	memset(pw, 0, sizeof *pw);
     87      1.1  christos 
     88      1.1  christos 	if (!(pvt = memget(sizeof *pvt))) {
     89      1.1  christos 		memput(pw, sizeof *pw);
     90      1.1  christos 		errno = ENOMEM;
     91      1.1  christos 		return (NULL);
     92      1.1  christos 	}
     93      1.1  christos 	memset(pvt, 0, sizeof *pvt);
     94      1.1  christos 	pvt->girpdata = this->private;
     95      1.1  christos 
     96      1.1  christos 	pw->private = pvt;
     97      1.1  christos 	pw->close = pw_close;
     98      1.1  christos 	pw->next = pw_next;
     99      1.1  christos 	pw->byname = pw_byname;
    100      1.1  christos 	pw->byuid = pw_byuid;
    101      1.1  christos 	pw->rewind = pw_rewind;
    102      1.1  christos 	pw->minimize = pw_minimize;
    103      1.1  christos 
    104      1.1  christos 	return (pw);
    105      1.1  christos }
    106      1.1  christos 
    107      1.1  christos /* Methods */
    108      1.1  christos 
    109      1.1  christos /*%
    110      1.1  christos  * void pw_close(struct irs_pw *this)
    111      1.1  christos  *
    112      1.1  christos  */
    113      1.1  christos 
    114      1.1  christos static void
    115      1.1  christos pw_close(struct irs_pw *this) {
    116      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    117      1.1  christos 
    118      1.1  christos 	pw_minimize(this);
    119      1.1  christos 
    120      1.1  christos 	free_passwd(&pvt->passwd);
    121      1.1  christos 
    122      1.1  christos 	memput(pvt, sizeof *pvt);
    123      1.1  christos 	memput(this, sizeof *this);
    124      1.1  christos }
    125      1.1  christos 
    126      1.1  christos /*%
    127      1.1  christos  * struct passwd * pw_next(struct irs_pw *this)
    128      1.1  christos  *
    129      1.1  christos  */
    130      1.1  christos 
    131      1.1  christos static struct passwd *
    132      1.1  christos pw_next(struct irs_pw *this) {
    133      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    134      1.1  christos 	struct passwd *pw = &pvt->passwd;
    135      1.1  christos 	char *body;
    136      1.1  christos 	size_t bodylen;
    137      1.1  christos 	int code;
    138      1.1  christos 	char text[256];
    139      1.1  christos 
    140      1.1  christos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
    141      1.1  christos 		return (NULL);
    142      1.1  christos 	}
    143      1.1  christos 
    144      1.1  christos 	if (irs_irp_send_command(pvt->girpdata, "getpwent") != 0) {
    145      1.1  christos 		return (NULL);
    146      1.1  christos 	}
    147      1.1  christos 
    148      1.1  christos 	if (irs_irp_get_full_response(pvt->girpdata, &code,
    149      1.1  christos 				      text, sizeof text,
    150      1.1  christos 				      &body, &bodylen) != 0) {
    151      1.1  christos 		return (NULL);
    152      1.1  christos 	}
    153      1.1  christos 
    154      1.1  christos 	if (code == IRPD_GETUSER_OK) {
    155      1.1  christos 		free_passwd(pw);
    156      1.1  christos 		if (irp_unmarshall_pw(pw, body) != 0) {
    157      1.1  christos 			pw = NULL;
    158      1.1  christos 		}
    159      1.1  christos 	} else {
    160      1.1  christos 		pw = NULL;
    161      1.1  christos 	}
    162      1.1  christos 
    163      1.1  christos 	if (body != NULL) {
    164      1.1  christos 		memput(body, bodylen);
    165      1.1  christos 	}
    166      1.1  christos 
    167      1.1  christos 	return (pw);
    168      1.1  christos }
    169      1.1  christos 
    170      1.1  christos /*%
    171      1.1  christos  * struct passwd * pw_byname(struct irs_pw *this, const char *name)
    172      1.1  christos  *
    173      1.1  christos  */
    174      1.1  christos 
    175      1.1  christos static struct passwd *
    176      1.1  christos pw_byname(struct irs_pw *this, const char *name) {
    177      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    178      1.1  christos 	struct passwd *pw = &pvt->passwd;
    179      1.1  christos 	char *body = NULL;
    180      1.1  christos 	char text[256];
    181      1.1  christos 	size_t bodylen;
    182      1.1  christos 	int code;
    183      1.1  christos 
    184      1.1  christos 	if (pw->pw_name != NULL && strcmp(name, pw->pw_name) == 0) {
    185      1.1  christos 		return (pw);
    186      1.1  christos 	}
    187      1.1  christos 
    188      1.1  christos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
    189      1.1  christos 		return (NULL);
    190      1.1  christos 	}
    191      1.1  christos 
    192      1.1  christos 	if (irs_irp_send_command(pvt->girpdata, "getpwnam %s", name) != 0) {
    193      1.1  christos 		return (NULL);
    194      1.1  christos 	}
    195      1.1  christos 
    196      1.1  christos 	if (irs_irp_get_full_response(pvt->girpdata, &code,
    197      1.1  christos 				      text, sizeof text,
    198      1.1  christos 				      &body, &bodylen) != 0) {
    199      1.1  christos 		return (NULL);
    200      1.1  christos 	}
    201      1.1  christos 
    202      1.1  christos 	if (code == IRPD_GETUSER_OK) {
    203      1.1  christos 		free_passwd(pw);
    204      1.1  christos 		if (irp_unmarshall_pw(pw, body) != 0) {
    205      1.1  christos 			pw = NULL;
    206      1.1  christos 		}
    207      1.1  christos 	} else {
    208      1.1  christos 		pw = NULL;
    209      1.1  christos 	}
    210      1.1  christos 
    211      1.1  christos 	if (body != NULL) {
    212      1.1  christos 		memput(body, bodylen);
    213      1.1  christos 	}
    214      1.1  christos 
    215      1.1  christos 	return (pw);
    216      1.1  christos }
    217      1.1  christos 
    218      1.1  christos /*%
    219      1.1  christos  * struct passwd * pw_byuid(struct irs_pw *this, uid_t uid)
    220      1.1  christos  *
    221      1.1  christos  */
    222      1.1  christos 
    223      1.1  christos static struct passwd *
    224      1.1  christos pw_byuid(struct irs_pw *this, uid_t uid) {
    225      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    226      1.1  christos 	char *body;
    227      1.1  christos 	char text[256];
    228      1.1  christos 	size_t bodylen;
    229      1.1  christos 	int code;
    230      1.1  christos 	struct passwd *pw = &pvt->passwd;
    231      1.1  christos 
    232      1.1  christos 	if (pw->pw_name != NULL && pw->pw_uid == uid) {
    233      1.1  christos 		return (pw);
    234      1.1  christos 	}
    235      1.1  christos 
    236      1.1  christos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
    237      1.1  christos 		return (NULL);
    238      1.1  christos 	}
    239      1.1  christos 
    240      1.1  christos 	if (irs_irp_send_command(pvt->girpdata, "getpwuid %d", uid) != 0) {
    241      1.1  christos 		return (NULL);
    242      1.1  christos 	}
    243      1.1  christos 
    244      1.1  christos 	if (irs_irp_get_full_response(pvt->girpdata, &code,
    245      1.1  christos 				      text, sizeof text,
    246      1.1  christos 				      &body, &bodylen) != 0) {
    247      1.1  christos 		return (NULL);
    248      1.1  christos 	}
    249      1.1  christos 
    250      1.1  christos 	if (code == IRPD_GETUSER_OK) {
    251      1.1  christos 		free_passwd(pw);
    252      1.1  christos 		if (irp_unmarshall_pw(pw, body) != 0) {
    253      1.1  christos 			pw = NULL;
    254      1.1  christos 		}
    255      1.1  christos 	} else {
    256      1.1  christos 		pw = NULL;
    257      1.1  christos 	}
    258      1.1  christos 
    259      1.1  christos 	if (body != NULL) {
    260      1.1  christos 		memput(body, bodylen);
    261      1.1  christos 	}
    262      1.1  christos 
    263      1.1  christos 	return (pw);
    264      1.1  christos }
    265      1.1  christos 
    266      1.1  christos /*%
    267      1.1  christos  * void pw_rewind(struct irs_pw *this)
    268      1.1  christos  *
    269      1.1  christos  */
    270      1.1  christos 
    271      1.1  christos static void
    272      1.1  christos pw_rewind(struct irs_pw *this) {
    273      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    274      1.1  christos 	char text[256];
    275      1.1  christos 	int code;
    276      1.1  christos 
    277      1.1  christos 	if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) {
    278      1.1  christos 		return;
    279      1.1  christos 	}
    280      1.1  christos 
    281      1.1  christos 	if (irs_irp_send_command(pvt->girpdata, "setpwent") != 0) {
    282      1.1  christos 		return;
    283      1.1  christos 	}
    284      1.1  christos 
    285      1.1  christos 	code = irs_irp_read_response(pvt->girpdata, text, sizeof text);
    286      1.1  christos 	if (code != IRPD_GETUSER_SETOK) {
    287      1.1  christos 		if (irp_log_errors) {
    288      1.1  christos 			syslog(LOG_WARNING, "setpwent failed: %s", text);
    289      1.1  christos 		}
    290      1.1  christos 	}
    291      1.1  christos 
    292      1.1  christos 	return;
    293      1.1  christos }
    294      1.1  christos 
    295      1.1  christos /*%
    296      1.1  christos  * void pw_minimize(struct irs_pw *this)
    297      1.1  christos  *
    298      1.1  christos  */
    299      1.1  christos 
    300      1.1  christos static void
    301      1.1  christos pw_minimize(struct irs_pw *this) {
    302      1.1  christos 	struct pvt *pvt = (struct pvt *)this->private;
    303      1.1  christos 
    304      1.1  christos 	irs_irp_disconnect(pvt->girpdata);
    305      1.1  christos }
    306      1.1  christos 
    307      1.1  christos 
    308      1.1  christos /* Private. */
    309      1.1  christos 
    310      1.1  christos /*%
    311      1.1  christos  *	Deallocate all the memory irp_unmarshall_pw allocated.
    312      1.1  christos  *
    313      1.1  christos  */
    314      1.1  christos 
    315      1.1  christos static void
    316      1.1  christos free_passwd(struct passwd *pw) {
    317      1.1  christos 	if (pw == NULL)
    318      1.1  christos 		return;
    319      1.1  christos 
    320      1.1  christos 	if (pw->pw_name != NULL)
    321      1.1  christos 		free(pw->pw_name);
    322      1.1  christos 
    323      1.1  christos 	if (pw->pw_passwd != NULL)
    324      1.1  christos 		free(pw->pw_passwd);
    325      1.1  christos 
    326      1.1  christos #ifdef HAVE_PW_CLASS
    327      1.1  christos 	if (pw->pw_class != NULL)
    328      1.1  christos 		free(pw->pw_class);
    329      1.1  christos #endif
    330      1.1  christos 
    331      1.1  christos 	if (pw->pw_gecos != NULL)
    332      1.1  christos 		free(pw->pw_gecos);
    333      1.1  christos 
    334      1.1  christos 	if (pw->pw_dir != NULL)
    335      1.1  christos 		free(pw->pw_dir);
    336      1.1  christos 
    337      1.1  christos 	if (pw->pw_shell != NULL)
    338      1.1  christos 		free(pw->pw_shell);
    339      1.1  christos }
    340      1.1  christos 
    341      1.1  christos #endif /* WANT_IRS_PW */
    342      1.1  christos /*! \file */
    343