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