summaryrefslogtreecommitdiff
path: root/usr.bin/rusers/rusers.c
diff options
context:
space:
mode:
authorPaul Janzen <pjanzen@cvs.openbsd.org>2001-02-17 16:52:24 +0000
committerPaul Janzen <pjanzen@cvs.openbsd.org>2001-02-17 16:52:24 +0000
commita02f8f4160774e947a13ddf8ad21bd9face885dd (patch)
tree5234e4f4f8f39e0360d69a7eee204f5e31f8c974 /usr.bin/rusers/rusers.c
parent1c02425bbc7085da25dc9a11935bc19d613b2772 (diff)
Tidy, be a bit more careful about buffers, and don't use strlen() on utmp.
Diffstat (limited to 'usr.bin/rusers/rusers.c')
-rw-r--r--usr.bin/rusers/rusers.c76
1 files changed, 39 insertions, 37 deletions
diff --git a/usr.bin/rusers/rusers.c b/usr.bin/rusers/rusers.c
index f9a319b002f..d89d604d870 100644
--- a/usr.bin/rusers/rusers.c
+++ b/usr.bin/rusers/rusers.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rusers.c,v 1.10 2001/02/05 21:41:07 deraadt Exp $ */
+/* $OpenBSD: rusers.c,v 1.11 2001/02/17 16:52:23 pjanzen Exp $ */
/*-
* Copyright (c) 1993 John Brezak
@@ -29,13 +29,14 @@
*/
#ifndef lint
-static char rcsid[] = "$OpenBSD: rusers.c,v 1.10 2001/02/05 21:41:07 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: rusers.c,v 1.11 2001/02/17 16:52:23 pjanzen Exp $";
#endif /* not lint */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netdb.h>
+#include <err.h>
#include <stdio.h>
#include <string.h>
#include <rpc/rpc.h>
@@ -52,9 +53,10 @@ static char rcsid[] = "$OpenBSD: rusers.c,v 1.10 2001/02/05 21:41:07 deraadt Exp
#include <rpcsvc/rnusers.h> /* Old version */
#define MAX_INT 0x7fffffff
+/* Preferred formatting */
#define HOST_WIDTH 20
#define LINE_WIDTH 8
-char *argv0;
+#define NAME_WIDTH 8
struct timeval timeout = { 25, 0 };
int longopt;
@@ -65,6 +67,8 @@ struct host_list {
struct in_addr addr;
} *hosts;
+extern char *__progname;
+
int
search_host(struct in_addr addr)
{
@@ -85,10 +89,8 @@ remember_host(struct in_addr addr)
{
struct host_list *hp;
- if (!(hp = (struct host_list *)malloc(sizeof(struct host_list)))) {
- fprintf(stderr, "%s: no memory.\n", argv0);
- exit(1);
- }
+ if (!(hp = (struct host_list *)malloc(sizeof(struct host_list))))
+ err(1, NULL);
hp->addr.s_addr = addr.s_addr;
hp->next = hosts;
hosts = hp;
@@ -97,11 +99,13 @@ remember_host(struct in_addr addr)
int
rusers_reply(char *replyp, struct sockaddr_in *raddrp)
{
- int x, idle;
- char date[32], idle_time[64], remote[64], local[64];
+ int x, idle, i;
+ char date[32], idle_time[64], remote[UT_HOSTSIZE + 1];
+ char local[HOST_WIDTH + LINE_WIDTH + 2];
+ char utline[UT_LINESIZE + 1];
struct hostent *hp;
utmpidlearr *up = (utmpidlearr *)replyp;
- char *host;
+ char *host, *tmp;
int days, hours, minutes, seconds;
if (search_host(raddrp->sin_addr))
@@ -121,9 +125,14 @@ rusers_reply(char *replyp, struct sockaddr_in *raddrp)
printf("%-*.*s ", HOST_WIDTH, HOST_WIDTH, host);
for (x = 0; x < up->uia_cnt; x++) {
- strncpy(date,
+ strlcpy(date,
&(ctime((time_t *)&(up->uia_arr[x]->ui_utmp.ut_time))[4]),
- sizeof(date)-1);
+ sizeof(date));
+ /* ctime() adds a trailing \n. Trimming it is only
+ * mandatory if the output formatting changes.
+ */
+ if ((tmp = strchr(date, '\n')) != NULL)
+ *tmp = '\0';
idle = up->uia_arr[x]->ui_idle;
sprintf(idle_time, " :%02d", idle);
@@ -152,22 +161,25 @@ rusers_reply(char *replyp, struct sockaddr_in *raddrp)
strncpy(remote, up->uia_arr[x]->ui_utmp.ut_host,
sizeof(remote)-1);
+ remote[sizeof(remote) - 1] = '\0';
if (strlen(remote) != 0)
- sprintf(remote, "(%.16s)",
+ snprintf(remote, UT_HOSTSIZE + 1, "(%.16s)",
up->uia_arr[x]->ui_utmp.ut_host);
if (longopt) {
strncpy(local, host, sizeof(local));
- local[HOST_WIDTH + LINE_WIDTH + 1 -
- strlen(up->uia_arr[x]->ui_utmp.ut_line) - 1] = 0;
+ strncpy(utline, up->uia_arr[x]->ui_utmp.ut_line,
+ UT_LINESIZE);
+ utline[sizeof(utline) - 1] = '\0';
+ i = sizeof(local) - strlen(utline) - 2;
+ if (i < 0)
+ i = 0;
+ local[i] = '\0';
strcat(local, ":");
- strncat(local, up->uia_arr[x]->ui_utmp.ut_line,
- sizeof (local) - strlen (local) - 1);
- local[sizeof (local) - 1] = 0;
+ strlcat(local, utline, sizeof(local));
-#define MAXNAME 8
printf("%-*.*s %-*.*s %-12.12s %8s %.18s\n",
- MAXNAME, UT_NAMESIZE,
+ NAME_WIDTH, UT_NAMESIZE,
up->uia_arr[x]->ui_utmp.ut_name,
HOST_WIDTH+LINE_WIDTH+1, HOST_WIDTH+LINE_WIDTH+1,
local, date, idle_time, remote);
@@ -191,22 +203,19 @@ onehost(char *host)
struct hostent *hp;
hp = gethostbyname(host);
- if (hp == NULL) {
- fprintf(stderr, "%s: unknown host \"%s\"\n",
- argv0, host);
- exit(1);
- }
+ if (hp == NULL)
+ errx(1, "unknown host \"%s\"", host);
rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
if (rusers_clnt == NULL) {
- clnt_pcreateerror(argv0);
+ clnt_pcreateerror(__progname);
exit(1);
}
bzero((char *)&up, sizeof(up));
if (clnt_call(rusers_clnt, RUSERSPROC_NAMES, xdr_void, NULL,
xdr_utmpidlearr, &up, timeout) != RPC_SUCCESS) {
- clnt_perror(rusers_clnt, argv0);
+ clnt_perror(rusers_clnt, __progname);
exit(1);
}
addr.sin_addr.s_addr = *(int *)hp->h_addr;
@@ -223,16 +232,14 @@ allhosts(void)
clnt_stat = clnt_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
RUSERSPROC_NAMES, xdr_void, NULL, xdr_utmpidlearr,
(char *)&up, rusers_reply);
- if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT) {
- fprintf(stderr, "%s: %s\n", argv0, clnt_sperrno(clnt_stat));
- exit(1);
- }
+ if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
+ errx(1, "%s", clnt_sperrno(clnt_stat));
}
void
usage(void)
{
- fprintf(stderr, "Usage: %s [-la] [hosts ...]\n", argv0);
+ fprintf(stderr, "Usage: %s [-la] [hosts ...]\n", __progname);
exit(1);
}
@@ -242,11 +249,6 @@ main(int argc, char *argv[])
int ch;
extern int optind;
- if (!(argv0 = strrchr(argv[0], '/')))
- argv0 = argv[0];
- else
- argv0++;
-
while ((ch = getopt(argc, argv, "al")) != -1)
switch (ch) {
case 'a':