Home | History | Annotate | Line # | Download | only in rpc.rusersd
rusers_proc.c revision 1.14
      1 /*	$NetBSD: rusers_proc.c,v 1.14 1997/09/17 16:35:55 christos 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/cdefs.h>
     32 #ifndef lint
     33 __RCSID("$NetBSD: rusers_proc.c,v 1.14 1997/09/17 16:35:55 christos Exp $");
     34 #endif /* not lint */
     35 
     36 #include <stdio.h>
     37 #include <string.h>
     38 #include <stdlib.h>
     39 #include <unistd.h>
     40 #include <signal.h>
     41 #include <syslog.h>
     42 #include <utmp.h>
     43 
     44 #include <sys/types.h>
     45 #include <sys/time.h>
     46 #include <sys/socket.h>
     47 #include <sys/param.h>
     48 #include <sys/stat.h>
     49 
     50 #include <rpc/rpc.h>
     51 
     52 #include "rusers_proc.h"
     53 
     54 #ifdef XIDLE
     55 #include <setjmp.h>
     56 #include <X11/Xlib.h>
     57 #include <X11/extensions/xidle.h>
     58 #endif
     59 #include <rpcsvc/rusers.h>	/* New version */
     60 #include <rpcsvc/rnusers.h>	/* Old version */
     61 
     62 #define	IGNOREUSER	"sleeper"
     63 
     64 #ifdef OSF
     65 #define _PATH_UTMP UTMP_FILE
     66 #endif
     67 
     68 #ifndef _PATH_UTMP
     69 #define _PATH_UTMP "/etc/utmp"
     70 #endif
     71 
     72 #ifndef _PATH_DEV
     73 #define _PATH_DEV "/dev"
     74 #endif
     75 
     76 #ifndef UT_LINESIZE
     77 #define UT_LINESIZE sizeof(((struct utmp *)0)->ut_line)
     78 #endif
     79 #ifndef UT_NAMESIZE
     80 #define UT_NAMESIZE sizeof(((struct utmp *)0)->ut_name)
     81 #endif
     82 #ifndef UT_HOSTSIZE
     83 #define UT_HOSTSIZE sizeof(((struct utmp *)0)->ut_host)
     84 #endif
     85 
     86 typedef char ut_line_t[UT_LINESIZE];
     87 typedef char ut_name_t[UT_NAMESIZE];
     88 typedef char ut_host_t[UT_HOSTSIZE];
     89 
     90 static struct rusers_utmp utmps[MAXUSERS];
     91 static struct utmpidle *utmp_idlep[MAXUSERS];
     92 static struct utmpidle utmp_idle[MAXUSERS];
     93 static ut_line_t line[MAXUSERS];
     94 static ut_name_t name[MAXUSERS];
     95 static ut_host_t host[MAXUSERS];
     96 
     97 extern int from_inetd;
     98 
     99 static u_int getidle __P((char *, char *));
    100 static int *rusers_num_svc __P((void *, struct svc_req *));
    101 static utmp_array *do_names_3 __P((int));
    102 static struct utmpidlearr *do_names_2 __P((int));
    103 
    104 /* XXX */
    105 struct utmpidlearr *rusersproc_names_2_svc __P((void *, struct svc_req *));
    106 struct utmpidlearr *rusersproc_allnames_2_svc __P((void *, struct svc_req *));
    107 
    108 
    109 #ifdef XIDLE
    110 static FILE *ufp;
    111 static Display *dpy;
    112 static sigjmp_buf openAbort;
    113 
    114 static int XqueryIdle __P((char *));
    115 static void abortOpen __P((int));
    116 
    117 static void
    118 abortOpen(n)
    119 	int n;
    120 {
    121 	siglongjmp(openAbort, 1);
    122 }
    123 
    124 static int
    125 XqueryIdle(display)
    126 	char *display;
    127 {
    128 	int first_event, first_error;
    129 	Time IdleTime;
    130 
    131 	(void) signal(SIGALRM, abortOpen);
    132 	(void) alarm(10);
    133 	if (!sigsetjmp(openAbort, 0)) {
    134 		if ((dpy = XOpenDisplay(display)) == NULL) {
    135 			syslog(LOG_ERR, "cannot open display %s", display);
    136 			return (-1);
    137 		}
    138 		if (XidleQueryExtension(dpy, &first_event, &first_error)) {
    139 			if (!XGetIdleTime(dpy, &IdleTime)) {
    140 				syslog(LOG_ERR, "%s: unable to get idle time", display);
    141 				return (-1);
    142 			}
    143 		} else {
    144 			syslog(LOG_ERR, "%s: Xidle extension not loaded", display);
    145 			return (-1);
    146 		}
    147 		XCloseDisplay(dpy);
    148 	} else {
    149 		syslog(LOG_ERR, "%s: server grabbed for over 10 seconds", display);
    150 		return (-1);
    151 	}
    152 	(void) alarm(0);
    153 	(void) signal(SIGALRM, SIG_DFL);
    154 
    155 	IdleTime /= 1000;
    156 	return ((IdleTime + 30) / 60);
    157 }
    158 #endif
    159 
    160 static u_int
    161 getidle(tty, display)
    162 	char *tty, *display;
    163 {
    164 	struct stat st;
    165 	char devname[PATH_MAX];
    166 	time_t now;
    167 	u_long idle;
    168 
    169 	/*
    170 	 * If this is an X terminal or console, then try the
    171 	 * XIdle extension
    172 	 */
    173 #ifdef XIDLE
    174 	if (display && *display && (idle = XqueryIdle(display)) >= 0)
    175 		return (idle);
    176 #endif
    177 	idle = 0;
    178 	if (*tty == 'X') {
    179 		u_long kbd_idle, mouse_idle;
    180 #if !defined(i386)
    181 		kbd_idle = getidle("kbd", NULL);
    182 #else
    183 		/*
    184 		 * XXX Icky i386 console hack.
    185 		 */
    186 		kbd_idle = getidle("vga", NULL);
    187 #endif
    188 		mouse_idle = getidle("mouse", NULL);
    189 		idle = (kbd_idle < mouse_idle) ? kbd_idle : mouse_idle;
    190 	} else {
    191 		sprintf(devname, "%s/%s", _PATH_DEV, tty);
    192 		if (stat(devname, &st) < 0) {
    193 #ifdef DEBUG
    194 			printf("%s: %m\n", devname);
    195 #endif
    196 			return (-1);
    197 		}
    198 		time(&now);
    199 #ifdef DEBUG
    200 		printf("%s: now=%d atime=%d\n", devname, now, st.st_atime);
    201 #endif
    202 		idle = now - st.st_atime;
    203 		idle = (idle + 30) / 60; /* secs->mins */
    204 	}
    205 	if (idle < 0)
    206 		idle = 0;
    207 
    208 	return (idle);
    209 }
    210 
    211 static int *
    212 rusers_num_svc(arg, rqstp)
    213 	void *arg;
    214 	struct svc_req *rqstp;
    215 {
    216 	static int num_users = 0;
    217 	struct utmp usr;
    218 
    219 	ufp = fopen(_PATH_UTMP, "r");
    220 	if (!ufp) {
    221 		syslog(LOG_ERR, "%m");
    222 		return (0);
    223 	}
    224 
    225 	/* only entries with both name and line fields */
    226 	while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
    227 		if (*usr.ut_name && *usr.ut_line &&
    228 		    strncmp(usr.ut_name, IGNOREUSER,
    229 			    sizeof(usr.ut_name))
    230 #ifdef OSF
    231 		    && usr.ut_type == USER_PROCESS
    232 #endif
    233 		    ) {
    234 			num_users++;
    235 		}
    236 
    237 	fclose(ufp);
    238 	return (&num_users);
    239 }
    240 
    241 static utmp_array *
    242 do_names_3(int all)
    243 {
    244 	static utmp_array ut;
    245 	struct utmp usr;
    246 	int nusers = 0;
    247 
    248 	memset(&ut, 0, sizeof(ut));
    249 	ut.utmp_array_val = &utmps[0];
    250 
    251 	ufp = fopen(_PATH_UTMP, "r");
    252 	if (!ufp) {
    253 		syslog(LOG_ERR, "%m");
    254 		return (NULL);
    255 	}
    256 
    257 	/* only entries with both name and line fields */
    258 	while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1 &&
    259 	       nusers < MAXUSERS)
    260 		if (*usr.ut_name && *usr.ut_line &&
    261 		    strncmp(usr.ut_name, IGNOREUSER,
    262 			    sizeof(usr.ut_name))
    263 #ifdef OSF
    264 		    && usr.ut_type == USER_PROCESS
    265 #endif
    266 		    ) {
    267 			utmps[nusers].ut_type = RUSERS_USER_PROCESS;
    268 			utmps[nusers].ut_time =
    269 				usr.ut_time;
    270 			utmps[nusers].ut_idle =
    271 				getidle(usr.ut_line, usr.ut_host);
    272 			utmps[nusers].ut_line = line[nusers];
    273 			strncpy(line[nusers], usr.ut_line, sizeof(line[nusers]));
    274 			utmps[nusers].ut_user = name[nusers];
    275 			strncpy(name[nusers], usr.ut_name, sizeof(name[nusers]));
    276 			utmps[nusers].ut_host = host[nusers];
    277 			strncpy(host[nusers], usr.ut_host, sizeof(host[nusers]));
    278 			nusers++;
    279 		}
    280 	ut.utmp_array_len = nusers;
    281 
    282 	fclose(ufp);
    283 	return (&ut);
    284 }
    285 
    286 utmp_array *
    287 rusersproc_names_3_svc(arg, rqstp)
    288 	void *arg;
    289 	struct svc_req *rqstp;
    290 {
    291 	return (do_names_3(0));
    292 }
    293 
    294 utmp_array *
    295 rusersproc_allnames_3_svc(arg, rqstp)
    296 	void *arg;
    297 	struct svc_req *rqstp;
    298 {
    299 	return (do_names_3(1));
    300 }
    301 
    302 static struct utmpidlearr *
    303 do_names_2(int all)
    304 {
    305 	static struct utmpidlearr ut;
    306 	struct utmp usr;
    307 	int nusers = 0;
    308 
    309 	bzero((char *)&ut, sizeof(ut));
    310 	ut.uia_arr = utmp_idlep;
    311 	ut.uia_cnt = 0;
    312 
    313 	ufp = fopen(_PATH_UTMP, "r");
    314 	if (!ufp) {
    315 		syslog(LOG_ERR, "%m");
    316 		return (NULL);
    317 	}
    318 
    319 	/* only entries with both name and line fields */
    320 	while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1 &&
    321 	       nusers < MAXUSERS)
    322 		if (*usr.ut_name && *usr.ut_line &&
    323 		    strncmp(usr.ut_name, IGNOREUSER,
    324 			    sizeof(usr.ut_name))
    325 #ifdef OSF
    326 		    && usr.ut_type == USER_PROCESS
    327 #endif
    328 		    ) {
    329 			utmp_idlep[nusers] = &utmp_idle[nusers];
    330 			utmp_idle[nusers].ui_utmp.ut_time =
    331 				usr.ut_time;
    332 			utmp_idle[nusers].ui_idle =
    333 				getidle(usr.ut_line, usr.ut_host);
    334 			strncpy(utmp_idle[nusers].ui_utmp.ut_line, usr.ut_line, sizeof(utmp_idle[nusers].ui_utmp.ut_line));
    335 			strncpy(utmp_idle[nusers].ui_utmp.ut_name, usr.ut_name, sizeof(utmp_idle[nusers].ui_utmp.ut_name));
    336 			strncpy(utmp_idle[nusers].ui_utmp.ut_host, usr.ut_host, sizeof(utmp_idle[nusers].ui_utmp.ut_host));
    337 			nusers++;
    338 		}
    339 
    340 	ut.uia_cnt = nusers;
    341 	fclose(ufp);
    342 	return (&ut);
    343 }
    344 
    345 struct utmpidlearr *
    346 rusersproc_names_2_svc(arg, rqstp)
    347 	void *arg;
    348 	struct svc_req *rqstp;
    349 {
    350 	return (do_names_2(0));
    351 }
    352 
    353 struct utmpidlearr *
    354 rusersproc_allnames_2_svc(arg, rqstp)
    355 	void *arg;
    356 	struct svc_req *rqstp;
    357 {
    358 	return (do_names_2(1));
    359 }
    360 
    361 void
    362 rusers_service(rqstp, transp)
    363 	struct svc_req *rqstp;
    364 	SVCXPRT *transp;
    365 {
    366 	union {
    367 		int fill;
    368 	} argument;
    369 	char *result;
    370 	xdrproc_t xdr_argument, xdr_result;
    371 	char *(*local) __P((void *, struct svc_req *));
    372 
    373 	switch (rqstp->rq_proc) {
    374 	case NULLPROC:
    375 		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
    376 		goto leave;
    377 
    378 	case RUSERSPROC_NUM:
    379 		xdr_argument = (xdrproc_t)xdr_void;
    380 		xdr_result = (xdrproc_t)xdr_int;
    381 		switch (rqstp->rq_vers) {
    382 		case RUSERSVERS_3:
    383 		case RUSERSVERS_IDLE:
    384 			local = (char *(*) __P((void *, struct svc_req *)))
    385 					rusers_num_svc;
    386 			break;
    387 		default:
    388 			svcerr_progvers(transp, RUSERSVERS_IDLE, RUSERSVERS_3);
    389 			goto leave;
    390 			/*NOTREACHED*/
    391 		}
    392 		break;
    393 
    394 	case RUSERSPROC_NAMES:
    395 		xdr_argument = (xdrproc_t)xdr_void;
    396 		xdr_result = (xdrproc_t)xdr_utmp_array;
    397 		switch (rqstp->rq_vers) {
    398 		case RUSERSVERS_3:
    399 			local = (char *(*) __P((void *, struct svc_req *)))
    400 					rusersproc_names_3_svc;
    401 			break;
    402 
    403 		case RUSERSVERS_IDLE:
    404 			xdr_result = (xdrproc_t)xdr_utmpidlearr;
    405 			local = (char *(*) __P((void *, struct svc_req *)))
    406 					rusersproc_names_2_svc;
    407 			break;
    408 
    409 		default:
    410 			svcerr_progvers(transp, RUSERSVERS_IDLE, RUSERSVERS_3);
    411 			goto leave;
    412 			/*NOTREACHED*/
    413 		}
    414 		break;
    415 
    416 	case RUSERSPROC_ALLNAMES:
    417 		xdr_argument = (xdrproc_t)xdr_void;
    418 		xdr_result = (xdrproc_t)xdr_utmp_array;
    419 		switch (rqstp->rq_vers) {
    420 		case RUSERSVERS_3:
    421 			local = (char *(*) __P((void *, struct svc_req *)))
    422 					rusersproc_allnames_3_svc;
    423 			break;
    424 
    425 		case RUSERSVERS_IDLE:
    426 			xdr_result = (xdrproc_t)xdr_utmpidlearr;
    427 			local = (char *(*) __P((void *, struct svc_req *)))
    428 					rusersproc_allnames_2_svc;
    429 			break;
    430 
    431 		default:
    432 			svcerr_progvers(transp, RUSERSVERS_IDLE, RUSERSVERS_3);
    433 			goto leave;
    434 			/*NOTREACHED*/
    435 		}
    436 		break;
    437 
    438 	default:
    439 		svcerr_noproc(transp);
    440 		goto leave;
    441 	}
    442 	bzero((char *)&argument, sizeof(argument));
    443 	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
    444 		svcerr_decode(transp);
    445 		goto leave;
    446 	}
    447 	result = (*local)(&argument, rqstp);
    448 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
    449 		svcerr_systemerr(transp);
    450 	}
    451 	if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
    452 		(void)fprintf(stderr, "unable to free arguments\n");
    453 		exit(1);
    454 	}
    455 leave:
    456 	if (from_inetd)
    457 		exit(0);
    458 }
    459