Home | History | Annotate | Line # | Download | only in rpc.rusersd
rusers_proc.c revision 1.17
      1 /*	$NetBSD: rusers_proc.c,v 1.17 1998/04/01 14:47:01 kleink 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.17 1998/04/01 14:47:01 kleink 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 		sprintf(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(int all)
    248 {
    249 	static utmp_array ut;
    250 	struct utmp usr;
    251 	int nusers = 0;
    252 	FILE *ufp;
    253 
    254 	memset(&ut, 0, sizeof(ut));
    255 	ut.utmp_array_val = &utmps[0];
    256 
    257 	ufp = fopen(_PATH_UTMP, "r");
    258 	if (!ufp) {
    259 		syslog(LOG_ERR, "%m");
    260 		return (NULL);
    261 	}
    262 
    263 	/* only entries with both name and line fields */
    264 	while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1 &&
    265 	       nusers < MAXUSERS)
    266 		if (*usr.ut_name && *usr.ut_line &&
    267 		    strncmp(usr.ut_name, IGNOREUSER,
    268 			    sizeof(usr.ut_name))
    269 #ifdef OSF
    270 		    && usr.ut_type == USER_PROCESS
    271 #endif
    272 		    ) {
    273 			utmps[nusers].ut_type = RUSERS_USER_PROCESS;
    274 			utmps[nusers].ut_time =
    275 				usr.ut_time;
    276 			utmps[nusers].ut_idle =
    277 				getidle(usr.ut_line, usr.ut_host);
    278 			utmps[nusers].ut_line = line[nusers];
    279 			strncpy(line[nusers], usr.ut_line,
    280 			    sizeof(line[nusers]));
    281 			utmps[nusers].ut_user = name[nusers];
    282 			strncpy(name[nusers], usr.ut_name,
    283 			    sizeof(name[nusers]));
    284 			utmps[nusers].ut_host = host[nusers];
    285 			strncpy(host[nusers], usr.ut_host,
    286 			    sizeof(host[nusers]));
    287 			nusers++;
    288 		}
    289 	ut.utmp_array_len = nusers;
    290 
    291 	fclose(ufp);
    292 	return (&ut);
    293 }
    294 
    295 utmp_array *
    296 rusersproc_names_3_svc(arg, rqstp)
    297 	void *arg;
    298 	struct svc_req *rqstp;
    299 {
    300 	return (do_names_3(0));
    301 }
    302 
    303 utmp_array *
    304 rusersproc_allnames_3_svc(arg, rqstp)
    305 	void *arg;
    306 	struct svc_req *rqstp;
    307 {
    308 	return (do_names_3(1));
    309 }
    310 
    311 static struct utmpidlearr *
    312 do_names_2(int all)
    313 {
    314 	static struct utmpidlearr ut;
    315 	struct utmp usr;
    316 	int nusers = 0;
    317 	FILE *ufp;
    318 
    319 	bzero((char *)&ut, sizeof(ut));
    320 	ut.uia_arr = utmp_idlep;
    321 	ut.uia_cnt = 0;
    322 
    323 	ufp = fopen(_PATH_UTMP, "r");
    324 	if (!ufp) {
    325 		syslog(LOG_ERR, "%m");
    326 		return (NULL);
    327 	}
    328 
    329 	/* only entries with both name and line fields */
    330 	while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1 &&
    331 	       nusers < MAXUSERS)
    332 		if (*usr.ut_name && *usr.ut_line &&
    333 		    strncmp(usr.ut_name, IGNOREUSER,
    334 			    sizeof(usr.ut_name))
    335 #ifdef OSF
    336 		    && usr.ut_type == USER_PROCESS
    337 #endif
    338 		    ) {
    339 			utmp_idlep[nusers] = &utmp_idle[nusers];
    340 			utmp_idle[nusers].ui_utmp.ut_time =
    341 				usr.ut_time;
    342 			utmp_idle[nusers].ui_idle =
    343 				getidle(usr.ut_line, usr.ut_host);
    344 			strncpy(utmp_idle[nusers].ui_utmp.ut_line, usr.ut_line,
    345 			    sizeof(utmp_idle[nusers].ui_utmp.ut_line));
    346 			strncpy(utmp_idle[nusers].ui_utmp.ut_name, usr.ut_name,
    347 			    sizeof(utmp_idle[nusers].ui_utmp.ut_name));
    348 			strncpy(utmp_idle[nusers].ui_utmp.ut_host, usr.ut_host,
    349 			    sizeof(utmp_idle[nusers].ui_utmp.ut_host));
    350 			nusers++;
    351 		}
    352 
    353 	ut.uia_cnt = nusers;
    354 	fclose(ufp);
    355 	return (&ut);
    356 }
    357 
    358 struct utmpidlearr *
    359 rusersproc_names_2_svc(arg, rqstp)
    360 	void *arg;
    361 	struct svc_req *rqstp;
    362 {
    363 	return (do_names_2(0));
    364 }
    365 
    366 struct utmpidlearr *
    367 rusersproc_allnames_2_svc(arg, rqstp)
    368 	void *arg;
    369 	struct svc_req *rqstp;
    370 {
    371 	return (do_names_2(1));
    372 }
    373 
    374 void
    375 rusers_service(rqstp, transp)
    376 	struct svc_req *rqstp;
    377 	SVCXPRT *transp;
    378 {
    379 	union {
    380 		int fill;
    381 	} argument;
    382 	char *result;
    383 	xdrproc_t xdr_argument, xdr_result;
    384 	char *(*local) __P((void *, struct svc_req *));
    385 
    386 	switch (rqstp->rq_proc) {
    387 	case NULLPROC:
    388 		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
    389 		goto leave;
    390 
    391 	case RUSERSPROC_NUM:
    392 		xdr_argument = (xdrproc_t)xdr_void;
    393 		xdr_result = (xdrproc_t)xdr_int;
    394 		switch (rqstp->rq_vers) {
    395 		case RUSERSVERS_3:
    396 		case RUSERSVERS_IDLE:
    397 			local = (char *(*) __P((void *, struct svc_req *)))
    398 					rusers_num_svc;
    399 			break;
    400 		default:
    401 			svcerr_progvers(transp, RUSERSVERS_IDLE, RUSERSVERS_3);
    402 			goto leave;
    403 			/*NOTREACHED*/
    404 		}
    405 		break;
    406 
    407 	case RUSERSPROC_NAMES:
    408 		xdr_argument = (xdrproc_t)xdr_void;
    409 		xdr_result = (xdrproc_t)xdr_utmp_array;
    410 		switch (rqstp->rq_vers) {
    411 		case RUSERSVERS_3:
    412 			local = (char *(*) __P((void *, struct svc_req *)))
    413 					rusersproc_names_3_svc;
    414 			break;
    415 
    416 		case RUSERSVERS_IDLE:
    417 			xdr_result = (xdrproc_t)xdr_utmpidlearr;
    418 			local = (char *(*) __P((void *, struct svc_req *)))
    419 					rusersproc_names_2_svc;
    420 			break;
    421 
    422 		default:
    423 			svcerr_progvers(transp, RUSERSVERS_IDLE, RUSERSVERS_3);
    424 			goto leave;
    425 			/*NOTREACHED*/
    426 		}
    427 		break;
    428 
    429 	case RUSERSPROC_ALLNAMES:
    430 		xdr_argument = (xdrproc_t)xdr_void;
    431 		xdr_result = (xdrproc_t)xdr_utmp_array;
    432 		switch (rqstp->rq_vers) {
    433 		case RUSERSVERS_3:
    434 			local = (char *(*) __P((void *, struct svc_req *)))
    435 					rusersproc_allnames_3_svc;
    436 			break;
    437 
    438 		case RUSERSVERS_IDLE:
    439 			xdr_result = (xdrproc_t)xdr_utmpidlearr;
    440 			local = (char *(*) __P((void *, struct svc_req *)))
    441 					rusersproc_allnames_2_svc;
    442 			break;
    443 
    444 		default:
    445 			svcerr_progvers(transp, RUSERSVERS_IDLE, RUSERSVERS_3);
    446 			goto leave;
    447 			/*NOTREACHED*/
    448 		}
    449 		break;
    450 
    451 	default:
    452 		svcerr_noproc(transp);
    453 		goto leave;
    454 	}
    455 	bzero((char *)&argument, sizeof(argument));
    456 	if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
    457 		svcerr_decode(transp);
    458 		goto leave;
    459 	}
    460 	result = (*local)(&argument, rqstp);
    461 	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
    462 		svcerr_systemerr(transp);
    463 	}
    464 	if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
    465 		(void)fprintf(stderr, "unable to free arguments\n");
    466 		exit(1);
    467 	}
    468 leave:
    469 	if (from_inetd)
    470 		exit(0);
    471 }
    472