2006-05-11 05:25:23 +04:00
|
|
|
/* $NetBSD: rusers.c,v 1.23 2006/05/11 01:25:23 mrg Exp $ */
|
1997-01-09 23:18:21 +03:00
|
|
|
|
1993-06-03 23:32:26 +04:00
|
|
|
/*-
|
1993-11-21 22:01:01 +03: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. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR `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 AUTHOR 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.
|
1993-06-03 23:32:26 +04:00
|
|
|
*/
|
|
|
|
|
1997-08-24 06:40:40 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-08-02 21:48:44 +04:00
|
|
|
#ifndef lint
|
2006-05-11 05:25:23 +04:00
|
|
|
__RCSID("$NetBSD: rusers.c,v 1.23 2006/05/11 01:25:23 mrg Exp $");
|
1993-08-02 21:48:44 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1993-06-03 23:32:26 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
1998-04-02 14:59:14 +04:00
|
|
|
|
1997-08-24 06:40:40 +04:00
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <arpa/inet.h>
|
1998-02-03 07:08:01 +03:00
|
|
|
|
1997-08-24 06:40:40 +04:00
|
|
|
#include <err.h>
|
1993-06-03 23:32:26 +04:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
1997-08-24 06:40:40 +04:00
|
|
|
#include <stdlib.h>
|
1998-02-03 07:08:01 +03:00
|
|
|
#include <string.h>
|
1998-04-02 14:59:14 +04:00
|
|
|
#include <time.h>
|
1998-02-03 07:08:01 +03:00
|
|
|
#include <unistd.h>
|
1993-11-21 22:01:01 +03:00
|
|
|
#include <utmp.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For now we only try version 2 of the protocol. The current
|
1993-12-04 02:39:15 +03:00
|
|
|
* version is 3 (rusers.h), but only Solaris and NetBSD seem
|
1993-11-21 22:01:01 +03:00
|
|
|
* to support it currently.
|
|
|
|
*/
|
|
|
|
#include <rpcsvc/rnusers.h> /* Old version */
|
1993-06-03 23:32:26 +04:00
|
|
|
|
1997-08-24 06:40:40 +04:00
|
|
|
|
1993-06-03 23:32:26 +04:00
|
|
|
#define MAX_INT 0x7fffffff
|
|
|
|
|
1993-12-10 22:33:58 +03:00
|
|
|
struct timeval timeout = { 25, 0 };
|
1993-06-03 23:32:26 +04:00
|
|
|
int longopt;
|
|
|
|
int allopt;
|
|
|
|
|
2000-06-03 23:30:03 +04:00
|
|
|
void allhosts(void);
|
|
|
|
int main(int, char *[]);
|
|
|
|
void onehost(char *);
|
|
|
|
void remember_host(struct sockaddr *);
|
|
|
|
int rusers_reply(char *, struct netbuf *, struct netconfig *);
|
|
|
|
int search_host(struct sockaddr *);
|
|
|
|
void usage(void);
|
1997-08-24 06:40:40 +04:00
|
|
|
|
1993-06-03 23:32:26 +04:00
|
|
|
struct host_list {
|
|
|
|
struct host_list *next;
|
2000-06-03 23:30:03 +04:00
|
|
|
int family;
|
|
|
|
union {
|
|
|
|
struct in6_addr _addr6;
|
|
|
|
struct in_addr _addr4;
|
|
|
|
} addr;
|
1993-06-03 23:32:26 +04:00
|
|
|
} *hosts;
|
|
|
|
|
2000-06-03 23:30:03 +04:00
|
|
|
#define addr6 addr._addr6
|
|
|
|
#define addr4 addr._addr4
|
|
|
|
|
1993-11-10 07:19:03 +03:00
|
|
|
int
|
2000-06-03 23:30:03 +04:00
|
|
|
search_host(struct sockaddr *sa)
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
|
|
|
struct host_list *hp;
|
|
|
|
|
|
|
|
if (!hosts)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
for (hp = hosts; hp != NULL; hp = hp->next) {
|
2000-06-03 23:30:03 +04:00
|
|
|
switch (hp->family) {
|
|
|
|
case AF_INET6:
|
|
|
|
if (!memcmp(&hp->addr6,
|
|
|
|
&((struct sockaddr_in6 *)sa)->sin6_addr,
|
|
|
|
sizeof (struct in6_addr)))
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
if (!memcmp(&hp->addr4,
|
|
|
|
&((struct sockaddr_in *)sa)->sin_addr,
|
|
|
|
sizeof (struct in_addr)))
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
default:
|
2001-01-16 05:43:37 +03:00
|
|
|
break;
|
2000-06-03 23:30:03 +04:00
|
|
|
}
|
1993-06-03 23:32:26 +04:00
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1993-11-10 07:19:03 +03:00
|
|
|
void
|
2000-06-03 23:30:03 +04:00
|
|
|
remember_host(struct sockaddr *sa)
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
|
|
|
struct host_list *hp;
|
|
|
|
|
2000-06-03 23:30:03 +04:00
|
|
|
if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
|
|
|
|
err(1, "malloc");
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
hp->family = sa->sa_family;
|
1993-06-03 23:32:26 +04:00
|
|
|
hp->next = hosts;
|
2000-06-03 23:30:03 +04:00
|
|
|
switch (sa->sa_family) {
|
|
|
|
case AF_INET6:
|
|
|
|
memcpy(&hp->addr6, &((struct sockaddr_in6 *)sa)->sin6_addr,
|
|
|
|
sizeof (struct in6_addr));
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
memcpy(&hp->addr4, &((struct sockaddr_in *)sa)->sin_addr,
|
|
|
|
sizeof (struct in_addr));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err(1, "unknown address family");
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1993-06-03 23:32:26 +04:00
|
|
|
hosts = hp;
|
|
|
|
}
|
|
|
|
|
1993-11-21 22:01:01 +03:00
|
|
|
int
|
2000-06-03 23:30:03 +04:00
|
|
|
rusers_reply(char *replyp, struct netbuf *raddrp, struct netconfig *nconf)
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
2000-06-03 23:30:03 +04:00
|
|
|
char host[NI_MAXHOST];
|
1997-08-24 06:40:40 +04:00
|
|
|
int x;
|
1993-11-21 22:01:01 +03:00
|
|
|
struct utmpidlearr *up = (struct utmpidlearr *)replyp;
|
2000-06-03 23:30:03 +04:00
|
|
|
struct sockaddr *sa = raddrp->buf;
|
1993-11-10 07:19:03 +03:00
|
|
|
|
2000-06-03 23:30:03 +04:00
|
|
|
if (search_host(sa))
|
1993-06-03 23:32:26 +04:00
|
|
|
return(0);
|
|
|
|
|
1993-11-21 22:01:01 +03:00
|
|
|
if (!allopt && !up->uia_cnt)
|
1993-11-10 07:19:03 +03:00
|
|
|
return(0);
|
2000-06-03 23:30:03 +04:00
|
|
|
|
|
|
|
if (getnameinfo(sa, sa->sa_len, host, sizeof host, NULL, 0, 0))
|
|
|
|
return 0;
|
|
|
|
|
1997-08-24 06:40:40 +04:00
|
|
|
#define HOSTWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_host)
|
|
|
|
#define LINEWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_line)
|
|
|
|
#define NAMEWID (int)sizeof(up->uia_arr[0]->ui_utmp.ut_name)
|
|
|
|
|
1993-11-10 07:19:03 +03:00
|
|
|
if (!longopt)
|
1997-08-24 06:40:40 +04:00
|
|
|
printf("%-*.*s ", HOSTWID, HOSTWID, host);
|
1993-11-10 07:19:03 +03:00
|
|
|
|
1993-11-21 22:01:01 +03:00
|
|
|
for (x = 0; x < up->uia_cnt; x++) {
|
1997-08-24 06:40:40 +04:00
|
|
|
unsigned int minutes;
|
|
|
|
char date[26], idle[8];
|
|
|
|
char remote[HOSTWID + 3]; /* "(" host ")" \0 */
|
|
|
|
char local[HOSTWID + LINEWID + 2]; /* host ":" line \0 */
|
2006-05-11 05:25:23 +04:00
|
|
|
time_t uttime;
|
1997-08-24 06:40:40 +04:00
|
|
|
|
|
|
|
if (!longopt) {
|
|
|
|
printf("%.*s ", NAMEWID,
|
|
|
|
up->uia_arr[x]->ui_utmp.ut_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(local, sizeof(local), "%.*s:%s",
|
|
|
|
HOSTWID, host,
|
|
|
|
up->uia_arr[x]->ui_utmp.ut_line);
|
|
|
|
|
2006-05-11 05:25:23 +04:00
|
|
|
uttime = up->uia_arr[x]->ui_utmp.ut_time;
|
1997-08-24 06:40:40 +04:00
|
|
|
snprintf(date, sizeof(date), "%s",
|
2006-05-11 05:25:23 +04:00
|
|
|
&(ctime(&uttime))[4]);
|
1997-08-24 06:40:40 +04:00
|
|
|
|
|
|
|
minutes = up->uia_arr[x]->ui_idle;
|
|
|
|
if (minutes == MAX_INT)
|
|
|
|
strcpy(idle, "??");
|
|
|
|
else if (minutes == 0)
|
|
|
|
strcpy(idle, "");
|
1993-11-10 07:19:03 +03:00
|
|
|
else {
|
1997-08-24 06:40:40 +04:00
|
|
|
unsigned int days, hours;
|
|
|
|
|
|
|
|
days = minutes / (24 * 60);
|
|
|
|
minutes %= (24 * 60);
|
1998-07-03 22:35:35 +04:00
|
|
|
hours = minutes / 60;
|
|
|
|
minutes %= 60;
|
1997-08-24 06:40:40 +04:00
|
|
|
|
|
|
|
if (days > 0)
|
|
|
|
snprintf(idle, sizeof(idle), "%d d ", days);
|
|
|
|
else if (hours > 0)
|
|
|
|
snprintf(idle, sizeof(idle), "%2d:%02d",
|
|
|
|
hours, minutes);
|
|
|
|
else
|
|
|
|
snprintf(idle, sizeof(idle), ":%02d", minutes);
|
1993-11-10 07:19:03 +03:00
|
|
|
}
|
|
|
|
|
1997-08-24 06:40:40 +04:00
|
|
|
if (up->uia_arr[x]->ui_utmp.ut_host[0] != '\0')
|
|
|
|
snprintf(remote, sizeof(remote), "(%.*s)",
|
|
|
|
HOSTWID, up->uia_arr[x]->ui_utmp.ut_host);
|
|
|
|
else
|
|
|
|
remote[0] = '\0';
|
|
|
|
|
|
|
|
printf("%-*.*s %-*.*s %-12.12s %8.8s %s\n",
|
|
|
|
NAMEWID, NAMEWID, up->uia_arr[x]->ui_utmp.ut_name,
|
|
|
|
HOSTWID+LINEWID+1, HOSTWID+LINEWID+1, local,
|
|
|
|
date, idle, remote);
|
1993-11-10 07:19:03 +03:00
|
|
|
}
|
|
|
|
if (!longopt)
|
|
|
|
putchar('\n');
|
|
|
|
|
2000-06-03 23:30:03 +04:00
|
|
|
remember_host(sa);
|
1993-06-03 23:32:26 +04:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1993-11-21 22:01:01 +03:00
|
|
|
void
|
|
|
|
onehost(char *host)
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
1993-11-21 22:01:01 +03:00
|
|
|
struct utmpidlearr up;
|
1993-11-10 07:19:03 +03:00
|
|
|
CLIENT *rusers_clnt;
|
1997-08-24 06:40:40 +04:00
|
|
|
enum clnt_stat clnt_stat;
|
2000-06-03 23:30:03 +04:00
|
|
|
struct netbuf nb;
|
|
|
|
struct addrinfo *ai;
|
|
|
|
int ecode;
|
1993-11-10 07:19:03 +03:00
|
|
|
|
|
|
|
rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
|
|
|
|
if (rusers_clnt == NULL) {
|
2001-02-20 02:03:42 +03:00
|
|
|
clnt_pcreateerror(getprogname());
|
1993-11-10 07:19:03 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
1993-06-03 23:32:26 +04:00
|
|
|
|
2000-06-03 23:30:03 +04:00
|
|
|
ecode = getaddrinfo(host, NULL, NULL, &ai);
|
|
|
|
if (ecode != 0)
|
|
|
|
err(1, "%s", gai_strerror(ecode));
|
|
|
|
|
1997-08-24 06:40:40 +04:00
|
|
|
memset((char *)&up, 0, sizeof(up));
|
|
|
|
clnt_stat = clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
|
|
|
|
xdr_utmpidlearr, &up, timeout);
|
|
|
|
if (clnt_stat != RPC_SUCCESS)
|
|
|
|
errx(1, "%s", clnt_sperrno(clnt_stat));
|
2000-06-03 23:30:03 +04:00
|
|
|
nb.buf = ai->ai_addr;
|
|
|
|
nb.len = nb.maxlen = ai->ai_addrlen;
|
|
|
|
rusers_reply((char *)&up, &nb, NULL);
|
|
|
|
freeaddrinfo(ai);
|
1993-06-03 23:32:26 +04:00
|
|
|
}
|
|
|
|
|
1993-11-21 22:01:01 +03:00
|
|
|
void
|
|
|
|
allhosts(void)
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
1993-11-21 22:01:01 +03:00
|
|
|
struct utmpidlearr up;
|
1993-06-03 23:32:26 +04:00
|
|
|
enum clnt_stat clnt_stat;
|
|
|
|
|
1997-08-24 06:40:40 +04:00
|
|
|
memset((char *)&up, 0, sizeof(up));
|
2000-06-03 23:30:03 +04:00
|
|
|
clnt_stat = rpc_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
|
1993-11-10 07:19:03 +03:00
|
|
|
RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr,
|
2000-06-03 23:30:03 +04:00
|
|
|
(char *)&up, (resultproc_t)rusers_reply, "udp");
|
1997-08-24 06:40:40 +04:00
|
|
|
if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
|
|
|
|
errx(1, "%s", clnt_sperrno(clnt_stat));
|
1993-06-03 23:32:26 +04:00
|
|
|
}
|
|
|
|
|
1996-03-21 20:56:15 +03:00
|
|
|
void
|
|
|
|
usage(void)
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
2004-01-06 02:23:32 +03:00
|
|
|
fprintf(stderr, "usage: %s [-la] [hosts ...]\n", getprogname());
|
1993-11-10 07:19:03 +03:00
|
|
|
exit(1);
|
1993-06-03 23:32:26 +04:00
|
|
|
}
|
|
|
|
|
1996-03-21 20:56:15 +03:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
1993-11-10 07:19:03 +03:00
|
|
|
int ch;
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "al")) != -1)
|
|
|
|
switch (ch) {
|
|
|
|
case 'a':
|
|
|
|
allopt++;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
longopt++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
setlinebuf(stdout);
|
1993-06-03 23:32:26 +04:00
|
|
|
if (argc == optind)
|
|
|
|
allhosts();
|
|
|
|
else {
|
|
|
|
for (; optind < argc; optind++)
|
|
|
|
(void) onehost(argv[optind]);
|
|
|
|
}
|
1993-11-10 07:19:03 +03:00
|
|
|
exit(0);
|
1993-06-03 23:32:26 +04:00
|
|
|
}
|