1997-01-09 23:18:21 +03:00
|
|
|
/* $NetBSD: rusers.c,v 1.12 1997/01/09 20:21:18 tls Exp $ */
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
1993-08-02 21:48:44 +04:00
|
|
|
#ifndef lint
|
1997-01-09 23:18:21 +03:00
|
|
|
static char rcsid[] = "$NetBSD: rusers.c,v 1.12 1997/01/09 20:21:18 tls 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/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <arpa/inet.h>
|
1993-11-21 22:01:01 +03:00
|
|
|
#include <utmp.h>
|
|
|
|
#include <stdlib.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
|
|
|
|
|
|
|
#define MAX_INT 0x7fffffff
|
1993-06-08 18:45:37 +04:00
|
|
|
#define HOST_WIDTH 20
|
1993-11-10 07:19:03 +03:00
|
|
|
#define LINE_WIDTH 8
|
1993-06-03 23:32:26 +04:00
|
|
|
char *argv0;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
struct host_list {
|
|
|
|
struct host_list *next;
|
|
|
|
struct in_addr addr;
|
|
|
|
} *hosts;
|
|
|
|
|
1993-11-10 07:19:03 +03:00
|
|
|
int
|
1993-11-21 22:01:01 +03:00
|
|
|
search_host(struct in_addr addr)
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
|
|
|
struct host_list *hp;
|
|
|
|
|
|
|
|
if (!hosts)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
for (hp = hosts; hp != NULL; hp = hp->next) {
|
|
|
|
if (hp->addr.s_addr == addr.s_addr)
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
1993-11-10 07:19:03 +03:00
|
|
|
void
|
1993-11-21 22:01:01 +03:00
|
|
|
remember_host(struct in_addr addr)
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
|
|
|
struct host_list *hp;
|
|
|
|
|
|
|
|
if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
|
|
|
|
fprintf(stderr, "%s: no memory.\n", argv0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
hp->addr.s_addr = addr.s_addr;
|
|
|
|
hp->next = hosts;
|
|
|
|
hosts = hp;
|
|
|
|
}
|
|
|
|
|
1993-11-21 22:01:01 +03:00
|
|
|
int
|
|
|
|
rusers_reply(char *replyp, struct sockaddr_in *raddrp)
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
1993-11-10 07:19:03 +03:00
|
|
|
int x, idle;
|
1993-12-04 02:39:15 +03:00
|
|
|
char date[32], idle_time[64], remote[64], local[64];
|
1993-11-10 07:19:03 +03:00
|
|
|
struct hostent *hp;
|
1993-11-21 22:01:01 +03:00
|
|
|
struct utmpidlearr *up = (struct utmpidlearr *)replyp;
|
1993-11-10 07:19:03 +03:00
|
|
|
char *host;
|
|
|
|
int days, hours, minutes, seconds;
|
|
|
|
|
1993-06-03 23:32:26 +04:00
|
|
|
if (search_host(raddrp->sin_addr))
|
|
|
|
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);
|
|
|
|
|
|
|
|
hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
|
|
|
|
sizeof(struct in_addr), AF_INET);
|
|
|
|
if (hp)
|
|
|
|
host = hp->h_name;
|
|
|
|
else
|
|
|
|
host = inet_ntoa(raddrp->sin_addr);
|
|
|
|
|
|
|
|
if (!longopt)
|
|
|
|
printf("%-*.*s ", HOST_WIDTH, HOST_WIDTH, host);
|
|
|
|
|
1993-11-21 22:01:01 +03:00
|
|
|
for (x = 0; x < up->uia_cnt; x++) {
|
1993-11-10 07:19:03 +03:00
|
|
|
strncpy(date,
|
1993-11-21 22:01:01 +03:00
|
|
|
&(ctime((time_t *)&(up->uia_arr[x]->ui_utmp.ut_time))[4]),
|
1993-11-10 07:19:03 +03:00
|
|
|
sizeof(date)-1);
|
|
|
|
|
1993-11-21 22:01:01 +03:00
|
|
|
idle = up->uia_arr[x]->ui_idle;
|
1993-11-10 07:19:03 +03:00
|
|
|
sprintf(idle_time, " :%02d", idle);
|
|
|
|
if (idle == MAX_INT)
|
|
|
|
strcpy(idle_time, "??");
|
|
|
|
else if (idle == 0)
|
|
|
|
strcpy(idle_time, "");
|
|
|
|
else {
|
|
|
|
seconds = idle;
|
|
|
|
days = seconds/(60*60*24);
|
|
|
|
seconds %= (60*60*24);
|
|
|
|
hours = seconds/(60*60);
|
|
|
|
seconds %= (60*60);
|
|
|
|
minutes = seconds/60;
|
|
|
|
seconds %= 60;
|
|
|
|
if (idle > 60)
|
|
|
|
sprintf(idle_time, "%2d:%02d",
|
|
|
|
minutes, seconds);
|
|
|
|
if (idle >= (60*60))
|
|
|
|
sprintf(idle_time, "%2d:%02d:%02d",
|
|
|
|
hours, minutes, seconds);
|
|
|
|
if (idle >= (24*60*60))
|
|
|
|
sprintf(idle_time, "%d days, %d:%02d:%02d",
|
|
|
|
days, hours, minutes, seconds);
|
|
|
|
}
|
|
|
|
|
1993-11-21 22:01:01 +03:00
|
|
|
strncpy(remote, up->uia_arr[x]->ui_utmp.ut_host,
|
1993-11-10 07:19:03 +03:00
|
|
|
sizeof(remote)-1);
|
|
|
|
if (strlen(remote) != 0)
|
|
|
|
sprintf(remote, "(%.16s)",
|
1993-11-21 22:01:01 +03:00
|
|
|
up->uia_arr[x]->ui_utmp.ut_host);
|
1993-11-10 07:19:03 +03:00
|
|
|
|
1993-12-04 02:39:15 +03:00
|
|
|
if (longopt) {
|
|
|
|
strncpy(local, host, sizeof(local));
|
|
|
|
local[HOST_WIDTH + LINE_WIDTH + 1 -
|
|
|
|
strlen(up->uia_arr[x]->ui_utmp.ut_line) - 1] = 0;
|
|
|
|
strcat(local, ":");
|
|
|
|
strcat(local, up->uia_arr[x]->ui_utmp.ut_line);
|
|
|
|
|
|
|
|
printf("%-8.8s %-*.*s %-12.12s %8s %.18s\n",
|
1993-11-21 22:01:01 +03:00
|
|
|
up->uia_arr[x]->ui_utmp.ut_name,
|
1993-12-04 02:39:15 +03:00
|
|
|
HOST_WIDTH+LINE_WIDTH+1, HOST_WIDTH+LINE_WIDTH+1, local,
|
1993-11-10 07:19:03 +03:00
|
|
|
date,
|
|
|
|
idle_time,
|
|
|
|
remote);
|
1993-12-04 02:39:15 +03:00
|
|
|
} else
|
1993-11-21 23:02:28 +03:00
|
|
|
printf("%0.8s ",
|
1993-11-21 22:01:01 +03:00
|
|
|
up->uia_arr[x]->ui_utmp.ut_name);
|
1993-11-10 07:19:03 +03:00
|
|
|
}
|
|
|
|
if (!longopt)
|
|
|
|
putchar('\n');
|
|
|
|
|
1993-06-03 23:32:26 +04:00
|
|
|
remember_host(raddrp->sin_addr);
|
|
|
|
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;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
struct hostent *hp;
|
|
|
|
|
|
|
|
hp = gethostbyname(host);
|
|
|
|
if (hp == NULL) {
|
|
|
|
fprintf(stderr, "%s: unknown host \"%s\"\n",
|
|
|
|
argv0, host);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
|
|
|
|
if (rusers_clnt == NULL) {
|
|
|
|
clnt_pcreateerror(argv0);
|
|
|
|
exit(1);
|
|
|
|
}
|
1993-06-03 23:32:26 +04:00
|
|
|
|
|
|
|
bzero((char *)&up, sizeof(up));
|
1993-11-10 07:19:03 +03:00
|
|
|
if (clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
|
1993-12-10 22:33:58 +03:00
|
|
|
xdr_utmpidlearr, &up, timeout) != RPC_SUCCESS) {
|
1993-11-10 07:19:03 +03:00
|
|
|
clnt_perror(rusers_clnt, argv0);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
addr.sin_addr.s_addr = *(int *)hp->h_addr;
|
|
|
|
rusers_reply((char *)&up, &addr);
|
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;
|
|
|
|
|
|
|
|
bzero((char *)&up, sizeof(up));
|
1993-11-10 07:19:03 +03:00
|
|
|
clnt_stat = clnt_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
|
|
|
|
RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr,
|
|
|
|
&up, rusers_reply);
|
1993-06-03 23:32:26 +04:00
|
|
|
if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
|
|
|
|
fprintf(stderr, "%s: %s\n", argv0, clnt_sperrno(clnt_stat));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-03-21 20:56:15 +03:00
|
|
|
void
|
|
|
|
usage(void)
|
1993-06-03 23:32:26 +04:00
|
|
|
{
|
1993-11-10 07:19:03 +03:00
|
|
|
fprintf(stderr, "Usage: %s [-la] [hosts ...]\n", argv0);
|
|
|
|
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;
|
|
|
|
extern int optind;
|
|
|
|
|
|
|
|
if (!(argv0 = rindex(argv[0], '/')))
|
|
|
|
argv0 = argv[0];
|
|
|
|
else
|
|
|
|
argv0++;
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|