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