1994-08-28 15:06:02 +00:00
|
|
|
/*-
|
2017-11-18 14:26:50 +00:00
|
|
|
* SPDX-License-Identifier: BSD-4-Clause
|
|
|
|
*
|
1994-08-28 15:06:02 +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.
|
|
|
|
*/
|
|
|
|
|
2002-04-28 11:13:20 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-08-28 15:06:02 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2002-04-28 11:13:20 +00:00
|
|
|
|
2001-06-19 03:37:21 +00:00
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <rpc/pmap_clnt.h>
|
|
|
|
#include <rpcsvc/rnusers.h>
|
2002-04-28 11:13:20 +00:00
|
|
|
|
2001-06-19 03:37:21 +00:00
|
|
|
#include <arpa/inet.h>
|
2002-04-28 11:13:20 +00:00
|
|
|
|
1997-08-08 12:12:54 +00:00
|
|
|
#include <err.h>
|
1994-08-28 15:06:02 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
1997-08-08 12:12:54 +00:00
|
|
|
#include <stdlib.h>
|
2002-04-28 11:13:20 +00:00
|
|
|
#include <string.h>
|
2004-05-19 21:34:52 +00:00
|
|
|
#include <timeconv.h>
|
1997-08-08 12:12:54 +00:00
|
|
|
#include <unistd.h>
|
1994-08-28 15:06:02 +00:00
|
|
|
|
2001-06-19 03:37:21 +00:00
|
|
|
#define MAX_INT 0x7fffffff
|
|
|
|
#define HOST_WIDTH 20
|
|
|
|
#define LINE_WIDTH 15
|
1994-08-28 15:06:02 +00:00
|
|
|
|
2011-11-06 08:16:47 +00:00
|
|
|
static int longopt;
|
|
|
|
static int allopt;
|
1994-08-28 15:06:02 +00:00
|
|
|
|
2011-11-06 08:16:47 +00:00
|
|
|
static struct host_list {
|
2001-06-19 03:37:21 +00:00
|
|
|
struct host_list *next;
|
|
|
|
struct in_addr addr;
|
1994-08-28 15:06:02 +00:00
|
|
|
} *hosts;
|
|
|
|
|
2002-04-28 11:13:20 +00:00
|
|
|
static int
|
1997-08-08 12:12:54 +00:00
|
|
|
search_host(struct in_addr addr)
|
1994-08-28 15:06:02 +00:00
|
|
|
{
|
|
|
|
struct host_list *hp;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2001-06-19 03:37:21 +00:00
|
|
|
if (hosts == NULL)
|
|
|
|
return (0);
|
1994-08-28 15:06:02 +00:00
|
|
|
|
|
|
|
for (hp = hosts; hp != NULL; hp = hp->next) {
|
|
|
|
if (hp->addr.s_addr == addr.s_addr)
|
2001-06-19 03:37:21 +00:00
|
|
|
return (1);
|
1994-08-28 15:06:02 +00:00
|
|
|
}
|
2001-06-19 03:37:21 +00:00
|
|
|
return (0);
|
1994-08-28 15:06:02 +00:00
|
|
|
}
|
|
|
|
|
2002-04-28 11:13:20 +00:00
|
|
|
static void
|
1997-08-08 12:12:54 +00:00
|
|
|
remember_host(struct in_addr addr)
|
1994-08-28 15:06:02 +00:00
|
|
|
{
|
|
|
|
struct host_list *hp;
|
|
|
|
|
2001-06-19 03:37:21 +00:00
|
|
|
if ((hp = (struct host_list *)malloc(sizeof(struct host_list))) == NULL)
|
1997-08-08 12:12:54 +00:00
|
|
|
errx(1, "no memory");
|
1994-08-28 15:06:02 +00:00
|
|
|
hp->addr.s_addr = addr.s_addr;
|
|
|
|
hp->next = hosts;
|
|
|
|
hosts = hp;
|
|
|
|
}
|
|
|
|
|
2002-04-28 11:13:20 +00:00
|
|
|
static int
|
2018-06-25 11:44:56 +00:00
|
|
|
rusers_reply(void *replyp, struct sockaddr_in *raddrp)
|
1994-08-28 15:06:02 +00:00
|
|
|
{
|
2018-06-25 11:44:56 +00:00
|
|
|
unsigned int x;
|
2002-04-28 11:13:20 +00:00
|
|
|
int idle;
|
2001-06-19 03:37:21 +00:00
|
|
|
char date[32], idle_time[64], remote[64];
|
|
|
|
struct hostent *hp;
|
2004-05-19 21:34:52 +00:00
|
|
|
utmpidlearr *up, u;
|
2001-06-19 03:37:21 +00:00
|
|
|
char *host;
|
|
|
|
int days, hours, minutes, seconds;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2004-05-19 21:34:52 +00:00
|
|
|
up = &u;
|
|
|
|
memcpy(up, replyp, sizeof(*up));
|
1994-08-28 15:06:02 +00:00
|
|
|
if (search_host(raddrp->sin_addr))
|
2001-06-19 03:37:21 +00:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (!allopt && up->utmpidlearr_len == 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
hp = gethostbyaddr((char *)&raddrp->sin_addr.s_addr,
|
|
|
|
sizeof(struct in_addr), AF_INET);
|
|
|
|
if (hp != NULL)
|
|
|
|
host = hp->h_name;
|
|
|
|
else
|
|
|
|
host = inet_ntoa(raddrp->sin_addr);
|
|
|
|
|
|
|
|
if (!longopt)
|
|
|
|
printf("%-*s ", HOST_WIDTH, host);
|
|
|
|
|
|
|
|
for (x = 0; x < up->utmpidlearr_len; x++) {
|
2002-01-19 23:20:02 +00:00
|
|
|
time_t t = _int_to_time(up->utmpidlearr_val[x].ui_utmp.ut_time);
|
2001-10-28 20:24:51 +00:00
|
|
|
strncpy(date, &(ctime(&t)[4]), sizeof(date) - 1);
|
2001-06-19 03:37:21 +00:00
|
|
|
|
|
|
|
idle = up->utmpidlearr_val[x].ui_idle;
|
|
|
|
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, "%d:%02d", minutes, seconds);
|
|
|
|
if (idle >= (60 * 60))
|
|
|
|
sprintf(idle_time, "%d:%02d:%02d",
|
|
|
|
hours, minutes, seconds);
|
|
|
|
if (idle >= (24 * 60 * 60))
|
|
|
|
sprintf(idle_time, "%d days, %d:%02d:%02d",
|
|
|
|
days, hours, minutes, seconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(remote, up->utmpidlearr_val[x].ui_utmp.ut_host,
|
|
|
|
sizeof(remote) - 1);
|
|
|
|
if (strlen(remote) != 0)
|
|
|
|
sprintf(remote, "(%.16s)",
|
|
|
|
up->utmpidlearr_val[x].ui_utmp.ut_host);
|
|
|
|
|
|
|
|
if (longopt)
|
|
|
|
printf("%-8.8s %*s:%-*.*s %-12.12s %6s %.18s\n",
|
|
|
|
up->utmpidlearr_val[x].ui_utmp.ut_name,
|
|
|
|
HOST_WIDTH, host, LINE_WIDTH, LINE_WIDTH,
|
|
|
|
up->utmpidlearr_val[x].ui_utmp.ut_line, date,
|
|
|
|
idle_time, remote );
|
|
|
|
else
|
|
|
|
printf("%s ",
|
|
|
|
up->utmpidlearr_val[x].ui_utmp.ut_name);
|
|
|
|
}
|
|
|
|
if (!longopt)
|
|
|
|
putchar('\n');
|
1995-05-30 06:41:30 +00:00
|
|
|
|
1994-08-28 15:06:02 +00:00
|
|
|
remember_host(raddrp->sin_addr);
|
2001-06-19 03:37:21 +00:00
|
|
|
return (0);
|
1994-08-28 15:06:02 +00:00
|
|
|
}
|
|
|
|
|
2002-04-28 11:13:20 +00:00
|
|
|
static void
|
1994-08-28 15:06:02 +00:00
|
|
|
onehost(char *host)
|
|
|
|
{
|
2001-06-19 03:37:21 +00:00
|
|
|
utmpidlearr up;
|
|
|
|
CLIENT *rusers_clnt;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
struct hostent *hp;
|
|
|
|
struct timeval tv;
|
1995-05-30 06:41:30 +00:00
|
|
|
|
2001-06-19 03:37:21 +00:00
|
|
|
hp = gethostbyname(host);
|
|
|
|
if (hp == NULL)
|
|
|
|
errx(1, "unknown host \"%s\"", host);
|
1994-08-28 15:06:02 +00:00
|
|
|
|
2001-06-19 03:37:21 +00:00
|
|
|
rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
|
|
|
|
if (rusers_clnt == NULL)
|
|
|
|
errx(1, "%s", clnt_spcreateerror(""));
|
1994-08-28 15:06:02 +00:00
|
|
|
|
2018-06-25 11:44:56 +00:00
|
|
|
memset(&up, 0, sizeof(up));
|
2001-06-19 03:37:21 +00:00
|
|
|
tv.tv_sec = 15; /* XXX ?? */
|
1996-12-30 15:28:38 +00:00
|
|
|
tv.tv_usec = 0;
|
2003-10-26 04:57:32 +00:00
|
|
|
if (clnt_call(rusers_clnt, RUSERSPROC_NAMES, (xdrproc_t)xdr_void, NULL,
|
|
|
|
(xdrproc_t)xdr_utmpidlearr, &up, tv) != RPC_SUCCESS)
|
2001-06-19 03:37:21 +00:00
|
|
|
errx(1, "%s", clnt_sperror(rusers_clnt, ""));
|
2004-05-19 21:34:52 +00:00
|
|
|
memcpy(&addr.sin_addr.s_addr, hp->h_addr, sizeof(addr.sin_addr.s_addr));
|
2018-06-25 11:44:56 +00:00
|
|
|
rusers_reply(&up, &addr);
|
2001-06-19 03:48:26 +00:00
|
|
|
clnt_destroy(rusers_clnt);
|
1994-08-28 15:06:02 +00:00
|
|
|
}
|
|
|
|
|
2002-04-28 11:13:20 +00:00
|
|
|
static void
|
|
|
|
allhosts(void)
|
1994-08-28 15:06:02 +00:00
|
|
|
{
|
1997-08-08 12:12:54 +00:00
|
|
|
utmpidlearr up;
|
1994-08-28 15:06:02 +00:00
|
|
|
enum clnt_stat clnt_stat;
|
|
|
|
|
2018-06-25 11:44:56 +00:00
|
|
|
memset(&up, 0, sizeof(up));
|
2001-06-19 03:37:21 +00:00
|
|
|
clnt_stat = clnt_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
|
2003-10-26 04:57:32 +00:00
|
|
|
RUSERSPROC_NAMES, (xdrproc_t)xdr_void, NULL,
|
|
|
|
(xdrproc_t)xdr_utmpidlearr, (char *)&up,
|
|
|
|
(resultproc_t)rusers_reply);
|
1997-08-08 12:12:54 +00:00
|
|
|
if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
|
|
|
|
errx(1, "%s", clnt_sperrno(clnt_stat));
|
1994-08-28 15:06:02 +00:00
|
|
|
}
|
|
|
|
|
1997-08-08 12:12:54 +00:00
|
|
|
static void
|
2002-04-28 11:13:20 +00:00
|
|
|
usage(void)
|
1994-08-28 15:06:02 +00:00
|
|
|
{
|
2001-06-19 03:37:21 +00:00
|
|
|
|
2005-05-21 09:55:10 +00:00
|
|
|
fprintf(stderr, "usage: rusers [-al] [host ...]\n");
|
2001-06-19 03:37:21 +00:00
|
|
|
exit(1);
|
1994-08-28 15:06:02 +00:00
|
|
|
}
|
|
|
|
|
1997-08-08 12:12:54 +00:00
|
|
|
int
|
1994-08-28 15:06:02 +00:00
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2001-06-19 03:37:21 +00: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);
|
1994-08-28 15:06:02 +00:00
|
|
|
if (argc == optind)
|
|
|
|
allhosts();
|
|
|
|
else {
|
|
|
|
for (; optind < argc; optind++)
|
2001-06-19 03:37:21 +00:00
|
|
|
(void)onehost(argv[optind]);
|
1994-08-28 15:06:02 +00:00
|
|
|
}
|
2001-06-19 03:37:21 +00:00
|
|
|
exit(0);
|
1994-08-28 15:06:02 +00:00
|
|
|
}
|