rusers_proc.c revision 1.9 1 /*-
2 * Copyright (c) 1993 John Brezak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef lint
30 static char rcsid[] = "$Id: rusers_proc.c,v 1.9 1995/01/13 19:59:13 mycroft Exp $";
31 #endif /* not lint */
32
33 #include <signal.h>
34 #include <sys/types.h>
35 #include <sys/time.h>
36 #include <utmp.h>
37 #include <stdio.h>
38 #include <syslog.h>
39 #include <rpc/rpc.h>
40 #include <sys/socket.h>
41 #include <sys/param.h>
42 #include <sys/stat.h>
43 #ifdef XIDLE
44 #include <setjmp.h>
45 #include <X11/Xlib.h>
46 #include <X11/extensions/xidle.h>
47 #endif
48 #include <rpcsvc/rusers.h> /* New version */
49 #include <rpcsvc/rnusers.h> /* Old version */
50
51 #define IGNOREUSER "sleeper"
52
53 #ifdef OSF
54 #define _PATH_UTMP UTMP_FILE
55 #endif
56
57 #ifndef _PATH_UTMP
58 #define _PATH_UTMP "/etc/utmp"
59 #endif
60
61 #ifndef _PATH_DEV
62 #define _PATH_DEV "/dev"
63 #endif
64
65 #ifndef UT_LINESIZE
66 #define UT_LINESIZE sizeof(((struct utmp *)0)->ut_line)
67 #endif
68 #ifndef UT_NAMESIZE
69 #define UT_NAMESIZE sizeof(((struct utmp *)0)->ut_name)
70 #endif
71 #ifndef UT_HOSTSIZE
72 #define UT_HOSTSIZE sizeof(((struct utmp *)0)->ut_host)
73 #endif
74
75 typedef char ut_line_t[UT_LINESIZE];
76 typedef char ut_name_t[UT_NAMESIZE];
77 typedef char ut_host_t[UT_HOSTSIZE];
78
79 struct rusers_utmp utmps[MAXUSERS];
80 struct utmpidle *utmp_idlep[MAXUSERS];
81 struct utmpidle utmp_idle[MAXUSERS];
82 ut_line_t line[MAXUSERS];
83 ut_name_t name[MAXUSERS];
84 ut_host_t host[MAXUSERS];
85
86 extern int from_inetd;
87
88 FILE *ufp;
89
90 #ifdef XIDLE
91 Display *dpy;
92
93 static sigjmp_buf openAbort;
94
95 static void
96 abortOpen()
97 {
98 siglongjmp(openAbort, 1);
99 }
100
101 XqueryIdle(display)
102 char *display;
103 {
104 int first_event, first_error;
105 Time IdleTime;
106
107 (void) signal(SIGALRM, abortOpen);
108 (void) alarm(10);
109 if (!sigsetjmp(openAbort)) {
110 if ((dpy = XOpenDisplay(display)) == NULL) {
111 syslog(LOG_ERR, "cannot open display %s", display);
112 return (-1);
113 }
114 if (XidleQueryExtension(dpy, &first_event, &first_error)) {
115 if (!XGetIdleTime(dpy, &IdleTime)) {
116 syslog(LOG_ERR, "%s: unable to get idle time", display);
117 return (-1);
118 }
119 } else {
120 syslog(LOG_ERR, "%s: Xidle extension not loaded", display);
121 return (-1);
122 }
123 XCloseDisplay(dpy);
124 } else {
125 syslog(LOG_ERR, "%s: server grabbed for over 10 seconds", display);
126 return (-1);
127 }
128 (void) alarm(0);
129 (void) signal(SIGALRM, SIG_DFL);
130
131 IdleTime /= 1000;
132 return ((IdleTime + 30) / 60);
133 }
134 #endif
135
136 static u_int
137 getidle(tty, display)
138 char *tty, *display;
139 {
140 struct stat st;
141 char devname[PATH_MAX];
142 time_t now;
143 u_long idle;
144
145 /*
146 * If this is an X terminal or console, then try the
147 * XIdle extension
148 */
149 #ifdef XIDLE
150 if (display && *display && (idle = XqueryIdle(display)) >= 0)
151 return (idle);
152 #endif
153 idle = 0;
154 if (*tty == 'X') {
155 u_long kbd_idle, mouse_idle;
156 #if !defined(i386)
157 kbd_idle = getidle("kbd", NULL);
158 #else
159 #if __GNUC__ >= 2
160 #warning i386 console hack here
161 #endif
162 kbd_idle = getidle("vga", NULL);
163 #endif
164 mouse_idle = getidle("mouse", NULL);
165 idle = (kbd_idle < mouse_idle) ? kbd_idle : mouse_idle;
166 } else {
167 sprintf(devname, "%s/%s", _PATH_DEV, tty);
168 if (stat(devname, &st) < 0) {
169 #ifdef DEBUG
170 printf("%s: %m\n", devname);
171 #endif
172 return (-1);
173 }
174 time(&now);
175 #ifdef DEBUG
176 printf("%s: now=%d atime=%d\n", devname, now, st.st_atime);
177 #endif
178 idle = now - st.st_atime;
179 idle = (idle + 30) / 60; /* secs->mins */
180 }
181 if (idle < 0)
182 idle = 0;
183
184 return (idle);
185 }
186
187 int *
188 rusers_num()
189 {
190 static int num_users = 0;
191 struct utmp usr;
192
193 ufp = fopen(_PATH_UTMP, "r");
194 if (!ufp) {
195 syslog(LOG_ERR, "%m");
196 return (0);
197 }
198
199 /* only entries with both name and line fields */
200 while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1)
201 if (*usr.ut_name && *usr.ut_line &&
202 strncmp(usr.ut_name, IGNOREUSER,
203 sizeof(usr.ut_name))
204 #ifdef OSF
205 && usr.ut_type == USER_PROCESS
206 #endif
207 ) {
208 num_users++;
209 }
210
211 fclose(ufp);
212 return (&num_users);
213 }
214
215 static utmp_array *
216 do_names_3(int all)
217 {
218 static utmp_array ut;
219 struct utmp usr;
220 int nusers = 0;
221
222 bzero((char *)&ut, sizeof(ut));
223 ut.utmp_array_val = &utmps[0];
224
225 ufp = fopen(_PATH_UTMP, "r");
226 if (!ufp) {
227 syslog(LOG_ERR, "%m");
228 return (NULL);
229 }
230
231 /* only entries with both name and line fields */
232 while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1 &&
233 nusers < MAXUSERS)
234 if (*usr.ut_name && *usr.ut_line &&
235 strncmp(usr.ut_name, IGNOREUSER,
236 sizeof(usr.ut_name))
237 #ifdef OSF
238 && usr.ut_type == USER_PROCESS
239 #endif
240 ) {
241 utmps[nusers].ut_type = RUSERS_USER_PROCESS;
242 utmps[nusers].ut_time =
243 usr.ut_time;
244 utmps[nusers].ut_idle =
245 getidle(usr.ut_line, usr.ut_host);
246 utmps[nusers].ut_line = line[nusers];
247 strncpy(line[nusers], usr.ut_line, sizeof(line[nusers]));
248 utmps[nusers].ut_user = name[nusers];
249 strncpy(name[nusers], usr.ut_name, sizeof(name[nusers]));
250 utmps[nusers].ut_host = host[nusers];
251 strncpy(host[nusers], usr.ut_host, sizeof(host[nusers]));
252 nusers++;
253 }
254 ut.utmp_array_len = nusers;
255
256 fclose(ufp);
257 return (&ut);
258 }
259
260 utmp_array *
261 rusersproc_names_3()
262 {
263 return (do_names_3(0));
264 }
265
266 utmp_array *
267 rusersproc_allnames_3()
268 {
269 return (do_names_3(1));
270 }
271
272 static struct utmpidlearr *
273 do_names_2(int all)
274 {
275 static struct utmpidlearr ut;
276 struct utmp usr;
277 int nusers = 0;
278
279 bzero((char *)&ut, sizeof(ut));
280 ut.uia_arr = utmp_idlep;
281 ut.uia_cnt = 0;
282
283 ufp = fopen(_PATH_UTMP, "r");
284 if (!ufp) {
285 syslog(LOG_ERR, "%m");
286 return (NULL);
287 }
288
289 /* only entries with both name and line fields */
290 while (fread((char *)&usr, sizeof(usr), 1, ufp) == 1 &&
291 nusers < MAXUSERS)
292 if (*usr.ut_name && *usr.ut_line &&
293 strncmp(usr.ut_name, IGNOREUSER,
294 sizeof(usr.ut_name))
295 #ifdef OSF
296 && usr.ut_type == USER_PROCESS
297 #endif
298 ) {
299 utmp_idlep[nusers] = &utmp_idle[nusers];
300 utmp_idle[nusers].ui_utmp.ut_time =
301 usr.ut_time;
302 utmp_idle[nusers].ui_idle =
303 getidle(usr.ut_line, usr.ut_host);
304 strncpy(utmp_idle[nusers].ui_utmp.ut_line, usr.ut_line, sizeof(utmp_idle[nusers].ui_utmp.ut_line));
305 strncpy(utmp_idle[nusers].ui_utmp.ut_name, usr.ut_name, sizeof(utmp_idle[nusers].ui_utmp.ut_name));
306 strncpy(utmp_idle[nusers].ui_utmp.ut_host, usr.ut_host, sizeof(utmp_idle[nusers].ui_utmp.ut_host));
307 nusers++;
308 }
309
310 ut.uia_cnt = nusers;
311 fclose(ufp);
312 return (&ut);
313 }
314
315 struct utmpidlearr *
316 rusersproc_names_2()
317 {
318 return (do_names_2(0));
319 }
320
321 struct utmpidlearr *
322 rusersproc_allnames_2()
323 {
324 return (do_names_2(1));
325 }
326
327 void
328 rusers_service(rqstp, transp)
329 struct svc_req *rqstp;
330 SVCXPRT *transp;
331 {
332 union {
333 int fill;
334 } argument;
335 char *result;
336 bool_t (*xdr_argument)(), (*xdr_result)();
337 char *(*local)();
338
339 switch (rqstp->rq_proc) {
340 case NULLPROC:
341 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
342 goto leave;
343
344 case RUSERSPROC_NUM:
345 xdr_argument = xdr_void;
346 xdr_result = xdr_int;
347 switch (rqstp->rq_vers) {
348 case RUSERSVERS_3:
349 case RUSERSVERS_IDLE:
350 local = (char *(*)()) rusers_num;
351 break;
352 default:
353 svcerr_progvers(transp, RUSERSVERS_IDLE, RUSERSVERS_3);
354 goto leave;
355 /*NOTREACHED*/
356 }
357 break;
358
359 case RUSERSPROC_NAMES:
360 xdr_argument = xdr_void;
361 xdr_result = xdr_utmp_array;
362 switch (rqstp->rq_vers) {
363 case RUSERSVERS_3:
364 local = (char *(*)()) rusersproc_names_3;
365 break;
366
367 case RUSERSVERS_IDLE:
368 xdr_result = xdr_utmpidlearr;
369 local = (char *(*)()) rusersproc_names_2;
370 break;
371
372 default:
373 svcerr_progvers(transp, RUSERSVERS_IDLE, RUSERSVERS_3);
374 goto leave;
375 /*NOTREACHED*/
376 }
377 break;
378
379 case RUSERSPROC_ALLNAMES:
380 xdr_argument = xdr_void;
381 xdr_result = xdr_utmp_array;
382 switch (rqstp->rq_vers) {
383 case RUSERSVERS_3:
384 local = (char *(*)()) rusersproc_allnames_3;
385 break;
386
387 case RUSERSVERS_IDLE:
388 xdr_result = xdr_utmpidlearr;
389 local = (char *(*)()) rusersproc_allnames_2;
390 break;
391
392 default:
393 svcerr_progvers(transp, RUSERSVERS_IDLE, RUSERSVERS_3);
394 goto leave;
395 /*NOTREACHED*/
396 }
397 break;
398
399 default:
400 svcerr_noproc(transp);
401 goto leave;
402 }
403 bzero((char *)&argument, sizeof(argument));
404 if (!svc_getargs(transp, xdr_argument, (caddr_t)&argument)) {
405 svcerr_decode(transp);
406 goto leave;
407 }
408 result = (*local)(&argument, rqstp);
409 if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
410 svcerr_systemerr(transp);
411 }
412 if (!svc_freeargs(transp, xdr_argument, (caddr_t)&argument)) {
413 (void)fprintf(stderr, "unable to free arguments\n");
414 exit(1);
415 }
416 leave:
417 if (from_inetd)
418 exit(0);
419 }
420