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