Home | History | Annotate | Line # | Download | only in rpc.yppasswdd
rpc.yppasswdd.c revision 1.4
      1 /*	$NetBSD: rpc.yppasswdd.c,v 1.4 2000/08/03 08:22:33 ad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Mats O Jansson <moj (at) stacken.kth.se>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Mats O Jansson
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifndef lint
     36 __RCSID("$NetBSD: rpc.yppasswdd.c,v 1.4 2000/08/03 08:22:33 ad Exp $");
     37 #endif /* not lint */
     38 
     39 #include <sys/types.h>
     40 #include <sys/wait.h>
     41 
     42 #include <err.h>
     43 #include <limits.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <signal.h>
     48 #include <unistd.h>
     49 #include <util.h>
     50 
     51 #include <rpc/rpc.h>
     52 #include <rpc/pmap_clnt.h>
     53 #include <rpcsvc/yppasswd.h>
     54 
     55 #include "extern.h"
     56 
     57 extern	char *__progname;		/* from crt0.s */
     58 
     59 int	noshell, nogecos, nopw, domake;
     60 char	make_arg[_POSIX2_LINE_MAX] = "make";
     61 
     62 int	main(int, char *[]);
     63 void	yppasswddprog_1(struct svc_req *, SVCXPRT *);
     64 void	usage(void);
     65 
     66 int
     67 main(int argc, char *argv[])
     68 {
     69 	SVCXPRT *transp;
     70 	int i;
     71 
     72 	for (i = 1; i < argc; i++) {
     73 		if (argv[i][0] == '-') {
     74 			if (strcmp("-noshell", argv[i]) == 0)
     75 				noshell = 1;
     76 			else if (strcmp("-nogecos", argv[i]) == 0)
     77 				nogecos = 1;
     78 			else if (strcmp("-nopw", argv[i]) == 0)
     79 				nopw = 1;
     80 			else if (strcmp("-m", argv[i]) == 0) {
     81 				domake = 1;
     82 				for (; i < argc; i++) {
     83 					strcat(make_arg, " ");
     84 					strcat(make_arg, argv[i]);
     85 				}
     86 			} else
     87 				usage();
     88 		} else
     89 			usage();
     90 	}
     91 
     92 	if (daemon(0, 0))
     93 		err(EXIT_FAILURE, "can't detach");
     94 	pidfile(NULL);
     95 
     96 	(void) pmap_unset(YPPASSWDPROG, YPPASSWDVERS);
     97 
     98 	transp = svcudp_create(RPC_ANYSOCK);
     99 	if (transp == NULL)
    100 		errx(EXIT_FAILURE, "cannot create UDP service");
    101 
    102 	if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswddprog_1,
    103 	    IPPROTO_UDP))
    104 		errx(EXIT_FAILURE,
    105 		    "unable to register YPPASSWDPROG/YPPASSWDVERS/UDP");
    106 
    107 	transp = svctcp_create(RPC_ANYSOCK, 0, 0);
    108 	if (transp == NULL)
    109 		errx(EXIT_FAILURE, "cannot create TCP service");
    110 
    111 	if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswddprog_1,
    112 	    IPPROTO_TCP))
    113 		errx(EXIT_FAILURE,
    114 		    "unable to register YPPASSWDPROG/YPPASSWDVERS/TCP");
    115 
    116 	svc_run();
    117 	errx(EXIT_FAILURE, "svc_run returned");
    118 	/* NOTREACHED */
    119 }
    120 
    121 void
    122 yppasswddprog_1(struct svc_req *rqstp, SVCXPRT *transp)
    123 {
    124 	union {
    125 		yppasswd yppasswdproc_update_1_arg;
    126 	} argument;
    127 
    128 	switch (rqstp->rq_proc) {
    129 	case NULLPROC:
    130 		(void) svc_sendreply(transp, xdr_void, (char *) NULL);
    131 		return;
    132 
    133 	case YPPASSWDPROC_UPDATE:
    134 		/*
    135 		 * We'd like this to look like a regular RPC
    136 		 * stub, but we have to send the reply in the
    137 		 * handler in order to avoid both signal race
    138 		 * conditions locally and timeouts on the
    139 		 * client.
    140 		 */
    141 		(void) memset(&argument, 0, sizeof(argument));
    142 		if (!svc_getargs(transp, xdr_yppasswd, (caddr_t) & argument)) {
    143 			svcerr_decode(transp);
    144 			return;
    145 		}
    146 		make_passwd((yppasswd *)&argument, rqstp, transp);
    147 		if (!svc_freeargs(transp, xdr_yppasswd, (caddr_t) &argument))
    148 			errx(EXIT_FAILURE, "unable to free arguments");
    149 		return;
    150 	}
    151 
    152 	svcerr_noproc(transp);
    153 }
    154 
    155 void
    156 usage(void)
    157 {
    158 
    159 	fprintf(stderr, "usage: %s [-noshell] [-nogecos] [-nopw] "
    160 	    "[-m arg1 arg2 ...]\n", __progname);
    161 	exit(EXIT_FAILURE);
    162 }
    163