tftpd.c (65850) | tftpd.c (71616) |
---|---|
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 --- 28 unchanged lines hidden (view full) --- 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)tftpd.c 8.1 (Berkeley) 6/4/93"; 43#endif 44static const char rcsid[] = | 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 --- 28 unchanged lines hidden (view full) --- 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#ifndef lint 41#if 0 42static char sccsid[] = "@(#)tftpd.c 8.1 (Berkeley) 6/4/93"; 43#endif 44static const char rcsid[] = |
45 "$FreeBSD: head/libexec/tftpd/tftpd.c 65850 2000-09-14 19:08:29Z wollman $"; | 45 "$FreeBSD: head/libexec/tftpd/tftpd.c 71616 2001-01-25 04:20:25Z billf $"; |
46#endif /* not lint */ 47 48/* 49 * Trivial file transfer protocol server. 50 * 51 * This version includes many modifications by Jim Guyton 52 * <guyton@rand-unix>. 53 */ --- 47 unchanged lines hidden (view full) --- 101 */ 102#define MAXDIRS 20 103static struct dirlist { 104 char *name; 105 int len; 106} dirs[MAXDIRS+1]; 107static int suppress_naks; 108static int logging; | 46#endif /* not lint */ 47 48/* 49 * Trivial file transfer protocol server. 50 * 51 * This version includes many modifications by Jim Guyton 52 * <guyton@rand-unix>. 53 */ --- 47 unchanged lines hidden (view full) --- 101 */ 102#define MAXDIRS 20 103static struct dirlist { 104 char *name; 105 int len; 106} dirs[MAXDIRS+1]; 107static int suppress_naks; 108static int logging; |
109static int ipchroot; |
|
109 110static char *errtomsg __P((int)); 111static void nak __P((int)); 112 113int 114main(argc, argv) 115 int argc; 116 char *argv[]; 117{ 118 register struct tftphdr *tp; 119 register int n; 120 int ch, on; 121 struct sockaddr_in sin; 122 char *chroot_dir = NULL; 123 struct passwd *nobody; 124 char *chuser = "nobody"; 125 126 openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_FTP); | 110 111static char *errtomsg __P((int)); 112static void nak __P((int)); 113 114int 115main(argc, argv) 116 int argc; 117 char *argv[]; 118{ 119 register struct tftphdr *tp; 120 register int n; 121 int ch, on; 122 struct sockaddr_in sin; 123 char *chroot_dir = NULL; 124 struct passwd *nobody; 125 char *chuser = "nobody"; 126 127 openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_FTP); |
127 while ((ch = getopt(argc, argv, "lns:u:")) != -1) { | 128 while ((ch = getopt(argc, argv, "cClns:u:")) != -1) { |
128 switch (ch) { | 129 switch (ch) { |
130 case 'c': 131 ipchroot = 1; 132 break; 133 case 'C': 134 ipchroot = 2; 135 break; |
|
129 case 'l': 130 logging = 1; 131 break; 132 case 'n': 133 suppress_naks = 1; 134 break; 135 case 's': 136 chroot_dir = optarg; --- 17 unchanged lines hidden (view full) --- 154 dirp++; 155 } 156 } 157 } 158 else if (chroot_dir) { 159 dirs->name = "/"; 160 dirs->len = 1; 161 } | 136 case 'l': 137 logging = 1; 138 break; 139 case 'n': 140 suppress_naks = 1; 141 break; 142 case 's': 143 chroot_dir = optarg; --- 17 unchanged lines hidden (view full) --- 161 dirp++; 162 } 163 } 164 } 165 else if (chroot_dir) { 166 dirs->name = "/"; 167 dirs->len = 1; 168 } |
169 if (ipchroot && chroot_dir == NULL) { 170 syslog(LOG_ERR, "-c requires -s"); 171 exit(1); 172 } |
|
162 163 on = 1; 164 if (ioctl(0, FIONBIO, &on) < 0) { 165 syslog(LOG_ERR, "ioctl(FIONBIO): %m"); 166 exit(1); 167 } 168 fromlen = sizeof (from); 169 n = recvfrom(0, buf, sizeof (buf), 0, --- 54 unchanged lines hidden (view full) --- 224 } 225 226 /* 227 * Since we exit here, we should do that only after the above 228 * recvfrom to keep inetd from constantly forking should there 229 * be a problem. See the above comment about system clogging. 230 */ 231 if (chroot_dir) { | 173 174 on = 1; 175 if (ioctl(0, FIONBIO, &on) < 0) { 176 syslog(LOG_ERR, "ioctl(FIONBIO): %m"); 177 exit(1); 178 } 179 fromlen = sizeof (from); 180 n = recvfrom(0, buf, sizeof (buf), 0, --- 54 unchanged lines hidden (view full) --- 235 } 236 237 /* 238 * Since we exit here, we should do that only after the above 239 * recvfrom to keep inetd from constantly forking should there 240 * be a problem. See the above comment about system clogging. 241 */ 242 if (chroot_dir) { |
243 if (ipchroot) { 244 char *tempchroot; 245 struct stat sb; 246 int statret; 247 248 tempchroot = inet_ntoa(from.sin_addr); 249 asprintf(&tempchroot, "%s/%s", chroot_dir, tempchroot); 250 statret = stat(tempchroot, &sb); 251 if ((sb.st_mode & S_IFDIR) && 252 (statret == 0 || (statret == -1 && ipchroot == 1))) 253 chroot_dir = tempchroot; 254 } |
|
232 /* Must get this before chroot because /etc might go away */ 233 if ((nobody = getpwnam(chuser)) == NULL) { 234 syslog(LOG_ERR, "%s: no such user", chuser); 235 exit(1); 236 } 237 if (chroot(chroot_dir)) { 238 syslog(LOG_ERR, "chroot: %s: %m", chroot_dir); 239 exit(1); --- 440 unchanged lines hidden --- | 255 /* Must get this before chroot because /etc might go away */ 256 if ((nobody = getpwnam(chuser)) == NULL) { 257 syslog(LOG_ERR, "%s: no such user", chuser); 258 exit(1); 259 } 260 if (chroot(chroot_dir)) { 261 syslog(LOG_ERR, "chroot: %s: %m", chroot_dir); 262 exit(1); --- 440 unchanged lines hidden --- |