1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. 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. Neither the name of the University nor the names of its contributors 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. 28 * 29 * @(#)fingerd.c 8.1 (Berkeley) 6/4/93 30 * $FreeBSD: src/libexec/fingerd/fingerd.c,v 1.26 2008/08/04 01:25:48 cperciva Exp $ 31 * $DragonFly: src/libexec/fingerd/fingerd.c,v 1.3 2003/11/14 03:54:29 dillon Exp $ 32 */ 33 34 #include <sys/types.h> 35 #include <sys/param.h> 36 #include <sys/socket.h> 37 #include <netinet/in.h> 38 #include <netinet/tcp.h> 39 #include <arpa/inet.h> 40 #include <errno.h> 41 42 #include <unistd.h> 43 #include <syslog.h> 44 #include <libutil.h> 45 #include <netdb.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include "pathnames.h" 50 51 void logerr(const char *, ...) __printflike(1, 2) __dead2; 52 53 int 54 main(int argc, char *argv[]) 55 { 56 FILE *fp; 57 int ch; 58 char *lp; 59 struct sockaddr_storage ss; 60 socklen_t sval; 61 int p[2], logging, pflag, secure; 62 #define ENTRIES 50 63 char **ap, *av[ENTRIES + 1], **comp, line[1024], *prog; 64 char rhost[MAXHOSTNAMELEN]; 65 66 prog = _PATH_FINGER; 67 logging = pflag = secure = 0; 68 openlog("fingerd", LOG_PID | LOG_CONS, LOG_DAEMON); 69 opterr = 0; 70 while ((ch = getopt(argc, argv, "lp:s")) != -1) 71 switch (ch) { 72 case 'l': 73 logging = 1; 74 break; 75 case 'p': 76 prog = optarg; 77 pflag = 1; 78 break; 79 case 's': 80 secure = 1; 81 break; 82 case '?': 83 default: 84 logerr("illegal option -- %c", optopt); 85 } 86 87 /* 88 * Enable server-side Transaction TCP. 89 */ 90 { 91 int one = 1; 92 if (setsockopt(STDOUT_FILENO, IPPROTO_TCP, TCP_NOPUSH, &one, 93 sizeof one) < 0) { 94 logerr("setsockopt(TCP_NOPUSH) failed: %m"); 95 } 96 } 97 98 if (!fgets(line, sizeof(line), stdin)) 99 exit(1); 100 101 if (logging || pflag) { 102 sval = sizeof(ss); 103 if (getpeername(0, (struct sockaddr *)&ss, &sval) < 0) 104 logerr("getpeername: %s", strerror(errno)); 105 realhostname_sa(rhost, sizeof rhost - 1, 106 (struct sockaddr *)&ss, sval); 107 rhost[sizeof(rhost) - 1] = '\0'; 108 if (pflag) 109 setenv("FINGERD_REMOTE_HOST", rhost, 1); 110 } 111 112 if (logging) { 113 char *t; 114 char *end; 115 116 end = memchr(line, 0, sizeof(line)); 117 if (end == NULL) { 118 if ((t = malloc(sizeof(line) + 1)) == NULL) 119 logerr("malloc: %s", strerror(errno)); 120 memcpy(t, line, sizeof(line)); 121 t[sizeof(line)] = 0; 122 } else { 123 if ((t = strdup(line)) == NULL) 124 logerr("strdup: %s", strerror(errno)); 125 } 126 for (end = t; *end; end++) 127 if (*end == '\n' || *end == '\r') 128 *end = ' '; 129 syslog(LOG_NOTICE, "query from %s: `%s'", rhost, t); 130 } 131 132 comp = &av[1]; 133 av[2] = "--"; 134 for (lp = line, ap = &av[3];;) { 135 *ap = strtok(lp, " \t\r\n"); 136 if (!*ap) { 137 if (secure && ap == &av[3]) { 138 puts("must provide username\r\n"); 139 exit(1); 140 } 141 break; 142 } 143 if (secure && strchr(*ap, '@')) { 144 puts("forwarding service denied\r\n"); 145 exit(1); 146 } 147 148 /* RFC742: "/[Ww]" == "-l" */ 149 if ((*ap)[0] == '/' && ((*ap)[1] == 'W' || (*ap)[1] == 'w')) { 150 av[1] = "-l"; 151 comp = &av[0]; 152 } 153 else if (++ap == av + ENTRIES) { 154 *ap = NULL; 155 break; 156 } 157 lp = NULL; 158 } 159 160 if ((lp = strrchr(prog, '/')) != NULL) 161 *comp = ++lp; 162 else 163 *comp = prog; 164 if (pipe(p) < 0) 165 logerr("pipe: %s", strerror(errno)); 166 167 switch(vfork()) { 168 case 0: 169 close(p[0]); 170 if (p[1] != STDOUT_FILENO) { 171 dup2(p[1], STDOUT_FILENO); 172 close(p[1]); 173 } 174 dup2(STDOUT_FILENO, STDERR_FILENO); 175 176 execv(prog, comp); 177 write(STDERR_FILENO, prog, strlen(prog)); 178 #define MSG ": cannot execute\n" 179 write(STDERR_FILENO, MSG, strlen(MSG)); 180 #undef MSG 181 _exit(1); 182 case -1: 183 logerr("fork: %s", strerror(errno)); 184 } 185 close(p[1]); 186 if (!(fp = fdopen(p[0], "r"))) 187 logerr("fdopen: %s", strerror(errno)); 188 while ((ch = getc(fp)) != EOF) { 189 if (ch == '\n') 190 putchar('\r'); 191 putchar(ch); 192 } 193 exit(0); 194 } 195 196 #include <stdarg.h> 197 198 void 199 logerr(const char *fmt, ...) 200 { 201 va_list ap; 202 va_start(ap, fmt); 203 vsyslog(LOG_ERR, fmt, ap); 204 va_end(ap); 205 exit(1); 206 /* NOTREACHED */ 207 } 208