rusers_proc.c revision 1.16 1 /* $NetBSD: rusers_proc.c,v 1.16 1998/01/20 17:39:14 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.16 1998/01/20 17:39:14 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 Display *dpy;
111 static sigjmp_buf openAbort;
112
113 static int XqueryIdle __P((char *));
114 static void abortOpen __P((int));
115
116 static void
117 abortOpen(n)
118 int n;
119 {
120 siglongjmp(openAbort, 1);
121 }
122
123 static int
124 XqueryIdle(display)
125 char *display;
126 {
127 int first_event, first_error;
128 Time IdleTime;
129
130 (void) signal(SIGALRM, abortOpen);
131 (void) alarm(10);
132 if (!sigsetjmp(openAbort, 0)) {
133 if ((dpy = XOpenDisplay(display)) == NULL) {
134 syslog(LOG_DEBUG, "cannot open display %s", display);
135 return (-1);
136 }
137 if (XidleQueryExtension(dpy, &first_event, &first_error)) {
138 if (!XGetIdleTime(dpy, &IdleTime)) {
139 syslog(LOG_DEBUG, "%s: unable to get idle time",
140 display);
141 return (-1);
142 }
143 } else {
144 syslog(LOG_DEBUG, "%s: Xidle extension not loaded",
145 display);
146 return (-1);
147 }
148 XCloseDisplay(dpy);
149 } else {
150 syslog(LOG_DEBUG, "%s: server grabbed for over 10 seconds",
151 display);
152 return (-1);
153 }
154 (void) alarm(0);
155 (void) signal(SIGALRM, SIG_DFL);
156
157 IdleTime /= 1000;
158 return ((IdleTime + 30) / 60);
159 }
160 #endif /* XIDLE */
161
162 static u_int
163 getidle(tty, display)
164 char *tty, *display;
165 {
166 struct stat st;
167 char devname[PATH_MAX];
168 time_t now;
169 u_long idle;
170
171 /*
172 * If this is an X terminal or console, then try the
173 * XIdle extension
174 */
175 #ifdef XIDLE
176 if (display && *display && strchr(display, ':') != NULL &&
177 (idle = XqueryIdle(display)) >= 0)
178 return (idle);
179 #endif
180 idle = 0;
181 if (*tty == 'X') {
182 u_long kbd_idle, mouse_idle;
183 #if !defined(i386)
184 kbd_idle = getidle("kbd", NULL);
185 #else
186 /*
187 * XXX Icky i386 console hack.
188 */
189 kbd_idle = getidle("vga", NULL);
190 #endif
191 mouse_idle = getidle("mouse", NULL);
192 idle = (kbd_idle < mouse_idle) ? kbd_idle : mouse_idle;
193 } else {
194 sprintf(devname, "%s/%s", _PATH_DEV, tty);
195 if (stat(devname, &st) < 0) {
196 #ifdef DEBUG
197 printf("%s: %m\n", devname);
198 #endif
199 return (-1);
200 }
201 time(&now);
202 #ifdef DEBUG
203 printf("%s: now=%d atime=%d\n", devname, now, st.st_atime);
204 #endif
205 idle = now - st.st_atime;
206 idle = (idle + 30) / 60; /* secs->mins */
207 }
208 if (idle < 0)
209 idle = 0;
210
211 return (idle);
212 }
213
214 static int *
215 rusers_num_svc(arg, rqstp)
216 void *arg;
217 struct svc_req *rqstp;
218 {
219 static int num_users = 0;
220 struct utmp usr;
221 FILE *ufp;
222
223 ufp = fopen(_PATH_UTMP, "r");
224 if (!ufp) {
225 syslog(LOG_ERR, "%m");
226 return (0);
227 }
228
229 /* only entries with both name and line fields */
230 while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
231 if (*usr.ut_name && *usr.ut_line &&
232 strncmp(usr.ut_name, IGNOREUSER,
233 sizeof(usr.ut_name))
234 #ifdef OSF
235 && usr.ut_type == USER_PROCESS
236 #endif
237 ) {
238 num_users++;
239 }
240
241 fclose(ufp);
242 return (&num_users);
243 }
244
245 static utmp_array *
246 do_names_3(int all)
247 {
248 static utmp_array ut;
249 struct utmp usr;
250 int nusers = 0;
251 FILE *ufp;
252
253 memset(&ut, 0, sizeof(ut));
254 ut.utmp_array_val = &utmps[0];
255
256 ufp = fopen(_PATH_UTMP, "r");
257 if (!ufp) {
258 syslog(LOG_ERR, "%m");
259 return (NULL);
260 }
261
262 /* only entries with both name and line fields */
263 while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1 &&
264 nusers < MAXUSERS)
265 if (*usr.ut_name && *usr.ut_line &&
266 strncmp(usr.ut_name, IGNOREUSER,
267 sizeof(usr.ut_name))
268 #ifdef OSF
269 && usr.ut_type == USER_PROCESS
270 #endif
271 ) {
272 utmps[nusers].ut_type = RUSERS_USER_PROCESS;
273 utmps[nusers].ut_time =
274 usr.ut_time;
275 utmps[nusers].ut_idle =
276 getidle(usr.ut_line, usr.ut_host);
277 utmps[nusers].ut_line = line[nusers];
278 strncpy(line[nusers], usr.ut_line,
279 sizeof(line[nusers]));
280 utmps[nusers].ut_user = name[nusers];
281 strncpy(name[nusers], usr.ut_name,
282 sizeof(name[nusers]));
283 utmps[nusers].ut_host = host[nusers];
284 strncpy(host[nusers], usr.ut_host,
285 sizeof(host[nusers]));
286 nusers++;
287 }
288 ut.utmp_array_len = nusers;
289
290 fclose(ufp);
291 return (&ut);
292 }
293
294 utmp_array *
295 rusersproc_names_3_svc(arg, rqstp)
296 void *arg;
297 struct svc_req *rqstp;
298 {
299 return (do_names_3(0));
300 }
301
302 utmp_array *
303 rusersproc_allnames_3_svc(arg, rqstp)
304 void *arg;
305 struct svc_req *rqstp;
306 {
307 return (do_names_3(1));
308 }
309
310 static struct utmpidlearr *
311 do_names_2(int all)
312 {
313 static struct utmpidlearr ut;
314 struct utmp usr;
315 int nusers = 0;
316 FILE *ufp;
317
318 bzero((char *)&ut, sizeof(ut));
319 ut.uia_arr = utmp_idlep;
320 ut.uia_cnt = 0;
321
322 ufp = fopen(_PATH_UTMP, "r");
323 if (!ufp) {
324 syslog(LOG_ERR, "%m");
325 return (NULL);
326 }
327
328 /* only entries with both name and line fields */
329 while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1 &&
330 nusers < MAXUSERS)
331 if (*usr.ut_name && *usr.ut_line &&
332 strncmp(usr.ut_name, IGNOREUSER,
333 sizeof(usr.ut_name))
334 #ifdef OSF
335 && usr.ut_type == USER_PROCESS
336 #endif
337 ) {
338 utmp_idlep[nusers] = &utmp_idle[nusers];
339 utmp_idle[nusers].ui_utmp.ut_time =
340 usr.ut_time;
341 utmp_idle[nusers].ui_idle =
342 getidle(usr.ut_line, usr.ut_host);
343 strncpy(utmp_idle[nusers].ui_utmp.ut_line, usr.ut_line,
344 sizeof(utmp_idle[nusers].ui_utmp.ut_line));
345 strncpy(utmp_idle[nusers].ui_utmp.ut_name, usr.ut_name,
346 sizeof(utmp_idle[nusers].ui_utmp.ut_name));
347 strncpy(utmp_idle[nusers].ui_utmp.ut_host, usr.ut_host,
348 sizeof(utmp_idle[nusers].ui_utmp.ut_host));
349 nusers++;
350 }
351
352 ut.uia_cnt = nusers;
353 fclose(ufp);
354 return (&ut);
355 }
356
357 struct utmpidlearr *
358 rusersproc_names_2_svc(arg, rqstp)
359 void *arg;
360 struct svc_req *rqstp;
361 {
362 return (do_names_2(0));
363 }
364
365 struct utmpidlearr *
366 rusersproc_allnames_2_svc(arg, rqstp)
367 void *arg;
368 struct svc_req *rqstp;
369 {
370 return (do_names_2(1));
371 }
372
373 void
374 rusers_service(rqstp, transp)
375 struct svc_req *rqstp;
376 SVCXPRT *transp;
377 {
378 union {
379 int fill;
380 } argument;
381 char *result;
382 xdrproc_t xdr_argument, xdr_result;
383 char *(*local) __P((void *, struct svc_req *));
384
385 switch (rqstp->rq_proc) {
386 case NULLPROC:
387 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
388 goto leave;
389
390 case RUSERSPROC_NUM:
391 xdr_argument = (xdrproc_t)xdr_void;
392 xdr_result = (xdrproc_t)xdr_int;
393 switch (rqstp->rq_vers) {
394 case RUSERSVERS_3:
395 case RUSERSVERS_IDLE:
396 local = (char *(*) __P((void *, struct svc_req *)))
397 rusers_num_svc;
398 break;
399 default:
400 svcerr_progvers(transp, RUSERSVERS_IDLE, RUSERSVERS_3);
401 goto leave;
402 /*NOTREACHED*/
403 }
404 break;
405
406 case RUSERSPROC_NAMES:
407 xdr_argument = (xdrproc_t)xdr_void;
408 xdr_result = (xdrproc_t)xdr_utmp_array;
409 switch (rqstp->rq_vers) {
410 case RUSERSVERS_3:
411 local = (char *(*) __P((void *, struct svc_req *)))
412 rusersproc_names_3_svc;
413 break;
414
415 case RUSERSVERS_IDLE:
416 xdr_result = (xdrproc_t)xdr_utmpidlearr;
417 local = (char *(*) __P((void *, struct svc_req *)))
418 rusersproc_names_2_svc;
419 break;
420
421 default:
422 svcerr_progvers(transp, RUSERSVERS_IDLE, RUSERSVERS_3);
423 goto leave;
424 /*NOTREACHED*/
425 }
426 break;
427
428 case RUSERSPROC_ALLNAMES:
429 xdr_argument = (xdrproc_t)xdr_void;
430 xdr_result = (xdrproc_t)xdr_utmp_array;
431 switch (rqstp->rq_vers) {
432 case RUSERSVERS_3:
433 local = (char *(*) __P((void *, struct svc_req *)))
434 rusersproc_allnames_3_svc;
435 break;
436
437 case RUSERSVERS_IDLE:
438 xdr_result = (xdrproc_t)xdr_utmpidlearr;
439 local = (char *(*) __P((void *, struct svc_req *)))
440 rusersproc_allnames_2_svc;
441 break;
442
443 default:
444 svcerr_progvers(transp, RUSERSVERS_IDLE, RUSERSVERS_3);
445 goto leave;
446 /*NOTREACHED*/
447 }
448 break;
449
450 default:
451 svcerr_noproc(transp);
452 goto leave;
453 }
454 bzero((char *)&argument, sizeof(argument));
455 if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
456 svcerr_decode(transp);
457 goto leave;
458 }
459 result = (*local)(&argument, rqstp);
460 if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
461 svcerr_systemerr(transp);
462 }
463 if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
464 (void)fprintf(stderr, "unable to free arguments\n");
465 exit(1);
466 }
467 leave:
468 if (from_inetd)
469 exit(0);
470 }
471