Index: head/libexec/ftpd/extern.h =================================================================== --- head/libexec/ftpd/extern.h (revision 299355) +++ head/libexec/ftpd/extern.h (revision 299356) @@ -1,114 +1,114 @@ /*- * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)extern.h 8.2 (Berkeley) 4/4/94 * $FreeBSD$ */ #include #include void blkfree(char **); char **copyblk(char **); void cwd(char *); void delete(char *); void dologout(int); void fatalerror(char *); void ftpd_logwtmp(char *, char *, struct sockaddr *addr); int ftpd_pclose(FILE *); FILE *ftpd_popen(char *, char *); -int getline(char *, int, FILE *); +int get_line(char *, int, FILE *); void lreply(int, const char *, ...) __printflike(2, 3); void makedir(char *); void nack(char *); void pass(char *); void passive(void); void long_passive(char *, int); void perror_reply(int, char *); void pwd(void); void removedir(char *); void renamecmd(char *, char *); char *renamefrom(char *); void reply(int, const char *, ...) __printflike(2, 3); void retrieve(char *, char *); void send_file_list(char *); #ifdef OLD_SETPROCTITLE void setproctitle(const char *, ...); #endif void statcmd(void); void statfilecmd(char *); void store(char *, char *, int); void upper(char *); void user(char *); void yyerror(char *); int yyparse(void); int ls_main(int, char **); extern int assumeutf8; extern char cbuf[]; extern union sockunion data_dest; extern int epsvall; extern int form; extern int ftpdebug; extern int guest; extern union sockunion his_addr; extern char *homedir; extern int hostinfo; extern char *hostname; extern int maxtimeout; extern int logged_in; extern int logging; extern int noepsv; extern int noguestretr; extern int noretr; extern int paranoid; extern struct passwd *pw; extern int pdata; extern char proctitle[]; extern int readonly; extern off_t restart_point; extern int timeout; extern char tmpline[]; extern int type; extern char *typenames[]; /* defined in included from ftpd.c */ extern int usedefault; struct sockaddr_in; struct sockaddr_in6; union sockunion { struct sockinet { u_char si_len; u_char si_family; u_short si_port; } su_si; struct sockaddr_in su_sin; struct sockaddr_in6 su_sin6; }; #define su_len su_si.si_len #define su_family su_si.si_family #define su_port su_si.si_port Index: head/libexec/ftpd/ftpcmd.y =================================================================== --- head/libexec/ftpd/ftpcmd.y (revision 299355) +++ head/libexec/ftpd/ftpcmd.y (revision 299356) @@ -1,1813 +1,1813 @@ /* * Copyright (c) 1985, 1988, 1993, 1994 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#)ftpcmd.y 8.3 (Berkeley) 4/6/94 */ /* * Grammar for FTP commands. * See RFC 959. */ %{ #ifndef lint #if 0 static char sccsid[] = "@(#)ftpcmd.y 8.3 (Berkeley) 4/6/94"; #endif #endif /* not lint */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "extern.h" #include "pathnames.h" off_t restart_point; static int cmd_type; static int cmd_form; static int cmd_bytesz; static int state; char cbuf[512]; char *fromname = NULL; %} %union { struct { off_t o; int i; } u; char *s; } %token A B C E F I L N P R S T ALL SP CRLF COMMA USER PASS ACCT REIN QUIT PORT PASV TYPE STRU MODE RETR STOR APPE MLFL MAIL MSND MSOM MSAM MRSQ MRCP ALLO REST RNFR RNTO ABOR DELE CWD LIST NLST SITE STAT HELP NOOP MKD RMD PWD CDUP STOU SMNT SYST SIZE MDTM LPRT LPSV EPRT EPSV FEAT UMASK IDLE CHMOD MDFIVE LEXERR NOTIMPL %token STRING %token NUMBER %type check_login octal_number byte_size %type check_login_ro check_login_epsv %type struct_code mode_code type_code form_code %type pathstring pathname password username %type ALL NOTIMPL %start cmd_list %% cmd_list : /* empty */ | cmd_list cmd { if (fromname) free(fromname); fromname = NULL; restart_point = 0; } | cmd_list rcmd ; cmd : USER SP username CRLF { user($3); free($3); } | PASS SP password CRLF { pass($3); free($3); } | PASS CRLF { pass(""); } | PORT check_login SP host_port CRLF { if (epsvall) { reply(501, "No PORT allowed after EPSV ALL."); goto port_done; } if (!$2) goto port_done; if (port_check("PORT") == 1) goto port_done; #ifdef INET6 if ((his_addr.su_family != AF_INET6 || !IN6_IS_ADDR_V4MAPPED(&his_addr.su_sin6.sin6_addr))) { /* shoud never happen */ usedefault = 1; reply(500, "Invalid address rejected."); goto port_done; } port_check_v6("pcmd"); #endif port_done: ; } | LPRT check_login SP host_long_port CRLF { if (epsvall) { reply(501, "No LPRT allowed after EPSV ALL."); goto lprt_done; } if (!$2) goto lprt_done; if (port_check("LPRT") == 1) goto lprt_done; #ifdef INET6 if (his_addr.su_family != AF_INET6) { usedefault = 1; reply(500, "Invalid address rejected."); goto lprt_done; } if (port_check_v6("LPRT") == 1) goto lprt_done; #endif lprt_done: ; } | EPRT check_login SP STRING CRLF { char delim; char *tmp = NULL; char *p, *q; char *result[3]; struct addrinfo hints; struct addrinfo *res; int i; if (epsvall) { reply(501, "No EPRT allowed after EPSV ALL."); goto eprt_done; } if (!$2) goto eprt_done; memset(&data_dest, 0, sizeof(data_dest)); tmp = strdup($4); if (ftpdebug) syslog(LOG_DEBUG, "%s", tmp); if (!tmp) { fatalerror("not enough core"); /*NOTREACHED*/ } p = tmp; delim = p[0]; p++; memset(result, 0, sizeof(result)); for (i = 0; i < 3; i++) { q = strchr(p, delim); if (!q || *q != delim) { parsefail: reply(500, "Invalid argument, rejected."); if (tmp) free(tmp); usedefault = 1; goto eprt_done; } *q++ = '\0'; result[i] = p; if (ftpdebug) syslog(LOG_DEBUG, "%d: %s", i, p); p = q; } /* some more sanity check */ p = result[0]; while (*p) { if (!isdigit(*p)) goto parsefail; p++; } p = result[2]; while (*p) { if (!isdigit(*p)) goto parsefail; p++; } /* grab address */ memset(&hints, 0, sizeof(hints)); if (atoi(result[0]) == 1) hints.ai_family = PF_INET; #ifdef INET6 else if (atoi(result[0]) == 2) hints.ai_family = PF_INET6; #endif else hints.ai_family = PF_UNSPEC; /*XXX*/ hints.ai_socktype = SOCK_STREAM; i = getaddrinfo(result[1], result[2], &hints, &res); if (i) goto parsefail; memcpy(&data_dest, res->ai_addr, res->ai_addrlen); #ifdef INET6 if (his_addr.su_family == AF_INET6 && data_dest.su_family == AF_INET6) { /* XXX more sanity checks! */ data_dest.su_sin6.sin6_scope_id = his_addr.su_sin6.sin6_scope_id; } #endif free(tmp); tmp = NULL; if (port_check("EPRT") == 1) goto eprt_done; #ifdef INET6 if (his_addr.su_family != AF_INET6) { usedefault = 1; reply(500, "Invalid address rejected."); goto eprt_done; } if (port_check_v6("EPRT") == 1) goto eprt_done; #endif eprt_done: free($4); } | PASV check_login CRLF { if (epsvall) reply(501, "No PASV allowed after EPSV ALL."); else if ($2) passive(); } | LPSV check_login CRLF { if (epsvall) reply(501, "No LPSV allowed after EPSV ALL."); else if ($2) long_passive("LPSV", PF_UNSPEC); } | EPSV check_login_epsv SP NUMBER CRLF { if ($2) { int pf; switch ($4.i) { case 1: pf = PF_INET; break; #ifdef INET6 case 2: pf = PF_INET6; break; #endif default: pf = -1; /*junk value*/ break; } long_passive("EPSV", pf); } } | EPSV check_login_epsv SP ALL CRLF { if ($2) { reply(200, "EPSV ALL command successful."); epsvall++; } } | EPSV check_login_epsv CRLF { if ($2) long_passive("EPSV", PF_UNSPEC); } | TYPE check_login SP type_code CRLF { if ($2) { switch (cmd_type) { case TYPE_A: if (cmd_form == FORM_N) { reply(200, "Type set to A."); type = cmd_type; form = cmd_form; } else reply(504, "Form must be N."); break; case TYPE_E: reply(504, "Type E not implemented."); break; case TYPE_I: reply(200, "Type set to I."); type = cmd_type; break; case TYPE_L: #if CHAR_BIT == 8 if (cmd_bytesz == 8) { reply(200, "Type set to L (byte size 8)."); type = cmd_type; } else reply(504, "Byte size must be 8."); #else /* CHAR_BIT == 8 */ UNIMPLEMENTED for CHAR_BIT != 8 #endif /* CHAR_BIT == 8 */ } } } | STRU check_login SP struct_code CRLF { if ($2) { switch ($4) { case STRU_F: reply(200, "STRU F accepted."); break; default: reply(504, "Unimplemented STRU type."); } } } | MODE check_login SP mode_code CRLF { if ($2) { switch ($4) { case MODE_S: reply(200, "MODE S accepted."); break; default: reply(502, "Unimplemented MODE type."); } } } | ALLO check_login SP NUMBER CRLF { if ($2) { reply(202, "ALLO command ignored."); } } | ALLO check_login SP NUMBER SP R SP NUMBER CRLF { if ($2) { reply(202, "ALLO command ignored."); } } | RETR check_login SP pathname CRLF { if (noretr || (guest && noguestretr)) reply(500, "RETR command disabled."); else if ($2 && $4 != NULL) retrieve(NULL, $4); if ($4 != NULL) free($4); } | STOR check_login_ro SP pathname CRLF { if ($2 && $4 != NULL) store($4, "w", 0); if ($4 != NULL) free($4); } | APPE check_login_ro SP pathname CRLF { if ($2 && $4 != NULL) store($4, "a", 0); if ($4 != NULL) free($4); } | NLST check_login CRLF { if ($2) send_file_list("."); } | NLST check_login SP pathstring CRLF { if ($2) send_file_list($4); free($4); } | LIST check_login CRLF { if ($2) retrieve(_PATH_LS " -lgA", ""); } | LIST check_login SP pathstring CRLF { if ($2) retrieve(_PATH_LS " -lgA %s", $4); free($4); } | STAT check_login SP pathname CRLF { if ($2 && $4 != NULL) statfilecmd($4); if ($4 != NULL) free($4); } | STAT check_login CRLF { if ($2) { statcmd(); } } | DELE check_login_ro SP pathname CRLF { if ($2 && $4 != NULL) delete($4); if ($4 != NULL) free($4); } | RNTO check_login_ro SP pathname CRLF { if ($2 && $4 != NULL) { if (fromname) { renamecmd(fromname, $4); free(fromname); fromname = NULL; } else { reply(503, "Bad sequence of commands."); } } if ($4 != NULL) free($4); } | ABOR check_login CRLF { if ($2) reply(225, "ABOR command successful."); } | CWD check_login CRLF { if ($2) { cwd(homedir); } } | CWD check_login SP pathname CRLF { if ($2 && $4 != NULL) cwd($4); if ($4 != NULL) free($4); } | HELP CRLF { help(cmdtab, NULL); } | HELP SP STRING CRLF { char *cp = $3; if (strncasecmp(cp, "SITE", 4) == 0) { cp = $3 + 4; if (*cp == ' ') cp++; if (*cp) help(sitetab, cp); else help(sitetab, NULL); } else help(cmdtab, $3); free($3); } | NOOP CRLF { reply(200, "NOOP command successful."); } | MKD check_login_ro SP pathname CRLF { if ($2 && $4 != NULL) makedir($4); if ($4 != NULL) free($4); } | RMD check_login_ro SP pathname CRLF { if ($2 && $4 != NULL) removedir($4); if ($4 != NULL) free($4); } | PWD check_login CRLF { if ($2) pwd(); } | CDUP check_login CRLF { if ($2) cwd(".."); } | SITE SP HELP CRLF { help(sitetab, NULL); } | SITE SP HELP SP STRING CRLF { help(sitetab, $5); free($5); } | SITE SP MDFIVE check_login SP pathname CRLF { char p[64], *q; if ($4 && $6) { q = MD5File($6, p); if (q != NULL) reply(200, "MD5(%s) = %s", $6, p); else perror_reply(550, $6); } if ($6) free($6); } | SITE SP UMASK check_login CRLF { int oldmask; if ($4) { oldmask = umask(0); (void) umask(oldmask); reply(200, "Current UMASK is %03o.", oldmask); } } | SITE SP UMASK check_login SP octal_number CRLF { int oldmask; if ($4) { if (($6 == -1) || ($6 > 0777)) { reply(501, "Bad UMASK value."); } else { oldmask = umask($6); reply(200, "UMASK set to %03o (was %03o).", $6, oldmask); } } } | SITE SP CHMOD check_login_ro SP octal_number SP pathname CRLF { if ($4 && ($8 != NULL)) { if (($6 == -1 ) || ($6 > 0777)) reply(501, "Bad mode value."); else if (chmod($8, $6) < 0) perror_reply(550, $8); else reply(200, "CHMOD command successful."); } if ($8 != NULL) free($8); } | SITE SP check_login IDLE CRLF { if ($3) reply(200, "Current IDLE time limit is %d seconds; max %d.", timeout, maxtimeout); } | SITE SP check_login IDLE SP NUMBER CRLF { if ($3) { if ($6.i < 30 || $6.i > maxtimeout) { reply(501, "Maximum IDLE time must be between 30 and %d seconds.", maxtimeout); } else { timeout = $6.i; (void) alarm(timeout); reply(200, "Maximum IDLE time set to %d seconds.", timeout); } } } | STOU check_login_ro SP pathname CRLF { if ($2 && $4 != NULL) store($4, "w", 1); if ($4 != NULL) free($4); } | FEAT CRLF { lreply(211, "Extensions supported:"); #if 0 /* XXX these two keywords are non-standard */ printf(" EPRT\r\n"); if (!noepsv) printf(" EPSV\r\n"); #endif printf(" MDTM\r\n"); printf(" REST STREAM\r\n"); printf(" SIZE\r\n"); if (assumeutf8) { /* TVFS requires UTF8, see RFC 3659 */ printf(" TVFS\r\n"); printf(" UTF8\r\n"); } reply(211, "End."); } | SYST check_login CRLF { if ($2) { if (hostinfo) #ifdef BSD reply(215, "UNIX Type: L%d Version: BSD-%d", CHAR_BIT, BSD); #else /* BSD */ reply(215, "UNIX Type: L%d", CHAR_BIT); #endif /* BSD */ else reply(215, "UNKNOWN Type: L%d", CHAR_BIT); } } /* * SIZE is not in RFC959, but Postel has blessed it and * it will be in the updated RFC. * * Return size of file in a format suitable for * using with RESTART (we just count bytes). */ | SIZE check_login SP pathname CRLF { if ($2 && $4 != NULL) sizecmd($4); if ($4 != NULL) free($4); } /* * MDTM is not in RFC959, but Postel has blessed it and * it will be in the updated RFC. * * Return modification time of file as an ISO 3307 * style time. E.g. YYYYMMDDHHMMSS or YYYYMMDDHHMMSS.xxx * where xxx is the fractional second (of any precision, * not necessarily 3 digits) */ | MDTM check_login SP pathname CRLF { if ($2 && $4 != NULL) { struct stat stbuf; if (stat($4, &stbuf) < 0) perror_reply(550, $4); else if (!S_ISREG(stbuf.st_mode)) { reply(550, "%s: not a plain file.", $4); } else { struct tm *t; t = gmtime(&stbuf.st_mtime); reply(213, "%04d%02d%02d%02d%02d%02d", 1900 + t->tm_year, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); } } if ($4 != NULL) free($4); } | QUIT CRLF { reply(221, "Goodbye."); dologout(0); } | NOTIMPL { nack($1); } | error { yyclearin; /* discard lookahead data */ yyerrok; /* clear error condition */ state = CMD; /* reset lexer state */ } ; rcmd : RNFR check_login_ro SP pathname CRLF { restart_point = 0; if ($2 && $4) { if (fromname) free(fromname); fromname = NULL; if (renamefrom($4)) fromname = $4; else free($4); } else if ($4) { free($4); } } | REST check_login SP NUMBER CRLF { if ($2) { if (fromname) free(fromname); fromname = NULL; restart_point = $4.o; reply(350, "Restarting at %jd. %s", (intmax_t)restart_point, "Send STORE or RETRIEVE to initiate transfer."); } } ; username : STRING ; password : /* empty */ { $$ = (char *)calloc(1, sizeof(char)); } | STRING ; byte_size : NUMBER { $$ = $1.i; } ; host_port : NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER { char *a, *p; data_dest.su_len = sizeof(struct sockaddr_in); data_dest.su_family = AF_INET; p = (char *)&data_dest.su_sin.sin_port; p[0] = $9.i; p[1] = $11.i; a = (char *)&data_dest.su_sin.sin_addr; a[0] = $1.i; a[1] = $3.i; a[2] = $5.i; a[3] = $7.i; } ; host_long_port : NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER { char *a, *p; memset(&data_dest, 0, sizeof(data_dest)); data_dest.su_len = sizeof(struct sockaddr_in6); data_dest.su_family = AF_INET6; p = (char *)&data_dest.su_port; p[0] = $39.i; p[1] = $41.i; a = (char *)&data_dest.su_sin6.sin6_addr; a[0] = $5.i; a[1] = $7.i; a[2] = $9.i; a[3] = $11.i; a[4] = $13.i; a[5] = $15.i; a[6] = $17.i; a[7] = $19.i; a[8] = $21.i; a[9] = $23.i; a[10] = $25.i; a[11] = $27.i; a[12] = $29.i; a[13] = $31.i; a[14] = $33.i; a[15] = $35.i; if (his_addr.su_family == AF_INET6) { /* XXX more sanity checks! */ data_dest.su_sin6.sin6_scope_id = his_addr.su_sin6.sin6_scope_id; } if ($1.i != 6 || $3.i != 16 || $37.i != 2) memset(&data_dest, 0, sizeof(data_dest)); } | NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER COMMA NUMBER { char *a, *p; memset(&data_dest, 0, sizeof(data_dest)); data_dest.su_sin.sin_len = sizeof(struct sockaddr_in); data_dest.su_family = AF_INET; p = (char *)&data_dest.su_port; p[0] = $15.i; p[1] = $17.i; a = (char *)&data_dest.su_sin.sin_addr; a[0] = $5.i; a[1] = $7.i; a[2] = $9.i; a[3] = $11.i; if ($1.i != 4 || $3.i != 4 || $13.i != 2) memset(&data_dest, 0, sizeof(data_dest)); } ; form_code : N { $$ = FORM_N; } | T { $$ = FORM_T; } | C { $$ = FORM_C; } ; type_code : A { cmd_type = TYPE_A; cmd_form = FORM_N; } | A SP form_code { cmd_type = TYPE_A; cmd_form = $3; } | E { cmd_type = TYPE_E; cmd_form = FORM_N; } | E SP form_code { cmd_type = TYPE_E; cmd_form = $3; } | I { cmd_type = TYPE_I; } | L { cmd_type = TYPE_L; cmd_bytesz = CHAR_BIT; } | L SP byte_size { cmd_type = TYPE_L; cmd_bytesz = $3; } /* this is for a bug in the BBN ftp */ | L byte_size { cmd_type = TYPE_L; cmd_bytesz = $2; } ; struct_code : F { $$ = STRU_F; } | R { $$ = STRU_R; } | P { $$ = STRU_P; } ; mode_code : S { $$ = MODE_S; } | B { $$ = MODE_B; } | C { $$ = MODE_C; } ; pathname : pathstring { if (logged_in && $1) { char *p; /* * Expand ~user manually since glob(3) * will return the unexpanded pathname * if the corresponding file/directory * doesn't exist yet. Using sole glob(3) * would break natural commands like * MKD ~user/newdir * or * RNTO ~/newfile */ if ((p = exptilde($1)) != NULL) { $$ = expglob(p); free(p); } else $$ = NULL; free($1); } else $$ = $1; } ; pathstring : STRING ; octal_number : NUMBER { int ret, dec, multby, digit; /* * Convert a number that was read as decimal number * to what it would be if it had been read as octal. */ dec = $1.i; multby = 1; ret = 0; while (dec) { digit = dec%10; if (digit > 7) { ret = -1; break; } ret += digit * multby; multby *= 8; dec /= 10; } $$ = ret; } ; check_login : /* empty */ { $$ = check_login1(); } ; check_login_epsv : /* empty */ { if (noepsv) { reply(500, "EPSV command disabled."); $$ = 0; } else $$ = check_login1(); } ; check_login_ro : /* empty */ { if (readonly) { reply(550, "Permission denied."); $$ = 0; } else $$ = check_login1(); } ; %% #define CMD 0 /* beginning of command */ #define ARGS 1 /* expect miscellaneous arguments */ #define STR1 2 /* expect SP followed by STRING */ #define STR2 3 /* expect STRING */ #define OSTR 4 /* optional SP then STRING */ #define ZSTR1 5 /* optional SP then optional STRING */ #define ZSTR2 6 /* optional STRING after SP */ #define SITECMD 7 /* SITE command */ #define NSTR 8 /* Number followed by a string */ #define MAXGLOBARGS 1000 #define MAXASIZE 10240 /* Deny ASCII SIZE on files larger than that */ struct tab { char *name; short token; short state; short implemented; /* 1 if command is implemented */ char *help; }; struct tab cmdtab[] = { /* In order defined in RFC 765 */ { "USER", USER, STR1, 1, " username" }, { "PASS", PASS, ZSTR1, 1, "[ [password]]" }, { "ACCT", ACCT, STR1, 0, "(specify account)" }, { "SMNT", SMNT, ARGS, 0, "(structure mount)" }, { "REIN", REIN, ARGS, 0, "(reinitialize server state)" }, { "QUIT", QUIT, ARGS, 1, "(terminate service)", }, { "PORT", PORT, ARGS, 1, " b0, b1, b2, b3, b4, b5" }, { "LPRT", LPRT, ARGS, 1, " af, hal, h1, h2, h3,..., pal, p1, p2..." }, { "EPRT", EPRT, STR1, 1, " |af|addr|port|" }, { "PASV", PASV, ARGS, 1, "(set server in passive mode)" }, { "LPSV", LPSV, ARGS, 1, "(set server in passive mode)" }, { "EPSV", EPSV, ARGS, 1, "[ af|ALL]" }, { "TYPE", TYPE, ARGS, 1, " { A | E | I | L }" }, { "STRU", STRU, ARGS, 1, "(specify file structure)" }, { "MODE", MODE, ARGS, 1, "(specify transfer mode)" }, { "RETR", RETR, STR1, 1, " file-name" }, { "STOR", STOR, STR1, 1, " file-name" }, { "APPE", APPE, STR1, 1, " file-name" }, { "MLFL", MLFL, OSTR, 0, "(mail file)" }, { "MAIL", MAIL, OSTR, 0, "(mail to user)" }, { "MSND", MSND, OSTR, 0, "(mail send to terminal)" }, { "MSOM", MSOM, OSTR, 0, "(mail send to terminal or mailbox)" }, { "MSAM", MSAM, OSTR, 0, "(mail send to terminal and mailbox)" }, { "MRSQ", MRSQ, OSTR, 0, "(mail recipient scheme question)" }, { "MRCP", MRCP, STR1, 0, "(mail recipient)" }, { "ALLO", ALLO, ARGS, 1, "allocate storage (vacuously)" }, { "REST", REST, ARGS, 1, " offset (restart command)" }, { "RNFR", RNFR, STR1, 1, " file-name" }, { "RNTO", RNTO, STR1, 1, " file-name" }, { "ABOR", ABOR, ARGS, 1, "(abort operation)" }, { "DELE", DELE, STR1, 1, " file-name" }, { "CWD", CWD, OSTR, 1, "[ directory-name ]" }, { "XCWD", CWD, OSTR, 1, "[ directory-name ]" }, { "LIST", LIST, OSTR, 1, "[ path-name ]" }, { "NLST", NLST, OSTR, 1, "[ path-name ]" }, { "SITE", SITE, SITECMD, 1, "site-cmd [ arguments ]" }, { "SYST", SYST, ARGS, 1, "(get type of operating system)" }, { "FEAT", FEAT, ARGS, 1, "(get extended features)" }, { "STAT", STAT, OSTR, 1, "[ path-name ]" }, { "HELP", HELP, OSTR, 1, "[ ]" }, { "NOOP", NOOP, ARGS, 1, "" }, { "MKD", MKD, STR1, 1, " path-name" }, { "XMKD", MKD, STR1, 1, " path-name" }, { "RMD", RMD, STR1, 1, " path-name" }, { "XRMD", RMD, STR1, 1, " path-name" }, { "PWD", PWD, ARGS, 1, "(return current directory)" }, { "XPWD", PWD, ARGS, 1, "(return current directory)" }, { "CDUP", CDUP, ARGS, 1, "(change to parent directory)" }, { "XCUP", CDUP, ARGS, 1, "(change to parent directory)" }, { "STOU", STOU, STR1, 1, " file-name" }, { "SIZE", SIZE, OSTR, 1, " path-name" }, { "MDTM", MDTM, OSTR, 1, " path-name" }, { NULL, 0, 0, 0, 0 } }; struct tab sitetab[] = { { "MD5", MDFIVE, STR1, 1, "[ file-name ]" }, { "UMASK", UMASK, ARGS, 1, "[ umask ]" }, { "IDLE", IDLE, ARGS, 1, "[ maximum-idle-time ]" }, { "CHMOD", CHMOD, NSTR, 1, " mode file-name" }, { "HELP", HELP, OSTR, 1, "[ ]" }, { NULL, 0, 0, 0, 0 } }; static char *copy(char *); static char *expglob(char *); static char *exptilde(char *); static void help(struct tab *, char *); static struct tab * lookup(struct tab *, char *); static int port_check(const char *); #ifdef INET6 static int port_check_v6(const char *); #endif static void sizecmd(char *); static void toolong(int); #ifdef INET6 static void v4map_data_dest(void); #endif static int yylex(void); static struct tab * lookup(struct tab *p, char *cmd) { for (; p->name != NULL; p++) if (strcmp(cmd, p->name) == 0) return (p); return (0); } #include /* - * getline - a hacked up version of fgets to ignore TELNET escape codes. + * get_line - a hacked up version of fgets to ignore TELNET escape codes. */ int -getline(char *s, int n, FILE *iop) +get_line(char *s, int n, FILE *iop) { int c; register char *cs; sigset_t sset, osset; cs = s; /* tmpline may contain saved command from urgent mode interruption */ for (c = 0; tmpline[c] != '\0' && --n > 0; ++c) { *cs++ = tmpline[c]; if (tmpline[c] == '\n') { *cs++ = '\0'; if (ftpdebug) syslog(LOG_DEBUG, "command: %s", s); tmpline[0] = '\0'; return(0); } if (c == 0) tmpline[0] = '\0'; } /* SIGURG would interrupt stdio if not blocked during the read loop */ sigemptyset(&sset); sigaddset(&sset, SIGURG); sigprocmask(SIG_BLOCK, &sset, &osset); while ((c = getc(iop)) != EOF) { c &= 0377; if (c == IAC) { if ((c = getc(iop)) == EOF) goto got_eof; c &= 0377; switch (c) { case WILL: case WONT: if ((c = getc(iop)) == EOF) goto got_eof; printf("%c%c%c", IAC, DONT, 0377&c); (void) fflush(stdout); continue; case DO: case DONT: if ((c = getc(iop)) == EOF) goto got_eof; printf("%c%c%c", IAC, WONT, 0377&c); (void) fflush(stdout); continue; case IAC: break; default: continue; /* ignore command */ } } *cs++ = c; if (--n <= 0) { /* * If command doesn't fit into buffer, discard the * rest of the command and indicate truncation. * This prevents the command to be split up into * multiple commands. */ while (c != '\n' && (c = getc(iop)) != EOF) ; return (-2); } if (c == '\n') break; } got_eof: sigprocmask(SIG_SETMASK, &osset, NULL); if (c == EOF && cs == s) return (-1); *cs++ = '\0'; if (ftpdebug) { if (!guest && strncasecmp("pass ", s, 5) == 0) { /* Don't syslog passwords */ syslog(LOG_DEBUG, "command: %.5s ???", s); } else { register char *cp; register int len; /* Don't syslog trailing CR-LF */ len = strlen(s); cp = s + len - 1; while (cp >= s && (*cp == '\n' || *cp == '\r')) { --cp; --len; } syslog(LOG_DEBUG, "command: %.*s", len, s); } } return (0); } static void toolong(int signo) { reply(421, "Timeout (%d seconds): closing control connection.", timeout); if (logging) syslog(LOG_INFO, "User %s timed out after %d seconds", (pw ? pw -> pw_name : "unknown"), timeout); dologout(1); } static int yylex(void) { static int cpos; char *cp, *cp2; struct tab *p; int n; char c; for (;;) { switch (state) { case CMD: (void) signal(SIGALRM, toolong); (void) alarm(timeout); - n = getline(cbuf, sizeof(cbuf)-1, stdin); + n = get_line(cbuf, sizeof(cbuf)-1, stdin); if (n == -1) { reply(221, "You could at least say goodbye."); dologout(0); } else if (n == -2) { reply(500, "Command too long."); (void) alarm(0); continue; } (void) alarm(0); #ifdef SETPROCTITLE if (strncasecmp(cbuf, "PASS", 4) != 0) setproctitle("%s: %s", proctitle, cbuf); #endif /* SETPROCTITLE */ if ((cp = strchr(cbuf, '\r'))) { *cp++ = '\n'; *cp = '\0'; } if ((cp = strpbrk(cbuf, " \n"))) cpos = cp - cbuf; if (cpos == 0) cpos = 4; c = cbuf[cpos]; cbuf[cpos] = '\0'; upper(cbuf); p = lookup(cmdtab, cbuf); cbuf[cpos] = c; if (p != 0) { yylval.s = p->name; if (!p->implemented) return (NOTIMPL); /* state remains CMD */ state = p->state; return (p->token); } break; case SITECMD: if (cbuf[cpos] == ' ') { cpos++; return (SP); } cp = &cbuf[cpos]; if ((cp2 = strpbrk(cp, " \n"))) cpos = cp2 - cbuf; c = cbuf[cpos]; cbuf[cpos] = '\0'; upper(cp); p = lookup(sitetab, cp); cbuf[cpos] = c; if (guest == 0 && p != 0) { yylval.s = p->name; if (!p->implemented) { state = CMD; return (NOTIMPL); } state = p->state; return (p->token); } state = CMD; break; case ZSTR1: case OSTR: if (cbuf[cpos] == '\n') { state = CMD; return (CRLF); } /* FALLTHROUGH */ case STR1: dostr1: if (cbuf[cpos] == ' ') { cpos++; state = state == OSTR ? STR2 : state+1; return (SP); } break; case ZSTR2: if (cbuf[cpos] == '\n') { state = CMD; return (CRLF); } /* FALLTHROUGH */ case STR2: cp = &cbuf[cpos]; n = strlen(cp); cpos += n - 1; /* * Make sure the string is nonempty and \n terminated. */ if (n > 1 && cbuf[cpos] == '\n') { cbuf[cpos] = '\0'; yylval.s = copy(cp); cbuf[cpos] = '\n'; state = ARGS; return (STRING); } break; case NSTR: if (cbuf[cpos] == ' ') { cpos++; return (SP); } if (isdigit(cbuf[cpos])) { cp = &cbuf[cpos]; while (isdigit(cbuf[++cpos])) ; c = cbuf[cpos]; cbuf[cpos] = '\0'; yylval.u.i = atoi(cp); cbuf[cpos] = c; state = STR1; return (NUMBER); } state = STR1; goto dostr1; case ARGS: if (isdigit(cbuf[cpos])) { cp = &cbuf[cpos]; while (isdigit(cbuf[++cpos])) ; c = cbuf[cpos]; cbuf[cpos] = '\0'; yylval.u.i = atoi(cp); yylval.u.o = strtoull(cp, NULL, 10); cbuf[cpos] = c; return (NUMBER); } if (strncasecmp(&cbuf[cpos], "ALL", 3) == 0 && !isalnum(cbuf[cpos + 3])) { cpos += 3; return ALL; } switch (cbuf[cpos++]) { case '\n': state = CMD; return (CRLF); case ' ': return (SP); case ',': return (COMMA); case 'A': case 'a': return (A); case 'B': case 'b': return (B); case 'C': case 'c': return (C); case 'E': case 'e': return (E); case 'F': case 'f': return (F); case 'I': case 'i': return (I); case 'L': case 'l': return (L); case 'N': case 'n': return (N); case 'P': case 'p': return (P); case 'R': case 'r': return (R); case 'S': case 's': return (S); case 'T': case 't': return (T); } break; default: fatalerror("Unknown state in scanner."); } state = CMD; return (LEXERR); } } void upper(char *s) { while (*s != '\0') { if (islower(*s)) *s = toupper(*s); s++; } } static char * copy(char *s) { char *p; p = malloc(strlen(s) + 1); if (p == NULL) fatalerror("Ran out of memory."); (void) strcpy(p, s); return (p); } static void help(struct tab *ctab, char *s) { struct tab *c; int width, NCMDS; char *type; if (ctab == sitetab) type = "SITE "; else type = ""; width = 0, NCMDS = 0; for (c = ctab; c->name != NULL; c++) { int len = strlen(c->name); if (len > width) width = len; NCMDS++; } width = (width + 8) &~ 7; if (s == 0) { int i, j, w; int columns, lines; lreply(214, "The following %scommands are recognized %s.", type, "(* =>'s unimplemented)"); columns = 76 / width; if (columns == 0) columns = 1; lines = (NCMDS + columns - 1) / columns; for (i = 0; i < lines; i++) { printf(" "); for (j = 0; j < columns; j++) { c = ctab + j * lines + i; printf("%s%c", c->name, c->implemented ? ' ' : '*'); if (c + lines >= &ctab[NCMDS]) break; w = strlen(c->name) + 1; while (w < width) { putchar(' '); w++; } } printf("\r\n"); } (void) fflush(stdout); if (hostinfo) reply(214, "Direct comments to ftp-bugs@%s.", hostname); else reply(214, "End."); return; } upper(s); c = lookup(ctab, s); if (c == NULL) { reply(502, "Unknown command %s.", s); return; } if (c->implemented) reply(214, "Syntax: %s%s %s", type, c->name, c->help); else reply(214, "%s%-*s\t%s; unimplemented.", type, width, c->name, c->help); } static void sizecmd(char *filename) { switch (type) { case TYPE_L: case TYPE_I: { struct stat stbuf; if (stat(filename, &stbuf) < 0) perror_reply(550, filename); else if (!S_ISREG(stbuf.st_mode)) reply(550, "%s: not a plain file.", filename); else reply(213, "%jd", (intmax_t)stbuf.st_size); break; } case TYPE_A: { FILE *fin; int c; off_t count; struct stat stbuf; fin = fopen(filename, "r"); if (fin == NULL) { perror_reply(550, filename); return; } if (fstat(fileno(fin), &stbuf) < 0) { perror_reply(550, filename); (void) fclose(fin); return; } else if (!S_ISREG(stbuf.st_mode)) { reply(550, "%s: not a plain file.", filename); (void) fclose(fin); return; } else if (stbuf.st_size > MAXASIZE) { reply(550, "%s: too large for type A SIZE.", filename); (void) fclose(fin); return; } count = 0; while((c=getc(fin)) != EOF) { if (c == '\n') /* will get expanded to \r\n */ count++; count++; } (void) fclose(fin); reply(213, "%jd", (intmax_t)count); break; } default: reply(504, "SIZE not implemented for type %s.", typenames[type]); } } /* Return 1, if port check is done. Return 0, if not yet. */ static int port_check(const char *pcmd) { if (his_addr.su_family == AF_INET) { if (data_dest.su_family != AF_INET) { usedefault = 1; reply(500, "Invalid address rejected."); return 1; } if (paranoid && ((ntohs(data_dest.su_port) < IPPORT_RESERVED) || memcmp(&data_dest.su_sin.sin_addr, &his_addr.su_sin.sin_addr, sizeof(data_dest.su_sin.sin_addr)))) { usedefault = 1; reply(500, "Illegal PORT range rejected."); } else { usedefault = 0; if (pdata >= 0) { (void) close(pdata); pdata = -1; } reply(200, "%s command successful.", pcmd); } return 1; } return 0; } static int check_login1(void) { if (logged_in) return 1; else { reply(530, "Please login with USER and PASS."); return 0; } } /* * Replace leading "~user" in a pathname by the user's login directory. * Returned string will be in a freshly malloced buffer unless it's NULL. */ static char * exptilde(char *s) { char *p, *q; char *path, *user; struct passwd *ppw; if ((p = strdup(s)) == NULL) return (NULL); if (*p != '~') return (p); user = p + 1; /* skip tilde */ if ((path = strchr(p, '/')) != NULL) *(path++) = '\0'; /* separate ~user from the rest of path */ if (*user == '\0') /* no user specified, use the current user */ user = pw->pw_name; /* read passwd even for the current user since we may be chrooted */ if ((ppw = getpwnam(user)) != NULL) { /* user found, substitute login directory for ~user */ if (path) asprintf(&q, "%s/%s", ppw->pw_dir, path); else q = strdup(ppw->pw_dir); free(p); p = q; } else { /* user not found, undo the damage */ if (path) path[-1] = '/'; } return (p); } /* * Expand glob(3) patterns possibly present in a pathname. * Avoid expanding to a pathname including '\r' or '\n' in order to * not disrupt the FTP protocol. * The expansion found must be unique. * Return the result as a malloced string, or NULL if an error occurred. * * Problem: this production is used for all pathname * processing, but only gives a 550 error reply. * This is a valid reply in some cases but not in others. */ static char * expglob(char *s) { char *p, **pp, *rval; int flags = GLOB_BRACE | GLOB_NOCHECK; int n; glob_t gl; memset(&gl, 0, sizeof(gl)); flags |= GLOB_LIMIT; gl.gl_matchc = MAXGLOBARGS; if (glob(s, flags, NULL, &gl) == 0 && gl.gl_pathc != 0) { for (pp = gl.gl_pathv, p = NULL, n = 0; *pp; pp++) if (*(*pp + strcspn(*pp, "\r\n")) == '\0') { p = *pp; n++; } if (n == 0) rval = strdup(s); else if (n == 1) rval = strdup(p); else { reply(550, "Wildcard is ambiguous."); rval = NULL; } } else { reply(550, "Wildcard expansion error."); rval = NULL; } globfree(&gl); return (rval); } #ifdef INET6 /* Return 1, if port check is done. Return 0, if not yet. */ static int port_check_v6(const char *pcmd) { if (his_addr.su_family == AF_INET6) { if (IN6_IS_ADDR_V4MAPPED(&his_addr.su_sin6.sin6_addr)) /* Convert data_dest into v4 mapped sockaddr.*/ v4map_data_dest(); if (data_dest.su_family != AF_INET6) { usedefault = 1; reply(500, "Invalid address rejected."); return 1; } if (paranoid && ((ntohs(data_dest.su_port) < IPPORT_RESERVED) || memcmp(&data_dest.su_sin6.sin6_addr, &his_addr.su_sin6.sin6_addr, sizeof(data_dest.su_sin6.sin6_addr)))) { usedefault = 1; reply(500, "Illegal PORT range rejected."); } else { usedefault = 0; if (pdata >= 0) { (void) close(pdata); pdata = -1; } reply(200, "%s command successful.", pcmd); } return 1; } return 0; } static void v4map_data_dest(void) { struct in_addr savedaddr; int savedport; if (data_dest.su_family != AF_INET) { usedefault = 1; reply(500, "Invalid address rejected."); return; } savedaddr = data_dest.su_sin.sin_addr; savedport = data_dest.su_port; memset(&data_dest, 0, sizeof(data_dest)); data_dest.su_sin6.sin6_len = sizeof(struct sockaddr_in6); data_dest.su_sin6.sin6_family = AF_INET6; data_dest.su_sin6.sin6_port = savedport; memset((caddr_t)&data_dest.su_sin6.sin6_addr.s6_addr[10], 0xff, 2); memcpy((caddr_t)&data_dest.su_sin6.sin6_addr.s6_addr[12], (caddr_t)&savedaddr, sizeof(savedaddr)); } #endif Index: head/libexec/ftpd/ftpd.c =================================================================== --- head/libexec/ftpd/ftpd.c (revision 299355) +++ head/libexec/ftpd/ftpd.c (revision 299356) @@ -1,3490 +1,3490 @@ /* * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if 0 #ifndef lint static char copyright[] = "@(#) Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #endif #ifndef lint #if 0 static char sccsid[] = "@(#)ftpd.c 8.4 (Berkeley) 4/16/94"; #endif #endif /* not lint */ #include __FBSDID("$FreeBSD$"); /* * FTP server. */ #include #include #include #include #include #include #include #include #include #include #include #define FTP_NAMES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef LOGIN_CAP #include #endif #ifdef USE_PAM #include #endif #include "pathnames.h" #include "extern.h" #include static char version[] = "Version 6.00LS"; #undef main union sockunion ctrl_addr; union sockunion data_source; union sockunion data_dest; union sockunion his_addr; union sockunion pasv_addr; int daemon_mode; int data; int dataport; int hostinfo = 1; /* print host-specific info in messages */ int logged_in; struct passwd *pw; char *homedir; int ftpdebug; int timeout = 900; /* timeout after 15 minutes of inactivity */ int maxtimeout = 7200;/* don't allow idle time to be set beyond 2 hours */ int logging; int restricted_data_ports = 1; int paranoid = 1; /* be extra careful about security */ int anon_only = 0; /* Only anonymous ftp allowed */ int assumeutf8 = 0; /* Assume that server file names are in UTF-8 */ int guest; int dochroot; char *chrootdir; int dowtmp = 1; int stats; int statfd = -1; int type; int form; int stru; /* avoid C keyword */ int mode; int usedefault = 1; /* for data transfers */ int pdata = -1; /* for passive mode */ int readonly = 0; /* Server is in readonly mode. */ int noepsv = 0; /* EPSV command is disabled. */ int noretr = 0; /* RETR command is disabled. */ int noguestretr = 0; /* RETR command is disabled for anon users. */ int noguestmkd = 0; /* MKD command is disabled for anon users. */ int noguestmod = 1; /* anon users may not modify existing files. */ off_t file_size; off_t byte_count; #if !defined(CMASK) || CMASK == 0 #undef CMASK #define CMASK 027 #endif int defumask = CMASK; /* default umask value */ char tmpline[7]; char *hostname; int epsvall = 0; #ifdef VIRTUAL_HOSTING char *ftpuser; static struct ftphost { struct ftphost *next; struct addrinfo *hostinfo; char *hostname; char *anonuser; char *statfile; char *welcome; char *loginmsg; } *thishost, *firsthost; #endif char remotehost[NI_MAXHOST]; char *ident = NULL; static char wtmpid[20]; #ifdef USE_PAM static int auth_pam(struct passwd**, const char*); pam_handle_t *pamh = NULL; #endif static struct opie opiedata; static char opieprompt[OPIE_CHALLENGE_MAX+1]; static int pwok; char *pid_file = NULL; /* means default location to pidfile(3) */ /* * Limit number of pathnames that glob can return. * A limit of 0 indicates the number of pathnames is unlimited. */ #define MAXGLOBARGS 16384 # /* * Timeout intervals for retrying connections * to hosts that don't accept PORT cmds. This * is a kludge, but given the problems with TCP... */ #define SWAITMAX 90 /* wait at most 90 seconds */ #define SWAITINT 5 /* interval between retries */ int swaitmax = SWAITMAX; int swaitint = SWAITINT; #ifdef SETPROCTITLE #ifdef OLD_SETPROCTITLE char **Argv = NULL; /* pointer to argument vector */ char *LastArgv = NULL; /* end of argv */ #endif /* OLD_SETPROCTITLE */ char proctitle[LINE_MAX]; /* initial part of title */ #endif /* SETPROCTITLE */ #define LOGCMD(cmd, file) logcmd((cmd), (file), NULL, -1) #define LOGCMD2(cmd, file1, file2) logcmd((cmd), (file1), (file2), -1) #define LOGBYTES(cmd, file, cnt) logcmd((cmd), (file), NULL, (cnt)) static volatile sig_atomic_t recvurg; static int transflag; /* NB: for debugging only */ #define STARTXFER flagxfer(1) #define ENDXFER flagxfer(0) #define START_UNSAFE maskurg(1) #define END_UNSAFE maskurg(0) /* It's OK to put an `else' clause after this macro. */ #define CHECKOOB(action) \ if (recvurg) { \ recvurg = 0; \ if (myoob()) { \ ENDXFER; \ action; \ } \ } #ifdef VIRTUAL_HOSTING static void inithosts(int); static void selecthost(union sockunion *); #endif static void ack(char *); static void sigurg(int); static void maskurg(int); static void flagxfer(int); static int myoob(void); static int checkuser(char *, char *, int, char **, int *); static FILE *dataconn(char *, off_t, char *); static void dolog(struct sockaddr *); static void end_login(void); static FILE *getdatasock(char *); static int guniquefd(char *, char **); static void lostconn(int); static void sigquit(int); static int receive_data(FILE *, FILE *); static int send_data(FILE *, FILE *, size_t, off_t, int); static struct passwd * sgetpwnam(char *); static char *sgetsave(char *); static void reapchild(int); static void appendf(char **, char *, ...) __printflike(2, 3); static void logcmd(char *, char *, char *, off_t); static void logxfer(char *, off_t, time_t); static char *doublequote(char *); static int *socksetup(int, char *, const char *); int main(int argc, char *argv[], char **envp) { socklen_t addrlen; int ch, on = 1, tos; char *cp, line[LINE_MAX]; FILE *fd; char *bindname = NULL; const char *bindport = "ftp"; int family = AF_UNSPEC; struct sigaction sa; tzset(); /* in case no timezone database in ~ftp */ sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; #ifdef OLD_SETPROCTITLE /* * Save start and extent of argv for setproctitle. */ Argv = argv; while (*envp) envp++; LastArgv = envp[-1] + strlen(envp[-1]); #endif /* OLD_SETPROCTITLE */ /* * Prevent diagnostic messages from appearing on stderr. * We run as a daemon or from inetd; in both cases, there's * more reason in logging to syslog. */ (void) freopen(_PATH_DEVNULL, "w", stderr); opterr = 0; /* * LOG_NDELAY sets up the logging connection immediately, * necessary for anonymous ftp's that chroot and can't do it later. */ openlog("ftpd", LOG_PID | LOG_NDELAY, LOG_FTP); while ((ch = getopt(argc, argv, "468a:AdDEhlmMoOp:P:rRSt:T:u:UvW")) != -1) { switch (ch) { case '4': family = (family == AF_INET6) ? AF_UNSPEC : AF_INET; break; case '6': family = (family == AF_INET) ? AF_UNSPEC : AF_INET6; break; case '8': assumeutf8 = 1; break; case 'a': bindname = optarg; break; case 'A': anon_only = 1; break; case 'd': ftpdebug++; break; case 'D': daemon_mode++; break; case 'E': noepsv = 1; break; case 'h': hostinfo = 0; break; case 'l': logging++; /* > 1 == extra logging */ break; case 'm': noguestmod = 0; break; case 'M': noguestmkd = 1; break; case 'o': noretr = 1; break; case 'O': noguestretr = 1; break; case 'p': pid_file = optarg; break; case 'P': bindport = optarg; break; case 'r': readonly = 1; break; case 'R': paranoid = 0; break; case 'S': stats++; break; case 't': timeout = atoi(optarg); if (maxtimeout < timeout) maxtimeout = timeout; break; case 'T': maxtimeout = atoi(optarg); if (timeout > maxtimeout) timeout = maxtimeout; break; case 'u': { long val = 0; val = strtol(optarg, &optarg, 8); if (*optarg != '\0' || val < 0) syslog(LOG_WARNING, "bad value for -u"); else defumask = val; break; } case 'U': restricted_data_ports = 0; break; case 'v': ftpdebug++; break; case 'W': dowtmp = 0; break; default: syslog(LOG_WARNING, "unknown flag -%c ignored", optopt); break; } } if (daemon_mode) { int *ctl_sock, fd, maxfd = -1, nfds, i; fd_set defreadfds, readfds; pid_t pid; struct pidfh *pfh; if ((pfh = pidfile_open(pid_file, 0600, &pid)) == NULL) { if (errno == EEXIST) { syslog(LOG_ERR, "%s already running, pid %d", getprogname(), (int)pid); exit(1); } syslog(LOG_WARNING, "pidfile_open: %m"); } /* * Detach from parent. */ if (daemon(1, 1) < 0) { syslog(LOG_ERR, "failed to become a daemon"); exit(1); } if (pfh != NULL && pidfile_write(pfh) == -1) syslog(LOG_WARNING, "pidfile_write: %m"); sa.sa_handler = reapchild; (void)sigaction(SIGCHLD, &sa, NULL); #ifdef VIRTUAL_HOSTING inithosts(family); #endif /* * Open a socket, bind it to the FTP port, and start * listening. */ ctl_sock = socksetup(family, bindname, bindport); if (ctl_sock == NULL) exit(1); FD_ZERO(&defreadfds); for (i = 1; i <= *ctl_sock; i++) { FD_SET(ctl_sock[i], &defreadfds); if (listen(ctl_sock[i], 32) < 0) { syslog(LOG_ERR, "control listen: %m"); exit(1); } if (maxfd < ctl_sock[i]) maxfd = ctl_sock[i]; } /* * Loop forever accepting connection requests and forking off * children to handle them. */ while (1) { FD_COPY(&defreadfds, &readfds); nfds = select(maxfd + 1, &readfds, NULL, NULL, 0); if (nfds <= 0) { if (nfds < 0 && errno != EINTR) syslog(LOG_WARNING, "select: %m"); continue; } pid = -1; for (i = 1; i <= *ctl_sock; i++) if (FD_ISSET(ctl_sock[i], &readfds)) { addrlen = sizeof(his_addr); fd = accept(ctl_sock[i], (struct sockaddr *)&his_addr, &addrlen); if (fd == -1) { syslog(LOG_WARNING, "accept: %m"); continue; } switch (pid = fork()) { case 0: /* child */ (void) dup2(fd, 0); (void) dup2(fd, 1); (void) close(fd); for (i = 1; i <= *ctl_sock; i++) close(ctl_sock[i]); if (pfh != NULL) pidfile_close(pfh); goto gotchild; case -1: syslog(LOG_WARNING, "fork: %m"); /* FALLTHROUGH */ default: close(fd); } } } } else { addrlen = sizeof(his_addr); if (getpeername(0, (struct sockaddr *)&his_addr, &addrlen) < 0) { syslog(LOG_ERR, "getpeername (%s): %m",argv[0]); exit(1); } #ifdef VIRTUAL_HOSTING if (his_addr.su_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&his_addr.su_sin6.sin6_addr)) family = AF_INET; else family = his_addr.su_family; inithosts(family); #endif } gotchild: sa.sa_handler = SIG_DFL; (void)sigaction(SIGCHLD, &sa, NULL); sa.sa_handler = sigurg; sa.sa_flags = 0; /* don't restart syscalls for SIGURG */ (void)sigaction(SIGURG, &sa, NULL); sigfillset(&sa.sa_mask); /* block all signals in handler */ sa.sa_flags = SA_RESTART; sa.sa_handler = sigquit; (void)sigaction(SIGHUP, &sa, NULL); (void)sigaction(SIGINT, &sa, NULL); (void)sigaction(SIGQUIT, &sa, NULL); (void)sigaction(SIGTERM, &sa, NULL); sa.sa_handler = lostconn; (void)sigaction(SIGPIPE, &sa, NULL); addrlen = sizeof(ctrl_addr); if (getsockname(0, (struct sockaddr *)&ctrl_addr, &addrlen) < 0) { syslog(LOG_ERR, "getsockname (%s): %m",argv[0]); exit(1); } dataport = ntohs(ctrl_addr.su_port) - 1; /* as per RFC 959 */ #ifdef VIRTUAL_HOSTING /* select our identity from virtual host table */ selecthost(&ctrl_addr); #endif #ifdef IP_TOS if (ctrl_addr.su_family == AF_INET) { tos = IPTOS_LOWDELAY; if (setsockopt(0, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) syslog(LOG_WARNING, "control setsockopt (IP_TOS): %m"); } #endif /* * Disable Nagle on the control channel so that we don't have to wait * for peer's ACK before issuing our next reply. */ if (setsockopt(0, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) syslog(LOG_WARNING, "control setsockopt (TCP_NODELAY): %m"); data_source.su_port = htons(ntohs(ctrl_addr.su_port) - 1); (void)snprintf(wtmpid, sizeof(wtmpid), "%xftpd", getpid()); /* Try to handle urgent data inline */ #ifdef SO_OOBINLINE if (setsockopt(0, SOL_SOCKET, SO_OOBINLINE, &on, sizeof(on)) < 0) syslog(LOG_WARNING, "control setsockopt (SO_OOBINLINE): %m"); #endif #ifdef F_SETOWN if (fcntl(fileno(stdin), F_SETOWN, getpid()) == -1) syslog(LOG_ERR, "fcntl F_SETOWN: %m"); #endif dolog((struct sockaddr *)&his_addr); /* * Set up default state */ data = -1; type = TYPE_A; form = FORM_N; stru = STRU_F; mode = MODE_S; tmpline[0] = '\0'; /* If logins are disabled, print out the message. */ if ((fd = fopen(_PATH_NOLOGIN,"r")) != NULL) { while (fgets(line, sizeof(line), fd) != NULL) { if ((cp = strchr(line, '\n')) != NULL) *cp = '\0'; lreply(530, "%s", line); } (void) fflush(stdout); (void) fclose(fd); reply(530, "System not available."); exit(0); } #ifdef VIRTUAL_HOSTING fd = fopen(thishost->welcome, "r"); #else fd = fopen(_PATH_FTPWELCOME, "r"); #endif if (fd != NULL) { while (fgets(line, sizeof(line), fd) != NULL) { if ((cp = strchr(line, '\n')) != NULL) *cp = '\0'; lreply(220, "%s", line); } (void) fflush(stdout); (void) fclose(fd); /* reply(220,) must follow */ } #ifndef VIRTUAL_HOSTING if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) fatalerror("Ran out of memory."); if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0) hostname[0] = '\0'; hostname[MAXHOSTNAMELEN - 1] = '\0'; #endif if (hostinfo) reply(220, "%s FTP server (%s) ready.", hostname, version); else reply(220, "FTP server ready."); for (;;) (void) yyparse(); /* NOTREACHED */ } static void lostconn(int signo) { if (ftpdebug) syslog(LOG_DEBUG, "lost connection"); dologout(1); } static void sigquit(int signo) { syslog(LOG_ERR, "got signal %d", signo); dologout(1); } #ifdef VIRTUAL_HOSTING /* * read in virtual host tables (if they exist) */ static void inithosts(int family) { int insert; size_t len; FILE *fp; char *cp, *mp, *line; char *hostname; char *vhost, *anonuser, *statfile, *welcome, *loginmsg; struct ftphost *hrp, *lhrp; struct addrinfo hints, *res, *ai; /* * Fill in the default host information */ if ((hostname = malloc(MAXHOSTNAMELEN)) == NULL) fatalerror("Ran out of memory."); if (gethostname(hostname, MAXHOSTNAMELEN - 1) < 0) hostname[0] = '\0'; hostname[MAXHOSTNAMELEN - 1] = '\0'; if ((hrp = malloc(sizeof(struct ftphost))) == NULL) fatalerror("Ran out of memory."); hrp->hostname = hostname; hrp->hostinfo = NULL; memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_PASSIVE; hints.ai_family = family; hints.ai_socktype = SOCK_STREAM; if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0) hrp->hostinfo = res; hrp->statfile = _PATH_FTPDSTATFILE; hrp->welcome = _PATH_FTPWELCOME; hrp->loginmsg = _PATH_FTPLOGINMESG; hrp->anonuser = "ftp"; hrp->next = NULL; thishost = firsthost = lhrp = hrp; if ((fp = fopen(_PATH_FTPHOSTS, "r")) != NULL) { int addrsize, gothost; void *addr; struct hostent *hp; while ((line = fgetln(fp, &len)) != NULL) { int i, hp_error; /* skip comments */ if (line[0] == '#') continue; if (line[len - 1] == '\n') { line[len - 1] = '\0'; mp = NULL; } else { if ((mp = malloc(len + 1)) == NULL) fatalerror("Ran out of memory."); memcpy(mp, line, len); mp[len] = '\0'; line = mp; } cp = strtok(line, " \t"); /* skip empty lines */ if (cp == NULL) goto nextline; vhost = cp; /* set defaults */ anonuser = "ftp"; statfile = _PATH_FTPDSTATFILE; welcome = _PATH_FTPWELCOME; loginmsg = _PATH_FTPLOGINMESG; /* * Preparse the line so we can use its info * for all the addresses associated with * the virtual host name. * Field 0, the virtual host name, is special: * it's already parsed off and will be strdup'ed * later, after we know its canonical form. */ for (i = 1; i < 5 && (cp = strtok(NULL, " \t")); i++) if (*cp != '-' && (cp = strdup(cp))) switch (i) { case 1: /* anon user permissions */ anonuser = cp; break; case 2: /* statistics file */ statfile = cp; break; case 3: /* welcome message */ welcome = cp; break; case 4: /* login message */ loginmsg = cp; break; default: /* programming error */ abort(); /* NOTREACHED */ } hints.ai_flags = AI_PASSIVE; hints.ai_family = family; hints.ai_socktype = SOCK_STREAM; if (getaddrinfo(vhost, NULL, &hints, &res) != 0) goto nextline; for (ai = res; ai != NULL && ai->ai_addr != NULL; ai = ai->ai_next) { gothost = 0; for (hrp = firsthost; hrp != NULL; hrp = hrp->next) { struct addrinfo *hi; for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) if (hi->ai_addrlen == ai->ai_addrlen && memcmp(hi->ai_addr, ai->ai_addr, ai->ai_addr->sa_len) == 0) { gothost++; break; } if (gothost) break; } if (hrp == NULL) { if ((hrp = malloc(sizeof(struct ftphost))) == NULL) goto nextline; hrp->hostname = NULL; insert = 1; } else { if (hrp->hostinfo && hrp->hostinfo != res) freeaddrinfo(hrp->hostinfo); insert = 0; /* host already in the chain */ } hrp->hostinfo = res; /* * determine hostname to use. * force defined name if there is a valid alias * otherwise fallback to primary hostname */ /* XXX: getaddrinfo() can't do alias check */ switch(hrp->hostinfo->ai_family) { case AF_INET: addr = &((struct sockaddr_in *)hrp->hostinfo->ai_addr)->sin_addr; addrsize = sizeof(struct in_addr); break; case AF_INET6: addr = &((struct sockaddr_in6 *)hrp->hostinfo->ai_addr)->sin6_addr; addrsize = sizeof(struct in6_addr); break; default: /* should not reach here */ freeaddrinfo(hrp->hostinfo); if (insert) free(hrp); /*not in chain, can free*/ else hrp->hostinfo = NULL; /*mark as blank*/ goto nextline; /* NOTREACHED */ } if ((hp = getipnodebyaddr(addr, addrsize, hrp->hostinfo->ai_family, &hp_error)) != NULL) { if (strcmp(vhost, hp->h_name) != 0) { if (hp->h_aliases == NULL) vhost = hp->h_name; else { i = 0; while (hp->h_aliases[i] && strcmp(vhost, hp->h_aliases[i]) != 0) ++i; if (hp->h_aliases[i] == NULL) vhost = hp->h_name; } } } if (hrp->hostname && strcmp(hrp->hostname, vhost) != 0) { free(hrp->hostname); hrp->hostname = NULL; } if (hrp->hostname == NULL && (hrp->hostname = strdup(vhost)) == NULL) { freeaddrinfo(hrp->hostinfo); hrp->hostinfo = NULL; /* mark as blank */ if (hp) freehostent(hp); goto nextline; } hrp->anonuser = anonuser; hrp->statfile = statfile; hrp->welcome = welcome; hrp->loginmsg = loginmsg; if (insert) { hrp->next = NULL; lhrp->next = hrp; lhrp = hrp; } if (hp) freehostent(hp); } nextline: if (mp) free(mp); } (void) fclose(fp); } } static void selecthost(union sockunion *su) { struct ftphost *hrp; u_int16_t port; #ifdef INET6 struct in6_addr *mapped_in6 = NULL; #endif struct addrinfo *hi; #ifdef INET6 /* * XXX IPv4 mapped IPv6 addr consideraton, * specified in rfc2373. */ if (su->su_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr)) mapped_in6 = &su->su_sin6.sin6_addr; #endif hrp = thishost = firsthost; /* default */ port = su->su_port; su->su_port = 0; while (hrp != NULL) { for (hi = hrp->hostinfo; hi != NULL; hi = hi->ai_next) { if (memcmp(su, hi->ai_addr, hi->ai_addrlen) == 0) { thishost = hrp; goto found; } #ifdef INET6 /* XXX IPv4 mapped IPv6 addr consideraton */ if (hi->ai_addr->sa_family == AF_INET && mapped_in6 != NULL && (memcmp(&mapped_in6->s6_addr[12], &((struct sockaddr_in *)hi->ai_addr)->sin_addr, sizeof(struct in_addr)) == 0)) { thishost = hrp; goto found; } #endif } hrp = hrp->next; } found: su->su_port = port; /* setup static variables as appropriate */ hostname = thishost->hostname; ftpuser = thishost->anonuser; } #endif /* * Helper function for sgetpwnam(). */ static char * sgetsave(char *s) { char *new = malloc(strlen(s) + 1); if (new == NULL) { reply(421, "Ran out of memory."); dologout(1); /* NOTREACHED */ } (void) strcpy(new, s); return (new); } /* * Save the result of a getpwnam. Used for USER command, since * the data returned must not be clobbered by any other command * (e.g., globbing). * NB: The data returned by sgetpwnam() will remain valid until * the next call to this function. Its difference from getpwnam() * is that sgetpwnam() is known to be called from ftpd code only. */ static struct passwd * sgetpwnam(char *name) { static struct passwd save; struct passwd *p; if ((p = getpwnam(name)) == NULL) return (p); if (save.pw_name) { free(save.pw_name); free(save.pw_passwd); free(save.pw_class); free(save.pw_gecos); free(save.pw_dir); free(save.pw_shell); } save = *p; save.pw_name = sgetsave(p->pw_name); save.pw_passwd = sgetsave(p->pw_passwd); save.pw_class = sgetsave(p->pw_class); save.pw_gecos = sgetsave(p->pw_gecos); save.pw_dir = sgetsave(p->pw_dir); save.pw_shell = sgetsave(p->pw_shell); return (&save); } static int login_attempts; /* number of failed login attempts */ static int askpasswd; /* had user command, ask for passwd */ static char curname[MAXLOGNAME]; /* current USER name */ /* * USER command. * Sets global passwd pointer pw if named account exists and is acceptable; * sets askpasswd if a PASS command is expected. If logged in previously, * need to reset state. If name is "ftp" or "anonymous", the name is not in * _PATH_FTPUSERS, and ftp account exists, set guest and pw, then just return. * If account doesn't exist, ask for passwd anyway. Otherwise, check user * requesting login privileges. Disallow anyone who does not have a standard * shell as returned by getusershell(). Disallow anyone mentioned in the file * _PATH_FTPUSERS to allow people such as root and uucp to be avoided. */ void user(char *name) { int ecode; char *cp, *shell; if (logged_in) { if (guest) { reply(530, "Can't change user from guest login."); return; } else if (dochroot) { reply(530, "Can't change user from chroot user."); return; } end_login(); } guest = 0; #ifdef VIRTUAL_HOSTING pw = sgetpwnam(thishost->anonuser); #else pw = sgetpwnam("ftp"); #endif if (strcmp(name, "ftp") == 0 || strcmp(name, "anonymous") == 0) { if (checkuser(_PATH_FTPUSERS, "ftp", 0, NULL, &ecode) || (ecode != 0 && ecode != ENOENT)) reply(530, "User %s access denied.", name); else if (checkuser(_PATH_FTPUSERS, "anonymous", 0, NULL, &ecode) || (ecode != 0 && ecode != ENOENT)) reply(530, "User %s access denied.", name); else if (pw != NULL) { guest = 1; askpasswd = 1; reply(331, "Guest login ok, send your email address as password."); } else reply(530, "User %s unknown.", name); if (!askpasswd && logging) syslog(LOG_NOTICE, "ANONYMOUS FTP LOGIN REFUSED FROM %s", remotehost); return; } if (anon_only != 0) { reply(530, "Sorry, only anonymous ftp allowed."); return; } if ((pw = sgetpwnam(name))) { if ((shell = pw->pw_shell) == NULL || *shell == 0) shell = _PATH_BSHELL; setusershell(); while ((cp = getusershell()) != NULL) if (strcmp(cp, shell) == 0) break; endusershell(); if (cp == NULL || (checkuser(_PATH_FTPUSERS, name, 1, NULL, &ecode) || (ecode != 0 && ecode != ENOENT))) { reply(530, "User %s access denied.", name); if (logging) syslog(LOG_NOTICE, "FTP LOGIN REFUSED FROM %s, %s", remotehost, name); pw = NULL; return; } } if (logging) strncpy(curname, name, sizeof(curname)-1); pwok = 0; #ifdef USE_PAM /* XXX Kluge! The conversation mechanism needs to be fixed. */ #endif if (opiechallenge(&opiedata, name, opieprompt) == 0) { pwok = (pw != NULL) && opieaccessfile(remotehost) && opiealways(pw->pw_dir); reply(331, "Response to %s %s for %s.", opieprompt, pwok ? "requested" : "required", name); } else { pwok = 1; reply(331, "Password required for %s.", name); } askpasswd = 1; /* * Delay before reading passwd after first failed * attempt to slow down passwd-guessing programs. */ if (login_attempts) sleep(login_attempts); } /* * Check if a user is in the file "fname", * return a pointer to a malloc'd string with the rest * of the matching line in "residue" if not NULL. */ static int checkuser(char *fname, char *name, int pwset, char **residue, int *ecode) { FILE *fd; int found = 0; size_t len; char *line, *mp, *p; if (ecode != NULL) *ecode = 0; if ((fd = fopen(fname, "r")) != NULL) { while (!found && (line = fgetln(fd, &len)) != NULL) { /* skip comments */ if (line[0] == '#') continue; if (line[len - 1] == '\n') { line[len - 1] = '\0'; mp = NULL; } else { if ((mp = malloc(len + 1)) == NULL) fatalerror("Ran out of memory."); memcpy(mp, line, len); mp[len] = '\0'; line = mp; } /* avoid possible leading and trailing whitespace */ p = strtok(line, " \t"); /* skip empty lines */ if (p == NULL) goto nextline; /* * if first chr is '@', check group membership */ if (p[0] == '@') { int i = 0; struct group *grp; if (p[1] == '\0') /* single @ matches anyone */ found = 1; else { if ((grp = getgrnam(p+1)) == NULL) goto nextline; /* * Check user's default group */ if (pwset && grp->gr_gid == pw->pw_gid) found = 1; /* * Check supplementary groups */ while (!found && grp->gr_mem[i]) found = strcmp(name, grp->gr_mem[i++]) == 0; } } /* * Otherwise, just check for username match */ else found = strcmp(p, name) == 0; /* * Save the rest of line to "residue" if matched */ if (found && residue) { if ((p = strtok(NULL, "")) != NULL) p += strspn(p, " \t"); if (p && *p) { if ((*residue = strdup(p)) == NULL) fatalerror("Ran out of memory."); } else *residue = NULL; } nextline: if (mp) free(mp); } (void) fclose(fd); } else if (ecode != NULL) *ecode = errno; return (found); } /* * Terminate login as previous user, if any, resetting state; * used when USER command is given or login fails. */ static void end_login(void) { #ifdef USE_PAM int e; #endif (void) seteuid(0); if (logged_in && dowtmp) ftpd_logwtmp(wtmpid, NULL, NULL); pw = NULL; #ifdef LOGIN_CAP setusercontext(NULL, getpwuid(0), 0, LOGIN_SETALL & ~(LOGIN_SETLOGIN | LOGIN_SETUSER | LOGIN_SETGROUP | LOGIN_SETPATH | LOGIN_SETENV)); #endif #ifdef USE_PAM if (pamh) { if ((e = pam_setcred(pamh, PAM_DELETE_CRED)) != PAM_SUCCESS) syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); if ((e = pam_close_session(pamh,0)) != PAM_SUCCESS) syslog(LOG_ERR, "pam_close_session: %s", pam_strerror(pamh, e)); if ((e = pam_end(pamh, e)) != PAM_SUCCESS) syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); pamh = NULL; } #endif logged_in = 0; guest = 0; dochroot = 0; } #ifdef USE_PAM /* * the following code is stolen from imap-uw PAM authentication module and * login.c */ #define COPY_STRING(s) (s ? strdup(s) : NULL) struct cred_t { const char *uname; /* user name */ const char *pass; /* password */ }; typedef struct cred_t cred_t; static int auth_conv(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata) { int i; cred_t *cred = (cred_t *) appdata; struct pam_response *reply; reply = calloc(num_msg, sizeof *reply); if (reply == NULL) return PAM_BUF_ERR; for (i = 0; i < num_msg; i++) { switch (msg[i]->msg_style) { case PAM_PROMPT_ECHO_ON: /* assume want user name */ reply[i].resp_retcode = PAM_SUCCESS; reply[i].resp = COPY_STRING(cred->uname); /* PAM frees resp. */ break; case PAM_PROMPT_ECHO_OFF: /* assume want password */ reply[i].resp_retcode = PAM_SUCCESS; reply[i].resp = COPY_STRING(cred->pass); /* PAM frees resp. */ break; case PAM_TEXT_INFO: case PAM_ERROR_MSG: reply[i].resp_retcode = PAM_SUCCESS; reply[i].resp = NULL; break; default: /* unknown message style */ free(reply); return PAM_CONV_ERR; } } *resp = reply; return PAM_SUCCESS; } /* * Attempt to authenticate the user using PAM. Returns 0 if the user is * authenticated, or 1 if not authenticated. If some sort of PAM system * error occurs (e.g., the "/etc/pam.conf" file is missing) then this * function returns -1. This can be used as an indication that we should * fall back to a different authentication mechanism. */ static int auth_pam(struct passwd **ppw, const char *pass) { const char *tmpl_user; const void *item; int rval; int e; cred_t auth_cred = { (*ppw)->pw_name, pass }; struct pam_conv conv = { &auth_conv, &auth_cred }; e = pam_start("ftpd", (*ppw)->pw_name, &conv, &pamh); if (e != PAM_SUCCESS) { /* * In OpenPAM, it's OK to pass NULL to pam_strerror() * if context creation has failed in the first place. */ syslog(LOG_ERR, "pam_start: %s", pam_strerror(NULL, e)); return -1; } e = pam_set_item(pamh, PAM_RHOST, remotehost); if (e != PAM_SUCCESS) { syslog(LOG_ERR, "pam_set_item(PAM_RHOST): %s", pam_strerror(pamh, e)); if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); } pamh = NULL; return -1; } e = pam_authenticate(pamh, 0); switch (e) { case PAM_SUCCESS: /* * With PAM we support the concept of a "template" * user. The user enters a login name which is * authenticated by PAM, usually via a remote service * such as RADIUS or TACACS+. If authentication * succeeds, a different but related "template" name * is used for setting the credentials, shell, and * home directory. The name the user enters need only * exist on the remote authentication server, but the * template name must be present in the local password * database. * * This is supported by two various mechanisms in the * individual modules. However, from the application's * point of view, the template user is always passed * back as a changed value of the PAM_USER item. */ if ((e = pam_get_item(pamh, PAM_USER, &item)) == PAM_SUCCESS) { tmpl_user = (const char *) item; if (strcmp((*ppw)->pw_name, tmpl_user) != 0) *ppw = getpwnam(tmpl_user); } else syslog(LOG_ERR, "Couldn't get PAM_USER: %s", pam_strerror(pamh, e)); rval = 0; break; case PAM_AUTH_ERR: case PAM_USER_UNKNOWN: case PAM_MAXTRIES: rval = 1; break; default: syslog(LOG_ERR, "pam_authenticate: %s", pam_strerror(pamh, e)); rval = -1; break; } if (rval == 0) { e = pam_acct_mgmt(pamh, 0); if (e != PAM_SUCCESS) { syslog(LOG_ERR, "pam_acct_mgmt: %s", pam_strerror(pamh, e)); rval = 1; } } if (rval != 0) { if ((e = pam_end(pamh, e)) != PAM_SUCCESS) { syslog(LOG_ERR, "pam_end: %s", pam_strerror(pamh, e)); } pamh = NULL; } return rval; } #endif /* USE_PAM */ void pass(char *passwd) { int rval, ecode; FILE *fd; #ifdef LOGIN_CAP login_cap_t *lc = NULL; #endif #ifdef USE_PAM int e; #endif char *residue = NULL; char *xpasswd; if (logged_in || askpasswd == 0) { reply(503, "Login with USER first."); return; } askpasswd = 0; if (!guest) { /* "ftp" is only account allowed no password */ if (pw == NULL) { rval = 1; /* failure below */ goto skip; } #ifdef USE_PAM rval = auth_pam(&pw, passwd); if (rval >= 0) { opieunlock(); goto skip; } #endif if (opieverify(&opiedata, passwd) == 0) xpasswd = pw->pw_passwd; else if (pwok) { xpasswd = crypt(passwd, pw->pw_passwd); if (passwd[0] == '\0' && pw->pw_passwd[0] != '\0') xpasswd = ":"; } else { rval = 1; goto skip; } rval = strcmp(pw->pw_passwd, xpasswd); if (pw->pw_expire && time(NULL) >= pw->pw_expire) rval = 1; /* failure */ skip: /* * If rval == 1, the user failed the authentication check * above. If rval == 0, either PAM or local authentication * succeeded. */ if (rval) { reply(530, "Login incorrect."); if (logging) { syslog(LOG_NOTICE, "FTP LOGIN FAILED FROM %s", remotehost); syslog(LOG_AUTHPRIV | LOG_NOTICE, "FTP LOGIN FAILED FROM %s, %s", remotehost, curname); } pw = NULL; if (login_attempts++ >= 5) { syslog(LOG_NOTICE, "repeated login failures from %s", remotehost); exit(0); } return; } } login_attempts = 0; /* this time successful */ if (setegid(pw->pw_gid) < 0) { reply(550, "Can't set gid."); return; } /* May be overridden by login.conf */ (void) umask(defumask); #ifdef LOGIN_CAP if ((lc = login_getpwclass(pw)) != NULL) { char remote_ip[NI_MAXHOST]; if (getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, remote_ip, sizeof(remote_ip) - 1, NULL, 0, NI_NUMERICHOST)) *remote_ip = 0; remote_ip[sizeof(remote_ip) - 1] = 0; if (!auth_hostok(lc, remotehost, remote_ip)) { syslog(LOG_INFO|LOG_AUTH, "FTP LOGIN FAILED (HOST) as %s: permission denied.", pw->pw_name); reply(530, "Permission denied."); pw = NULL; return; } if (!auth_timeok(lc, time(NULL))) { reply(530, "Login not available right now."); pw = NULL; return; } } setusercontext(lc, pw, 0, LOGIN_SETALL & ~(LOGIN_SETUSER | LOGIN_SETPATH | LOGIN_SETENV)); #else setlogin(pw->pw_name); (void) initgroups(pw->pw_name, pw->pw_gid); #endif #ifdef USE_PAM if (pamh) { if ((e = pam_open_session(pamh, 0)) != PAM_SUCCESS) { syslog(LOG_ERR, "pam_open_session: %s", pam_strerror(pamh, e)); } else if ((e = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) { syslog(LOG_ERR, "pam_setcred: %s", pam_strerror(pamh, e)); } } #endif dochroot = checkuser(_PATH_FTPCHROOT, pw->pw_name, 1, &residue, &ecode) #ifdef LOGIN_CAP /* Allow login.conf configuration as well */ || login_getcapbool(lc, "ftp-chroot", 0) #endif ; /* * It is possible that checkuser() failed to open the chroot file. * If this is the case, report that logins are un-available, since we * have no way of checking whether or not the user should be chrooted. * We ignore ENOENT since it is not required that this file be present. */ if (ecode != 0 && ecode != ENOENT) { reply(530, "Login not available right now."); return; } chrootdir = NULL; /* Disable wtmp logging when chrooting. */ if (dochroot || guest) dowtmp = 0; if (dowtmp) ftpd_logwtmp(wtmpid, pw->pw_name, (struct sockaddr *)&his_addr); logged_in = 1; if (guest && stats && statfd < 0) #ifdef VIRTUAL_HOSTING statfd = open(thishost->statfile, O_WRONLY|O_APPEND); #else statfd = open(_PATH_FTPDSTATFILE, O_WRONLY|O_APPEND); #endif if (statfd < 0) stats = 0; /* * For a chrooted local user, * a) see whether ftpchroot(5) specifies a chroot directory, * b) extract the directory pathname from the line, * c) expand it to the absolute pathname if necessary. */ if (dochroot && residue && (chrootdir = strtok(residue, " \t")) != NULL) { if (chrootdir[0] != '/') asprintf(&chrootdir, "%s/%s", pw->pw_dir, chrootdir); else chrootdir = strdup(chrootdir); /* make it permanent */ if (chrootdir == NULL) fatalerror("Ran out of memory."); } if (guest || dochroot) { /* * If no chroot directory set yet, use the login directory. * Copy it so it can be modified while pw->pw_dir stays intact. */ if (chrootdir == NULL && (chrootdir = strdup(pw->pw_dir)) == NULL) fatalerror("Ran out of memory."); /* * Check for the "/chroot/./home" syntax, * separate the chroot and home directory pathnames. */ if ((homedir = strstr(chrootdir, "/./")) != NULL) { *(homedir++) = '\0'; /* wipe '/' */ homedir++; /* skip '.' */ } else { /* * We MUST do a chdir() after the chroot. Otherwise * the old current directory will be accessible as "." * outside the new root! */ homedir = "/"; } /* * Finally, do chroot() */ if (chroot(chrootdir) < 0) { reply(550, "Can't change root."); goto bad; } __FreeBSD_libc_enter_restricted_mode(); } else /* real user w/o chroot */ homedir = pw->pw_dir; /* * Set euid *before* doing chdir() so * a) the user won't be carried to a directory that he couldn't reach * on his own due to no permission to upper path components, * b) NFS mounted homedirs w/restrictive permissions will be accessible * (uid 0 has no root power over NFS if not mapped explicitly.) */ if (seteuid(pw->pw_uid) < 0) { reply(550, "Can't set uid."); goto bad; } if (chdir(homedir) < 0) { if (guest || dochroot) { reply(550, "Can't change to base directory."); goto bad; } else { if (chdir("/") < 0) { reply(550, "Root is inaccessible."); goto bad; } lreply(230, "No directory! Logging in with home=/."); } } /* * Display a login message, if it exists. * N.B. reply(230,) must follow the message. */ #ifdef VIRTUAL_HOSTING fd = fopen(thishost->loginmsg, "r"); #else fd = fopen(_PATH_FTPLOGINMESG, "r"); #endif if (fd != NULL) { char *cp, line[LINE_MAX]; while (fgets(line, sizeof(line), fd) != NULL) { if ((cp = strchr(line, '\n')) != NULL) *cp = '\0'; lreply(230, "%s", line); } (void) fflush(stdout); (void) fclose(fd); } if (guest) { if (ident != NULL) free(ident); ident = strdup(passwd); if (ident == NULL) fatalerror("Ran out of memory."); reply(230, "Guest login ok, access restrictions apply."); #ifdef SETPROCTITLE #ifdef VIRTUAL_HOSTING if (thishost != firsthost) snprintf(proctitle, sizeof(proctitle), "%s: anonymous(%s)/%s", remotehost, hostname, passwd); else #endif snprintf(proctitle, sizeof(proctitle), "%s: anonymous/%s", remotehost, passwd); setproctitle("%s", proctitle); #endif /* SETPROCTITLE */ if (logging) syslog(LOG_INFO, "ANONYMOUS FTP LOGIN FROM %s, %s", remotehost, passwd); } else { if (dochroot) reply(230, "User %s logged in, " "access restrictions apply.", pw->pw_name); else reply(230, "User %s logged in.", pw->pw_name); #ifdef SETPROCTITLE snprintf(proctitle, sizeof(proctitle), "%s: user/%s", remotehost, pw->pw_name); setproctitle("%s", proctitle); #endif /* SETPROCTITLE */ if (logging) syslog(LOG_INFO, "FTP LOGIN FROM %s as %s", remotehost, pw->pw_name); } if (logging && (guest || dochroot)) syslog(LOG_INFO, "session root changed to %s", chrootdir); #ifdef LOGIN_CAP login_close(lc); #endif if (residue) free(residue); return; bad: /* Forget all about it... */ #ifdef LOGIN_CAP login_close(lc); #endif if (residue) free(residue); end_login(); } void retrieve(char *cmd, char *name) { FILE *fin, *dout; struct stat st; int (*closefunc)(FILE *); time_t start; if (cmd == 0) { fin = fopen(name, "r"), closefunc = fclose; st.st_size = 0; } else { char line[BUFSIZ]; (void) snprintf(line, sizeof(line), cmd, name), name = line; fin = ftpd_popen(line, "r"), closefunc = ftpd_pclose; st.st_size = -1; st.st_blksize = BUFSIZ; } if (fin == NULL) { if (errno != 0) { perror_reply(550, name); if (cmd == 0) { LOGCMD("get", name); } } return; } byte_count = -1; if (cmd == 0) { if (fstat(fileno(fin), &st) < 0) { perror_reply(550, name); goto done; } if (!S_ISREG(st.st_mode)) { /* * Never sending a raw directory is a workaround * for buggy clients that will attempt to RETR * a directory before listing it, e.g., Mozilla. * Preventing a guest from getting irregular files * is a simple security measure. */ if (S_ISDIR(st.st_mode) || guest) { reply(550, "%s: not a plain file.", name); goto done; } st.st_size = -1; /* st.st_blksize is set for all descriptor types */ } } if (restart_point) { if (type == TYPE_A) { off_t i, n; int c; n = restart_point; i = 0; while (i++ < n) { if ((c=getc(fin)) == EOF) { perror_reply(550, name); goto done; } if (c == '\n') i++; } } else if (lseek(fileno(fin), restart_point, L_SET) < 0) { perror_reply(550, name); goto done; } } dout = dataconn(name, st.st_size, "w"); if (dout == NULL) goto done; time(&start); send_data(fin, dout, st.st_blksize, st.st_size, restart_point == 0 && cmd == 0 && S_ISREG(st.st_mode)); if (cmd == 0 && guest && stats && byte_count > 0) logxfer(name, byte_count, start); (void) fclose(dout); data = -1; pdata = -1; done: if (cmd == 0) LOGBYTES("get", name, byte_count); (*closefunc)(fin); } void store(char *name, char *mode, int unique) { int fd; FILE *fout, *din; int (*closefunc)(FILE *); if (*mode == 'a') { /* APPE */ if (unique) { /* Programming error */ syslog(LOG_ERR, "Internal: unique flag to APPE"); unique = 0; } if (guest && noguestmod) { reply(550, "Appending to existing file denied."); goto err; } restart_point = 0; /* not affected by preceding REST */ } if (unique) /* STOU overrides REST */ restart_point = 0; if (guest && noguestmod) { if (restart_point) { /* guest STOR w/REST */ reply(550, "Modifying existing file denied."); goto err; } else /* treat guest STOR as STOU */ unique = 1; } if (restart_point) mode = "r+"; /* so ASCII manual seek can work */ if (unique) { if ((fd = guniquefd(name, &name)) < 0) goto err; fout = fdopen(fd, mode); } else fout = fopen(name, mode); closefunc = fclose; if (fout == NULL) { perror_reply(553, name); goto err; } byte_count = -1; if (restart_point) { if (type == TYPE_A) { off_t i, n; int c; n = restart_point; i = 0; while (i++ < n) { if ((c=getc(fout)) == EOF) { perror_reply(550, name); goto done; } if (c == '\n') i++; } /* * We must do this seek to "current" position * because we are changing from reading to * writing. */ if (fseeko(fout, 0, SEEK_CUR) < 0) { perror_reply(550, name); goto done; } } else if (lseek(fileno(fout), restart_point, L_SET) < 0) { perror_reply(550, name); goto done; } } din = dataconn(name, -1, "r"); if (din == NULL) goto done; if (receive_data(din, fout) == 0) { if (unique) reply(226, "Transfer complete (unique file name:%s).", name); else reply(226, "Transfer complete."); } (void) fclose(din); data = -1; pdata = -1; done: LOGBYTES(*mode == 'a' ? "append" : "put", name, byte_count); (*closefunc)(fout); return; err: LOGCMD(*mode == 'a' ? "append" : "put" , name); return; } static FILE * getdatasock(char *mode) { int on = 1, s, t, tries; if (data >= 0) return (fdopen(data, mode)); s = socket(data_dest.su_family, SOCK_STREAM, 0); if (s < 0) goto bad; if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) syslog(LOG_WARNING, "data setsockopt (SO_REUSEADDR): %m"); /* anchor socket to avoid multi-homing problems */ data_source = ctrl_addr; data_source.su_port = htons(dataport); (void) seteuid(0); for (tries = 1; ; tries++) { /* * We should loop here since it's possible that * another ftpd instance has passed this point and is * trying to open a data connection in active mode now. * Until the other connection is opened, we'll be getting * EADDRINUSE because no SOCK_STREAM sockets in the system * can share both local and remote addresses, localIP:20 * and *:* in this case. */ if (bind(s, (struct sockaddr *)&data_source, data_source.su_len) >= 0) break; if (errno != EADDRINUSE || tries > 10) goto bad; sleep(tries); } (void) seteuid(pw->pw_uid); #ifdef IP_TOS if (data_source.su_family == AF_INET) { on = IPTOS_THROUGHPUT; if (setsockopt(s, IPPROTO_IP, IP_TOS, &on, sizeof(int)) < 0) syslog(LOG_WARNING, "data setsockopt (IP_TOS): %m"); } #endif #ifdef TCP_NOPUSH /* * Turn off push flag to keep sender TCP from sending short packets * at the boundaries of each write(). */ on = 1; if (setsockopt(s, IPPROTO_TCP, TCP_NOPUSH, &on, sizeof on) < 0) syslog(LOG_WARNING, "data setsockopt (TCP_NOPUSH): %m"); #endif return (fdopen(s, mode)); bad: /* Return the real value of errno (close may change it) */ t = errno; (void) seteuid(pw->pw_uid); (void) close(s); errno = t; return (NULL); } static FILE * dataconn(char *name, off_t size, char *mode) { char sizebuf[32]; FILE *file; int retry = 0, tos, conerrno; file_size = size; byte_count = 0; if (size != -1) (void) snprintf(sizebuf, sizeof(sizebuf), " (%jd bytes)", (intmax_t)size); else *sizebuf = '\0'; if (pdata >= 0) { union sockunion from; socklen_t fromlen = ctrl_addr.su_len; int flags, s; struct timeval timeout; fd_set set; FD_ZERO(&set); FD_SET(pdata, &set); timeout.tv_usec = 0; timeout.tv_sec = 120; /* * Granted a socket is in the blocking I/O mode, * accept() will block after a successful select() * if the selected connection dies in between. * Therefore set the non-blocking I/O flag here. */ if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || fcntl(pdata, F_SETFL, flags | O_NONBLOCK) == -1) goto pdata_err; if (select(pdata+1, &set, NULL, NULL, &timeout) <= 0 || (s = accept(pdata, (struct sockaddr *) &from, &fromlen)) < 0) goto pdata_err; (void) close(pdata); pdata = s; /* * Unset the inherited non-blocking I/O flag * on the child socket so stdio can work on it. */ if ((flags = fcntl(pdata, F_GETFL, 0)) == -1 || fcntl(pdata, F_SETFL, flags & ~O_NONBLOCK) == -1) goto pdata_err; #ifdef IP_TOS if (from.su_family == AF_INET) { tos = IPTOS_THROUGHPUT; if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(int)) < 0) syslog(LOG_WARNING, "pdata setsockopt (IP_TOS): %m"); } #endif reply(150, "Opening %s mode data connection for '%s'%s.", type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); return (fdopen(pdata, mode)); pdata_err: reply(425, "Can't open data connection."); (void) close(pdata); pdata = -1; return (NULL); } if (data >= 0) { reply(125, "Using existing data connection for '%s'%s.", name, sizebuf); usedefault = 1; return (fdopen(data, mode)); } if (usedefault) data_dest = his_addr; usedefault = 1; do { file = getdatasock(mode); if (file == NULL) { char hostbuf[NI_MAXHOST], portbuf[NI_MAXSERV]; if (getnameinfo((struct sockaddr *)&data_source, data_source.su_len, hostbuf, sizeof(hostbuf) - 1, portbuf, sizeof(portbuf) - 1, NI_NUMERICHOST|NI_NUMERICSERV)) *hostbuf = *portbuf = 0; hostbuf[sizeof(hostbuf) - 1] = 0; portbuf[sizeof(portbuf) - 1] = 0; reply(425, "Can't create data socket (%s,%s): %s.", hostbuf, portbuf, strerror(errno)); return (NULL); } data = fileno(file); conerrno = 0; if (connect(data, (struct sockaddr *)&data_dest, data_dest.su_len) == 0) break; conerrno = errno; (void) fclose(file); data = -1; if (conerrno == EADDRINUSE) { sleep(swaitint); retry += swaitint; } else { break; } } while (retry <= swaitmax); if (conerrno != 0) { reply(425, "Can't build data connection: %s.", strerror(conerrno)); return (NULL); } reply(150, "Opening %s mode data connection for '%s'%s.", type == TYPE_A ? "ASCII" : "BINARY", name, sizebuf); return (file); } /* * A helper macro to avoid code duplication * in send_data() and receive_data(). * * XXX We have to block SIGURG during putc() because BSD stdio * is unable to restart interrupted write operations and hence * the entire buffer contents will be lost as soon as a write() * call indicates EINTR to stdio. */ #define FTPD_PUTC(ch, file, label) \ do { \ int ret; \ \ do { \ START_UNSAFE; \ ret = putc((ch), (file)); \ END_UNSAFE; \ CHECKOOB(return (-1)) \ else if (ferror(file)) \ goto label; \ clearerr(file); \ } while (ret == EOF); \ } while (0) /* * Transfer the contents of "instr" to "outstr" peer using the appropriate * encapsulation of the data subject to Mode, Structure, and Type. * * NB: Form isn't handled. */ static int send_data(FILE *instr, FILE *outstr, size_t blksize, off_t filesize, int isreg) { int c, cp, filefd, netfd; char *buf; STARTXFER; switch (type) { case TYPE_A: cp = EOF; for (;;) { c = getc(instr); CHECKOOB(return (-1)) else if (c == EOF && ferror(instr)) goto file_err; if (c == EOF) { if (ferror(instr)) { /* resume after OOB */ clearerr(instr); continue; } if (feof(instr)) /* EOF */ break; syslog(LOG_ERR, "Internal: impossible condition" " on file after getc()"); goto file_err; } if (c == '\n' && cp != '\r') { FTPD_PUTC('\r', outstr, data_err); byte_count++; } FTPD_PUTC(c, outstr, data_err); byte_count++; cp = c; } #ifdef notyet /* BSD stdio isn't ready for that */ while (fflush(outstr) == EOF) { CHECKOOB(return (-1)) else goto data_err; clearerr(outstr); } ENDXFER; #else ENDXFER; if (fflush(outstr) == EOF) goto data_err; #endif reply(226, "Transfer complete."); return (0); case TYPE_I: case TYPE_L: /* * isreg is only set if we are not doing restart and we * are sending a regular file */ netfd = fileno(outstr); filefd = fileno(instr); if (isreg) { char *msg = "Transfer complete."; off_t cnt, offset; int err; cnt = offset = 0; while (filesize > 0) { err = sendfile(filefd, netfd, offset, 0, NULL, &cnt, 0); /* * Calculate byte_count before OOB processing. * It can be used in myoob() later. */ byte_count += cnt; offset += cnt; filesize -= cnt; CHECKOOB(return (-1)) else if (err == -1) { if (errno != EINTR && cnt == 0 && offset == 0) goto oldway; goto data_err; } if (err == -1) /* resume after OOB */ continue; /* * We hit the EOF prematurely. * Perhaps the file was externally truncated. */ if (cnt == 0) { msg = "Transfer finished due to " "premature end of file."; break; } } ENDXFER; reply(226, "%s", msg); return (0); } oldway: if ((buf = malloc(blksize)) == NULL) { ENDXFER; reply(451, "Ran out of memory."); return (-1); } for (;;) { int cnt, len; char *bp; cnt = read(filefd, buf, blksize); CHECKOOB(free(buf); return (-1)) else if (cnt < 0) { free(buf); goto file_err; } if (cnt < 0) /* resume after OOB */ continue; if (cnt == 0) /* EOF */ break; for (len = cnt, bp = buf; len > 0;) { cnt = write(netfd, bp, len); CHECKOOB(free(buf); return (-1)) else if (cnt < 0) { free(buf); goto data_err; } if (cnt <= 0) continue; len -= cnt; bp += cnt; byte_count += cnt; } } ENDXFER; free(buf); reply(226, "Transfer complete."); return (0); default: ENDXFER; reply(550, "Unimplemented TYPE %d in send_data.", type); return (-1); } data_err: ENDXFER; perror_reply(426, "Data connection"); return (-1); file_err: ENDXFER; perror_reply(551, "Error on input file"); return (-1); } /* * Transfer data from peer to "outstr" using the appropriate encapulation of * the data subject to Mode, Structure, and Type. * * N.B.: Form isn't handled. */ static int receive_data(FILE *instr, FILE *outstr) { int c, cp; int bare_lfs = 0; STARTXFER; switch (type) { case TYPE_I: case TYPE_L: for (;;) { int cnt, len; char *bp; char buf[BUFSIZ]; cnt = read(fileno(instr), buf, sizeof(buf)); CHECKOOB(return (-1)) else if (cnt < 0) goto data_err; if (cnt < 0) /* resume after OOB */ continue; if (cnt == 0) /* EOF */ break; for (len = cnt, bp = buf; len > 0;) { cnt = write(fileno(outstr), bp, len); CHECKOOB(return (-1)) else if (cnt < 0) goto file_err; if (cnt <= 0) continue; len -= cnt; bp += cnt; byte_count += cnt; } } ENDXFER; return (0); case TYPE_E: ENDXFER; reply(553, "TYPE E not implemented."); return (-1); case TYPE_A: cp = EOF; for (;;) { c = getc(instr); CHECKOOB(return (-1)) else if (c == EOF && ferror(instr)) goto data_err; if (c == EOF && ferror(instr)) { /* resume after OOB */ clearerr(instr); continue; } if (cp == '\r') { if (c != '\n') FTPD_PUTC('\r', outstr, file_err); } else if (c == '\n') bare_lfs++; if (c == '\r') { byte_count++; cp = c; continue; } /* Check for EOF here in order not to lose last \r. */ if (c == EOF) { if (feof(instr)) /* EOF */ break; syslog(LOG_ERR, "Internal: impossible condition" " on data stream after getc()"); goto data_err; } byte_count++; FTPD_PUTC(c, outstr, file_err); cp = c; } #ifdef notyet /* BSD stdio isn't ready for that */ while (fflush(outstr) == EOF) { CHECKOOB(return (-1)) else goto file_err; clearerr(outstr); } ENDXFER; #else ENDXFER; if (fflush(outstr) == EOF) goto file_err; #endif if (bare_lfs) { lreply(226, "WARNING! %d bare linefeeds received in ASCII mode.", bare_lfs); (void)printf(" File may not have transferred correctly.\r\n"); } return (0); default: ENDXFER; reply(550, "Unimplemented TYPE %d in receive_data.", type); return (-1); } data_err: ENDXFER; perror_reply(426, "Data connection"); return (-1); file_err: ENDXFER; perror_reply(452, "Error writing to file"); return (-1); } void statfilecmd(char *filename) { FILE *fin; int atstart; int c, code; char line[LINE_MAX]; struct stat st; code = lstat(filename, &st) == 0 && S_ISDIR(st.st_mode) ? 212 : 213; (void)snprintf(line, sizeof(line), _PATH_LS " -lgA %s", filename); fin = ftpd_popen(line, "r"); if (fin == NULL) { perror_reply(551, filename); return; } lreply(code, "Status of %s:", filename); atstart = 1; while ((c = getc(fin)) != EOF) { if (c == '\n') { if (ferror(stdout)){ perror_reply(421, "Control connection"); (void) ftpd_pclose(fin); dologout(1); /* NOTREACHED */ } if (ferror(fin)) { perror_reply(551, filename); (void) ftpd_pclose(fin); return; } (void) putc('\r', stdout); } /* * RFC 959 says neutral text should be prepended before * a leading 3-digit number followed by whitespace, but * many ftp clients can be confused by any leading digits, * as a matter of fact. */ if (atstart && isdigit(c)) (void) putc(' ', stdout); (void) putc(c, stdout); atstart = (c == '\n'); } (void) ftpd_pclose(fin); reply(code, "End of status."); } void statcmd(void) { union sockunion *su; u_char *a, *p; char hname[NI_MAXHOST]; int ispassive; if (hostinfo) { lreply(211, "%s FTP server status:", hostname); printf(" %s\r\n", version); } else lreply(211, "FTP server status:"); printf(" Connected to %s", remotehost); if (!getnameinfo((struct sockaddr *)&his_addr, his_addr.su_len, hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) { hname[sizeof(hname) - 1] = 0; if (strcmp(hname, remotehost) != 0) printf(" (%s)", hname); } printf("\r\n"); if (logged_in) { if (guest) printf(" Logged in anonymously\r\n"); else printf(" Logged in as %s\r\n", pw->pw_name); } else if (askpasswd) printf(" Waiting for password\r\n"); else printf(" Waiting for user name\r\n"); printf(" TYPE: %s", typenames[type]); if (type == TYPE_A || type == TYPE_E) printf(", FORM: %s", formnames[form]); if (type == TYPE_L) #if CHAR_BIT == 8 printf(" %d", CHAR_BIT); #else printf(" %d", bytesize); /* need definition! */ #endif printf("; STRUcture: %s; transfer MODE: %s\r\n", strunames[stru], modenames[mode]); if (data != -1) printf(" Data connection open\r\n"); else if (pdata != -1) { ispassive = 1; su = &pasv_addr; goto printaddr; } else if (usedefault == 0) { ispassive = 0; su = &data_dest; printaddr: #define UC(b) (((int) b) & 0xff) if (epsvall) { printf(" EPSV only mode (EPSV ALL)\r\n"); goto epsvonly; } /* PORT/PASV */ if (su->su_family == AF_INET) { a = (u_char *) &su->su_sin.sin_addr; p = (u_char *) &su->su_sin.sin_port; printf(" %s (%d,%d,%d,%d,%d,%d)\r\n", ispassive ? "PASV" : "PORT", UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); } /* LPRT/LPSV */ { int alen, af, i; switch (su->su_family) { case AF_INET: a = (u_char *) &su->su_sin.sin_addr; p = (u_char *) &su->su_sin.sin_port; alen = sizeof(su->su_sin.sin_addr); af = 4; break; case AF_INET6: a = (u_char *) &su->su_sin6.sin6_addr; p = (u_char *) &su->su_sin6.sin6_port; alen = sizeof(su->su_sin6.sin6_addr); af = 6; break; default: af = 0; break; } if (af) { printf(" %s (%d,%d,", ispassive ? "LPSV" : "LPRT", af, alen); for (i = 0; i < alen; i++) printf("%d,", UC(a[i])); printf("%d,%d,%d)\r\n", 2, UC(p[0]), UC(p[1])); } } epsvonly:; /* EPRT/EPSV */ { int af; switch (su->su_family) { case AF_INET: af = 1; break; case AF_INET6: af = 2; break; default: af = 0; break; } if (af) { union sockunion tmp; tmp = *su; if (tmp.su_family == AF_INET6) tmp.su_sin6.sin6_scope_id = 0; if (!getnameinfo((struct sockaddr *)&tmp, tmp.su_len, hname, sizeof(hname) - 1, NULL, 0, NI_NUMERICHOST)) { hname[sizeof(hname) - 1] = 0; printf(" %s |%d|%s|%d|\r\n", ispassive ? "EPSV" : "EPRT", af, hname, htons(tmp.su_port)); } } } #undef UC } else printf(" No data connection\r\n"); reply(211, "End of status."); } void fatalerror(char *s) { reply(451, "Error in server: %s", s); reply(221, "Closing connection due to server error."); dologout(0); /* NOTREACHED */ } void reply(int n, const char *fmt, ...) { va_list ap; (void)printf("%d ", n); va_start(ap, fmt); (void)vprintf(fmt, ap); va_end(ap); (void)printf("\r\n"); (void)fflush(stdout); if (ftpdebug) { syslog(LOG_DEBUG, "<--- %d ", n); va_start(ap, fmt); vsyslog(LOG_DEBUG, fmt, ap); va_end(ap); } } void lreply(int n, const char *fmt, ...) { va_list ap; (void)printf("%d- ", n); va_start(ap, fmt); (void)vprintf(fmt, ap); va_end(ap); (void)printf("\r\n"); (void)fflush(stdout); if (ftpdebug) { syslog(LOG_DEBUG, "<--- %d- ", n); va_start(ap, fmt); vsyslog(LOG_DEBUG, fmt, ap); va_end(ap); } } static void ack(char *s) { reply(250, "%s command successful.", s); } void nack(char *s) { reply(502, "%s command not implemented.", s); } /* ARGSUSED */ void yyerror(char *s) { char *cp; if ((cp = strchr(cbuf,'\n'))) *cp = '\0'; reply(500, "%s: command not understood.", cbuf); } void delete(char *name) { struct stat st; LOGCMD("delete", name); if (lstat(name, &st) < 0) { perror_reply(550, name); return; } if (S_ISDIR(st.st_mode)) { if (rmdir(name) < 0) { perror_reply(550, name); return; } goto done; } if (guest && noguestmod) { reply(550, "Operation not permitted."); return; } if (unlink(name) < 0) { perror_reply(550, name); return; } done: ack("DELE"); } void cwd(char *path) { if (chdir(path) < 0) perror_reply(550, path); else ack("CWD"); } void makedir(char *name) { char *s; LOGCMD("mkdir", name); if (guest && noguestmkd) reply(550, "Operation not permitted."); else if (mkdir(name, 0777) < 0) perror_reply(550, name); else { if ((s = doublequote(name)) == NULL) fatalerror("Ran out of memory."); reply(257, "\"%s\" directory created.", s); free(s); } } void removedir(char *name) { LOGCMD("rmdir", name); if (rmdir(name) < 0) perror_reply(550, name); else ack("RMD"); } void pwd(void) { char *s, path[MAXPATHLEN + 1]; if (getcwd(path, sizeof(path)) == NULL) perror_reply(550, "Get current directory"); else { if ((s = doublequote(path)) == NULL) fatalerror("Ran out of memory."); reply(257, "\"%s\" is current directory.", s); free(s); } } char * renamefrom(char *name) { struct stat st; if (guest && noguestmod) { reply(550, "Operation not permitted."); return (NULL); } if (lstat(name, &st) < 0) { perror_reply(550, name); return (NULL); } reply(350, "File exists, ready for destination name."); return (name); } void renamecmd(char *from, char *to) { struct stat st; LOGCMD2("rename", from, to); if (guest && (stat(to, &st) == 0)) { reply(550, "%s: permission denied.", to); return; } if (rename(from, to) < 0) perror_reply(550, "rename"); else ack("RNTO"); } static void dolog(struct sockaddr *who) { char who_name[NI_MAXHOST]; realhostname_sa(remotehost, sizeof(remotehost) - 1, who, who->sa_len); remotehost[sizeof(remotehost) - 1] = 0; if (getnameinfo(who, who->sa_len, who_name, sizeof(who_name) - 1, NULL, 0, NI_NUMERICHOST)) *who_name = 0; who_name[sizeof(who_name) - 1] = 0; #ifdef SETPROCTITLE #ifdef VIRTUAL_HOSTING if (thishost != firsthost) snprintf(proctitle, sizeof(proctitle), "%s: connected (to %s)", remotehost, hostname); else #endif snprintf(proctitle, sizeof(proctitle), "%s: connected", remotehost); setproctitle("%s", proctitle); #endif /* SETPROCTITLE */ if (logging) { #ifdef VIRTUAL_HOSTING if (thishost != firsthost) syslog(LOG_INFO, "connection from %s (%s) to %s", remotehost, who_name, hostname); else #endif syslog(LOG_INFO, "connection from %s (%s)", remotehost, who_name); } } /* * Record logout in wtmp file * and exit with supplied status. */ void dologout(int status) { if (logged_in && dowtmp) { (void) seteuid(0); ftpd_logwtmp(wtmpid, NULL, NULL); } /* beware of flushing buffers after a SIGPIPE */ _exit(status); } static void sigurg(int signo) { recvurg = 1; } static void maskurg(int flag) { int oerrno; sigset_t sset; if (!transflag) { syslog(LOG_ERR, "Internal: maskurg() while no transfer"); return; } oerrno = errno; sigemptyset(&sset); sigaddset(&sset, SIGURG); sigprocmask(flag ? SIG_BLOCK : SIG_UNBLOCK, &sset, NULL); errno = oerrno; } static void flagxfer(int flag) { if (flag) { if (transflag) syslog(LOG_ERR, "Internal: flagxfer(1): " "transfer already under way"); transflag = 1; maskurg(0); recvurg = 0; } else { if (!transflag) syslog(LOG_ERR, "Internal: flagxfer(0): " "no active transfer"); maskurg(1); transflag = 0; } } /* * Returns 0 if OK to resume or -1 if abort requested. */ static int myoob(void) { char *cp; int ret; if (!transflag) { syslog(LOG_ERR, "Internal: myoob() while no transfer"); return (0); } cp = tmpline; - ret = getline(cp, 7, stdin); + ret = get_line(cp, 7, stdin); if (ret == -1) { reply(221, "You could at least say goodbye."); dologout(0); } else if (ret == -2) { /* Ignore truncated command. */ return (0); } upper(cp); if (strcmp(cp, "ABOR\r\n") == 0) { tmpline[0] = '\0'; reply(426, "Transfer aborted. Data connection closed."); reply(226, "Abort successful."); return (-1); } if (strcmp(cp, "STAT\r\n") == 0) { tmpline[0] = '\0'; if (file_size != -1) reply(213, "Status: %jd of %jd bytes transferred.", (intmax_t)byte_count, (intmax_t)file_size); else reply(213, "Status: %jd bytes transferred.", (intmax_t)byte_count); } return (0); } /* * Note: a response of 425 is not mentioned as a possible response to * the PASV command in RFC959. However, it has been blessed as * a legitimate response by Jon Postel in a telephone conversation * with Rick Adams on 25 Jan 89. */ void passive(void) { socklen_t len; int on; char *p, *a; if (pdata >= 0) /* close old port if one set */ close(pdata); pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); if (pdata < 0) { perror_reply(425, "Can't open passive connection"); return; } on = 1; if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); (void) seteuid(0); #ifdef IP_PORTRANGE if (ctrl_addr.su_family == AF_INET) { on = restricted_data_ports ? IP_PORTRANGE_HIGH : IP_PORTRANGE_DEFAULT; if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, &on, sizeof(on)) < 0) goto pasv_error; } #endif #ifdef IPV6_PORTRANGE if (ctrl_addr.su_family == AF_INET6) { on = restricted_data_ports ? IPV6_PORTRANGE_HIGH : IPV6_PORTRANGE_DEFAULT; if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, &on, sizeof(on)) < 0) goto pasv_error; } #endif pasv_addr = ctrl_addr; pasv_addr.su_port = 0; if (bind(pdata, (struct sockaddr *)&pasv_addr, pasv_addr.su_len) < 0) goto pasv_error; (void) seteuid(pw->pw_uid); len = sizeof(pasv_addr); if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) goto pasv_error; if (listen(pdata, 1) < 0) goto pasv_error; if (pasv_addr.su_family == AF_INET) a = (char *) &pasv_addr.su_sin.sin_addr; else if (pasv_addr.su_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; else goto pasv_error; p = (char *) &pasv_addr.su_port; #define UC(b) (((int) b) & 0xff) reply(227, "Entering Passive Mode (%d,%d,%d,%d,%d,%d)", UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), UC(p[0]), UC(p[1])); return; pasv_error: (void) seteuid(pw->pw_uid); (void) close(pdata); pdata = -1; perror_reply(425, "Can't open passive connection"); return; } /* * Long Passive defined in RFC 1639. * 228 Entering Long Passive Mode * (af, hal, h1, h2, h3,..., pal, p1, p2...) */ void long_passive(char *cmd, int pf) { socklen_t len; int on; char *p, *a; if (pdata >= 0) /* close old port if one set */ close(pdata); if (pf != PF_UNSPEC) { if (ctrl_addr.su_family != pf) { switch (ctrl_addr.su_family) { case AF_INET: pf = 1; break; case AF_INET6: pf = 2; break; default: pf = 0; break; } /* * XXX * only EPRT/EPSV ready clients will understand this */ if (strcmp(cmd, "EPSV") == 0 && pf) { reply(522, "Network protocol mismatch, " "use (%d)", pf); } else reply(501, "Network protocol mismatch."); /*XXX*/ return; } } pdata = socket(ctrl_addr.su_family, SOCK_STREAM, 0); if (pdata < 0) { perror_reply(425, "Can't open passive connection"); return; } on = 1; if (setsockopt(pdata, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) syslog(LOG_WARNING, "pdata setsockopt (SO_REUSEADDR): %m"); (void) seteuid(0); pasv_addr = ctrl_addr; pasv_addr.su_port = 0; len = pasv_addr.su_len; #ifdef IP_PORTRANGE if (ctrl_addr.su_family == AF_INET) { on = restricted_data_ports ? IP_PORTRANGE_HIGH : IP_PORTRANGE_DEFAULT; if (setsockopt(pdata, IPPROTO_IP, IP_PORTRANGE, &on, sizeof(on)) < 0) goto pasv_error; } #endif #ifdef IPV6_PORTRANGE if (ctrl_addr.su_family == AF_INET6) { on = restricted_data_ports ? IPV6_PORTRANGE_HIGH : IPV6_PORTRANGE_DEFAULT; if (setsockopt(pdata, IPPROTO_IPV6, IPV6_PORTRANGE, &on, sizeof(on)) < 0) goto pasv_error; } #endif if (bind(pdata, (struct sockaddr *)&pasv_addr, len) < 0) goto pasv_error; (void) seteuid(pw->pw_uid); if (getsockname(pdata, (struct sockaddr *) &pasv_addr, &len) < 0) goto pasv_error; if (listen(pdata, 1) < 0) goto pasv_error; #define UC(b) (((int) b) & 0xff) if (strcmp(cmd, "LPSV") == 0) { p = (char *)&pasv_addr.su_port; switch (pasv_addr.su_family) { case AF_INET: a = (char *) &pasv_addr.su_sin.sin_addr; v4_reply: reply(228, "Entering Long Passive Mode (%d,%d,%d,%d,%d,%d,%d,%d,%d)", 4, 4, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), 2, UC(p[0]), UC(p[1])); return; case AF_INET6: if (IN6_IS_ADDR_V4MAPPED(&pasv_addr.su_sin6.sin6_addr)) { a = (char *) &pasv_addr.su_sin6.sin6_addr.s6_addr[12]; goto v4_reply; } a = (char *) &pasv_addr.su_sin6.sin6_addr; reply(228, "Entering Long Passive Mode " "(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)", 6, 16, UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]), UC(a[4]), UC(a[5]), UC(a[6]), UC(a[7]), UC(a[8]), UC(a[9]), UC(a[10]), UC(a[11]), UC(a[12]), UC(a[13]), UC(a[14]), UC(a[15]), 2, UC(p[0]), UC(p[1])); return; } } else if (strcmp(cmd, "EPSV") == 0) { switch (pasv_addr.su_family) { case AF_INET: case AF_INET6: reply(229, "Entering Extended Passive Mode (|||%d|)", ntohs(pasv_addr.su_port)); return; } } else { /* more proper error code? */ } pasv_error: (void) seteuid(pw->pw_uid); (void) close(pdata); pdata = -1; perror_reply(425, "Can't open passive connection"); return; } /* * Generate unique name for file with basename "local" * and open the file in order to avoid possible races. * Try "local" first, then "local.1", "local.2" etc, up to "local.99". * Return descriptor to the file, set "name" to its name. * * Generates failure reply on error. */ static int guniquefd(char *local, char **name) { static char new[MAXPATHLEN]; struct stat st; char *cp; int count; int fd; cp = strrchr(local, '/'); if (cp) *cp = '\0'; if (stat(cp ? local : ".", &st) < 0) { perror_reply(553, cp ? local : "."); return (-1); } if (cp) { /* * Let not overwrite dirname with counter suffix. * -4 is for /nn\0 * In this extreme case dot won't be put in front of suffix. */ if (strlen(local) > sizeof(new) - 4) { reply(553, "Pathname too long."); return (-1); } *cp = '/'; } /* -4 is for the .nn we put on the end below */ (void) snprintf(new, sizeof(new) - 4, "%s", local); cp = new + strlen(new); /* * Don't generate dotfile unless requested explicitly. * This covers the case when basename gets truncated off * by buffer size. */ if (cp > new && cp[-1] != '/') *cp++ = '.'; for (count = 0; count < 100; count++) { /* At count 0 try unmodified name */ if (count) (void)sprintf(cp, "%d", count); if ((fd = open(count ? new : local, O_RDWR | O_CREAT | O_EXCL, 0666)) >= 0) { *name = count ? new : local; return (fd); } if (errno != EEXIST) { perror_reply(553, count ? new : local); return (-1); } } reply(452, "Unique file name cannot be created."); return (-1); } /* * Format and send reply containing system error number. */ void perror_reply(int code, char *string) { reply(code, "%s: %s.", string, strerror(errno)); } static char *onefile[] = { "", 0 }; void send_file_list(char *whichf) { struct stat st; DIR *dirp = NULL; struct dirent *dir; FILE *dout = NULL; char **dirlist, *dirname; int simple = 0; int freeglob = 0; glob_t gl; if (strpbrk(whichf, "~{[*?") != NULL) { int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE; memset(&gl, 0, sizeof(gl)); gl.gl_matchc = MAXGLOBARGS; flags |= GLOB_LIMIT; freeglob = 1; if (glob(whichf, flags, 0, &gl)) { reply(550, "No matching files found."); goto out; } else if (gl.gl_pathc == 0) { errno = ENOENT; perror_reply(550, whichf); goto out; } dirlist = gl.gl_pathv; } else { onefile[0] = whichf; dirlist = onefile; simple = 1; } while ((dirname = *dirlist++)) { if (stat(dirname, &st) < 0) { /* * If user typed "ls -l", etc, and the client * used NLST, do what the user meant. */ if (dirname[0] == '-' && *dirlist == NULL && dout == NULL) retrieve(_PATH_LS " %s", dirname); else perror_reply(550, whichf); goto out; } if (S_ISREG(st.st_mode)) { if (dout == NULL) { dout = dataconn("file list", -1, "w"); if (dout == NULL) goto out; STARTXFER; } START_UNSAFE; fprintf(dout, "%s%s\n", dirname, type == TYPE_A ? "\r" : ""); END_UNSAFE; if (ferror(dout)) goto data_err; byte_count += strlen(dirname) + (type == TYPE_A ? 2 : 1); CHECKOOB(goto abrt); continue; } else if (!S_ISDIR(st.st_mode)) continue; if ((dirp = opendir(dirname)) == NULL) continue; while ((dir = readdir(dirp)) != NULL) { char nbuf[MAXPATHLEN]; CHECKOOB(goto abrt); if (dir->d_name[0] == '.' && dir->d_namlen == 1) continue; if (dir->d_name[0] == '.' && dir->d_name[1] == '.' && dir->d_namlen == 2) continue; snprintf(nbuf, sizeof(nbuf), "%s/%s", dirname, dir->d_name); /* * We have to do a stat to insure it's * not a directory or special file. */ if (simple || (stat(nbuf, &st) == 0 && S_ISREG(st.st_mode))) { if (dout == NULL) { dout = dataconn("file list", -1, "w"); if (dout == NULL) goto out; STARTXFER; } START_UNSAFE; if (nbuf[0] == '.' && nbuf[1] == '/') fprintf(dout, "%s%s\n", &nbuf[2], type == TYPE_A ? "\r" : ""); else fprintf(dout, "%s%s\n", nbuf, type == TYPE_A ? "\r" : ""); END_UNSAFE; if (ferror(dout)) goto data_err; byte_count += strlen(nbuf) + (type == TYPE_A ? 2 : 1); CHECKOOB(goto abrt); } } (void) closedir(dirp); dirp = NULL; } if (dout == NULL) reply(550, "No files found."); else if (ferror(dout)) data_err: perror_reply(550, "Data connection"); else reply(226, "Transfer complete."); out: if (dout) { ENDXFER; abrt: (void) fclose(dout); data = -1; pdata = -1; } if (dirp) (void) closedir(dirp); if (freeglob) { freeglob = 0; globfree(&gl); } } void reapchild(int signo) { while (waitpid(-1, NULL, WNOHANG) > 0); } #ifdef OLD_SETPROCTITLE /* * Clobber argv so ps will show what we're doing. (Stolen from sendmail.) * Warning, since this is usually started from inetd.conf, it often doesn't * have much of an environment or arglist to overwrite. */ void setproctitle(const char *fmt, ...) { int i; va_list ap; char *p, *bp, ch; char buf[LINE_MAX]; va_start(ap, fmt); (void)vsnprintf(buf, sizeof(buf), fmt, ap); /* make ps print our process name */ p = Argv[0]; *p++ = '-'; i = strlen(buf); if (i > LastArgv - p - 2) { i = LastArgv - p - 2; buf[i] = '\0'; } bp = buf; while (ch = *bp++) if (ch != '\n' && ch != '\r') *p++ = ch; while (p < LastArgv) *p++ = ' '; } #endif /* OLD_SETPROCTITLE */ static void appendf(char **strp, char *fmt, ...) { va_list ap; char *ostr, *p; va_start(ap, fmt); vasprintf(&p, fmt, ap); va_end(ap); if (p == NULL) fatalerror("Ran out of memory."); if (*strp == NULL) *strp = p; else { ostr = *strp; asprintf(strp, "%s%s", ostr, p); if (*strp == NULL) fatalerror("Ran out of memory."); free(ostr); } } static void logcmd(char *cmd, char *file1, char *file2, off_t cnt) { char *msg = NULL; char wd[MAXPATHLEN + 1]; if (logging <= 1) return; if (getcwd(wd, sizeof(wd) - 1) == NULL) strcpy(wd, strerror(errno)); appendf(&msg, "%s", cmd); if (file1) appendf(&msg, " %s", file1); if (file2) appendf(&msg, " %s", file2); if (cnt >= 0) appendf(&msg, " = %jd bytes", (intmax_t)cnt); appendf(&msg, " (wd: %s", wd); if (guest || dochroot) appendf(&msg, "; chrooted"); appendf(&msg, ")"); syslog(LOG_INFO, "%s", msg); free(msg); } static void logxfer(char *name, off_t size, time_t start) { char buf[MAXPATHLEN + 1024]; char path[MAXPATHLEN + 1]; time_t now; if (statfd >= 0) { time(&now); if (realpath(name, path) == NULL) { syslog(LOG_NOTICE, "realpath failed on %s: %m", path); return; } snprintf(buf, sizeof(buf), "%.20s!%s!%s!%s!%jd!%ld\n", ctime(&now)+4, ident, remotehost, path, (intmax_t)size, (long)(now - start + (now == start))); write(statfd, buf, strlen(buf)); } } static char * doublequote(char *s) { int n; char *p, *s2; for (p = s, n = 0; *p; p++) if (*p == '"') n++; if ((s2 = malloc(p - s + n + 1)) == NULL) return (NULL); for (p = s2; *s; s++, p++) { if ((*p = *s) == '"') *(++p) = '"'; } *p = '\0'; return (s2); } /* setup server socket for specified address family */ /* if af is PF_UNSPEC more than one socket may be returned */ /* the returned list is dynamically allocated, so caller needs to free it */ static int * socksetup(int af, char *bindname, const char *bindport) { struct addrinfo hints, *res, *r; int error, maxs, *s, *socks; const int on = 1; memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_PASSIVE; hints.ai_family = af; hints.ai_socktype = SOCK_STREAM; error = getaddrinfo(bindname, bindport, &hints, &res); if (error) { syslog(LOG_ERR, "%s", gai_strerror(error)); if (error == EAI_SYSTEM) syslog(LOG_ERR, "%s", strerror(errno)); return NULL; } /* Count max number of sockets we may open */ for (maxs = 0, r = res; r; r = r->ai_next, maxs++) ; socks = malloc((maxs + 1) * sizeof(int)); if (!socks) { freeaddrinfo(res); syslog(LOG_ERR, "couldn't allocate memory for sockets"); return NULL; } *socks = 0; /* num of sockets counter at start of array */ s = socks + 1; for (r = res; r; r = r->ai_next) { *s = socket(r->ai_family, r->ai_socktype, r->ai_protocol); if (*s < 0) { syslog(LOG_DEBUG, "control socket: %m"); continue; } if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) syslog(LOG_WARNING, "control setsockopt (SO_REUSEADDR): %m"); if (r->ai_family == AF_INET6) { if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) < 0) syslog(LOG_WARNING, "control setsockopt (IPV6_V6ONLY): %m"); } if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) { syslog(LOG_DEBUG, "control bind: %m"); close(*s); continue; } (*socks)++; s++; } if (res) freeaddrinfo(res); if (*socks == 0) { syslog(LOG_ERR, "control socket: Couldn't bind to any socket"); free(socks); return NULL; } return(socks); } Index: head/usr.bin/gencat/gencat.c =================================================================== --- head/usr.bin/gencat/gencat.c (revision 299355) +++ head/usr.bin/gencat/gencat.c (revision 299356) @@ -1,694 +1,694 @@ /* ex:ts=4 */ /* $NetBSD: gencat.c,v 1.18 2003/10/27 00:12:43 lukem Exp $ */ /* * Copyright (c) 1996 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by J.T. Conklin. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /*********************************************************** Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts. All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that Alfalfa's name not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. ALPHALPHA DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL ALPHALPHA BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. If you make any modifications, bugfixes or other changes to this software we'd appreciate it if you could send a copy to us so we can keep things up-to-date. Many thanks. Kee Hinckley Alfalfa Software, Inc. 267 Allston St., #3 Cambridge, MA 02139 USA nazgul@alfalfa.com ******************************************************************/ #include __FBSDID("$FreeBSD$"); #define _NLS_PRIVATE #include #include #include /* for htonl() */ #include #include #include #include #include #include #include #include #include struct _msgT { long msgId; char *str; LIST_ENTRY(_msgT) entries; }; struct _setT { long setId; LIST_HEAD(msghead, _msgT) msghead; LIST_ENTRY(_setT) entries; }; static LIST_HEAD(sethead, _setT) sethead; static struct _setT *curSet; static char *curline = NULL; static long lineno = 0; static char *cskip(char *); static void error(const char *); -static char *getline(int); +static char *get_line(int); static char *getmsg(int, char *, char); static void warning(const char *, const char *); static char *wskip(char *); static char *xstrdup(const char *); static void *xmalloc(size_t); static void *xrealloc(void *, size_t); void MCParse(int); void MCReadCat(int); void MCWriteCat(int); void MCDelMsg(int); void MCAddMsg(int, const char *); void MCAddSet(int); void MCDelSet(int); void usage(void); int main(int, char **); void usage(void) { fprintf(stderr, "usage: %s catfile msgfile ...\n", getprogname()); exit(1); } int main(int argc, char **argv) { int ofd, ifd; char *catfile = NULL; int c; #define DEPRECATEDMSG 1 #ifdef DEPRECATEDMSG while ((c = getopt(argc, argv, "new")) != -1) { #else while ((c = getopt(argc, argv, "")) != -1) { #endif switch (c) { #ifdef DEPRECATEDMSG case 'n': fprintf(stderr, "WARNING: Usage of \"-new\" argument is deprecated.\n"); case 'e': case 'w': break; #endif case '?': default: usage(); /* NOTREACHED */ } } argc -= optind; argv += optind; if (argc < 2) { usage(); /* NOTREACHED */ } catfile = *argv++; for (; *argv; argv++) { if ((ifd = open(*argv, O_RDONLY)) < 0) err(1, "Unable to read %s", *argv); MCParse(ifd); close(ifd); } if ((ofd = open(catfile, O_WRONLY | O_TRUNC | O_CREAT, 0666)) < 0) err(1, "Unable to create a new %s", catfile); MCWriteCat(ofd); exit(0); } static void warning(const char *cptr, const char *msg) { fprintf(stderr, "%s: %s on line %ld\n", getprogname(), msg, lineno); fprintf(stderr, "%s\n", curline); if (cptr) { char *tptr; for (tptr = curline; tptr < cptr; ++tptr) putc(' ', stderr); fprintf(stderr, "^\n"); } } #define CORRUPT() { error("corrupt message catalog"); } #define NOMEM() { error("out of memory"); } static void error(const char *msg) { warning(NULL, msg); exit(1); } static void * xmalloc(size_t len) { void *p; if ((p = malloc(len)) == NULL) NOMEM(); return (p); } static void * xrealloc(void *ptr, size_t size) { if ((ptr = realloc(ptr, size)) == NULL) NOMEM(); return (ptr); } static char * xstrdup(const char *str) { char *nstr; if ((nstr = strdup(str)) == NULL) NOMEM(); return (nstr); } static char * -getline(int fd) +get_line(int fd) { static long curlen = BUFSIZ; static char buf[BUFSIZ], *bptr = buf, *bend = buf; char *cptr, *cend; long buflen; if (!curline) { curline = xmalloc(curlen); } ++lineno; cptr = curline; cend = curline + curlen; for (;;) { for (; bptr < bend && cptr < cend; ++cptr, ++bptr) { if (*bptr == '\n') { *cptr = '\0'; ++bptr; return (curline); } else *cptr = *bptr; } if (cptr == cend) { cptr = curline = xrealloc(curline, curlen *= 2); cend = curline + curlen; } if (bptr == bend) { buflen = read(fd, buf, BUFSIZ); if (buflen <= 0) { if (cptr > curline) { *cptr = '\0'; return (curline); } return (NULL); } bend = buf + buflen; bptr = buf; } } } static char * wskip(char *cptr) { if (!*cptr || !isspace((unsigned char) *cptr)) { warning(cptr, "expected a space"); return (cptr); } while (*cptr && isspace((unsigned char) *cptr)) ++cptr; return (cptr); } static char * cskip(char *cptr) { if (!*cptr || isspace((unsigned char) *cptr)) { warning(cptr, "wasn't expecting a space"); return (cptr); } while (*cptr && !isspace((unsigned char) *cptr)) ++cptr; return (cptr); } static char * getmsg(int fd, char *cptr, char quote) { static char *msg = NULL; static long msglen = 0; long clen, i; char *tptr; if (quote && *cptr == quote) { ++cptr; } clen = strlen(cptr) + 1; if (clen > msglen) { if (msglen) msg = xrealloc(msg, clen); else msg = xmalloc(clen); msglen = clen; } tptr = msg; while (*cptr) { if (quote && *cptr == quote) { char *tmp; tmp = cptr + 1; if (*tmp && (!isspace((unsigned char) *tmp) || *wskip(tmp))) { warning(cptr, "unexpected quote character, ignoring"); *tptr++ = *cptr++; } else { *cptr = '\0'; } } else if (*cptr == '\\') { ++cptr; switch (*cptr) { case '\0': - cptr = getline(fd); + cptr = get_line(fd); if (!cptr) error("premature end of file"); msglen += strlen(cptr); i = tptr - msg; msg = xrealloc(msg, msglen); tptr = msg + i; break; #define CASEOF(CS, CH) \ case CS: \ *tptr++ = CH; \ ++cptr; \ break; \ CASEOF('n', '\n'); CASEOF('t', '\t'); CASEOF('v', '\v'); CASEOF('b', '\b'); CASEOF('r', '\r'); CASEOF('f', '\f'); CASEOF('"', '"'); CASEOF('\\', '\\'); default: if (quote && *cptr == quote) { *tptr++ = *cptr++; } else if (isdigit((unsigned char) *cptr)) { *tptr = 0; for (i = 0; i < 3; ++i) { if (!isdigit((unsigned char) *cptr)) break; if (*cptr > '7') warning(cptr, "octal number greater than 7?!"); *tptr *= 8; *tptr += (*cptr - '0'); ++cptr; } } else { warning(cptr, "unrecognized escape sequence"); } break; } } else { *tptr++ = *cptr++; } } *tptr = '\0'; return (msg); } void MCParse(int fd) { char *cptr, *str; int setid, msgid = 0; char quote = 0; /* XXX: init sethead? */ - while ((cptr = getline(fd))) { + while ((cptr = get_line(fd))) { if (*cptr == '$') { ++cptr; if (strncmp(cptr, "set", 3) == 0) { cptr += 3; cptr = wskip(cptr); setid = atoi(cptr); MCAddSet(setid); msgid = 0; } else if (strncmp(cptr, "delset", 6) == 0) { cptr += 6; cptr = wskip(cptr); setid = atoi(cptr); MCDelSet(setid); } else if (strncmp(cptr, "quote", 5) == 0) { cptr += 5; if (!*cptr) quote = 0; else { cptr = wskip(cptr); if (!*cptr) quote = 0; else quote = *cptr; } } else if (isspace((unsigned char) *cptr)) { ; } else { if (*cptr) { cptr = wskip(cptr); if (*cptr) warning(cptr, "unrecognized line"); } } } else { /* * First check for (and eat) empty lines.... */ if (!*cptr) continue; /* * We have a digit? Start of a message. Else, * syntax error. */ if (isdigit((unsigned char) *cptr)) { msgid = atoi(cptr); cptr = cskip(cptr); cptr = wskip(cptr); /* if (*cptr) ++cptr; */ } else { warning(cptr, "neither blank line nor start of a message id"); continue; } /* * If we have a message ID, but no message, * then this means "delete this message id * from the catalog". */ if (!*cptr) { MCDelMsg(msgid); } else { str = getmsg(fd, cptr, quote); MCAddMsg(msgid, str); } } } } /* * Write message catalog. * * The message catalog is first converted from its internal to its * external representation in a chunk of memory allocated for this * purpose. Then the completed catalog is written. This approach * avoids additional housekeeping variables and/or a lot of seeks * that would otherwise be required. */ void MCWriteCat(int fd) { int nsets; /* number of sets */ int nmsgs; /* number of msgs */ int string_size; /* total size of string pool */ int msgcat_size; /* total size of message catalog */ void *msgcat; /* message catalog data */ struct _nls_cat_hdr *cat_hdr; struct _nls_set_hdr *set_hdr; struct _nls_msg_hdr *msg_hdr; char *strings; struct _setT *set; struct _msgT *msg; int msg_index; int msg_offset; /* determine number of sets, number of messages, and size of the * string pool */ nsets = 0; nmsgs = 0; string_size = 0; for (set = sethead.lh_first; set != NULL; set = set->entries.le_next) { nsets++; for (msg = set->msghead.lh_first; msg != NULL; msg = msg->entries.le_next) { nmsgs++; string_size += strlen(msg->str) + 1; } } #ifdef DEBUG printf("number of sets: %d\n", nsets); printf("number of msgs: %d\n", nmsgs); printf("string pool size: %d\n", string_size); #endif /* determine size and then allocate buffer for constructing external * message catalog representation */ msgcat_size = sizeof(struct _nls_cat_hdr) + (nsets * sizeof(struct _nls_set_hdr)) + (nmsgs * sizeof(struct _nls_msg_hdr)) + string_size; msgcat = xmalloc(msgcat_size); memset(msgcat, '\0', msgcat_size); /* fill in msg catalog header */ cat_hdr = (struct _nls_cat_hdr *) msgcat; cat_hdr->__magic = htonl(_NLS_MAGIC); cat_hdr->__nsets = htonl(nsets); cat_hdr->__mem = htonl(msgcat_size - sizeof(struct _nls_cat_hdr)); cat_hdr->__msg_hdr_offset = htonl(nsets * sizeof(struct _nls_set_hdr)); cat_hdr->__msg_txt_offset = htonl(nsets * sizeof(struct _nls_set_hdr) + nmsgs * sizeof(struct _nls_msg_hdr)); /* compute offsets for set & msg header tables and string pool */ set_hdr = (struct _nls_set_hdr *)(void *)((char *)msgcat + sizeof(struct _nls_cat_hdr)); msg_hdr = (struct _nls_msg_hdr *)(void *)((char *)msgcat + sizeof(struct _nls_cat_hdr) + nsets * sizeof(struct _nls_set_hdr)); strings = (char *) msgcat + sizeof(struct _nls_cat_hdr) + nsets * sizeof(struct _nls_set_hdr) + nmsgs * sizeof(struct _nls_msg_hdr); msg_index = 0; msg_offset = 0; for (set = sethead.lh_first; set != NULL; set = set->entries.le_next) { nmsgs = 0; for (msg = set->msghead.lh_first; msg != NULL; msg = msg->entries.le_next) { int msg_len = strlen(msg->str) + 1; msg_hdr->__msgno = htonl(msg->msgId); msg_hdr->__msglen = htonl(msg_len); msg_hdr->__offset = htonl(msg_offset); memcpy(strings, msg->str, msg_len); strings += msg_len; msg_offset += msg_len; nmsgs++; msg_hdr++; } set_hdr->__setno = htonl(set->setId); set_hdr->__nmsgs = htonl(nmsgs); set_hdr->__index = htonl(msg_index); msg_index += nmsgs; set_hdr++; } /* write out catalog. XXX: should this be done in small chunks? */ write(fd, msgcat, msgcat_size); } void MCAddSet(int setId) { struct _setT *p, *q; if (setId <= 0) { error("setId's must be greater than zero"); /* NOTREACHED */ } if (setId > NL_SETMAX) { error("setId exceeds limit"); /* NOTREACHED */ } p = sethead.lh_first; q = NULL; for (; p != NULL && p->setId < setId; q = p, p = p->entries.le_next); if (p && p->setId == setId) { ; } else { p = xmalloc(sizeof(struct _setT)); memset(p, '\0', sizeof(struct _setT)); LIST_INIT(&p->msghead); p->setId = setId; if (q == NULL) { LIST_INSERT_HEAD(&sethead, p, entries); } else { LIST_INSERT_AFTER(q, p, entries); } } curSet = p; } void MCAddMsg(int msgId, const char *str) { struct _msgT *p, *q; if (!curSet) error("can't specify a message when no set exists"); if (msgId <= 0) { error("msgId's must be greater than zero"); /* NOTREACHED */ } if (msgId > NL_MSGMAX) { error("msgID exceeds limit"); /* NOTREACHED */ } p = curSet->msghead.lh_first; q = NULL; for (; p != NULL && p->msgId < msgId; q = p, p = p->entries.le_next); if (p && p->msgId == msgId) { free(p->str); } else { p = xmalloc(sizeof(struct _msgT)); memset(p, '\0', sizeof(struct _msgT)); if (q == NULL) { LIST_INSERT_HEAD(&curSet->msghead, p, entries); } else { LIST_INSERT_AFTER(q, p, entries); } } p->msgId = msgId; p->str = xstrdup(str); } void MCDelSet(int setId) { struct _setT *set; struct _msgT *msg; set = sethead.lh_first; for (; set != NULL && set->setId < setId; set = set->entries.le_next); if (set && set->setId == setId) { msg = set->msghead.lh_first; while (msg) { free(msg->str); LIST_REMOVE(msg, entries); } LIST_REMOVE(set, entries); return; } warning(NULL, "specified set doesn't exist"); } void MCDelMsg(int msgId) { struct _msgT *msg; if (!curSet) error("you can't delete a message before defining the set"); msg = curSet->msghead.lh_first; for (; msg != NULL && msg->msgId < msgId; msg = msg->entries.le_next); if (msg && msg->msgId == msgId) { free(msg->str); LIST_REMOVE(msg, entries); return; } warning(NULL, "specified msg doesn't exist"); } Index: head/usr.bin/uudecode/uudecode.c =================================================================== --- head/usr.bin/uudecode/uudecode.c (revision 299355) +++ head/usr.bin/uudecode/uudecode.c (revision 299356) @@ -1,466 +1,466 @@ /*- * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #if 0 #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1983, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint static char sccsid[] = "@(#)uudecode.c 8.2 (Berkeley) 4/2/94"; #endif /* not lint */ #endif #include __FBSDID("$FreeBSD$"); /* * uudecode [file ...] * * create the specified file, decoding as you go. * used with uuencode. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static const char *infile, *outfile; static FILE *infp, *outfp; static int base64, cflag, iflag, oflag, pflag, rflag, sflag; static void usage(void); static int decode(void); static int decode2(void); static int uu_decode(void); static int base64_decode(void); int main(int argc, char *argv[]) { int rval, ch; if (strcmp(basename(argv[0]), "b64decode") == 0) base64 = 1; while ((ch = getopt(argc, argv, "cimo:prs")) != -1) { switch (ch) { case 'c': if (oflag || rflag) usage(); cflag = 1; /* multiple uudecode'd files */ break; case 'i': iflag = 1; /* ask before override files */ break; case 'm': base64 = 1; break; case 'o': if (cflag || pflag || rflag || sflag) usage(); oflag = 1; /* output to the specified file */ sflag = 1; /* do not strip pathnames for output */ outfile = optarg; /* set the output filename */ break; case 'p': if (oflag) usage(); pflag = 1; /* print output to stdout */ break; case 'r': if (cflag || oflag) usage(); rflag = 1; /* decode raw data */ break; case 's': if (oflag) usage(); sflag = 1; /* do not strip pathnames for output */ break; default: usage(); } } argc -= optind; argv += optind; if (*argv != NULL) { rval = 0; do { infp = fopen(infile = *argv, "r"); if (infp == NULL) { warn("%s", *argv); rval = 1; continue; } rval |= decode(); fclose(infp); } while (*++argv); } else { infile = "stdin"; infp = stdin; rval = decode(); } exit(rval); } static int decode(void) { int r, v; if (rflag) { /* relaxed alternative to decode2() */ outfile = "/dev/stdout"; outfp = stdout; if (base64) return (base64_decode()); else return (uu_decode()); } v = decode2(); if (v == EOF) { warnx("%s: missing or bad \"begin\" line", infile); return (1); } for (r = v; cflag; r |= v) { v = decode2(); if (v == EOF) break; } return (r); } static int decode2(void) { int flags, fd, mode; size_t n, m; char *p, *q; void *handle; struct passwd *pw; struct stat st; char buf[MAXPATHLEN + 1]; base64 = 0; /* search for header line */ for (;;) { if (fgets(buf, sizeof(buf), infp) == NULL) return (EOF); p = buf; if (strncmp(p, "begin-base64 ", 13) == 0) { base64 = 1; p += 13; } else if (strncmp(p, "begin ", 6) == 0) p += 6; else continue; /* p points to mode */ q = strchr(p, ' '); if (q == NULL) continue; *q++ = '\0'; /* q points to filename */ n = strlen(q); while (n > 0 && (q[n-1] == '\n' || q[n-1] == '\r')) q[--n] = '\0'; /* found valid header? */ if (n > 0) break; } handle = setmode(p); if (handle == NULL) { warnx("%s: unable to parse file mode", infile); return (1); } mode = getmode(handle, 0) & 0666; free(handle); if (sflag) { /* don't strip, so try ~user/file expansion */ p = NULL; pw = NULL; if (*q == '~') p = strchr(q, '/'); if (p != NULL) { *p = '\0'; pw = getpwnam(q + 1); *p = '/'; } if (pw != NULL) { n = strlen(pw->pw_dir); if (buf + n > p) { /* make room */ m = strlen(p); if (sizeof(buf) < n + m) { warnx("%s: bad output filename", infile); return (1); } p = memmove(buf + n, p, m); } q = memcpy(p - n, pw->pw_dir, n); } } else { /* strip down to leaf name */ p = strrchr(q, '/'); if (p != NULL) q = p + 1; } if (!oflag) outfile = q; /* POSIX says "/dev/stdout" is a 'magic cookie' not a special file. */ if (pflag || strcmp(outfile, "/dev/stdout") == 0) outfp = stdout; else { flags = O_WRONLY | O_CREAT | O_EXCL; if (lstat(outfile, &st) == 0) { if (iflag) { warnc(EEXIST, "%s: %s", infile, outfile); return (0); } switch (st.st_mode & S_IFMT) { case S_IFREG: case S_IFLNK: /* avoid symlink attacks */ if (unlink(outfile) == 0 || errno == ENOENT) break; warn("%s: unlink %s", infile, outfile); return (1); case S_IFDIR: warnc(EISDIR, "%s: %s", infile, outfile); return (1); default: if (oflag) { /* trust command-line names */ flags &= ~O_EXCL; break; } warnc(EEXIST, "%s: %s", infile, outfile); return (1); } } else if (errno != ENOENT) { warn("%s: %s", infile, outfile); return (1); } if ((fd = open(outfile, flags, mode)) < 0 || (outfp = fdopen(fd, "w")) == NULL) { warn("%s: %s", infile, outfile); return (1); } } if (base64) return (base64_decode()); else return (uu_decode()); } static int -getline(char *buf, size_t size) +get_line(char *buf, size_t size) { if (fgets(buf, size, infp) != NULL) return (2); if (rflag) return (0); warnx("%s: %s: short file", infile, outfile); return (1); } static int checkend(const char *ptr, const char *end, const char *msg) { size_t n; n = strlen(end); if (strncmp(ptr, end, n) != 0 || strspn(ptr + n, " \t\r\n") != strlen(ptr + n)) { warnx("%s: %s: %s", infile, outfile, msg); return (1); } if (fclose(outfp) != 0) { warn("%s: %s", infile, outfile); return (1); } return (0); } static int uu_decode(void) { int i, ch; char *p; char buf[MAXPATHLEN+1]; /* for each input line */ for (;;) { - switch (getline(buf, sizeof(buf))) { + switch (get_line(buf, sizeof(buf))) { case 0: return (0); case 1: return (1); } #define DEC(c) (((c) - ' ') & 077) /* single character decode */ #define IS_DEC(c) ( (((c) - ' ') >= 0) && (((c) - ' ') <= 077 + 1) ) #define OUT_OF_RANGE do { \ warnx("%s: %s: character out of range: [%d-%d]", \ infile, outfile, 1 + ' ', 077 + ' ' + 1); \ return (1); \ } while (0) /* * `i' is used to avoid writing out all the characters * at the end of the file. */ p = buf; if ((i = DEC(*p)) <= 0) break; for (++p; i > 0; p += 4, i -= 3) if (i >= 3) { if (!(IS_DEC(*p) && IS_DEC(*(p + 1)) && IS_DEC(*(p + 2)) && IS_DEC(*(p + 3)))) OUT_OF_RANGE; ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4; putc(ch, outfp); ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2; putc(ch, outfp); ch = DEC(p[2]) << 6 | DEC(p[3]); putc(ch, outfp); } else { if (i >= 1) { if (!(IS_DEC(*p) && IS_DEC(*(p + 1)))) OUT_OF_RANGE; ch = DEC(p[0]) << 2 | DEC(p[1]) >> 4; putc(ch, outfp); } if (i >= 2) { if (!(IS_DEC(*(p + 1)) && IS_DEC(*(p + 2)))) OUT_OF_RANGE; ch = DEC(p[1]) << 4 | DEC(p[2]) >> 2; putc(ch, outfp); } if (i >= 3) { if (!(IS_DEC(*(p + 2)) && IS_DEC(*(p + 3)))) OUT_OF_RANGE; ch = DEC(p[2]) << 6 | DEC(p[3]); putc(ch, outfp); } } } - switch (getline(buf, sizeof(buf))) { + switch (get_line(buf, sizeof(buf))) { case 0: return (0); case 1: return (1); default: return (checkend(buf, "end", "no \"end\" line")); } } static int base64_decode(void) { int n, count, count4; char inbuf[MAXPATHLEN + 1], *p; unsigned char outbuf[MAXPATHLEN * 4]; char leftover[MAXPATHLEN + 1]; leftover[0] = '\0'; for (;;) { strcpy(inbuf, leftover); - switch (getline(inbuf + strlen(inbuf), + switch (get_line(inbuf + strlen(inbuf), sizeof(inbuf) - strlen(inbuf))) { case 0: return (0); case 1: return (1); } count = 0; count4 = -1; p = inbuf; while (*p != '\0') { /* * Base64 encoded strings have the following * characters in them: A-Z, a-z, 0-9 and +, / and = */ if (isalnum(*p) || *p == '+' || *p == '/' || *p == '=') count++; if (count % 4 == 0) count4 = p - inbuf; p++; } strcpy(leftover, inbuf + count4 + 1); inbuf[count4 + 1] = 0; n = b64_pton(inbuf, outbuf, sizeof(outbuf)); if (n < 0) break; fwrite(outbuf, 1, n, outfp); } return (checkend(inbuf, "====", "error decoding base64 input stream")); } static void usage(void) { (void)fprintf(stderr, "usage: uudecode [-cimprs] [file ...]\n" " uudecode [-i] -o output_file [file]\n" " b64decode [-cimprs] [file ...]\n" " b64decode [-i] -o output_file [file]\n"); exit(1); } Index: head/usr.sbin/inetd/builtins.c =================================================================== --- head/usr.sbin/inetd/builtins.c (revision 299355) +++ head/usr.sbin/inetd/builtins.c (revision 299356) @@ -1,814 +1,814 @@ /*- * Copyright (c) 1983, 1991, 1993, 1994 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "inetd.h" void chargen_dg(int, struct servtab *); void chargen_stream(int, struct servtab *); void daytime_dg(int, struct servtab *); void daytime_stream(int, struct servtab *); void discard_dg(int, struct servtab *); void discard_stream(int, struct servtab *); void echo_dg(int, struct servtab *); void echo_stream(int, struct servtab *); -static int getline(int, char *, int); +static int get_line(int, char *, int); void iderror(int, int, int, const char *); void ident_stream(int, struct servtab *); void initring(void); uint32_t machtime(void); void machtime_dg(int, struct servtab *); void machtime_stream(int, struct servtab *); char ring[128]; char *endring; struct biltin biltins[] = { /* Echo received data */ { "echo", SOCK_STREAM, 1, -1, echo_stream }, { "echo", SOCK_DGRAM, 0, 1, echo_dg }, /* Internet /dev/null */ { "discard", SOCK_STREAM, 1, -1, discard_stream }, { "discard", SOCK_DGRAM, 0, 1, discard_dg }, /* Return 32 bit time since 1900 */ { "time", SOCK_STREAM, 0, -1, machtime_stream }, { "time", SOCK_DGRAM, 0, 1, machtime_dg }, /* Return human-readable time */ { "daytime", SOCK_STREAM, 0, -1, daytime_stream }, { "daytime", SOCK_DGRAM, 0, 1, daytime_dg }, /* Familiar character generator */ { "chargen", SOCK_STREAM, 1, -1, chargen_stream }, { "chargen", SOCK_DGRAM, 0, 1, chargen_dg }, { "tcpmux", SOCK_STREAM, 1, -1, (bi_fn_t *)tcpmux }, { "auth", SOCK_STREAM, 1, -1, ident_stream }, { NULL, 0, 0, 0, NULL } }; /* * RFC864 Character Generator Protocol. Generates character data without * any regard for input. */ void initring(void) { int i; endring = ring; for (i = 0; i <= 128; ++i) if (isprint(i)) *endring++ = i; } /* Character generator * The RFC says that we should send back a random number of * characters chosen from the range 0 to 512. We send LINESIZ+2. */ /* ARGSUSED */ void chargen_dg(int s, struct servtab *sep) { struct sockaddr_storage ss; static char *rs; int len; socklen_t size; char text[LINESIZ+2]; if (endring == 0) { initring(); rs = ring; } size = sizeof(ss); if (recvfrom(s, text, sizeof(text), 0, (struct sockaddr *)&ss, &size) < 0) return; if (check_loop((struct sockaddr *)&ss, sep)) return; if ((len = endring - rs) >= LINESIZ) memmove(text, rs, LINESIZ); else { memmove(text, rs, len); memmove(text + len, ring, LINESIZ - len); } if (++rs == endring) rs = ring; text[LINESIZ] = '\r'; text[LINESIZ + 1] = '\n'; (void) sendto(s, text, sizeof(text), 0, (struct sockaddr *)&ss, size); } /* Character generator */ /* ARGSUSED */ void chargen_stream(int s, struct servtab *sep) { int len; char *rs, text[LINESIZ+2]; inetd_setproctitle(sep->se_service, s); if (!endring) { initring(); rs = ring; } text[LINESIZ] = '\r'; text[LINESIZ + 1] = '\n'; for (rs = ring;;) { if ((len = endring - rs) >= LINESIZ) memmove(text, rs, LINESIZ); else { memmove(text, rs, len); memmove(text + len, ring, LINESIZ - len); } if (++rs == endring) rs = ring; if (write(s, text, sizeof(text)) != sizeof(text)) break; } exit(0); } /* * RFC867 Daytime Protocol. Sends the current date and time as an ascii * character string without any regard for input. */ /* Return human-readable time of day */ /* ARGSUSED */ void daytime_dg(int s, struct servtab *sep) { char buffer[256]; time_t now; struct sockaddr_storage ss; socklen_t size; now = time((time_t *) 0); size = sizeof(ss); if (recvfrom(s, buffer, sizeof(buffer), 0, (struct sockaddr *)&ss, &size) < 0) return; if (check_loop((struct sockaddr *)&ss, sep)) return; (void) sprintf(buffer, "%.24s\r\n", ctime(&now)); (void) sendto(s, buffer, strlen(buffer), 0, (struct sockaddr *)&ss, size); } /* Return human-readable time of day */ /* ARGSUSED */ void daytime_stream(int s, struct servtab *sep __unused) { char buffer[256]; time_t now; now = time((time_t *) 0); (void) sprintf(buffer, "%.24s\r\n", ctime(&now)); (void) send(s, buffer, strlen(buffer), MSG_EOF); } /* * RFC863 Discard Protocol. Any data received is thrown away and no response * is sent. */ /* Discard service -- ignore data */ /* ARGSUSED */ void discard_dg(int s, struct servtab *sep __unused) { char buffer[BUFSIZE]; (void) read(s, buffer, sizeof(buffer)); } /* Discard service -- ignore data */ /* ARGSUSED */ void discard_stream(int s, struct servtab *sep) { int ret; char buffer[BUFSIZE]; inetd_setproctitle(sep->se_service, s); while (1) { while ((ret = read(s, buffer, sizeof(buffer))) > 0) ; if (ret == 0 || errno != EINTR) break; } exit(0); } /* * RFC862 Echo Protocol. Any data received is sent back to the sender as * received. */ /* Echo service -- echo data back */ /* ARGSUSED */ void echo_dg(int s, struct servtab *sep) { char buffer[65536]; /* Should be sizeof(max datagram). */ int i; socklen_t size; struct sockaddr_storage ss; size = sizeof(ss); if ((i = recvfrom(s, buffer, sizeof(buffer), 0, (struct sockaddr *)&ss, &size)) < 0) return; if (check_loop((struct sockaddr *)&ss, sep)) return; (void) sendto(s, buffer, i, 0, (struct sockaddr *)&ss, size); } /* Echo service -- echo data back */ /* ARGSUSED */ void echo_stream(int s, struct servtab *sep) { char buffer[BUFSIZE]; int i; inetd_setproctitle(sep->se_service, s); while ((i = read(s, buffer, sizeof(buffer))) > 0 && write(s, buffer, i) > 0) ; exit(0); } /* * RFC1413 Identification Protocol. Given a TCP port number pair, return a * character string which identifies the owner of that connection on the * server's system. Extended to allow for ~/.fakeid support and ~/.noident * support. */ /* RFC 1413 says the following are the only errors you can return. */ #define ID_INVALID "INVALID-PORT" /* Port number improperly specified. */ #define ID_NOUSER "NO-USER" /* Port not in use/not identifable. */ #define ID_HIDDEN "HIDDEN-USER" /* Hiden at user's request. */ #define ID_UNKNOWN "UNKNOWN-ERROR" /* Everything else. */ /* Generic ident_stream error-sending func */ /* ARGSUSED */ void iderror(int lport, int fport, int s, const char *er) { char *p; asprintf(&p, "%d , %d : ERROR : %s\r\n", lport, fport, er); if (p == NULL) { syslog(LOG_ERR, "asprintf: %m"); exit(EX_OSERR); } send(s, p, strlen(p), MSG_EOF); free(p); exit(0); } /* Ident service (AKA "auth") */ /* ARGSUSED */ void ident_stream(int s, struct servtab *sep) { struct utsname un; struct stat sb; struct sockaddr_in sin4[2]; #ifdef INET6 struct sockaddr_in6 sin6[2]; #endif struct sockaddr_storage ss[2]; struct xucred uc; struct timeval tv = { 10, 0 }, to; struct passwd *pw = NULL; fd_set fdset; char buf[BUFSIZE], *p, **av, *osname = NULL, e; char idbuf[MAXLOGNAME] = ""; /* Big enough to hold uid in decimal. */ socklen_t socklen; ssize_t ssize; size_t size, bufsiz; int c, fflag = 0, nflag = 0, rflag = 0, argc = 0; int gflag = 0, iflag = 0, Fflag = 0, getcredfail = 0, onreadlen; u_short lport, fport; inetd_setproctitle(sep->se_service, s); /* * Reset getopt() since we are a fork() but not an exec() from * a parent which used getopt() already. */ optind = 1; optreset = 1; /* * Take the internal argument vector and count it out to make an * argument count for getopt. This can be used for any internal * service to read arguments and use getopt() easily. */ for (av = sep->se_argv; *av; av++) argc++; if (argc) { int sec, usec; size_t i; u_int32_t rnd32; while ((c = getopt(argc, sep->se_argv, "d:fFgino:rt:")) != -1) switch (c) { case 'd': if (!gflag) strlcpy(idbuf, optarg, sizeof(idbuf)); break; case 'f': fflag = 1; break; case 'F': fflag = 1; Fflag=1; break; case 'g': gflag = 1; rnd32 = 0; /* Shush, compiler. */ /* * The number of bits in "rnd32" divided * by the number of bits needed per iteration * gives a more optimal way to reload the * random number only when necessary. * * 32 bits from arc4random corresponds to * about 6 base-36 digits, so we reseed evey 6. */ for (i = 0; i < sizeof(idbuf) - 1; i++) { static const char *const base36 = "0123456789" "abcdefghijklmnopqrstuvwxyz"; if (i % 6 == 0) rnd32 = arc4random(); idbuf[i] = base36[rnd32 % 36]; rnd32 /= 36; } idbuf[i] = '\0'; break; case 'i': iflag = 1; break; case 'n': nflag = 1; break; case 'o': osname = optarg; break; case 'r': rflag = 1; break; case 't': switch (sscanf(optarg, "%d.%d", &sec, &usec)) { case 2: tv.tv_usec = usec; /* FALLTHROUGH */ case 1: tv.tv_sec = sec; break; default: if (debug) warnx("bad -t argument"); break; } break; default: break; } } if (osname == NULL) { if (uname(&un) == -1) iderror(0, 0, s, ID_UNKNOWN); osname = un.sysname; } /* * We're going to prepare for and execute reception of a * packet of data from the user. The data is in the format * "local_port , foreign_port\r\n" (with local being the * server's port and foreign being the client's.) */ gettimeofday(&to, NULL); to.tv_sec += tv.tv_sec; to.tv_usec += tv.tv_usec; if (to.tv_usec >= 1000000) { to.tv_usec -= 1000000; to.tv_sec++; } size = 0; bufsiz = sizeof(buf) - 1; FD_ZERO(&fdset); while (bufsiz > 0) { gettimeofday(&tv, NULL); tv.tv_sec = to.tv_sec - tv.tv_sec; tv.tv_usec = to.tv_usec - tv.tv_usec; if (tv.tv_usec < 0) { tv.tv_usec += 1000000; tv.tv_sec--; } if (tv.tv_sec < 0) break; FD_SET(s, &fdset); if (select(s + 1, &fdset, NULL, NULL, &tv) == -1) iderror(0, 0, s, ID_UNKNOWN); if (ioctl(s, FIONREAD, &onreadlen) == -1) iderror(0, 0, s, ID_UNKNOWN); if ((size_t)onreadlen > bufsiz) onreadlen = bufsiz; ssize = read(s, &buf[size], (size_t)onreadlen); if (ssize == -1) iderror(0, 0, s, ID_UNKNOWN); else if (ssize == 0) break; bufsiz -= ssize; size += ssize; if (memchr(&buf[size - ssize], '\n', ssize) != NULL) break; } buf[size] = '\0'; /* Read two characters, and check for a delimiting character */ if (sscanf(buf, "%hu , %hu%c", &lport, &fport, &e) != 3 || isdigit(e)) iderror(0, 0, s, ID_INVALID); /* Send garbage? */ if (gflag) goto printit; /* * If not "real" (-r), send a HIDDEN-USER error for everything. * If -d is used to set a fallback username, this is used to * override it, and the fallback is returned instead. */ if (!rflag) { if (*idbuf == '\0') iderror(lport, fport, s, ID_HIDDEN); goto printit; } /* * We take the input and construct an array of two sockaddr_ins * which contain the local address information and foreign * address information, respectively, used to look up the * credentials for the socket (which are returned by the * sysctl "net.inet.tcp.getcred" when we call it.) */ socklen = sizeof(ss[0]); if (getsockname(s, (struct sockaddr *)&ss[0], &socklen) == -1) iderror(lport, fport, s, ID_UNKNOWN); socklen = sizeof(ss[1]); if (getpeername(s, (struct sockaddr *)&ss[1], &socklen) == -1) iderror(lport, fport, s, ID_UNKNOWN); if (ss[0].ss_family != ss[1].ss_family) iderror(lport, fport, s, ID_UNKNOWN); size = sizeof(uc); switch (ss[0].ss_family) { case AF_INET: sin4[0] = *(struct sockaddr_in *)&ss[0]; sin4[0].sin_port = htons(lport); sin4[1] = *(struct sockaddr_in *)&ss[1]; sin4[1].sin_port = htons(fport); if (sysctlbyname("net.inet.tcp.getcred", &uc, &size, sin4, sizeof(sin4)) == -1) getcredfail = errno; break; #ifdef INET6 case AF_INET6: sin6[0] = *(struct sockaddr_in6 *)&ss[0]; sin6[0].sin6_port = htons(lport); sin6[1] = *(struct sockaddr_in6 *)&ss[1]; sin6[1].sin6_port = htons(fport); if (sysctlbyname("net.inet6.tcp6.getcred", &uc, &size, sin6, sizeof(sin6)) == -1) getcredfail = errno; break; #endif default: /* should not reach here */ getcredfail = EAFNOSUPPORT; break; } if (getcredfail != 0 || uc.cr_version != XUCRED_VERSION) { if (*idbuf == '\0') iderror(lport, fport, s, getcredfail == ENOENT ? ID_NOUSER : ID_UNKNOWN); goto printit; } /* Look up the pw to get the username and home directory*/ errno = 0; pw = getpwuid(uc.cr_uid); if (pw == NULL) iderror(lport, fport, s, errno == 0 ? ID_NOUSER : ID_UNKNOWN); if (iflag) snprintf(idbuf, sizeof(idbuf), "%u", (unsigned)pw->pw_uid); else strlcpy(idbuf, pw->pw_name, sizeof(idbuf)); /* * If enabled, we check for a file named ".noident" in the user's * home directory. If found, we return HIDDEN-USER. */ if (nflag) { if (asprintf(&p, "%s/.noident", pw->pw_dir) == -1) iderror(lport, fport, s, ID_UNKNOWN); if (lstat(p, &sb) == 0) { free(p); iderror(lport, fport, s, ID_HIDDEN); } free(p); } /* * Here, if enabled, we read a user's ".fakeid" file in their * home directory. It consists of a line containing the name * they want. */ if (fflag) { int fakeid_fd; /* * Here we set ourself to effectively be the user, so we don't * open any files we have no permission to open, especially * symbolic links to sensitive root-owned files or devices. */ if (initgroups(pw->pw_name, pw->pw_gid) == -1) iderror(lport, fport, s, ID_UNKNOWN); if (seteuid(pw->pw_uid) == -1) iderror(lport, fport, s, ID_UNKNOWN); /* * We can't stat() here since that would be a race * condition. * Therefore, we open the file we have permissions to open * and if it's not a regular file, we close it and end up * returning the user's real username. */ if (asprintf(&p, "%s/.fakeid", pw->pw_dir) == -1) iderror(lport, fport, s, ID_UNKNOWN); fakeid_fd = open(p, O_RDONLY | O_NONBLOCK); free(p); if (fakeid_fd == -1 || fstat(fakeid_fd, &sb) == -1 || !S_ISREG(sb.st_mode)) goto fakeid_fail; if ((ssize = read(fakeid_fd, buf, sizeof(buf) - 1)) < 0) goto fakeid_fail; buf[ssize] = '\0'; /* * Usually, the file will have the desired identity * in the form "identity\n". Allow for leading white * space and trailing white space/end of line. */ p = buf; p += strspn(p, " \t"); p[strcspn(p, " \t\r\n")] = '\0'; if (strlen(p) > MAXLOGNAME - 1) /* Too long (including nul)? */ p[MAXLOGNAME - 1] = '\0'; /* * If the name is a zero-length string or matches it * the id or name of another user (unless permitted by -F) * then it is invalid. */ if (*p == '\0') goto fakeid_fail; if (!Fflag) { if (iflag) { if (p[strspn(p, "0123456789")] == '\0' && getpwuid(atoi(p)) != NULL) goto fakeid_fail; } else { if (getpwnam(p) != NULL) goto fakeid_fail; } } strlcpy(idbuf, p, sizeof(idbuf)); fakeid_fail: if (fakeid_fd != -1) close(fakeid_fd); } printit: /* Finally, we make and send the reply. */ if (asprintf(&p, "%d , %d : USERID : %s : %s\r\n", lport, fport, osname, idbuf) == -1) { syslog(LOG_ERR, "asprintf: %m"); exit(EX_OSERR); } send(s, p, strlen(p), MSG_EOF); free(p); exit(0); } /* * RFC738/868 Time Server. * Return a machine readable date and time, in the form of the * number of seconds since midnight, Jan 1, 1900. Since gettimeofday * returns the number of seconds since midnight, Jan 1, 1970, * we must add 2208988800 seconds to this figure to make up for * some seventy years Bell Labs was asleep. */ uint32_t machtime(void) { #define OFFSET ((uint32_t)25567 * 24*60*60) return (htonl((uint32_t)(time(NULL) + OFFSET))); #undef OFFSET } /* ARGSUSED */ void machtime_dg(int s, struct servtab *sep) { uint32_t result; struct sockaddr_storage ss; socklen_t size; size = sizeof(ss); if (recvfrom(s, (char *)&result, sizeof(result), 0, (struct sockaddr *)&ss, &size) < 0) return; if (check_loop((struct sockaddr *)&ss, sep)) return; result = machtime(); (void) sendto(s, (char *) &result, sizeof(result), 0, (struct sockaddr *)&ss, size); } /* ARGSUSED */ void machtime_stream(int s, struct servtab *sep __unused) { uint32_t result; result = machtime(); (void) send(s, (char *) &result, sizeof(result), MSG_EOF); } /* * RFC1078 TCP Port Service Multiplexer (TCPMUX). Service connections to * services based on the service name sent. * * Based on TCPMUX.C by Mark K. Lottor November 1988 * sri-nic::ps:tcpmux.c */ #define MAX_SERV_LEN (256+2) /* 2 bytes for \r\n */ #define strwrite(fd, buf) (void) write(fd, buf, sizeof(buf)-1) static int /* # of characters up to \r,\n or \0 */ -getline(int fd, char *buf, int len) +get_line(int fd, char *buf, int len) { int count = 0, n; struct sigaction sa; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); sa.sa_handler = SIG_DFL; sigaction(SIGALRM, &sa, (struct sigaction *)0); do { alarm(10); n = read(fd, buf, len-count); alarm(0); if (n == 0) return (count); if (n < 0) return (-1); while (--n >= 0) { if (*buf == '\r' || *buf == '\n' || *buf == '\0') return (count); count++; buf++; } } while (count < len); return (count); } struct servtab * tcpmux(int s) { struct servtab *sep; char service[MAX_SERV_LEN+1]; int len; /* Get requested service name */ - if ((len = getline(s, service, MAX_SERV_LEN)) < 0) { + if ((len = get_line(s, service, MAX_SERV_LEN)) < 0) { strwrite(s, "-Error reading service name\r\n"); return (NULL); } service[len] = '\0'; if (debug) warnx("tcpmux: someone wants %s", service); /* * Help is a required command, and lists available services, * one per line. */ if (!strcasecmp(service, "help")) { for (sep = servtab; sep; sep = sep->se_next) { if (!ISMUX(sep)) continue; (void)write(s,sep->se_service,strlen(sep->se_service)); strwrite(s, "\r\n"); } return (NULL); } /* Try matching a service in inetd.conf with the request */ for (sep = servtab; sep; sep = sep->se_next) { if (!ISMUX(sep)) continue; if (!strcasecmp(service, sep->se_service)) { if (ISMUXPLUS(sep)) { strwrite(s, "+Go\r\n"); } return (sep); } } strwrite(s, "-Service not available\r\n"); return (NULL); } Index: head/usr.sbin/lpr/lpd/printjob.c =================================================================== --- head/usr.sbin/lpr/lpd/printjob.c (revision 299355) +++ head/usr.sbin/lpr/lpd/printjob.c (revision 299356) @@ -1,2020 +1,2020 @@ /* * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. * * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1983, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #if 0 #ifndef lint static char sccsid[] = "@(#)printjob.c 8.7 (Berkeley) 5/10/95"; #endif /* not lint */ #endif #include "lp.cdefs.h" /* A cross-platform version of */ __FBSDID("$FreeBSD$"); /* * printjob -- print jobs in the queue. * * NOTE: the lock file is used to pass information to lpq and lprm. * it does not need to be removed because file locks are dynamic. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "lp.h" #include "lp.local.h" #include "pathnames.h" #include "extern.h" #define DORETURN 0 /* dofork should return "can't fork" error */ #define DOABORT 1 /* dofork should just die if fork() fails */ /* * The buffer size to use when reading/writing spool files. */ #define SPL_BUFSIZ BUFSIZ /* * Error tokens */ #define REPRINT -2 #define ERROR -1 #define OK 0 #define FATALERR 1 #define NOACCT 2 #define FILTERERR 3 #define ACCESS 4 static dev_t fdev; /* device of file pointed to by symlink */ static ino_t fino; /* inode of file pointed to by symlink */ static FILE *cfp; /* control file */ static pid_t of_pid; /* process id of output filter, if any */ static int child; /* id of any filters */ static int job_dfcnt; /* count of datafiles in current user job */ static int lfd; /* lock file descriptor */ static int ofd; /* output filter file descriptor */ static int tfd = -1; /* output filter temp file output */ static int pfd; /* prstatic inter file descriptor */ static int prchild; /* id of pr process */ static char title[80]; /* ``pr'' title */ static char locale[80]; /* ``pr'' locale */ /* these two are set from pp->daemon_user, but only if they are needed */ static char *daemon_uname; /* set from pwd->pw_name */ static int daemon_defgid; static char class[32]; /* classification field */ static char origin_host[MAXHOSTNAMELEN]; /* user's host machine */ /* indentation size in static characters */ static char indent[10] = "-i0"; static char jobname[100]; /* job or file name */ static char length[10] = "-l"; /* page length in lines */ static char logname[32]; /* user's login name */ static char pxlength[10] = "-y"; /* page length in pixels */ static char pxwidth[10] = "-x"; /* page width in pixels */ /* tempstderr is the filename used to catch stderr from exec-ing filters */ static char tempstderr[] = "errs.XXXXXXX"; static char width[10] = "-w"; /* page width in static characters */ #define TFILENAME "fltXXXXXX" static char tfile[] = TFILENAME; /* file name for filter output */ static void abortpr(int _signo); static void alarmhandler(int _signo); static void banner(struct printer *_pp, char *_name1, char *_name2); static int dofork(const struct printer *_pp, int _action); static int dropit(int _c); static int execfilter(struct printer *_pp, char *_f_cmd, char **_f_av, int _infd, int _outfd); static void init(struct printer *_pp); static void openpr(const struct printer *_pp); static void opennet(const struct printer *_pp); static void opentty(const struct printer *_pp); static void openrem(const struct printer *pp); static int print(struct printer *_pp, int _format, char *_file); static int printit(struct printer *_pp, char *_file); static void pstatus(const struct printer *_pp, const char *_msg, ...) __printflike(2, 3); static char response(const struct printer *_pp); static void scan_out(struct printer *_pp, int _scfd, char *_scsp, int _dlm); static char *scnline(int _key, char *_p, int _c); static int sendfile(struct printer *_pp, int _type, char *_file, char _format, int _copyreq); static int sendit(struct printer *_pp, char *_file); static void sendmail(struct printer *_pp, char *_userid, int _bombed); static void setty(const struct printer *_pp); static void wait4data(struct printer *_pp, const char *_dfile); void printjob(struct printer *pp) { struct stat stb; register struct jobqueue *q, **qp; struct jobqueue **queue; register int i, nitems; off_t pidoff; pid_t printpid; int errcnt, jobcount, statok, tempfd; jobcount = 0; init(pp); /* set up capabilities */ (void) write(STDOUT_FILENO, "", 1); /* ack that daemon is started */ (void) close(STDERR_FILENO); /* set up log file */ if (open(pp->log_file, O_WRONLY|O_APPEND, LOG_FILE_MODE) < 0) { syslog(LOG_ERR, "%s: open(%s): %m", pp->printer, pp->log_file); (void) open(_PATH_DEVNULL, O_WRONLY); } if(setgid(getegid()) != 0) err(1, "setgid() failed"); printpid = getpid(); /* for use with lprm */ setpgid((pid_t)0, printpid); /* * At initial lpd startup, printjob may be called with various * signal handlers in effect. After that initial startup, any * calls to printjob will have a *different* set of signal-handlers * in effect. Make sure all handlers are the ones we want. */ signal(SIGCHLD, SIG_DFL); signal(SIGHUP, abortpr); signal(SIGINT, abortpr); signal(SIGQUIT, abortpr); signal(SIGTERM, abortpr); /* * uses short form file names */ if (chdir(pp->spool_dir) < 0) { syslog(LOG_ERR, "%s: chdir(%s): %m", pp->printer, pp->spool_dir); exit(1); } statok = stat(pp->lock_file, &stb); if (statok == 0 && (stb.st_mode & LFM_PRINT_DIS)) exit(0); /* printing disabled */ umask(S_IWOTH); lfd = open(pp->lock_file, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK, LOCK_FILE_MODE); if (lfd < 0) { if (errno == EWOULDBLOCK) /* active daemon present */ exit(0); syslog(LOG_ERR, "%s: open(%s): %m", pp->printer, pp->lock_file); exit(1); } /* * If the initial call to stat() failed, then lock_file will have * been created by open(). Update &stb to match that new file. */ if (statok != 0) statok = stat(pp->lock_file, &stb); /* turn off non-blocking mode (was turned on for lock effects only) */ if (fcntl(lfd, F_SETFL, 0) < 0) { syslog(LOG_ERR, "%s: fcntl(%s): %m", pp->printer, pp->lock_file); exit(1); } ftruncate(lfd, 0); /* * write process id for others to know */ sprintf(line, "%u\n", printpid); pidoff = i = strlen(line); if (write(lfd, line, i) != i) { syslog(LOG_ERR, "%s: write(%s): %m", pp->printer, pp->lock_file); exit(1); } /* * search the spool directory for work and sort by queue order. */ if ((nitems = getq(pp, &queue)) < 0) { syslog(LOG_ERR, "%s: can't scan %s", pp->printer, pp->spool_dir); exit(1); } if (nitems == 0) /* no work to do */ exit(0); if (stb.st_mode & LFM_RESET_QUE) { /* reset queue flag */ if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) < 0) syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer, pp->lock_file); } /* create a file which will be used to hold stderr from filters */ if ((tempfd = mkstemp(tempstderr)) == -1) { syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer, tempstderr); exit(1); } if ((i = fchmod(tempfd, 0664)) == -1) { syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer, tempstderr); exit(1); } /* lpd doesn't need it to be open, it just needs it to exist */ close(tempfd); openpr(pp); /* open printer or remote */ again: /* * we found something to do now do it -- * write the name of the current control file into the lock file * so the spool queue program can tell what we're working on */ for (qp = queue; nitems--; free((char *) q)) { q = *qp++; if (stat(q->job_cfname, &stb) < 0) continue; errcnt = 0; restart: (void) lseek(lfd, pidoff, 0); (void) snprintf(line, sizeof(line), "%s\n", q->job_cfname); i = strlen(line); if (write(lfd, line, i) != i) syslog(LOG_ERR, "%s: write(%s): %m", pp->printer, pp->lock_file); if (!pp->remote) i = printit(pp, q->job_cfname); else i = sendit(pp, q->job_cfname); /* * Check to see if we are supposed to stop printing or * if we are to rebuild the queue. */ if (fstat(lfd, &stb) == 0) { /* stop printing before starting next job? */ if (stb.st_mode & LFM_PRINT_DIS) goto done; /* rebuild queue (after lpc topq) */ if (stb.st_mode & LFM_RESET_QUE) { for (free(q); nitems--; free(q)) q = *qp++; if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) < 0) syslog(LOG_WARNING, "%s: fchmod(%s): %m", pp->printer, pp->lock_file); break; } } if (i == OK) /* all files of this job printed */ jobcount++; else if (i == REPRINT && ++errcnt < 5) { /* try reprinting the job */ syslog(LOG_INFO, "restarting %s", pp->printer); if (of_pid > 0) { kill(of_pid, SIGCONT); /* to be sure */ (void) close(ofd); while ((i = wait(NULL)) > 0 && i != of_pid) ; if (i < 0) syslog(LOG_WARNING, "%s: after kill(of=%d), wait() returned: %m", pp->printer, of_pid); of_pid = 0; } (void) close(pfd); /* close printer */ if (ftruncate(lfd, pidoff) < 0) syslog(LOG_WARNING, "%s: ftruncate(%s): %m", pp->printer, pp->lock_file); openpr(pp); /* try to reopen printer */ goto restart; } else { syslog(LOG_WARNING, "%s: job could not be %s (%s)", pp->printer, pp->remote ? "sent to remote host" : "printed", q->job_cfname); if (i == REPRINT) { /* ensure we don't attempt this job again */ (void) unlink(q->job_cfname); q->job_cfname[0] = 'd'; (void) unlink(q->job_cfname); if (logname[0]) sendmail(pp, logname, FATALERR); } } } free(queue); /* * search the spool directory for more work. */ if ((nitems = getq(pp, &queue)) < 0) { syslog(LOG_ERR, "%s: can't scan %s", pp->printer, pp->spool_dir); exit(1); } if (nitems == 0) { /* no more work to do */ done: if (jobcount > 0) { /* jobs actually printed */ if (!pp->no_formfeed && !pp->tof) (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); if (pp->trailer != NULL) /* output trailer */ (void) write(ofd, pp->trailer, strlen(pp->trailer)); } (void) close(ofd); (void) wait(NULL); (void) unlink(tempstderr); exit(0); } goto again; } char fonts[4][50]; /* fonts for troff */ char ifonts[4][40] = { _PATH_VFONTR, _PATH_VFONTI, _PATH_VFONTB, _PATH_VFONTS, }; /* * The remaining part is the reading of the control file (cf) * and performing the various actions. */ static int printit(struct printer *pp, char *file) { register int i; char *cp; int bombed, didignorehdr; bombed = OK; didignorehdr = 0; /* * open control file; ignore if no longer there. */ if ((cfp = fopen(file, "r")) == NULL) { syslog(LOG_INFO, "%s: fopen(%s): %m", pp->printer, file); return (OK); } /* * Reset troff fonts. */ for (i = 0; i < 4; i++) strcpy(fonts[i], ifonts[i]); sprintf(&width[2], "%ld", pp->page_width); strcpy(indent+2, "0"); /* initialize job-specific count of datafiles processed */ job_dfcnt = 0; /* * read the control file for work to do * * file format -- first character in the line is a command * rest of the line is the argument. * valid commands are: * * S -- "stat info" for symbolic link protection * J -- "job name" on banner page * C -- "class name" on banner page * L -- "literal" user's name to print on banner * T -- "title" for pr * H -- "host name" of machine where lpr was done * P -- "person" user's login name * I -- "indent" amount to indent output * R -- laser dpi "resolution" * f -- "file name" name of text file to print * l -- "file name" text file with control chars * o -- "file name" postscript file, according to * the RFC. Here it is treated like an 'f'. * p -- "file name" text file to print with pr(1) * t -- "file name" troff(1) file to print * n -- "file name" ditroff(1) file to print * d -- "file name" dvi file to print * g -- "file name" plot(1G) file to print * v -- "file name" plain raster file to print * c -- "file name" cifplot file to print * 1 -- "R font file" for troff * 2 -- "I font file" for troff * 3 -- "B font file" for troff * 4 -- "S font file" for troff * N -- "name" of file (used by lpq) * U -- "unlink" name of file to remove * (after we print it. (Pass 2 only)). * M -- "mail" to user when done printing * Z -- "locale" for pr * - * getline reads a line and expands tabs to blanks + * get_line reads a line and expands tabs to blanks */ /* pass 1 */ - while (getline(cfp)) + while (get_line(cfp)) switch (line[0]) { case 'H': strlcpy(origin_host, line + 1, sizeof(origin_host)); if (class[0] == '\0') { strlcpy(class, line+1, sizeof(class)); } continue; case 'P': strlcpy(logname, line + 1, sizeof(logname)); if (pp->restricted) { /* restricted */ if (getpwnam(logname) == NULL) { bombed = NOACCT; sendmail(pp, line+1, bombed); goto pass2; } } continue; case 'S': cp = line+1; i = 0; while (*cp >= '0' && *cp <= '9') i = i * 10 + (*cp++ - '0'); fdev = i; cp++; i = 0; while (*cp >= '0' && *cp <= '9') i = i * 10 + (*cp++ - '0'); fino = i; continue; case 'J': if (line[1] != '\0') { strlcpy(jobname, line + 1, sizeof(jobname)); } else strcpy(jobname, " "); continue; case 'C': if (line[1] != '\0') strlcpy(class, line + 1, sizeof(class)); else if (class[0] == '\0') { /* XXX - why call gethostname instead of * just strlcpy'ing local_host? */ gethostname(class, sizeof(class)); class[sizeof(class) - 1] = '\0'; } continue; case 'T': /* header title for pr */ strlcpy(title, line + 1, sizeof(title)); continue; case 'L': /* identification line */ if (!pp->no_header && !pp->header_last) banner(pp, line+1, jobname); continue; case '1': /* troff fonts */ case '2': case '3': case '4': if (line[1] != '\0') { strlcpy(fonts[line[0]-'1'], line + 1, (size_t)50); } continue; case 'W': /* page width */ strlcpy(width+2, line + 1, sizeof(width) - 2); continue; case 'I': /* indent amount */ strlcpy(indent+2, line + 1, sizeof(indent) - 2); continue; case 'Z': /* locale for pr */ strlcpy(locale, line + 1, sizeof(locale)); continue; default: /* some file to print */ /* only lowercase cmd-codes include a file-to-print */ if ((line[0] < 'a') || (line[0] > 'z')) { /* ignore any other lines */ if (lflag <= 1) continue; if (!didignorehdr) { syslog(LOG_INFO, "%s: in %s :", pp->printer, file); didignorehdr = 1; } syslog(LOG_INFO, "%s: ignoring line: '%c' %s", pp->printer, line[0], &line[1]); continue; } i = print(pp, line[0], line+1); switch (i) { case ERROR: if (bombed == OK) bombed = FATALERR; break; case REPRINT: (void) fclose(cfp); return (REPRINT); case FILTERERR: case ACCESS: bombed = i; sendmail(pp, logname, bombed); } title[0] = '\0'; continue; case 'N': case 'U': case 'M': case 'R': continue; } /* pass 2 */ pass2: fseek(cfp, 0L, 0); - while (getline(cfp)) + while (get_line(cfp)) switch (line[0]) { case 'L': /* identification line */ if (!pp->no_header && pp->header_last) banner(pp, line+1, jobname); continue; case 'M': if (bombed < NOACCT) /* already sent if >= NOACCT */ sendmail(pp, line+1, bombed); continue; case 'U': if (strchr(line+1, '/')) continue; (void) unlink(line+1); } /* * clean-up in case another control file exists */ (void) fclose(cfp); (void) unlink(file); return (bombed == OK ? OK : ERROR); } /* * Print a file. * Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}. * Return -1 if a non-recoverable error occurred, * 2 if the filter detected some errors (but printed the job anyway), * 1 if we should try to reprint this job and * 0 if all is well. * Note: all filters take stdin as the file, stdout as the printer, * stderr as the log file, and must not ignore SIGINT. */ static int print(struct printer *pp, int format, char *file) { register int n, i; register char *prog; int fi, fo; FILE *fp; char *av[15], buf[SPL_BUFSIZ]; pid_t wpid; int p[2], retcode, stopped, wstatus, wstatus_set; struct stat stb; /* Make sure the entire data file has arrived. */ wait4data(pp, file); if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0) { syslog(LOG_INFO, "%s: unable to open %s ('%c' line)", pp->printer, file, format); return (ERROR); } /* * Check to see if data file is a symbolic link. If so, it should * still point to the same file or someone is trying to print * something he shouldn't. */ if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 && (stb.st_dev != fdev || stb.st_ino != fino)) return (ACCESS); job_dfcnt++; /* increment datafile counter for this job */ stopped = 0; /* output filter is not stopped */ /* everything seems OK, start it up */ if (!pp->no_formfeed && !pp->tof) { /* start on a fresh page */ (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); pp->tof = 1; } if (pp->filters[LPF_INPUT] == NULL && (format == 'f' || format == 'l' || format == 'o')) { pp->tof = 0; while ((n = read(fi, buf, SPL_BUFSIZ)) > 0) if (write(ofd, buf, n) != n) { (void) close(fi); return (REPRINT); } (void) close(fi); return (OK); } switch (format) { case 'p': /* print file using 'pr' */ if (pp->filters[LPF_INPUT] == NULL) { /* use output filter */ prog = _PATH_PR; i = 0; av[i++] = "pr"; av[i++] = width; av[i++] = length; av[i++] = "-h"; av[i++] = *title ? title : " "; av[i++] = "-L"; av[i++] = *locale ? locale : "C"; av[i++] = "-F"; av[i] = NULL; fo = ofd; goto start; } pipe(p); if ((prchild = dofork(pp, DORETURN)) == 0) { /* child */ dup2(fi, STDIN_FILENO); /* file is stdin */ dup2(p[1], STDOUT_FILENO); /* pipe is stdout */ closelog(); closeallfds(3); execl(_PATH_PR, "pr", width, length, "-h", *title ? title : " ", "-L", *locale ? locale : "C", "-F", (char *)0); syslog(LOG_ERR, "cannot execl %s", _PATH_PR); exit(2); } (void) close(p[1]); /* close output side */ (void) close(fi); if (prchild < 0) { prchild = 0; (void) close(p[0]); return (ERROR); } fi = p[0]; /* use pipe for input */ case 'f': /* print plain text file */ prog = pp->filters[LPF_INPUT]; av[1] = width; av[2] = length; av[3] = indent; n = 4; break; case 'o': /* print postscript file */ /* * Treat this as a "plain file with control characters", and * assume the standard LPF_INPUT filter will recognize that * the data is postscript and know what to do with it. These * 'o'-file requests could come from MacOS 10.1 systems. * (later versions of MacOS 10 will explicitly use 'l') * A postscript file can contain binary data, which is why 'l' * is somewhat more appropriate than 'f'. */ /* FALLTHROUGH */ case 'l': /* like 'f' but pass control characters */ prog = pp->filters[LPF_INPUT]; av[1] = "-c"; av[2] = width; av[3] = length; av[4] = indent; n = 5; break; case 'r': /* print a fortran text file */ prog = pp->filters[LPF_FORTRAN]; av[1] = width; av[2] = length; n = 3; break; case 't': /* print troff output */ case 'n': /* print ditroff output */ case 'd': /* print tex output */ (void) unlink(".railmag"); if ((fo = creat(".railmag", FILMOD)) < 0) { syslog(LOG_ERR, "%s: cannot create .railmag", pp->printer); (void) unlink(".railmag"); } else { for (n = 0; n < 4; n++) { if (fonts[n][0] != '/') (void) write(fo, _PATH_VFONT, sizeof(_PATH_VFONT) - 1); (void) write(fo, fonts[n], strlen(fonts[n])); (void) write(fo, "\n", 1); } (void) close(fo); } prog = (format == 't') ? pp->filters[LPF_TROFF] : ((format == 'n') ? pp->filters[LPF_DITROFF] : pp->filters[LPF_DVI]); av[1] = pxwidth; av[2] = pxlength; n = 3; break; case 'c': /* print cifplot output */ prog = pp->filters[LPF_CIFPLOT]; av[1] = pxwidth; av[2] = pxlength; n = 3; break; case 'g': /* print plot(1G) output */ prog = pp->filters[LPF_GRAPH]; av[1] = pxwidth; av[2] = pxlength; n = 3; break; case 'v': /* print raster output */ prog = pp->filters[LPF_RASTER]; av[1] = pxwidth; av[2] = pxlength; n = 3; break; default: (void) close(fi); syslog(LOG_ERR, "%s: illegal format character '%c'", pp->printer, format); return (ERROR); } if (prog == NULL) { (void) close(fi); syslog(LOG_ERR, "%s: no filter found in printcap for format character '%c'", pp->printer, format); return (ERROR); } if ((av[0] = strrchr(prog, '/')) != NULL) av[0]++; else av[0] = prog; av[n++] = "-n"; av[n++] = logname; av[n++] = "-h"; av[n++] = origin_host; av[n++] = pp->acct_file; av[n] = NULL; fo = pfd; if (of_pid > 0) { /* stop output filter */ write(ofd, "\031\1", 2); while ((wpid = wait3(&wstatus, WUNTRACED, 0)) > 0 && wpid != of_pid) ; if (wpid < 0) syslog(LOG_WARNING, "%s: after stopping 'of', wait3() returned: %m", pp->printer); else if (!WIFSTOPPED(wstatus)) { (void) close(fi); syslog(LOG_WARNING, "%s: output filter died " "(pid=%d retcode=%d termsig=%d)", pp->printer, of_pid, WEXITSTATUS(wstatus), WTERMSIG(wstatus)); return (REPRINT); } stopped++; } start: if ((child = dofork(pp, DORETURN)) == 0) { /* child */ dup2(fi, STDIN_FILENO); dup2(fo, STDOUT_FILENO); /* setup stderr for the filter (child process) * so it goes to our temporary errors file */ n = open(tempstderr, O_WRONLY|O_TRUNC, 0664); if (n >= 0) dup2(n, STDERR_FILENO); closelog(); closeallfds(3); execv(prog, av); syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer, prog); exit(2); } (void) close(fi); wstatus_set = 0; if (child < 0) retcode = 100; else { while ((wpid = wait(&wstatus)) > 0 && wpid != child) ; if (wpid < 0) { retcode = 100; syslog(LOG_WARNING, "%s: after execv(%s), wait() returned: %m", pp->printer, prog); } else { wstatus_set = 1; retcode = WEXITSTATUS(wstatus); } } child = 0; prchild = 0; if (stopped) { /* restart output filter */ if (kill(of_pid, SIGCONT) < 0) { syslog(LOG_ERR, "cannot restart output filter"); exit(1); } } pp->tof = 0; /* Copy the filter's output to "lf" logfile */ if ((fp = fopen(tempstderr, "r"))) { while (fgets(buf, sizeof(buf), fp)) fputs(buf, stderr); fclose(fp); } if (wstatus_set && !WIFEXITED(wstatus)) { syslog(LOG_WARNING, "%s: filter '%c' terminated (termsig=%d)", pp->printer, format, WTERMSIG(wstatus)); return (ERROR); } switch (retcode) { case 0: pp->tof = 1; return (OK); case 1: return (REPRINT); case 2: return (ERROR); default: syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)", pp->printer, format, retcode); return (FILTERERR); } } /* * Send the daemon control file (cf) and any data files. * Return -1 if a non-recoverable error occurred, 1 if a recoverable error and * 0 if all is well. */ static int sendit(struct printer *pp, char *file) { int dfcopies, err, i; char *cp, last[sizeof(line)]; /* * open control file */ if ((cfp = fopen(file, "r")) == NULL) return (OK); /* initialize job-specific count of datafiles processed */ job_dfcnt = 0; /* * read the control file for work to do * * file format -- first character in the line is a command * rest of the line is the argument. * commands of interest are: * * a-z -- "file name" name of file to print * U -- "unlink" name of file to remove * (after we print it. (Pass 2 only)). */ /* * pass 1 */ err = OK; - while (getline(cfp)) { + while (get_line(cfp)) { again: if (line[0] == 'S') { cp = line+1; i = 0; while (*cp >= '0' && *cp <= '9') i = i * 10 + (*cp++ - '0'); fdev = i; cp++; i = 0; while (*cp >= '0' && *cp <= '9') i = i * 10 + (*cp++ - '0'); fino = i; } else if (line[0] == 'H') { strlcpy(origin_host, line + 1, sizeof(origin_host)); if (class[0] == '\0') { strlcpy(class, line + 1, sizeof(class)); } } else if (line[0] == 'P') { strlcpy(logname, line + 1, sizeof(logname)); if (pp->restricted) { /* restricted */ if (getpwnam(logname) == NULL) { sendmail(pp, line+1, NOACCT); err = ERROR; break; } } } else if (line[0] == 'I') { strlcpy(indent+2, line + 1, sizeof(indent) - 2); } else if (line[0] >= 'a' && line[0] <= 'z') { dfcopies = 1; strcpy(last, line); - while ((i = getline(cfp)) != 0) { + while ((i = get_line(cfp)) != 0) { if (strcmp(last, line) != 0) break; dfcopies++; } switch (sendfile(pp, '\3', last+1, *last, dfcopies)) { case OK: if (i) goto again; break; case REPRINT: (void) fclose(cfp); return (REPRINT); case ACCESS: sendmail(pp, logname, ACCESS); case ERROR: err = ERROR; } break; } } if (err == OK && sendfile(pp, '\2', file, '\0', 1) > 0) { (void) fclose(cfp); return (REPRINT); } /* * pass 2 */ fseek(cfp, 0L, 0); - while (getline(cfp)) + while (get_line(cfp)) if (line[0] == 'U' && !strchr(line+1, '/')) (void) unlink(line+1); /* * clean-up in case another control file exists */ (void) fclose(cfp); (void) unlink(file); return (err); } /* * Send a data file to the remote machine and spool it. * Return positive if we should try resending. */ static int sendfile(struct printer *pp, int type, char *file, char format, int copyreq) { int i, amt; struct stat stb; char *av[15], *filtcmd; char buf[SPL_BUFSIZ], opt_c[4], opt_h[4], opt_n[4]; int copycnt, filtstat, narg, resp, sfd, sfres, sizerr, statrc; /* Make sure the entire data file has arrived. */ wait4data(pp, file); statrc = lstat(file, &stb); if (statrc < 0) { syslog(LOG_ERR, "%s: error from lstat(%s): %m", pp->printer, file); return (ERROR); } sfd = open(file, O_RDONLY); if (sfd < 0) { syslog(LOG_ERR, "%s: error from open(%s,O_RDONLY): %m", pp->printer, file); return (ERROR); } /* * Check to see if data file is a symbolic link. If so, it should * still point to the same file or someone is trying to print something * he shouldn't. */ if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(sfd, &stb) == 0 && (stb.st_dev != fdev || stb.st_ino != fino)) { close(sfd); return (ACCESS); } /* Everything seems OK for reading the file, now to send it */ filtcmd = NULL; sizerr = 0; tfd = -1; if (type == '\3') { /* * Type == 3 means this is a datafile, not a control file. * Increment the counter of data-files in this job, and * then check for input or output filters (which are only * applied to datafiles, not control files). */ job_dfcnt++; /* * Note that here we are filtering datafiles, one at a time, * as they are sent to the remote machine. Here, the *only* * difference between an input filter (`if=') and an output * filter (`of=') is the argument list that the filter is * started up with. Here, the output filter is executed * for each individual file as it is sent. This is not the * same as local print queues, where the output filter is * started up once, and then all jobs are passed thru that * single invocation of the output filter. * * Also note that a queue for a remote-machine can have an * input filter or an output filter, but not both. */ if (pp->filters[LPF_INPUT]) { filtcmd = pp->filters[LPF_INPUT]; av[0] = filtcmd; narg = 0; strcpy(opt_c, "-c"); strcpy(opt_h, "-h"); strcpy(opt_n, "-n"); if (format == 'l') av[++narg] = opt_c; av[++narg] = width; av[++narg] = length; av[++narg] = indent; av[++narg] = opt_n; av[++narg] = logname; av[++narg] = opt_h; av[++narg] = origin_host; av[++narg] = pp->acct_file; av[++narg] = NULL; } else if (pp->filters[LPF_OUTPUT]) { filtcmd = pp->filters[LPF_OUTPUT]; av[0] = filtcmd; narg = 0; av[++narg] = width; av[++narg] = length; av[++narg] = NULL; } } if (filtcmd) { /* * If there is an input or output filter, we have to run * the datafile thru that filter and store the result as * a temporary spool file, because the protocol requires * that we send the remote host the file-size before we * start to send any of the data. */ strcpy(tfile, TFILENAME); tfd = mkstemp(tfile); if (tfd == -1) { syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer, TFILENAME); sfres = ERROR; goto return_sfres; } filtstat = execfilter(pp, filtcmd, av, sfd, tfd); /* process the return-code from the filter */ switch (filtstat) { case 0: break; case 1: sfres = REPRINT; goto return_sfres; case 2: sfres = ERROR; goto return_sfres; default: syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)", pp->printer, format, filtstat); sfres = FILTERERR; goto return_sfres; } statrc = fstat(tfd, &stb); /* to find size of tfile */ if (statrc < 0) { syslog(LOG_ERR, "%s: error processing 'if', fstat(%s): %m", pp->printer, tfile); sfres = ERROR; goto return_sfres; } close(sfd); sfd = tfd; lseek(sfd, 0, SEEK_SET); } copycnt = 0; sendagain: copycnt++; if (copycnt < 2) (void) sprintf(buf, "%c%" PRId64 " %s\n", type, stb.st_size, file); else (void) sprintf(buf, "%c%" PRId64 " %s_c%d\n", type, stb.st_size, file, copycnt); amt = strlen(buf); for (i = 0; ; i++) { if (write(pfd, buf, amt) != amt || (resp = response(pp)) < 0 || resp == '\1') { sfres = REPRINT; goto return_sfres; } else if (resp == '\0') break; if (i == 0) pstatus(pp, "no space on remote; waiting for queue to drain"); if (i == 10) syslog(LOG_ALERT, "%s: can't send to %s; queue full", pp->printer, pp->remote_host); sleep(5 * 60); } if (i) pstatus(pp, "sending to %s", pp->remote_host); /* * XXX - we should change trstat_init()/trstat_write() to include * the copycnt in the statistics record it may write. */ if (type == '\3') trstat_init(pp, file, job_dfcnt); for (i = 0; i < stb.st_size; i += SPL_BUFSIZ) { amt = SPL_BUFSIZ; if (i + amt > stb.st_size) amt = stb.st_size - i; if (sizerr == 0 && read(sfd, buf, amt) != amt) sizerr = 1; if (write(pfd, buf, amt) != amt) { sfres = REPRINT; goto return_sfres; } } if (sizerr) { syslog(LOG_INFO, "%s: %s: changed size", pp->printer, file); /* tell recvjob to ignore this file */ (void) write(pfd, "\1", 1); sfres = ERROR; goto return_sfres; } if (write(pfd, "", 1) != 1 || response(pp)) { sfres = REPRINT; goto return_sfres; } if (type == '\3') { trstat_write(pp, TR_SENDING, stb.st_size, logname, pp->remote_host, origin_host); /* * Usually we only need to send one copy of a datafile, * because the control-file will simply print the same * file multiple times. However, some printers ignore * the control file, and simply print each data file as * it arrives. For such "remote hosts", we need to * transfer the same data file multiple times. Such a * a host is indicated by adding 'rc' to the printcap * entry. * XXX - Right now this ONLY works for remote hosts which * do ignore the name of the data file, because * this sends the file multiple times with slight * changes to the filename. To do this right would * require that we also rewrite the control file * to match those filenames. */ if (pp->resend_copies && (copycnt < copyreq)) { lseek(sfd, 0, SEEK_SET); goto sendagain; } } sfres = OK; return_sfres: (void)close(sfd); if (tfd != -1) { /* * If tfd is set, then it is the same value as sfd, and * therefore it is already closed at this point. All * we need to do is remove the temporary file. */ tfd = -1; unlink(tfile); } return (sfres); } /* * Some print servers send the control-file first, and then start sending the * matching data file(s). That is not the correct order. If some queue is * already printing an active job, then when that job is finished the queue * may proceed to the control file of any incoming print job. This turns * into a race between the process which is receiving the data file, and the * process which is actively printing the very same file. When the remote * server sends files in the wrong order, it is even possible that a queue * will start to print a data file before the file has been created! * * So before we start to print() or send() a data file, we call this routine * to make sure the data file is not still changing in size. Note that this * problem will only happen for jobs arriving from a remote host, and that * the process which has decided to print this job (and is thus making this * check) is *not* the process which is receiving the job. * * A second benefit of this is that any incoming job is guaranteed to appear * in a queue listing for at least a few seconds after it has arrived. Some * lpr implementations get confused if they send a job and it disappears * from the queue before they can check on it. */ #define MAXWAIT_ARRIVE 16 /* max to wait for the file to *exist* */ #define MAXWAIT_4DATA (20*60) /* max to wait for it to stop changing */ #define MINWAIT_4DATA 4 /* This value must be >= 1 */ #define DEBUG_MINWAIT 1 static void wait4data(struct printer *pp, const char *dfile) { const char *cp; int statres; u_int sleepreq; size_t dlen, hlen; time_t amtslept, cur_time, prev_mtime; struct stat statdf; /* Skip these checks if the print job is from the local host. */ dlen = strlen(dfile); hlen = strlen(local_host); if (dlen > hlen) { cp = dfile + dlen - hlen; if (strcmp(cp, local_host) == 0) return; } /* * If this data file does not exist, then wait up to MAXWAIT_ARRIVE * seconds for it to arrive. */ amtslept = 0; statres = stat(dfile, &statdf); while (statres < 0 && amtslept < MAXWAIT_ARRIVE) { if (amtslept == 0) pstatus(pp, "Waiting for data file from remote host"); amtslept += MINWAIT_4DATA - sleep(MINWAIT_4DATA); statres = stat(dfile, &statdf); } if (statres < 0) { /* The file still does not exist, so just give up on it. */ syslog(LOG_WARNING, "%s: wait4data() abandoned wait for %s", pp->printer, dfile); return; } /* * The file exists, so keep waiting until the data file has not * changed for some reasonable amount of time. Extra care is * taken when computing wait-times, just in case there are data * files with a last-modify time in the future. While that is * very unlikely to happen, it can happen when the system has * a flakey time-of-day clock. */ prev_mtime = statdf.st_mtime; cur_time = time(NULL); if (statdf.st_mtime >= cur_time - MINWAIT_4DATA) { if (statdf.st_mtime >= cur_time) /* some TOD oddity */ sleepreq = MINWAIT_4DATA; else sleepreq = cur_time - statdf.st_mtime; if (amtslept == 0) pstatus(pp, "Waiting for data file from remote host"); amtslept += sleepreq - sleep(sleepreq); statres = stat(dfile, &statdf); } sleepreq = MINWAIT_4DATA; while (statres == 0 && amtslept < MAXWAIT_4DATA) { if (statdf.st_mtime == prev_mtime) break; prev_mtime = statdf.st_mtime; amtslept += sleepreq - sleep(sleepreq); statres = stat(dfile, &statdf); } if (statres != 0) syslog(LOG_WARNING, "%s: %s disappeared during wait4data()", pp->printer, dfile); else if (amtslept > MAXWAIT_4DATA) syslog(LOG_WARNING, "%s: %s still changing after %lu secs in wait4data()", pp->printer, dfile, (unsigned long)amtslept); #if DEBUG_MINWAIT else if (amtslept > MINWAIT_4DATA) syslog(LOG_INFO, "%s: slept %lu secs in wait4data(%s)", pp->printer, (unsigned long)amtslept, dfile); #endif } #undef MAXWAIT_ARRIVE #undef MAXWAIT_4DATA #undef MINWAIT_4DATA /* * This routine is called to execute one of the filters as was * specified in a printcap entry. While the child-process will read * all of 'infd', it is up to the caller to close that file descriptor * in the parent process. */ static int execfilter(struct printer *pp, char *f_cmd, char *f_av[], int infd, int outfd) { pid_t fpid, wpid; int errfd, retcode, wstatus; FILE *errfp; char buf[BUFSIZ], *slash; fpid = dofork(pp, DORETURN); if (fpid != 0) { /* * This is the parent process, which just waits for the child * to complete and then returns the result. Note that it is * the child process which reads the input stream. */ if (fpid < 0) retcode = 100; else { while ((wpid = wait(&wstatus)) > 0 && wpid != fpid) ; if (wpid < 0) { retcode = 100; syslog(LOG_WARNING, "%s: after execv(%s), wait() returned: %m", pp->printer, f_cmd); } else retcode = WEXITSTATUS(wstatus); } /* * Copy everything the filter wrote to stderr from our * temporary errors file to the "lf=" logfile. */ errfp = fopen(tempstderr, "r"); if (errfp) { while (fgets(buf, sizeof(buf), errfp)) fputs(buf, stderr); fclose(errfp); } return (retcode); } /* * This is the child process, which is the one that executes the * given filter. */ /* * If the first parameter has any slashes in it, then change it * to point to the first character after the last slash. */ slash = strrchr(f_av[0], '/'); if (slash != NULL) f_av[0] = slash + 1; /* * XXX - in the future, this should setup an explicit list of * environment variables and use execve()! */ /* * Setup stdin, stdout, and stderr as we want them when the filter * is running. Stderr is setup so it points to a temporary errors * file, and the parent process will copy that temporary file to * the real logfile after the filter completes. */ dup2(infd, STDIN_FILENO); dup2(outfd, STDOUT_FILENO); errfd = open(tempstderr, O_WRONLY|O_TRUNC, 0664); if (errfd >= 0) dup2(errfd, STDERR_FILENO); closelog(); closeallfds(3); execv(f_cmd, f_av); syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer, f_cmd); exit(2); /* NOTREACHED */ } /* * Check to make sure there have been no errors and that both programs * are in sync with eachother. * Return non-zero if the connection was lost. */ static char response(const struct printer *pp) { char resp; if (read(pfd, &resp, 1) != 1) { syslog(LOG_INFO, "%s: lost connection", pp->printer); return (-1); } return (resp); } /* * Banner printing stuff */ static void banner(struct printer *pp, char *name1, char *name2) { time_t tvec; time(&tvec); if (!pp->no_formfeed && !pp->tof) (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); if (pp->short_banner) { /* short banner only */ if (class[0]) { (void) write(ofd, class, strlen(class)); (void) write(ofd, ":", 1); } (void) write(ofd, name1, strlen(name1)); (void) write(ofd, " Job: ", 7); (void) write(ofd, name2, strlen(name2)); (void) write(ofd, " Date: ", 8); (void) write(ofd, ctime(&tvec), 24); (void) write(ofd, "\n", 1); } else { /* normal banner */ (void) write(ofd, "\n\n\n", 3); scan_out(pp, ofd, name1, '\0'); (void) write(ofd, "\n\n", 2); scan_out(pp, ofd, name2, '\0'); if (class[0]) { (void) write(ofd,"\n\n\n",3); scan_out(pp, ofd, class, '\0'); } (void) write(ofd, "\n\n\n\n\t\t\t\t\tJob: ", 15); (void) write(ofd, name2, strlen(name2)); (void) write(ofd, "\n\t\t\t\t\tDate: ", 12); (void) write(ofd, ctime(&tvec), 24); (void) write(ofd, "\n", 1); } if (!pp->no_formfeed) (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); pp->tof = 1; } static char * scnline(int key, char *p, int c) { register int scnwidth; for (scnwidth = WIDTH; --scnwidth;) { key <<= 1; *p++ = key & 0200 ? c : BACKGND; } return (p); } #define TRC(q) (((q)-' ')&0177) static void scan_out(struct printer *pp, int scfd, char *scsp, int dlm) { register char *strp; register int nchrs, j; char outbuf[LINELEN+1], *sp, c, cc; int d, scnhgt; for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) { strp = &outbuf[0]; sp = scsp; for (nchrs = 0; ; ) { d = dropit(c = TRC(cc = *sp++)); if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d)) for (j = WIDTH; --j;) *strp++ = BACKGND; else strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc); if (*sp == dlm || *sp == '\0' || nchrs++ >= pp->page_width/(WIDTH+1)-1) break; *strp++ = BACKGND; *strp++ = BACKGND; } while (*--strp == BACKGND && strp >= outbuf) ; strp++; *strp++ = '\n'; (void) write(scfd, outbuf, strp-outbuf); } } static int dropit(int c) { switch(c) { case TRC('_'): case TRC(';'): case TRC(','): case TRC('g'): case TRC('j'): case TRC('p'): case TRC('q'): case TRC('y'): return (DROP); default: return (0); } } /* * sendmail --- * tell people about job completion */ static void sendmail(struct printer *pp, char *userid, int bombed) { register int i; int p[2], s; register const char *cp; struct stat stb; FILE *fp; pipe(p); if ((s = dofork(pp, DORETURN)) == 0) { /* child */ dup2(p[0], STDIN_FILENO); closelog(); closeallfds(3); if ((cp = strrchr(_PATH_SENDMAIL, '/')) != NULL) cp++; else cp = _PATH_SENDMAIL; execl(_PATH_SENDMAIL, cp, "-t", (char *)0); _exit(0); } else if (s > 0) { /* parent */ dup2(p[1], STDOUT_FILENO); printf("To: %s@%s\n", userid, origin_host); printf("Subject: %s printer job \"%s\"\n", pp->printer, *jobname ? jobname : ""); printf("Reply-To: root@%s\n\n", local_host); printf("Your printer job "); if (*jobname) printf("(%s) ", jobname); switch (bombed) { case OK: cp = "OK"; printf("\ncompleted successfully\n"); break; default: case FATALERR: cp = "FATALERR"; printf("\ncould not be printed\n"); break; case NOACCT: cp = "NOACCT"; printf("\ncould not be printed without an account on %s\n", local_host); break; case FILTERERR: cp = "FILTERERR"; if (stat(tempstderr, &stb) < 0 || stb.st_size == 0 || (fp = fopen(tempstderr, "r")) == NULL) { printf("\nhad some errors and may not have printed\n"); break; } printf("\nhad the following errors and may not have printed:\n"); while ((i = getc(fp)) != EOF) putchar(i); (void) fclose(fp); break; case ACCESS: cp = "ACCESS"; printf("\nwas not printed because it was not linked to the original file\n"); } fflush(stdout); (void) close(STDOUT_FILENO); } else { syslog(LOG_WARNING, "unable to send mail to %s: %m", userid); return; } (void) close(p[0]); (void) close(p[1]); wait(NULL); syslog(LOG_INFO, "mail sent to user %s about job %s on printer %s (%s)", userid, *jobname ? jobname : "", pp->printer, cp); } /* * dofork - fork with retries on failure */ static int dofork(const struct printer *pp, int action) { pid_t forkpid; int i, fail; struct passwd *pwd; forkpid = -1; if (daemon_uname == NULL) { pwd = getpwuid(pp->daemon_user); if (pwd == NULL) { syslog(LOG_ERR, "%s: Can't lookup default daemon uid (%ld) in password file", pp->printer, pp->daemon_user); goto error_ret; } daemon_uname = strdup(pwd->pw_name); daemon_defgid = pwd->pw_gid; } for (i = 0; i < 20; i++) { forkpid = fork(); if (forkpid < 0) { sleep((unsigned)(i*i)); continue; } /* * Child should run as daemon instead of root */ if (forkpid == 0) { errno = 0; fail = initgroups(daemon_uname, daemon_defgid); if (fail) { syslog(LOG_ERR, "%s: initgroups(%s,%u): %m", pp->printer, daemon_uname, daemon_defgid); break; } fail = setgid(daemon_defgid); if (fail) { syslog(LOG_ERR, "%s: setgid(%u): %m", pp->printer, daemon_defgid); break; } fail = setuid(pp->daemon_user); if (fail) { syslog(LOG_ERR, "%s: setuid(%ld): %m", pp->printer, pp->daemon_user); break; } } return (forkpid); } /* * An error occurred. If the error is in the child process, then * this routine MUST always exit(). DORETURN only effects how * errors should be handled in the parent process. */ error_ret: if (forkpid == 0) { syslog(LOG_ERR, "%s: dofork(): aborting child process...", pp->printer); exit(1); } syslog(LOG_ERR, "%s: dofork(): failure in fork", pp->printer); sleep(1); /* throttle errors, as a safety measure */ switch (action) { case DORETURN: return (-1); default: syslog(LOG_ERR, "bad action (%d) to dofork", action); /* FALLTHROUGH */ case DOABORT: exit(1); } /*NOTREACHED*/ } /* * Kill child processes to abort current job. */ static void abortpr(int signo __unused) { (void) unlink(tempstderr); kill(0, SIGINT); if (of_pid > 0) kill(of_pid, SIGCONT); while (wait(NULL) > 0) ; if (of_pid > 0 && tfd != -1) unlink(tfile); exit(0); } static void init(struct printer *pp) { char *s; sprintf(&width[2], "%ld", pp->page_width); sprintf(&length[2], "%ld", pp->page_length); sprintf(&pxwidth[2], "%ld", pp->page_pwidth); sprintf(&pxlength[2], "%ld", pp->page_plength); if ((s = checkremote(pp)) != NULL) { syslog(LOG_WARNING, "%s", s); free(s); } } void startprinting(const char *printer) { struct printer myprinter, *pp = &myprinter; int status; init_printer(pp); status = getprintcap(printer, pp); switch(status) { case PCAPERR_OSERR: syslog(LOG_ERR, "can't open printer description file: %m"); exit(1); case PCAPERR_NOTFOUND: syslog(LOG_ERR, "unknown printer: %s", printer); exit(1); case PCAPERR_TCLOOP: fatal(pp, "potential reference loop detected in printcap file"); default: break; } printjob(pp); } /* * Acquire line printer or remote connection. */ static void openpr(const struct printer *pp) { int p[2]; char *cp; if (pp->remote) { openrem(pp); /* * Lpd does support the setting of 'of=' filters for * jobs going to remote machines, but that does not * have the same meaning as 'of=' does when handling * local print queues. For remote machines, all 'of=' * filter processing is handled in sendfile(), and that * does not use these global "output filter" variables. */ ofd = -1; of_pid = 0; return; } else if (*pp->lp) { if (strchr(pp->lp, '@') != NULL) opennet(pp); else opentty(pp); } else { syslog(LOG_ERR, "%s: no line printer device or host name", pp->printer); exit(1); } /* * Start up an output filter, if needed. */ if (pp->filters[LPF_OUTPUT] && !pp->filters[LPF_INPUT] && !of_pid) { pipe(p); if (pp->remote) { strcpy(tfile, TFILENAME); tfd = mkstemp(tfile); } if ((of_pid = dofork(pp, DOABORT)) == 0) { /* child */ dup2(p[0], STDIN_FILENO); /* pipe is std in */ /* tfile/printer is stdout */ dup2(pp->remote ? tfd : pfd, STDOUT_FILENO); closelog(); closeallfds(3); if ((cp = strrchr(pp->filters[LPF_OUTPUT], '/')) == NULL) cp = pp->filters[LPF_OUTPUT]; else cp++; execl(pp->filters[LPF_OUTPUT], cp, width, length, (char *)0); syslog(LOG_ERR, "%s: execl(%s): %m", pp->printer, pp->filters[LPF_OUTPUT]); exit(1); } (void) close(p[0]); /* close input side */ ofd = p[1]; /* use pipe for output */ } else { ofd = pfd; of_pid = 0; } } /* * Printer connected directly to the network * or to a terminal server on the net */ static void opennet(const struct printer *pp) { register int i; int resp; u_long port; char *ep; void (*savealrm)(int); port = strtoul(pp->lp, &ep, 0); if (*ep != '@' || port > 65535) { syslog(LOG_ERR, "%s: bad port number: %s", pp->printer, pp->lp); exit(1); } ep++; for (i = 1; ; i = i < 256 ? i << 1 : i) { resp = -1; savealrm = signal(SIGALRM, alarmhandler); alarm(pp->conn_timeout); pfd = getport(pp, ep, port); alarm(0); (void)signal(SIGALRM, savealrm); if (pfd < 0 && errno == ECONNREFUSED) resp = 1; else if (pfd >= 0) { /* * need to delay a bit for rs232 lines * to stabilize in case printer is * connected via a terminal server */ delay(500); break; } if (i == 1) { if (resp < 0) pstatus(pp, "waiting for %s to come up", pp->lp); else pstatus(pp, "waiting for access to printer on %s", pp->lp); } sleep(i); } pstatus(pp, "sending to %s port %lu", ep, port); } /* * Printer is connected to an RS232 port on this host */ static void opentty(const struct printer *pp) { register int i; for (i = 1; ; i = i < 32 ? i << 1 : i) { pfd = open(pp->lp, pp->rw ? O_RDWR : O_WRONLY); if (pfd >= 0) { delay(500); break; } if (errno == ENOENT) { syslog(LOG_ERR, "%s: %m", pp->lp); exit(1); } if (i == 1) pstatus(pp, "waiting for %s to become ready (offline?)", pp->printer); sleep(i); } if (isatty(pfd)) setty(pp); pstatus(pp, "%s is ready and printing", pp->printer); } /* * Printer is on a remote host */ static void openrem(const struct printer *pp) { register int i; int resp; void (*savealrm)(int); for (i = 1; ; i = i < 256 ? i << 1 : i) { resp = -1; savealrm = signal(SIGALRM, alarmhandler); alarm(pp->conn_timeout); pfd = getport(pp, pp->remote_host, 0); alarm(0); (void)signal(SIGALRM, savealrm); if (pfd >= 0) { if ((writel(pfd, "\2", pp->remote_queue, "\n", (char *)0) == 2 + strlen(pp->remote_queue)) && (resp = response(pp)) == 0) break; (void) close(pfd); } if (i == 1) { if (resp < 0) pstatus(pp, "waiting for %s to come up", pp->remote_host); else { pstatus(pp, "waiting for queue to be enabled on %s", pp->remote_host); i = 256; } } sleep(i); } pstatus(pp, "sending to %s", pp->remote_host); } /* * setup tty lines. */ static void setty(const struct printer *pp) { struct termios ttybuf; if (ioctl(pfd, TIOCEXCL, (char *)0) < 0) { syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", pp->printer); exit(1); } if (tcgetattr(pfd, &ttybuf) < 0) { syslog(LOG_ERR, "%s: tcgetattr: %m", pp->printer); exit(1); } if (pp->baud_rate > 0) cfsetspeed(&ttybuf, pp->baud_rate); if (pp->mode_set) { char *s = strdup(pp->mode_set), *tmp; while ((tmp = strsep(&s, ",")) != NULL) { (void) msearch(tmp, &ttybuf); } } if (pp->mode_set != 0 || pp->baud_rate > 0) { if (tcsetattr(pfd, TCSAFLUSH, &ttybuf) == -1) { syslog(LOG_ERR, "%s: tcsetattr: %m", pp->printer); } } } #include static void pstatus(const struct printer *pp, const char *msg, ...) { int fd; char *buf; va_list ap; va_start(ap, msg); umask(S_IWOTH); fd = open(pp->status_file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE); if (fd < 0) { syslog(LOG_ERR, "%s: open(%s): %m", pp->printer, pp->status_file); exit(1); } ftruncate(fd, 0); vasprintf(&buf, msg, ap); va_end(ap); writel(fd, buf, "\n", (char *)0); close(fd); free(buf); } void alarmhandler(int signo __unused) { /* the signal is ignored */ /* (the '__unused' is just to avoid a compile-time warning) */ }