1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD: head/libexec/rpc.rusersd/rusers_proc.c 326025 2017-11-20 19:49:47Z pfg $
32  */
33 
34 #ifdef DEBUG
35 #include <errno.h>
36 #endif
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sys/param.h>
41 #include <sys/stat.h>
42 #include <syslog.h>
43 #include <utmpx.h>
44 #ifdef XIDLE
45 #include <setjmp.h>
46 #include <X11/Xlib.h>
47 #include <X11/extensions/xidle.h>
48 #endif
49 #include <rpcsvc/rnusers.h>
50 
51 #include "extern.h"
52 
53 #ifndef _PATH_DEV
54 #define _PATH_DEV "/dev"
55 #endif
56 
57 static utmpidle utmp_idle[MAXUSERS];
58 static utmp old_utmp[MAXUSERS];
59 static struct utmpx utmp_list[MAXUSERS];
60 
61 #ifdef XIDLE
62 static Display *dpy;
63 
64 static jmp_buf openAbort;
65 
66 static void
67 abortOpen(void)
68 {
69     longjmp (openAbort, 1);
70 }
71 
72 XqueryIdle(char *display)
73 {
74 	int first_event, first_error;
75 	Time IdleTime;
76 
77 	(void) signal (SIGALRM, abortOpen);
78 	(void) alarm ((unsigned) 10);
79 	if (!setjmp (openAbort)) {
80 		if (!(dpy= XOpenDisplay(display))) {
81 			syslog(LOG_ERR, "Cannot open display %s", display);
82 			return(-1);
83 		}
84 		if (XidleQueryExtension(dpy, &first_event, &first_error)) {
85 			if (!XGetIdleTime(dpy, &IdleTime)) {
86 				syslog(LOG_ERR, "%s: unable to get idle time", display);
87 				return(-1);
88 			}
89 		} else {
90 			syslog(LOG_ERR, "%s: Xidle extension not loaded", display);
91 			return(-1);
92 		}
93 		XCloseDisplay(dpy);
94 	} else {
95 		syslog(LOG_ERR, "%s: server grabbed for over 10 seconds", display);
96 		return(-1);
97 	}
98 	(void) signal (SIGALRM, SIG_DFL);
99 	(void) alarm ((unsigned) 0);
100 
101 	IdleTime /= 1000;
102 	return((IdleTime + 30) / 60);
103 }
104 #endif
105 
106 static u_int
107 getidle(const char *tty, const char *display __unused)
108 {
109 	struct stat st;
110 	char ttyname[PATH_MAX];
111 	time_t now;
112 	u_long idle;
113 
114 	/*
115 	 * If this is an X terminal or console, then try the
116 	 * XIdle extension
117 	 */
118 #ifdef XIDLE
119 	if (display && *display && (idle = XqueryIdle(display)) >= 0)
120 		return(idle);
121 #endif
122 	idle = 0;
123 	if (*tty == 'X') {
124 		u_long kbd_idle, mouse_idle;
125 #if !defined(__DragonFly__) && !defined(__FreeBSD__)
126 		kbd_idle = getidle("kbd", NULL);
127 #else
128 		kbd_idle = getidle("vga", NULL);
129 #endif
130 		mouse_idle = getidle("mouse", NULL);
131 		idle = (kbd_idle < mouse_idle)?kbd_idle:mouse_idle;
132 	} else {
133 		sprintf(ttyname, "%s/%s", _PATH_DEV, tty);
134 		if (stat(ttyname, &st) < 0) {
135 #ifdef DEBUG
136 			printf("%s: %s\n", ttyname, strerror(errno));
137 #endif
138 			return(-1);
139 		}
140 		time(&now);
141 #ifdef DEBUG
142 		printf("%s: now=%d atime=%d\n", ttyname, now,
143 		       st.st_atime);
144 #endif
145 		idle = now - st.st_atime;
146 		idle = (idle + 30) / 60; /* secs->mins */
147 	}
148 
149 	return(idle);
150 }
151 
152 static utmpidlearr *
153 do_names_2(void)
154 {
155 	static utmpidlearr ut;
156 	struct utmpx *usr;
157 	int nusers = 0;
158 
159 	memset(&ut, 0, sizeof(ut));
160 	ut.utmpidlearr_val = &utmp_idle[0];
161 
162 	setutxent();
163 	while ((usr = getutxent()) != NULL && nusers < MAXUSERS) {
164 		if (usr->ut_type != USER_PROCESS)
165 			continue;
166 
167 		memcpy(&utmp_list[nusers], usr, sizeof(*usr));
168 		utmp_idle[nusers].ui_utmp.ut_time = usr->ut_tv.tv_sec;
169 		utmp_idle[nusers].ui_idle =
170 		    getidle(usr->ut_line, usr->ut_host);
171 		utmp_idle[nusers].ui_utmp.ut_line =
172 		    utmp_list[nusers].ut_line;
173 		utmp_idle[nusers].ui_utmp.ut_name =
174 		    utmp_list[nusers].ut_user;
175 		utmp_idle[nusers].ui_utmp.ut_host =
176 		    utmp_list[nusers].ut_host;
177 
178 		nusers++;
179 	}
180 	endutxent();
181 
182 	ut.utmpidlearr_len = nusers;
183 	return(&ut);
184 }
185 
186 static int *
187 rusers_num(void *argp __unused, struct svc_req *rqstp __unused)
188 {
189 	static int num_users = 0;
190 	struct utmpx *usr;
191 
192 	setutxent();
193 	while ((usr = getutxent()) != NULL) {
194 		if (usr->ut_type != USER_PROCESS)
195 			continue;
196 		num_users++;
197 	}
198 	endutxent();
199 
200 	return(&num_users);
201 }
202 
203 static utmparr *
204 do_names_1(void)
205 {
206 	utmpidlearr *utidle;
207 	static utmparr ut;
208 	unsigned int i;
209 
210 	bzero((char *)&ut, sizeof(ut));
211 
212 	utidle = do_names_2();
213 	if (utidle) {
214 		ut.utmparr_len = utidle->utmpidlearr_len;
215 		ut.utmparr_val = &old_utmp[0];
216 		for (i = 0; i < ut.utmparr_len; i++)
217 			bcopy(&utmp_idle[i].ui_utmp, &old_utmp[i],
218 			      sizeof(old_utmp[0]));
219 
220 	}
221 
222 	return(&ut);
223 }
224 
225 utmpidlearr *
226 rusersproc_names_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
227 {
228 	return (do_names_2());
229 }
230 
231 utmpidlearr *
232 rusersproc_allnames_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
233 {
234 	return (do_names_2());
235 }
236 
237 utmparr *
238 rusersproc_names_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
239 {
240 	return (do_names_1());
241 }
242 
243 utmparr *
244 rusersproc_allnames_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
245 {
246 	return (do_names_1());
247 }
248 
249 typedef void *(*rusersproc_t)(void *, struct svc_req *);
250 
251 void
252 rusers_service(struct svc_req *rqstp, SVCXPRT *transp)
253 {
254 	union {
255 		int fill;
256 	} argument;
257 	char *result;
258 	xdrproc_t xdr_argument, xdr_result;
259 	rusersproc_t local;
260 
261 	switch (rqstp->rq_proc) {
262 	case NULLPROC:
263 		(void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
264 		goto leave;
265 
266 	case RUSERSPROC_NUM:
267 		xdr_argument = (xdrproc_t)xdr_void;
268 		xdr_result = (xdrproc_t)xdr_int;
269 		local = (rusersproc_t)rusers_num;
270 		break;
271 
272 	case RUSERSPROC_NAMES:
273 		xdr_argument = (xdrproc_t)xdr_void;
274 		xdr_result = (xdrproc_t)xdr_utmpidlearr;
275 		switch (rqstp->rq_vers) {
276 		case RUSERSVERS_ORIG:
277 			local = (rusersproc_t)rusersproc_names_1_svc;
278 			break;
279 		case RUSERSVERS_IDLE:
280 			local = (rusersproc_t)rusersproc_names_2_svc;
281 			break;
282 		default:
283 			svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
284 			goto leave;
285 			/*NOTREACHED*/
286 		}
287 		break;
288 
289 	case RUSERSPROC_ALLNAMES:
290 		xdr_argument = (xdrproc_t)xdr_void;
291 		xdr_result = (xdrproc_t)xdr_utmpidlearr;
292 		switch (rqstp->rq_vers) {
293 		case RUSERSVERS_ORIG:
294 			local = (rusersproc_t)rusersproc_allnames_1_svc;
295 			break;
296 		case RUSERSVERS_IDLE:
297 			local = (rusersproc_t)rusersproc_allnames_2_svc;
298 			break;
299 		default:
300 			svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
301 			goto leave;
302 			/*NOTREACHED*/
303 		}
304 		break;
305 
306 	default:
307 		svcerr_noproc(transp);
308 		goto leave;
309 	}
310 	bzero(&argument, sizeof(argument));
311 	if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) {
312 		svcerr_decode(transp);
313 		goto leave;
314 	}
315 	result = (*local)(&argument, rqstp);
316 	if (result != NULL &&
317 	    !svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
318 		svcerr_systemerr(transp);
319 	}
320 	if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, &argument)) {
321 		syslog(LOG_ERR, "unable to free arguments");
322 		exit(1);
323 	}
324 leave:
325 	if (from_inetd)
326 		exit(0);
327 }
328