Home | History | Annotate | Line # | Download | only in rpc.yppasswdd
rpc.yppasswdd.c revision 1.2
      1 /*	$NetBSD: rpc.yppasswdd.c,v 1.2 1997/07/18 07:47:30 thorpej 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/types.h>
     35 #include <sys/wait.h>
     36 #include <err.h>
     37 #include <limits.h>
     38 #include <stdio.h>
     39 #include <stdlib.h>
     40 #include <string.h>
     41 #include <signal.h>
     42 #include <unistd.h>
     43 
     44 #include <rpc/rpc.h>
     45 #include <rpc/pmap_clnt.h>
     46 #include <rpcsvc/yppasswd.h>
     47 
     48 extern	char *__progname;		/* from crt0.s */
     49 
     50 int	noshell, nogecos, nopw, domake;
     51 char	make_arg[_POSIX2_LINE_MAX] = "make";
     52 
     53 extern	void make_passwd __P((yppasswd *, struct svc_req *, SVCXPRT *));
     54 
     55 int	main __P((int, char *[]));
     56 void	yppasswddprog_1 __P((struct svc_req *, SVCXPRT *));
     57 void	usage __P((void));
     58 
     59 int
     60 main(argc, argv)
     61 	int     argc;
     62 	char   *argv[];
     63 {
     64 	SVCXPRT *transp;
     65 	int i;
     66 
     67 	for (i = 1; i < argc; i++) {
     68 		if (argv[i][0] == '-') {
     69 			if (strcmp("-noshell", argv[i]) == 0)
     70 				noshell = 1;
     71 			else if (strcmp("-nogecos", argv[i]) == 0)
     72 				nogecos = 1;
     73 			else if (strcmp("-nopw", argv[i]) == 0)
     74 				nopw = 1;
     75 			else if (strcmp("-m", argv[i]) == 0) {
     76 				domake = 1;
     77 				for (; i < argc; i++) {
     78 					strcat(make_arg, " ");
     79 					strcat(make_arg, argv[i]);
     80 				}
     81 			} else
     82 				usage();
     83 		} else
     84 			usage();
     85 	}
     86 
     87 	if (daemon(0, 0))
     88 		err(1, "can't detach");
     89 
     90 	(void) pmap_unset(YPPASSWDPROG, YPPASSWDVERS);
     91 
     92 	transp = svcudp_create(RPC_ANYSOCK);
     93 	if (transp == NULL)
     94 		errx(1, "cannot create UDP service");
     95 
     96 	if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswddprog_1,
     97 	    IPPROTO_UDP))
     98 		errx(1, "unable to register YPPASSWDPROG/YPPASSWDVERS/UDP");
     99 
    100 	transp = svctcp_create(RPC_ANYSOCK, 0, 0);
    101 	if (transp == NULL)
    102 		errx(1, "cannot create TCP service");
    103 
    104 	if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswddprog_1,
    105 	    IPPROTO_TCP))
    106 		errx(1, "unable to register YPPASSWDPROG/YPPASSWDVERS/TCP");
    107 
    108 	svc_run();
    109 	errx(1, "svc_run returned");
    110 }
    111 
    112 void
    113 yppasswddprog_1(rqstp, transp)
    114 	struct svc_req *rqstp;
    115 	SVCXPRT *transp;
    116 {
    117 	union {
    118 		yppasswd yppasswdproc_update_1_arg;
    119 	} argument;
    120 
    121 	switch (rqstp->rq_proc) {
    122 	case NULLPROC:
    123 		(void) svc_sendreply(transp, xdr_void, (char *) NULL);
    124 		return;
    125 
    126 	case YPPASSWDPROC_UPDATE:
    127 		/*
    128 		 * We'd like this to look like a regular RPC
    129 		 * stub, but we have to send the reply in the
    130 		 * handler in order to avoid both signal race
    131 		 * conditions locally and timeouts on the
    132 		 * client.
    133 		 */
    134 		(void) memset(&argument, 0, sizeof(argument));
    135 		if (!svc_getargs(transp, xdr_yppasswd, (caddr_t) & argument)) {
    136 			svcerr_decode(transp);
    137 			return;
    138 		}
    139 		make_passwd((yppasswd *)&argument, rqstp, transp);
    140 		if (!svc_freeargs(transp, xdr_yppasswd, (caddr_t) &argument))
    141 			errx(1, "unable to free arguments");
    142 		return;
    143 	}
    144 
    145 	svcerr_noproc(transp);
    146 }
    147 
    148 void
    149 usage()
    150 {
    151 
    152 	fprintf(stderr, "usage: %s [-noshell] [-nogecos] [-nopw] "
    153 	    "[-m arg1 arg2 ...]\n", __progname);
    154 	exit(1);
    155 }
    156