1994-08-28 14:53:45 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1993, John Brezak
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-11-26 07:36:51 +00:00
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1994-08-28 14:53:45 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1998-01-07 07:54:18 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
1994-08-28 14:53:45 +00:00
|
|
|
#include <stdio.h>
|
1997-11-26 07:36:51 +00:00
|
|
|
#include <string.h>
|
1994-08-28 14:53:45 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
2003-06-02 02:35:18 +00:00
|
|
|
#include <stdlib.h>
|
1997-11-26 07:36:51 +00:00
|
|
|
#include <syslog.h>
|
2010-01-13 18:28:58 +00:00
|
|
|
#include <utmpx.h>
|
1994-08-28 14:53:45 +00:00
|
|
|
#ifdef XIDLE
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/extensions/xidle.h>
|
|
|
|
#endif
|
|
|
|
#include <rpcsvc/rnusers.h>
|
|
|
|
|
|
|
|
#ifndef _PATH_DEV
|
|
|
|
#define _PATH_DEV "/dev"
|
|
|
|
#endif
|
|
|
|
|
2009-12-28 23:01:24 +00:00
|
|
|
static utmpidle utmp_idle[MAXUSERS];
|
2009-12-29 10:28:20 +00:00
|
|
|
static utmp old_utmp[MAXUSERS];
|
|
|
|
static struct utmpx utmp_list[MAXUSERS];
|
1994-08-28 14:53:45 +00:00
|
|
|
|
|
|
|
extern int from_inetd;
|
|
|
|
|
2009-12-28 23:01:24 +00:00
|
|
|
void rusers_service(struct svc_req *, SVCXPRT *);
|
|
|
|
|
1994-08-28 14:53:45 +00:00
|
|
|
#ifdef XIDLE
|
2009-12-28 23:01:24 +00:00
|
|
|
static Display *dpy;
|
1994-08-28 14:53:45 +00:00
|
|
|
|
|
|
|
static jmp_buf openAbort;
|
|
|
|
|
|
|
|
static void
|
2002-02-07 05:24:53 +00:00
|
|
|
abortOpen(void)
|
1994-08-28 14:53:45 +00:00
|
|
|
{
|
|
|
|
longjmp (openAbort, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
XqueryIdle(char *display)
|
|
|
|
{
|
2009-12-28 23:01:24 +00:00
|
|
|
int first_event, first_error;
|
|
|
|
Time IdleTime;
|
|
|
|
|
|
|
|
(void) signal (SIGALRM, abortOpen);
|
|
|
|
(void) alarm ((unsigned) 10);
|
|
|
|
if (!setjmp (openAbort)) {
|
|
|
|
if (!(dpy= XOpenDisplay(display))) {
|
|
|
|
syslog(LOG_ERR, "Cannot open display %s", display);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
if (XidleQueryExtension(dpy, &first_event, &first_error)) {
|
|
|
|
if (!XGetIdleTime(dpy, &IdleTime)) {
|
|
|
|
syslog(LOG_ERR, "%s: unable to get idle time", display);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
syslog(LOG_ERR, "%s: Xidle extension not loaded", display);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
XCloseDisplay(dpy);
|
|
|
|
} else {
|
|
|
|
syslog(LOG_ERR, "%s: server grabbed for over 10 seconds", display);
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
(void) signal (SIGALRM, SIG_DFL);
|
|
|
|
(void) alarm ((unsigned) 0);
|
|
|
|
|
|
|
|
IdleTime /= 1000;
|
|
|
|
return((IdleTime + 30) / 60);
|
1994-08-28 14:53:45 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static u_int
|
2009-12-28 23:01:24 +00:00
|
|
|
getidle(const char *tty, const char *display __unused)
|
1994-08-28 14:53:45 +00:00
|
|
|
{
|
2009-12-28 23:01:24 +00:00
|
|
|
struct stat st;
|
|
|
|
char ttyname[PATH_MAX];
|
|
|
|
time_t now;
|
|
|
|
u_long idle;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is an X terminal or console, then try the
|
|
|
|
* XIdle extension
|
|
|
|
*/
|
1994-08-28 14:53:45 +00:00
|
|
|
#ifdef XIDLE
|
2009-12-28 23:01:24 +00:00
|
|
|
if (display && *display && (idle = XqueryIdle(display)) >= 0)
|
|
|
|
return(idle);
|
1994-08-28 14:53:45 +00:00
|
|
|
#endif
|
2009-12-28 23:01:24 +00:00
|
|
|
idle = 0;
|
|
|
|
if (*tty == 'X') {
|
|
|
|
u_long kbd_idle, mouse_idle;
|
1994-11-04 02:14:13 +00:00
|
|
|
#if !defined(__FreeBSD__)
|
2009-12-28 23:01:24 +00:00
|
|
|
kbd_idle = getidle("kbd", NULL);
|
1994-08-28 14:53:45 +00:00
|
|
|
#else
|
2009-12-28 23:01:24 +00:00
|
|
|
kbd_idle = getidle("vga", NULL);
|
1994-08-28 14:53:45 +00:00
|
|
|
#endif
|
2009-12-28 23:01:24 +00:00
|
|
|
mouse_idle = getidle("mouse", NULL);
|
|
|
|
idle = (kbd_idle < mouse_idle)?kbd_idle:mouse_idle;
|
|
|
|
} else {
|
|
|
|
sprintf(ttyname, "%s/%s", _PATH_DEV, tty);
|
|
|
|
if (stat(ttyname, &st) < 0) {
|
1994-08-28 14:53:45 +00:00
|
|
|
#ifdef DEBUG
|
2009-12-28 23:01:24 +00:00
|
|
|
printf("%s: %s\n", ttyname, strerror(errno));
|
1994-08-28 14:53:45 +00:00
|
|
|
#endif
|
2009-12-28 23:01:24 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
time(&now);
|
1994-08-28 14:53:45 +00:00
|
|
|
#ifdef DEBUG
|
2009-12-28 23:01:24 +00:00
|
|
|
printf("%s: now=%d atime=%d\n", ttyname, now,
|
|
|
|
st.st_atime);
|
1994-08-28 14:53:45 +00:00
|
|
|
#endif
|
2009-12-28 23:01:24 +00:00
|
|
|
idle = now - st.st_atime;
|
|
|
|
idle = (idle + 30) / 60; /* secs->mins */
|
|
|
|
}
|
1994-08-28 14:53:45 +00:00
|
|
|
|
2009-12-28 23:01:24 +00:00
|
|
|
return(idle);
|
1994-08-28 14:53:45 +00:00
|
|
|
}
|
1995-05-30 05:51:47 +00:00
|
|
|
|
1994-08-28 14:53:45 +00:00
|
|
|
static utmpidlearr *
|
2009-12-28 23:01:24 +00:00
|
|
|
do_names_2(void)
|
1994-08-28 14:53:45 +00:00
|
|
|
{
|
2009-12-28 23:01:24 +00:00
|
|
|
static utmpidlearr ut;
|
2009-12-29 10:28:20 +00:00
|
|
|
struct utmpx *usr;
|
2009-12-28 23:01:24 +00:00
|
|
|
int nusers = 0;
|
1995-05-30 05:51:47 +00:00
|
|
|
|
2009-12-29 10:28:20 +00:00
|
|
|
memset(&ut, 0, sizeof(ut));
|
2009-12-28 23:01:24 +00:00
|
|
|
ut.utmpidlearr_val = &utmp_idle[0];
|
1995-05-30 05:51:47 +00:00
|
|
|
|
2009-12-29 10:28:20 +00:00
|
|
|
setutxent();
|
|
|
|
while ((usr = getutxent()) != NULL && nusers < MAXUSERS) {
|
|
|
|
if (usr->ut_type != USER_PROCESS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
memcpy(&utmp_list[nusers], usr, sizeof(*usr));
|
|
|
|
utmp_idle[nusers].ui_utmp.ut_time = usr->ut_tv.tv_sec;
|
|
|
|
utmp_idle[nusers].ui_idle =
|
|
|
|
getidle(usr->ut_line, usr->ut_host);
|
|
|
|
utmp_idle[nusers].ui_utmp.ut_line =
|
|
|
|
utmp_list[nusers].ut_line;
|
|
|
|
utmp_idle[nusers].ui_utmp.ut_name =
|
|
|
|
utmp_list[nusers].ut_user;
|
|
|
|
utmp_idle[nusers].ui_utmp.ut_host =
|
|
|
|
utmp_list[nusers].ut_host;
|
|
|
|
|
|
|
|
nusers++;
|
2009-12-28 23:01:24 +00:00
|
|
|
}
|
2009-12-29 10:28:20 +00:00
|
|
|
endutxent();
|
1994-08-28 14:53:45 +00:00
|
|
|
|
2009-12-28 23:01:24 +00:00
|
|
|
ut.utmpidlearr_len = nusers;
|
|
|
|
return(&ut);
|
1994-08-28 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2009-12-28 23:01:24 +00:00
|
|
|
static int *
|
|
|
|
rusers_num(void *argp __unused, struct svc_req *rqstp __unused)
|
1994-08-28 14:53:45 +00:00
|
|
|
{
|
2009-12-28 23:01:24 +00:00
|
|
|
static int num_users = 0;
|
2009-12-29 10:28:20 +00:00
|
|
|
struct utmpx *usr;
|
|
|
|
|
|
|
|
setutxent();
|
|
|
|
while ((usr = getutxent()) != NULL) {
|
|
|
|
if (usr->ut_type != USER_PROCESS)
|
|
|
|
continue;
|
|
|
|
num_users++;
|
2009-12-28 23:01:24 +00:00
|
|
|
}
|
2009-12-29 10:28:20 +00:00
|
|
|
endutxent();
|
|
|
|
|
2009-12-28 23:01:24 +00:00
|
|
|
return(&num_users);
|
1994-08-28 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static utmparr *
|
2009-12-28 23:01:24 +00:00
|
|
|
do_names_1(void)
|
1994-08-28 14:53:45 +00:00
|
|
|
{
|
2009-12-28 23:01:24 +00:00
|
|
|
utmpidlearr *utidle;
|
|
|
|
static utmparr ut;
|
|
|
|
unsigned int i;
|
1995-05-30 05:51:47 +00:00
|
|
|
|
2009-12-28 23:01:24 +00:00
|
|
|
bzero((char *)&ut, sizeof(ut));
|
1995-05-30 05:51:47 +00:00
|
|
|
|
2009-12-28 23:01:24 +00:00
|
|
|
utidle = do_names_2();
|
|
|
|
if (utidle) {
|
|
|
|
ut.utmparr_len = utidle->utmpidlearr_len;
|
|
|
|
ut.utmparr_val = &old_utmp[0];
|
|
|
|
for (i = 0; i < ut.utmparr_len; i++)
|
|
|
|
bcopy(&utmp_idle[i].ui_utmp, &old_utmp[i],
|
|
|
|
sizeof(old_utmp[0]));
|
1995-05-30 05:51:47 +00:00
|
|
|
|
2009-12-28 23:01:24 +00:00
|
|
|
}
|
1995-05-30 05:51:47 +00:00
|
|
|
|
2009-12-28 23:01:24 +00:00
|
|
|
return(&ut);
|
1994-08-28 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
utmpidlearr *
|
2009-12-28 23:01:24 +00:00
|
|
|
rusersproc_names_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
|
1994-08-28 14:53:45 +00:00
|
|
|
{
|
2009-12-28 23:01:24 +00:00
|
|
|
|
|
|
|
return (do_names_2());
|
1994-08-28 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
utmpidlearr *
|
2009-12-28 23:01:24 +00:00
|
|
|
rusersproc_allnames_2_svc(void *argp __unused, struct svc_req *rqstp __unused)
|
1994-08-28 14:53:45 +00:00
|
|
|
{
|
2009-12-28 23:01:24 +00:00
|
|
|
|
|
|
|
return (do_names_2());
|
1994-08-28 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
utmparr *
|
2009-12-28 23:01:24 +00:00
|
|
|
rusersproc_names_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
|
1994-08-28 14:53:45 +00:00
|
|
|
{
|
2009-12-28 23:01:24 +00:00
|
|
|
|
|
|
|
return (do_names_1());
|
1994-08-28 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
utmparr *
|
2009-12-28 23:01:24 +00:00
|
|
|
rusersproc_allnames_1_svc(void *argp __unused, struct svc_req *rqstp __unused)
|
1994-08-28 14:53:45 +00:00
|
|
|
{
|
2009-12-28 23:01:24 +00:00
|
|
|
|
|
|
|
return (do_names_1());
|
1994-08-28 14:53:45 +00:00
|
|
|
}
|
|
|
|
|
2009-12-28 23:01:24 +00:00
|
|
|
typedef void *(*rusersproc_t)(void *, struct svc_req *);
|
|
|
|
|
1994-08-28 14:53:45 +00:00
|
|
|
void
|
2002-02-07 05:24:53 +00:00
|
|
|
rusers_service(struct svc_req *rqstp, SVCXPRT *transp)
|
1994-08-28 14:53:45 +00:00
|
|
|
{
|
|
|
|
union {
|
|
|
|
int fill;
|
|
|
|
} argument;
|
|
|
|
char *result;
|
2009-12-28 23:01:24 +00:00
|
|
|
xdrproc_t xdr_argument, xdr_result;
|
|
|
|
rusersproc_t local;
|
1994-08-28 14:53:45 +00:00
|
|
|
|
|
|
|
switch (rqstp->rq_proc) {
|
|
|
|
case NULLPROC:
|
2002-04-28 15:18:50 +00:00
|
|
|
(void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
|
1994-08-28 14:53:45 +00:00
|
|
|
goto leave;
|
|
|
|
|
|
|
|
case RUSERSPROC_NUM:
|
2009-12-28 23:01:24 +00:00
|
|
|
xdr_argument = (xdrproc_t)xdr_void;
|
|
|
|
xdr_result = (xdrproc_t)xdr_int;
|
|
|
|
local = (rusersproc_t)rusers_num;
|
1994-08-28 14:53:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RUSERSPROC_NAMES:
|
2009-12-28 23:01:24 +00:00
|
|
|
xdr_argument = (xdrproc_t)xdr_void;
|
|
|
|
xdr_result = (xdrproc_t)xdr_utmpidlearr;
|
|
|
|
switch (rqstp->rq_vers) {
|
|
|
|
case RUSERSVERS_ORIG:
|
|
|
|
local = (rusersproc_t)rusersproc_names_1_svc;
|
|
|
|
break;
|
|
|
|
case RUSERSVERS_IDLE:
|
|
|
|
local = (rusersproc_t)rusersproc_names_2_svc;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
|
|
|
|
goto leave;
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1994-08-28 14:53:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RUSERSPROC_ALLNAMES:
|
2009-12-28 23:01:24 +00:00
|
|
|
xdr_argument = (xdrproc_t)xdr_void;
|
|
|
|
xdr_result = (xdrproc_t)xdr_utmpidlearr;
|
|
|
|
switch (rqstp->rq_vers) {
|
|
|
|
case RUSERSVERS_ORIG:
|
|
|
|
local = (rusersproc_t)rusersproc_allnames_1_svc;
|
|
|
|
break;
|
|
|
|
case RUSERSVERS_IDLE:
|
|
|
|
local = (rusersproc_t)rusersproc_allnames_2_svc;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
svcerr_progvers(transp, RUSERSVERS_ORIG, RUSERSVERS_IDLE);
|
|
|
|
goto leave;
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
1994-08-28 14:53:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
svcerr_noproc(transp);
|
|
|
|
goto leave;
|
|
|
|
}
|
2002-04-28 15:18:50 +00:00
|
|
|
bzero(&argument, sizeof(argument));
|
|
|
|
if (!svc_getargs(transp, (xdrproc_t)xdr_argument, &argument)) {
|
1994-08-28 14:53:45 +00:00
|
|
|
svcerr_decode(transp);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
result = (*local)(&argument, rqstp);
|
2002-04-28 15:18:50 +00:00
|
|
|
if (result != NULL &&
|
|
|
|
!svc_sendreply(transp, (xdrproc_t)xdr_result, result)) {
|
1994-08-28 14:53:45 +00:00
|
|
|
svcerr_systemerr(transp);
|
|
|
|
}
|
2002-04-28 15:18:50 +00:00
|
|
|
if (!svc_freeargs(transp, (xdrproc_t)xdr_argument, &argument)) {
|
1998-01-07 07:54:18 +00:00
|
|
|
syslog(LOG_ERR, "unable to free arguments");
|
|
|
|
exit(1);
|
|
|
|
}
|
1994-08-28 14:53:45 +00:00
|
|
|
leave:
|
2009-12-28 23:01:24 +00:00
|
|
|
if (from_inetd)
|
|
|
|
exit(0);
|
1994-08-28 14:53:45 +00:00
|
|
|
}
|