1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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 #ifndef lint 33 static const char copyright[] = 34 "@(#) Copyright (c) 1983, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"; 36 #endif /* not lint */ 37 38 #ifndef lint 39 #endif /* not lint */ 40 41 #include <sys/types.h> 42 #include <sys/param.h> 43 #include <sys/socket.h> 44 #include <netinet/in.h> 45 #include <netinet/tcp.h> 46 #include <arpa/inet.h> 47 #include <errno.h> 48 49 #include <unistd.h> 50 #include <syslog.h> 51 #include <libutil.h> 52 #include <netdb.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include "pathnames.h" 57 #ifdef USE_BLACKLIST 58 #include <blacklist.h> 59 #endif 60 61 void logerr(const char *, ...) __printflike(1, 2) __dead2; 62 63 int 64 main(int argc, char *argv[]) 65 { 66 FILE *fp; 67 int ch; 68 char *lp; 69 struct sockaddr_storage ss; 70 socklen_t sval; 71 int p[2], debug, kflag, logging, pflag, secure; 72 #define ENTRIES 50 73 char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog; 74 char rhost[MAXHOSTNAMELEN]; 75 76 prog = _PATH_FINGER; 77 debug = logging = kflag = pflag = secure = 0; 78 openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON); 79 opterr = 0; 80 while ((ch = getopt(argc, argv, "dklp:s")) != -1) 81 switch (ch) { 82 case 'd': 83 debug = 1; 84 break; 85 case 'k': 86 kflag = 1; 87 break; 88 case 'l': 89 logging = 1; 90 break; 91 case 'p': 92 prog = optarg; 93 pflag = 1; 94 break; 95 case 's': 96 secure = 1; 97 break; 98 case '?': 99 default: 100 logerr("illegal option -- %c", optopt); 101 } 102 103 /* 104 * Enable server-side Transaction TCP. 105 */ 106 if (!debug) { 107 int one = 1; 108 if (setsockopt(STDOUT_FILENO, IPPROTO_TCP, TCP_NOPUSH, &one, 109 sizeof one) < 0) { 110 logerr("setsockopt(TCP_NOPUSH) failed: %m"); 111 } 112 } 113 114 if (!fgets(line, sizeof(line), stdin)) 115 exit(1); 116 117 if (!debug && (logging || pflag)) { 118 sval = sizeof(ss); 119 if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0) 120 logerr("getpeername: %s", strerror(errno)); 121 realhostname_sa(rhost, sizeof rhost - 1, 122 (struct sockaddr *)&ss, sval); 123 rhost[sizeof(rhost) - 1] = '\0'; 124 if (pflag) 125 setenv("FINGERD_REMOTE_HOST", rhost, 1); 126 } 127 128 if (logging) { 129 char *t; 130 char *end; 131 132 end = memchr(line, 0, sizeof(line)); 133 if (end == NULL) { 134 if ((t = malloc(sizeof(line) + 1)) == NULL) 135 logerr("malloc: %s", strerror(errno)); 136 memcpy(t, line, sizeof(line)); 137 t[sizeof(line)] = 0; 138 } else { 139 if ((t = strdup(line)) == NULL) 140 logerr("strdup: %s", strerror(errno)); 141 } 142 for (end = t; *end; end++) 143 if (*end == '\n' || *end == '\r') 144 *end = ' '; 145 syslog(LOG_NOTICE, "query from %s: `%s'", rhost, t); 146 } 147 148 comp = &av[2]; 149 av[3] = "--"; 150 if (kflag) 151 *comp-- = "-k"; 152 for (lp = line, ap = &av[4];;) { 153 *ap = strtok(lp, " \t\r\n"); 154 if (!*ap) { 155 if (secure && ap == &av[4]) { 156 #ifdef USE_BLACKLIST 157 blacklist(1, STDIN_FILENO, "nousername"); 158 #endif 159 puts("must provide username\r\n"); 160 exit(1); 161 } 162 break; 163 } 164 if (secure && strchr(*ap, '@')) { 165 #ifdef USE_BLACKLIST 166 blacklist(1, STDIN_FILENO, "noforwarding"); 167 #endif 168 puts("forwarding service denied\r\n"); 169 exit(1); 170 } 171 172 /* RFC742: "/[Ww]" == "-l" */ 173 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) { 174 *comp-- = "-l"; 175 } 176 else if (++ap == av + ENTRIES) { 177 *ap = NULL; 178 break; 179 } 180 lp = NULL; 181 } 182 183 if ((lp = strrchr(prog, '/')) != NULL) 184 *comp = ++lp; 185 else 186 *comp = prog; 187 if (pipe(p) < 0) 188 logerr("pipe: %s", strerror(errno)); 189 190 if (debug) { 191 fprintf(stderr, "%s", prog); 192 for (ap = comp; *ap != NULL; ++ap) 193 fprintf(stderr, " %s", *ap); 194 fprintf(stderr, "\n"); 195 } 196 197 switch(vfork()) { 198 case 0: 199 (void)close(p[0]); 200 if (p[1] != STDOUT_FILENO) { 201 (void)dup2(p[1], STDOUT_FILENO); 202 (void)close(p[1]); 203 } 204 dup2(STDOUT_FILENO, STDERR_FILENO); 205 206 #ifdef USE_BLACKLIST 207 blacklist(0, STDIN_FILENO, "success"); 208 #endif 209 execv(prog, comp); 210 write(STDERR_FILENO, prog, strlen(prog)); 211 #define MSG ": cannot execute\n" 212 write(STDERR_FILENO, MSG, strlen(MSG)); 213 #undef MSG 214 _exit(1); 215 case -1: 216 logerr("fork: %s", strerror(errno)); 217 } 218 (void)close(p[1]); 219 if (!(fp = fdopen(p[0], "r"))) 220 logerr("fdopen: %s", strerror(errno)); 221 while ((ch = getc(fp)) != EOF) { 222 if (ch == '\n') 223 putchar('\r'); 224 putchar(ch); 225 } 226 exit(0); 227 } 228 229 #include <stdarg.h> 230 231 void 232 logerr(const char *fmt, ...) 233 { 234 va_list ap; 235 va_start(ap, fmt); 236 (void)vsyslog(LOG_ERR, fmt, ap); 237 va_end(ap); 238 exit(1); 239 /* NOTREACHED */ 240 } 241