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