rusers_proc.c revision 1.20 1 /* $NetBSD: rusers_proc.c,v 1.20 1998/08/12 14:47:30 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.20 1998/08/12 14:47:30 christos 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 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 int
164 getidle(tty, display)
165 char *tty, *display;
166 {
167 struct stat st;
168 char devname[PATH_MAX];
169 time_t now;
170 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 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) == -1) {
197 syslog(LOG_WARNING, "Cannot stat %s (%m)", devname);
198 return 0;
199 }
200 (void)time(&now);
201 #ifdef DEBUG
202 printf("%s: now=%ld atime=%ld\n", devname,
203 (long)now, (long)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(all)
247 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
301 return (do_names_3(0));
302 }
303
304 utmp_array *
305 rusersproc_allnames_3_svc(arg, rqstp)
306 void *arg;
307 struct svc_req *rqstp;
308 {
309
310 return (do_names_3(1));
311 }
312
313 static struct utmpidlearr *
314 do_names_2(int all)
315 {
316 static struct utmpidlearr ut;
317 struct utmp usr;
318 int nusers = 0;
319 FILE *ufp;
320
321 memset((char *)&ut, 0, sizeof(ut));
322 ut.uia_arr = utmp_idlep;
323 ut.uia_cnt = 0;
324
325 ufp = fopen(_PATH_UTMP, "r");
326 if (!ufp) {
327 syslog(LOG_ERR, "%m");
328 return (NULL);
329 }
330
331 /* only entries with both name and line fields */
332 while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1 &&
333 nusers < MAXUSERS)
334 if (*usr.ut_name && *usr.ut_line &&
335 strncmp(usr.ut_name, IGNOREUSER,
336 sizeof(usr.ut_name))
337 #ifdef OSF
338 && usr.ut_type == USER_PROCESS
339 #endif
340 ) {
341 utmp_idlep[nusers] = &utmp_idle[nusers];
342 utmp_idle[nusers].ui_utmp.ut_time =
343 usr.ut_time;
344 utmp_idle[nusers].ui_idle =
345 getidle(usr.ut_line, usr.ut_host);
346 strncpy(utmp_idle[nusers].ui_utmp.ut_line, usr.ut_line,
347 sizeof(utmp_idle[nusers].ui_utmp.ut_line));
348 strncpy(utmp_idle[nusers].ui_utmp.ut_name, usr.ut_name,
349 sizeof(utmp_idle[nusers].ui_utmp.ut_name));
350 strncpy(utmp_idle[nusers].ui_utmp.ut_host, usr.ut_host,
351 sizeof(utmp_idle[nusers].ui_utmp.ut_host));
352 nusers++;
353 }
354
355 ut.uia_cnt = nusers;
356 fclose(ufp);
357 return (&ut);
358 }
359
360 struct utmpidlearr *
361 rusersproc_names_2_svc(arg, rqstp)
362 void *arg;
363 struct svc_req *rqstp;
364 {
365 return (do_names_2(0));
366 }
367
368 struct utmpidlearr *
369 rusersproc_allnames_2_svc(arg, rqstp)
370 void *arg;
371 struct svc_req *rqstp;
372 {
373 return (do_names_2(1));
374 }
375
376 void
377 rusers_service(rqstp, transp)
378 struct svc_req *rqstp;
379 SVCXPRT *transp;
380 {
381 union {
382 int fill;
383 } argument;
384 char *result;
385 xdrproc_t xdr_argument, xdr_result;
386 char *(*local) __P((void *, struct svc_req *));
387
388 switch (rqstp->rq_proc) {
389 case NULLPROC:
390 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
391 goto leave;
392
393 case RUSERSPROC_NUM:
394 xdr_argument = (xdrproc_t)xdr_void;
395 xdr_result = (xdrproc_t)xdr_int;
396 switch (rqstp->rq_vers) {
397 case RUSERSVERS_3:
398 case RUSERSVERS_IDLE:
399 local = (char *(*) __P((void *, struct svc_req *)))
400 rusers_num_svc;
401 break;
402 default:
403 svcerr_progvers(transp, RUSERSVERS_IDLE, RUSERSVERS_3);
404 goto leave;
405 /*NOTREACHED*/
406 }
407 break;
408
409 case RUSERSPROC_NAMES:
410 xdr_argument = (xdrproc_t)xdr_void;
411 xdr_result = (xdrproc_t)xdr_utmp_array;
412 switch (rqstp->rq_vers) {
413 case RUSERSVERS_3:
414 local = (char *(*) __P((void *, struct svc_req *)))
415 rusersproc_names_3_svc;
416 break;
417
418 case RUSERSVERS_IDLE:
419 xdr_result = (xdrproc_t)xdr_utmpidlearr;
420 local = (char *(*) __P((void *, struct svc_req *)))
421 rusersproc_names_2_svc;
422 break;
423
424 default:
425 svcerr_progvers(transp, RUSERSVERS_IDLE, RUSERSVERS_3);
426 goto leave;
427 /*NOTREACHED*/
428 }
429 break;
430
431 case RUSERSPROC_ALLNAMES:
432 xdr_argument = (xdrproc_t)xdr_void;
433 xdr_result = (xdrproc_t)xdr_utmp_array;
434 switch (rqstp->rq_vers) {
435 case RUSERSVERS_3:
436 local = (char *(*) __P((void *, struct svc_req *)))
437 rusersproc_allnames_3_svc;
438 break;
439
440 case RUSERSVERS_IDLE:
441 xdr_result = (xdrproc_t)xdr_utmpidlearr;
442 local = (char *(*) __P((void *, struct svc_req *)))
443 rusersproc_allnames_2_svc;
444 break;
445
446 default:
447 svcerr_progvers(transp, RUSERSVERS_IDLE, RUSERSVERS_3);
448 goto leave;
449 /*NOTREACHED*/
450 }
451 break;
452
453 default:
454 svcerr_noproc(transp);
455 goto leave;
456 }
457 memset((char *)&argument, 0, sizeof(argument));
458 if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
459 svcerr_decode(transp);
460 goto leave;
461 }
462 result = (*local)(&argument, rqstp);
463 if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
464 svcerr_systemerr(transp);
465 }
466 if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
467 (void)fprintf(stderr, "unable to free arguments\n");
468 exit(1);
469 }
470 leave:
471 if (from_inetd)
472 exit(0);
473 }
474