1  | 
     | 
     | 
    /*	$OpenBSD: rusersd.c,v 1.19 2016/09/04 15:03:13 jca Exp $	*/  | 
    
    
    2  | 
     | 
     | 
     | 
    
    
    3  | 
     | 
     | 
    /*-  | 
    
    
    4  | 
     | 
     | 
     *  Copyright (c) 1993 John Brezak  | 
    
    
    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. The name of the author may not be used to endorse or promote products  | 
    
    
    16  | 
     | 
     | 
     *     derived from this software without specific prior written permission.  | 
    
    
    17  | 
     | 
     | 
     *  | 
    
    
    18  | 
     | 
     | 
     * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR  | 
    
    
    19  | 
     | 
     | 
     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  | 
    
    
    20  | 
     | 
     | 
     * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE  | 
    
    
    21  | 
     | 
     | 
     * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,  | 
    
    
    22  | 
     | 
     | 
     * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES  | 
    
    
    23  | 
     | 
     | 
     * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR  | 
    
    
    24  | 
     | 
     | 
     * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  | 
    
    
    25  | 
     | 
     | 
     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,  | 
    
    
    26  | 
     | 
     | 
     * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN  | 
    
    
    27  | 
     | 
     | 
     * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  | 
    
    
    28  | 
     | 
     | 
     * POSSIBILITY OF SUCH DAMAGE.  | 
    
    
    29  | 
     | 
     | 
     */  | 
    
    
    30  | 
     | 
     | 
     | 
    
    
    31  | 
     | 
     | 
    #include <sys/types.h>  | 
    
    
    32  | 
     | 
     | 
    #include <sys/socket.h>  | 
    
    
    33  | 
     | 
     | 
    #include <sys/file.h>  | 
    
    
    34  | 
     | 
     | 
    #include <stdio.h>  | 
    
    
    35  | 
     | 
     | 
    #include <signal.h>  | 
    
    
    36  | 
     | 
     | 
    #include <unistd.h>  | 
    
    
    37  | 
     | 
     | 
    #include <stdlib.h>  | 
    
    
    38  | 
     | 
     | 
    #include <pwd.h>  | 
    
    
    39  | 
     | 
     | 
    #include <syslog.h>  | 
    
    
    40  | 
     | 
     | 
    #include <rpc/rpc.h>  | 
    
    
    41  | 
     | 
     | 
    #include <rpcsvc/rusers.h>	/* New version */  | 
    
    
    42  | 
     | 
     | 
    #include <rpcsvc/rnusers.h>	/* Old version */  | 
    
    
    43  | 
     | 
     | 
    #include <rpc/pmap_clnt.h>  | 
    
    
    44  | 
     | 
     | 
    #include <utmp.h>  | 
    
    
    45  | 
     | 
     | 
     | 
    
    
    46  | 
     | 
     | 
    extern void rusers_service(struct svc_req *, SVCXPRT *);  | 
    
    
    47  | 
     | 
     | 
     | 
    
    
    48  | 
     | 
     | 
    int from_inetd = 1;  | 
    
    
    49  | 
     | 
     | 
    int utmp_fd;  | 
    
    
    50  | 
     | 
     | 
     | 
    
    
    51  | 
     | 
     | 
    /* ARGSUSED */  | 
    
    
    52  | 
     | 
     | 
    static void  | 
    
    
    53  | 
     | 
     | 
    cleanup(int signo)  | 
    
    
    54  | 
     | 
     | 
    { | 
    
    
    55  | 
     | 
     | 
    	(void) pmap_unset(RUSERSPROG, RUSERSVERS_3);	/* XXX signal races */  | 
    
    
    56  | 
     | 
     | 
    	(void) pmap_unset(RUSERSPROG, RUSERSVERS_IDLE);  | 
    
    
    57  | 
     | 
     | 
    	(void) pmap_unset(RUSERSPROG, RUSERSVERS_ORIG);  | 
    
    
    58  | 
     | 
     | 
    	_exit(0);  | 
    
    
    59  | 
     | 
     | 
    }  | 
    
    
    60  | 
     | 
     | 
     | 
    
    
    61  | 
     | 
     | 
    int  | 
    
    
    62  | 
     | 
     | 
    main(int argc, char *argv[])  | 
    
    
    63  | 
     | 
     | 
    { | 
    
    
    64  | 
     | 
     | 
    	int sock = 0, proto = 0;  | 
    
    
    65  | 
     | 
     | 
    	socklen_t fromlen;  | 
    
    
    66  | 
     | 
     | 
    	struct sockaddr_storage from;  | 
    
    
    67  | 
     | 
     | 
    	struct passwd *pw;  | 
    
    
    68  | 
     | 
     | 
    	SVCXPRT *transp;  | 
    
    
    69  | 
     | 
     | 
     | 
    
    
    70  | 
     | 
     | 
    	if ((utmp_fd = open(_PATH_UTMP, O_RDONLY)) == -1) { | 
    
    
    71  | 
     | 
     | 
    		syslog(LOG_ERR, "cannot open %s", _PATH_UTMP);  | 
    
    
    72  | 
     | 
     | 
    		exit(1);  | 
    
    
    73  | 
     | 
     | 
    	}  | 
    
    
    74  | 
     | 
     | 
     | 
    
    
    75  | 
     | 
     | 
    	openlog("rpc.rusersd", LOG_NDELAY|LOG_CONS|LOG_PID, LOG_DAEMON); | 
    
    
    76  | 
     | 
     | 
     | 
    
    
    77  | 
     | 
     | 
    	pw = getpwnam("_rusersd"); | 
    
    
    78  | 
     | 
     | 
    	if (!pw) { | 
    
    
    79  | 
     | 
     | 
    		syslog(LOG_ERR, "no such user _rusersd");  | 
    
    
    80  | 
     | 
     | 
    		exit(1);  | 
    
    
    81  | 
     | 
     | 
    	}  | 
    
    
    82  | 
     | 
     | 
    	if (chroot("/var/empty") == -1) { | 
    
    
    83  | 
     | 
     | 
    		syslog(LOG_ERR, "cannot chdir to /var/empty.");  | 
    
    
    84  | 
     | 
     | 
    		exit(1);  | 
    
    
    85  | 
     | 
     | 
    	}  | 
    
    
    86  | 
     | 
     | 
    	chdir("/"); | 
    
    
    87  | 
     | 
     | 
     | 
    
    
    88  | 
     | 
     | 
    	setgroups(1, &pw->pw_gid);  | 
    
    
    89  | 
     | 
     | 
    	setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid);  | 
    
    
    90  | 
     | 
     | 
    	setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);  | 
    
    
    91  | 
     | 
     | 
     | 
    
    
    92  | 
     | 
     | 
    	/*  | 
    
    
    93  | 
     | 
     | 
    	 * See if inetd started us  | 
    
    
    94  | 
     | 
     | 
    	 */  | 
    
    
    95  | 
     | 
     | 
    	fromlen = sizeof(from);  | 
    
    
    96  | 
     | 
     | 
    	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) { | 
    
    
    97  | 
     | 
     | 
    		from_inetd = 0;  | 
    
    
    98  | 
     | 
     | 
    		sock = RPC_ANYSOCK;  | 
    
    
    99  | 
     | 
     | 
    		proto = IPPROTO_UDP;  | 
    
    
    100  | 
     | 
     | 
    	}  | 
    
    
    101  | 
     | 
     | 
     | 
    
    
    102  | 
     | 
     | 
    	if (!from_inetd) { | 
    
    
    103  | 
     | 
     | 
    		daemon(0, 0);  | 
    
    
    104  | 
     | 
     | 
     | 
    
    
    105  | 
     | 
     | 
    		(void) pmap_unset(RUSERSPROG, RUSERSVERS_3);  | 
    
    
    106  | 
     | 
     | 
    		(void) pmap_unset(RUSERSPROG, RUSERSVERS_IDLE);  | 
    
    
    107  | 
     | 
     | 
    		(void) pmap_unset(RUSERSPROG, RUSERSVERS_ORIG);  | 
    
    
    108  | 
     | 
     | 
     | 
    
    
    109  | 
     | 
     | 
    		(void) signal(SIGINT, cleanup);  | 
    
    
    110  | 
     | 
     | 
    		(void) signal(SIGTERM, cleanup);  | 
    
    
    111  | 
     | 
     | 
    		(void) signal(SIGHUP, cleanup);  | 
    
    
    112  | 
     | 
     | 
    	}  | 
    
    
    113  | 
     | 
     | 
     | 
    
    
    114  | 
     | 
     | 
    	transp = svcudp_create(sock);  | 
    
    
    115  | 
     | 
     | 
    	if (transp == NULL) { | 
    
    
    116  | 
     | 
     | 
    		syslog(LOG_ERR, "cannot create udp service.");  | 
    
    
    117  | 
     | 
     | 
    		exit(1);  | 
    
    
    118  | 
     | 
     | 
    	}  | 
    
    
    119  | 
     | 
     | 
    	if (!svc_register(transp, RUSERSPROG, RUSERSVERS_3, rusers_service, proto)) { | 
    
    
    120  | 
     | 
     | 
    		syslog(LOG_ERR,  | 
    
    
    121  | 
     | 
     | 
    		    "unable to register (RUSERSPROG, RUSERSVERS_3, %s).",  | 
    
    
    122  | 
     | 
     | 
    		    proto ? "udp" : "(inetd)");  | 
    
    
    123  | 
     | 
     | 
    		exit(1);  | 
    
    
    124  | 
     | 
     | 
    	}  | 
    
    
    125  | 
     | 
     | 
    	if (!svc_register(transp, RUSERSPROG, RUSERSVERS_IDLE, rusers_service, proto)) { | 
    
    
    126  | 
     | 
     | 
    		syslog(LOG_ERR,  | 
    
    
    127  | 
     | 
     | 
    		    "unable to register (RUSERSPROG, RUSERSVERS_IDLE, %s).",  | 
    
    
    128  | 
     | 
     | 
    		    proto ? "udp" : "(inetd)");  | 
    
    
    129  | 
     | 
     | 
    		exit(1);  | 
    
    
    130  | 
     | 
     | 
    	}  | 
    
    
    131  | 
     | 
     | 
    	if (!svc_register(transp, RUSERSPROG, RUSERSVERS_ORIG, rusers_service, proto)) { | 
    
    
    132  | 
     | 
     | 
    		syslog(LOG_ERR,  | 
    
    
    133  | 
     | 
     | 
    		    "unable to register (RUSERSPROG, RUSERSVERS_ORIG, %s).",  | 
    
    
    134  | 
     | 
     | 
    		    proto ? "udp" : "(inetd)");  | 
    
    
    135  | 
     | 
     | 
    		exit(1);  | 
    
    
    136  | 
     | 
     | 
    	}  | 
    
    
    137  | 
     | 
     | 
     | 
    
    
    138  | 
     | 
     | 
    	svc_run();  | 
    
    
    139  | 
     | 
     | 
    	syslog(LOG_ERR, "svc_run returned");  | 
    
    
    140  | 
     | 
     | 
    	exit(1);  | 
    
    
    141  | 
     | 
     | 
    }  |