1 /* $NetBSD: fingerd.c,v 1.25 2009/03/14 13:59:28 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\ 35 The Regents of the University of California. All rights reserved."); 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "from: @(#)fingerd.c 8.1 (Berkeley) 6/4/93"; 41 #else 42 __RCSID("$NetBSD: fingerd.c,v 1.25 2009/03/14 13:59:28 lukem Exp $"); 43 #endif 44 #endif /* not lint */ 45 46 #include <sys/param.h> 47 #include <sys/socket.h> 48 #include <netinet/in.h> 49 #include <arpa/inet.h> 50 #include <errno.h> 51 52 #include <unistd.h> 53 #include <syslog.h> 54 #include <netdb.h> 55 #include <stdarg.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include "pathnames.h" 60 61 void err(const char *, ...); 62 int main(int, char *[]); 63 64 int 65 main(int argc, char *argv[]) 66 { 67 FILE *fp; 68 int ch, ac = 2; 69 char *lp = NULL /* XXX gcc */; 70 struct sockaddr_storage ss; 71 int p[2], logging, no_forward, user_required, short_list; 72 socklen_t sval; 73 #define ENTRIES 50 74 char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog, *s; 75 #if 0 76 const char *av[ENTRIES + 1], **comp; 77 const char *prog; 78 #endif 79 char hostbuf[MAXHOSTNAMELEN]; 80 81 prog = __UNCONST(_PATH_FINGER); 82 logging = no_forward = user_required = short_list = 0; 83 openlog("fingerd", LOG_PID, LOG_DAEMON); 84 opterr = 0; 85 while ((ch = getopt(argc, argv, "gsluShmpP:8")) != -1) { 86 switch (ch) { 87 case 'l': 88 logging = 1; 89 break; 90 case 'P': 91 prog = optarg; 92 break; 93 case 's': 94 no_forward = 1; 95 break; 96 case 'u': 97 user_required = 1; 98 break; 99 case 'S': 100 short_list = 1; 101 av[ac++] = __UNCONST("-s"); 102 break; 103 case 'h': 104 av[ac++] = __UNCONST("-h"); 105 break; 106 case 'm': 107 av[ac++] = __UNCONST("-m"); 108 break; 109 case 'p': 110 av[ac++] = __UNCONST("-p"); 111 break; 112 case 'g': 113 av[ac++] = __UNCONST("-g"); 114 break; 115 case '8': 116 av[ac++] = __UNCONST("-8"); 117 break; 118 case '?': 119 default: 120 err("illegal option -- %c", optopt); 121 } 122 if (ac >= ENTRIES) 123 err("Too many options provided"); 124 } 125 126 127 if (logging) { 128 sval = sizeof(ss); 129 if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0) 130 err("getpeername: %s", strerror(errno)); 131 (void)getnameinfo((struct sockaddr *)&ss, sval, 132 hostbuf, sizeof(hostbuf), NULL, 0, 0); 133 lp = hostbuf; 134 } 135 136 if (!fgets(line, sizeof(line), stdin)) { 137 if (logging) 138 syslog(LOG_NOTICE, "query from %s", lp); 139 exit(1); 140 } 141 while ((s = strrchr(line, '\n')) != NULL || 142 (s = strrchr(line, '\r')) != NULL) 143 *s = '\0'; 144 145 if (logging) { 146 if (*line == '\0') 147 syslog(LOG_NOTICE, "query from %s", lp); 148 else 149 syslog(LOG_NOTICE, "query from %s: %s", lp, line); 150 } 151 152 if (ac >= ENTRIES) 153 err("Too many options provided"); 154 av[ac++] = __UNCONST("--"); 155 comp = &av[1]; 156 for (lp = line, ap = &av[ac]; ac < ENTRIES;) { 157 if ((*ap = strtok(lp, " \t\r\n")) == NULL) 158 break; 159 lp = NULL; 160 if (no_forward && strchr(*ap, '@')) { 161 (void) puts("forwarding service denied\r\n"); 162 exit(1); 163 } 164 165 ch = strlen(*ap); 166 while ((*ap)[ch-1] == '@') 167 (*ap)[--ch] = '\0'; 168 if (**ap == '\0') 169 continue; 170 171 /* RFC1196: "/[Ww]" == "-l" */ 172 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) { 173 if (!short_list) { 174 av[1] = __UNCONST("-l"); 175 comp = &av[0]; 176 } 177 } else { 178 ap++; 179 ac++; 180 } 181 } 182 av[ENTRIES - 1] = NULL; 183 184 if ((lp = strrchr(prog, '/'))) 185 *comp = ++lp; 186 else 187 *comp = prog; 188 189 if (user_required) { 190 for (ap = comp + 1; strcmp("--", *(ap++)); ); 191 if (*ap == NULL) { 192 (void) puts("must provide username\r\n"); 193 exit(1); 194 } 195 } 196 197 if (pipe(p) < 0) 198 err("pipe: %s", strerror(errno)); 199 200 switch(fork()) { 201 case 0: 202 (void) close(p[0]); 203 if (p[1] != 1) { 204 (void) dup2(p[1], 1); 205 (void) close(p[1]); 206 } 207 execv(prog, comp); 208 err("execv: %s: %s", prog, strerror(errno)); 209 _exit(1); 210 case -1: 211 err("fork: %s", strerror(errno)); 212 } 213 (void) close(p[1]); 214 if (!(fp = fdopen(p[0], "r"))) 215 err("fdopen: %s", strerror(errno)); 216 while ((ch = getc(fp)) != EOF) { 217 if (ch == '\n') 218 putchar('\r'); 219 putchar(ch); 220 } 221 exit(0); 222 } 223 224 void 225 err(const char *fmt, ...) 226 { 227 va_list ap; 228 229 va_start(ap, fmt); 230 (void) vsyslog(LOG_ERR, fmt, ap); 231 va_end(ap); 232 exit(1); 233 /* NOTREACHED */ 234 } 235