nrelease - fix/improve livecd
[dragonfly.git] / usr.bin / rusers / rusers.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1993, John Brezak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
dc71b7ab 13 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
1de703da 28 *
ce0e08e2 29 * $FreeBSD: src/usr.bin/rusers/rusers.c,v 1.17 2005/05/21 09:55:08 ru Exp $
815943a9 30 * $DragonFly: src/usr.bin/rusers/rusers.c,v 1.4 2008/10/16 01:52:33 swildner Exp $
984263bc
MD
31 */
32
984263bc
MD
33#include <sys/types.h>
34#include <sys/socket.h>
ce0e08e2 35
984263bc
MD
36#include <rpc/rpc.h>
37#include <rpc/pmap_clnt.h>
38#include <rpcsvc/rnusers.h>
ce0e08e2 39
984263bc 40#include <arpa/inet.h>
ce0e08e2 41
984263bc
MD
42#include <err.h>
43#include <netdb.h>
44#include <stdio.h>
45#include <stdlib.h>
ce0e08e2 46#include <string.h>
984263bc
MD
47#include <unistd.h>
48
49#define MAX_INT 0x7fffffff
50#define HOST_WIDTH 20
51#define LINE_WIDTH 15
52
53int longopt;
54int allopt;
55
56struct host_list {
57 struct host_list *next;
58 struct in_addr addr;
59} *hosts;
60
ce0e08e2 61static int
984263bc
MD
62search_host(struct in_addr addr)
63{
64 struct host_list *hp;
65
66 if (hosts == NULL)
67 return (0);
68
69 for (hp = hosts; hp != NULL; hp = hp->next) {
70 if (hp->addr.s_addr == addr.s_addr)
71 return (1);
72 }
73 return (0);
74}
75
ce0e08e2 76static void
984263bc
MD
77remember_host(struct in_addr addr)
78{
79 struct host_list *hp;
80
81 if ((hp = (struct host_list *)malloc(sizeof(struct host_list))) == NULL)
82 errx(1, "no memory");
83 hp->addr.s_addr = addr.s_addr;
84 hp->next = hosts;
85 hosts = hp;
86}
87
ce0e08e2
PA
88static int
89rusers_reply(caddr_t replyp, struct sockaddr_in *raddrp)
984263bc 90{
ce0e08e2
PA
91 u_int x;
92 int idle;
984263bc
MD
93 char date[32], idle_time[64], remote[64];
94 struct hostent *hp;
ce0e08e2 95 utmpidlearr *up, u;
984263bc
MD
96 char *host;
97 int days, hours, minutes, seconds;
98
ce0e08e2
PA
99 up = &u;
100 memcpy(up, replyp, sizeof(*up));
984263bc
MD
101 if (search_host(raddrp->sin_addr))
102 return (0);
103
104 if (!allopt && up->utmpidlearr_len == 0)
105 return (0);
106
15b85273
SW
107 hp = gethostbyaddr(&raddrp->sin_addr.s_addr, sizeof(struct in_addr),
108 AF_INET);
984263bc
MD
109 if (hp != NULL)
110 host = hp->h_name;
111 else
112 host = inet_ntoa(raddrp->sin_addr);
113
114 if (!longopt)
115 printf("%-*s ", HOST_WIDTH, host);
116
117 for (x = 0; x < up->utmpidlearr_len; x++) {
118 strncpy(date,
119 &(ctime((time_t *)&(up->utmpidlearr_val[x].ui_utmp.ut_time))[4]),
120 sizeof(date) - 1);
121
122 idle = up->utmpidlearr_val[x].ui_idle;
123 sprintf(idle_time, " :%02d", idle);
124 if (idle == MAX_INT)
125 strcpy(idle_time, "??");
126 else if (idle == 0)
127 strcpy(idle_time, "");
128 else {
129 seconds = idle;
130 days = seconds / (60 * 60 * 24);
131 seconds %= (60 * 60 * 24);
132 hours = seconds / (60 * 60);
133 seconds %= (60 * 60);
134 minutes = seconds / 60;
135 seconds %= 60;
136 if (idle > 60)
137 sprintf(idle_time, "%d:%02d", minutes, seconds);
138 if (idle >= (60 * 60))
139 sprintf(idle_time, "%d:%02d:%02d",
140 hours, minutes, seconds);
141 if (idle >= (24 * 60 * 60))
142 sprintf(idle_time, "%d days, %d:%02d:%02d",
143 days, hours, minutes, seconds);
144 }
145
146 strncpy(remote, up->utmpidlearr_val[x].ui_utmp.ut_host,
147 sizeof(remote) - 1);
148 if (strlen(remote) != 0)
149 sprintf(remote, "(%.16s)",
150 up->utmpidlearr_val[x].ui_utmp.ut_host);
151
152 if (longopt)
153 printf("%-8.8s %*s:%-*.*s %-12.12s %6s %.18s\n",
154 up->utmpidlearr_val[x].ui_utmp.ut_name,
155 HOST_WIDTH, host, LINE_WIDTH, LINE_WIDTH,
156 up->utmpidlearr_val[x].ui_utmp.ut_line, date,
157 idle_time, remote );
158 else
159 printf("%s ",
160 up->utmpidlearr_val[x].ui_utmp.ut_name);
161 }
162 if (!longopt)
163 putchar('\n');
164
165 remember_host(raddrp->sin_addr);
166 return (0);
167}
168
ce0e08e2 169static void
984263bc
MD
170onehost(char *host)
171{
172 utmpidlearr up;
173 CLIENT *rusers_clnt;
174 struct sockaddr_in addr;
175 struct hostent *hp;
176 struct timeval tv;
177
178 hp = gethostbyname(host);
179 if (hp == NULL)
180 errx(1, "unknown host \"%s\"", host);
181
182 rusers_clnt = clnt_create(host, RUSERSPROG, RUSERSVERS_IDLE, "udp");
183 if (rusers_clnt == NULL)
184 errx(1, "%s", clnt_spcreateerror(""));
185
186 bzero((char *)&up, sizeof(up));
187 tv.tv_sec = 15; /* XXX ?? */
188 tv.tv_usec = 0;
ce0e08e2
PA
189 if (clnt_call(rusers_clnt, RUSERSPROC_NAMES, (xdrproc_t)xdr_void, NULL,
190 (xdrproc_t)xdr_utmpidlearr, &up, tv) != RPC_SUCCESS)
984263bc 191 errx(1, "%s", clnt_sperror(rusers_clnt, ""));
ce0e08e2
PA
192 memcpy(&addr.sin_addr.s_addr, hp->h_addr, sizeof(addr.sin_addr.s_addr));
193 rusers_reply((caddr_t)&up, &addr);
984263bc
MD
194 clnt_destroy(rusers_clnt);
195}
196
ce0e08e2 197static void
815943a9 198allhosts(void)
984263bc
MD
199{
200 utmpidlearr up;
201 enum clnt_stat clnt_stat;
202
203 bzero((char *)&up, sizeof(up));
204 clnt_stat = clnt_broadcast(RUSERSPROG, RUSERSVERS_IDLE,
ce0e08e2
PA
205 RUSERSPROC_NAMES, (xdrproc_t)xdr_void, NULL,
206 (xdrproc_t)xdr_utmpidlearr, (char *)&up,
207 (resultproc_t)rusers_reply);
984263bc
MD
208 if (clnt_stat != RPC_SUCCESS && clnt_stat != RPC_TIMEDOUT)
209 errx(1, "%s", clnt_sperrno(clnt_stat));
210}
211
212static void
815943a9 213usage(void)
984263bc
MD
214{
215
ce0e08e2 216 fprintf(stderr, "usage: rusers [-al] [host ...]\n");
984263bc
MD
217 exit(1);
218}
219
220int
221main(int argc, char *argv[])
222{
223 int ch;
224
225 while ((ch = getopt(argc, argv, "al")) != -1)
226 switch (ch) {
227 case 'a':
228 allopt++;
229 break;
230 case 'l':
231 longopt++;
232 break;
233 default:
234 usage();
235 /* NOTREACHED */
236 }
237
238 setlinebuf(stdout);
239 if (argc == optind)
240 allhosts();
241 else {
242 for (; optind < argc; optind++)
ce0e08e2 243 onehost(argv[optind]);
984263bc
MD
244 }
245 exit(0);
246}