xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/in.ftpd/paths.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate /****************************************************************************
9*0Sstevel@tonic-gate   Copyright (c) 1999,2000 WU-FTPD Development Group.
10*0Sstevel@tonic-gate   All rights reserved.
11*0Sstevel@tonic-gate 
12*0Sstevel@tonic-gate   Portions Copyright (c) 1980, 1985, 1988, 1989, 1990, 1991, 1993, 1994
13*0Sstevel@tonic-gate     The Regents of the University of California.
14*0Sstevel@tonic-gate   Portions Copyright (c) 1993, 1994 Washington University in Saint Louis.
15*0Sstevel@tonic-gate   Portions Copyright (c) 1996, 1998 Berkeley Software Design, Inc.
16*0Sstevel@tonic-gate   Portions Copyright (c) 1989 Massachusetts Institute of Technology.
17*0Sstevel@tonic-gate   Portions Copyright (c) 1998 Sendmail, Inc.
18*0Sstevel@tonic-gate   Portions Copyright (c) 1983, 1995, 1996, 1997 Eric P.  Allman.
19*0Sstevel@tonic-gate   Portions Copyright (c) 1997 by Stan Barber.
20*0Sstevel@tonic-gate   Portions Copyright (c) 1997 by Kent Landfield.
21*0Sstevel@tonic-gate   Portions Copyright (c) 1991, 1992, 1993, 1994, 1995, 1996, 1997
22*0Sstevel@tonic-gate     Free Software Foundation, Inc.
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate   Use and distribution of this software and its source code are governed
25*0Sstevel@tonic-gate   by the terms and conditions of the WU-FTPD Software License ("LICENSE").
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate   If you did not receive a copy of the license, it may be obtained online
28*0Sstevel@tonic-gate   at http://www.wu-ftpd.org/license.html.
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate   $Id: paths.c,v 1.7 2000/07/01 18:17:39 wuftpd Exp $
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate ****************************************************************************/
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  * paths.c - setting up the correct pathing to support files/directories
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  * INITAL AUTHOR - Kent Landfield  <kent@landfield.com>
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate #include "config.h"
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <stdio.h>
41*0Sstevel@tonic-gate #include <unistd.h>
42*0Sstevel@tonic-gate #include <string.h>
43*0Sstevel@tonic-gate #include <syslog.h>
44*0Sstevel@tonic-gate #include <sys/param.h>
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #include "pathnames.h"
47*0Sstevel@tonic-gate #include "proto.h"
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #ifdef  VIRTUAL
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #include <sys/types.h>
52*0Sstevel@tonic-gate #include <sys/stat.h>
53*0Sstevel@tonic-gate #include <sys/socket.h>
54*0Sstevel@tonic-gate #include <netinet/in.h>
55*0Sstevel@tonic-gate #include <arpa/inet.h>
56*0Sstevel@tonic-gate #include <netdb.h>
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate int virtual_mode = 0;
59*0Sstevel@tonic-gate int virtual_ftpaccess = 0;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate extern int debug;
62*0Sstevel@tonic-gate extern char virtual_hostname[];
63*0Sstevel@tonic-gate extern char virtual_address[];
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate #endif
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate #ifndef MAXHOSTNAMELEN
68*0Sstevel@tonic-gate #define MAXHOSTNAMELEN 64
69*0Sstevel@tonic-gate #endif
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /*
72*0Sstevel@tonic-gate    ** Pathing storage
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate #define _PATHS_DEFINED_ 1
76*0Sstevel@tonic-gate char _path_ftpaccess[MAXPATHLEN];
77*0Sstevel@tonic-gate char _path_ftpusers[MAXPATHLEN];
78*0Sstevel@tonic-gate char _path_ftphosts[MAXPATHLEN];
79*0Sstevel@tonic-gate char _path_private[MAXPATHLEN];
80*0Sstevel@tonic-gate char _path_cvt[MAXPATHLEN];
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate extern char logfile[];
83*0Sstevel@tonic-gate extern char hostname[];
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate void setup_paths(void);
86*0Sstevel@tonic-gate 
87*0Sstevel@tonic-gate /*
88*0Sstevel@tonic-gate    ** Virtual hosting has to support many different types of needs. There
89*0Sstevel@tonic-gate    ** must be complete support for the various ftpd system files and their
90*0Sstevel@tonic-gate    ** functionality.
91*0Sstevel@tonic-gate    **
92*0Sstevel@tonic-gate    ** Full support on a virtual host basis:
93*0Sstevel@tonic-gate    ** -------------------------------------
94*0Sstevel@tonic-gate    **  _PATH_FTPACCESS
95*0Sstevel@tonic-gate    **  _PATH_FTPUSERS
96*0Sstevel@tonic-gate    **  _PATH_PRIVATE
97*0Sstevel@tonic-gate    **  _PATH_FTPHOSTS
98*0Sstevel@tonic-gate    **  _PATH_CVT
99*0Sstevel@tonic-gate    **
100*0Sstevel@tonic-gate    ** Set in a site's ftpaccess file
101*0Sstevel@tonic-gate    **  _PATH_XFERLOG
102*0Sstevel@tonic-gate    **
103*0Sstevel@tonic-gate    ** Supported on a site basis:
104*0Sstevel@tonic-gate    ** --------------------------
105*0Sstevel@tonic-gate    **  _PATH_FTPSERVERS
106*0Sstevel@tonic-gate    **  _PATH_EXECPATH
107*0Sstevel@tonic-gate    **  _PATH_PIDNAMES
108*0Sstevel@tonic-gate    **  _PATH_UTMP
109*0Sstevel@tonic-gate    **  _PATH_WTMP
110*0Sstevel@tonic-gate    **  _PATH_LASTLOG
111*0Sstevel@tonic-gate    **  _PATH_BSHELL
112*0Sstevel@tonic-gate    **  _PATH_DEVNULL
113*0Sstevel@tonic-gate  */
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
116*0Sstevel@tonic-gate /* FUNCTION  : setup_paths                                                  */
117*0Sstevel@tonic-gate /* PURPOSE   : Determine appropriate paths to various configuration files.  */
118*0Sstevel@tonic-gate /* ARGUMENTS : None                                                         */
119*0Sstevel@tonic-gate /* RETURNS   : None                                                         */
120*0Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
121*0Sstevel@tonic-gate 
setup_paths(void)122*0Sstevel@tonic-gate void setup_paths(void)
123*0Sstevel@tonic-gate {
124*0Sstevel@tonic-gate #ifdef VIRTUAL
125*0Sstevel@tonic-gate     char *sp;
126*0Sstevel@tonic-gate     char configdir[MAXPATHLEN];
127*0Sstevel@tonic-gate     char filepath[MAXPATHLEN];
128*0Sstevel@tonic-gate #ifdef INET6
129*0Sstevel@tonic-gate     char hostaddress[INET6_ADDRSTRLEN];
130*0Sstevel@tonic-gate #else
131*0Sstevel@tonic-gate     char hostaddress[32];
132*0Sstevel@tonic-gate #endif
133*0Sstevel@tonic-gate     FILE *svrfp;
134*0Sstevel@tonic-gate     struct stat st;
135*0Sstevel@tonic-gate #if defined(UNIXWARE) || defined(AIX)
136*0Sstevel@tonic-gate     size_t virtual_len;
137*0Sstevel@tonic-gate #else
138*0Sstevel@tonic-gate     int virtual_len;
139*0Sstevel@tonic-gate #endif
140*0Sstevel@tonic-gate     struct SOCKSTORAGE virtual_addr;
141*0Sstevel@tonic-gate #endif
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate     (void) strlcpy(_path_ftpaccess, _PATH_FTPACCESS, sizeof(_path_ftpaccess));
144*0Sstevel@tonic-gate     (void) strlcpy(_path_ftpusers, _PATH_FTPUSERS, sizeof(_path_ftpusers));
145*0Sstevel@tonic-gate     (void) strlcpy(_path_private, _PATH_PRIVATE, sizeof(_path_private));
146*0Sstevel@tonic-gate     (void) strlcpy(_path_cvt, _PATH_CVT, sizeof(_path_cvt));
147*0Sstevel@tonic-gate     (void) strlcpy(logfile, _PATH_XFERLOG, MAXPATHLEN);
148*0Sstevel@tonic-gate     (void) strlcpy(_path_ftphosts, _PATH_FTPHOSTS, sizeof(_path_ftphosts));
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate #ifdef VIRTUAL
151*0Sstevel@tonic-gate     /*
152*0Sstevel@tonic-gate        ** Open PATH_FTPSERVERS config file.  If the file does not
153*0Sstevel@tonic-gate        ** exist then revert to using the standard _PATH_* path defines.
154*0Sstevel@tonic-gate      */
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate     if ((svrfp = fopen(_PATH_FTPSERVERS, "r")) != NULL) {
157*0Sstevel@tonic-gate 	/*
158*0Sstevel@tonic-gate 	   ** OK.  The ftpservers file exists and is open.
159*0Sstevel@tonic-gate 	   **
160*0Sstevel@tonic-gate 	   ** Format of the file is:
161*0Sstevel@tonic-gate 	   **    ipaddr/hostname   directory-containing-configuration-files
162*0Sstevel@tonic-gate 	   **
163*0Sstevel@tonic-gate 	   **    208.196.145.10   /etc/ftpd/ftpaccess.somedomain/
164*0Sstevel@tonic-gate 	   **    208.196.145.200  /etc/ftpd/ftpaccess.someotherdomain/
165*0Sstevel@tonic-gate 	   **    some.domain      INTERNAL
166*0Sstevel@tonic-gate 	   **
167*0Sstevel@tonic-gate 	   ** Parse the file and try to match the IP address to one found
168*0Sstevel@tonic-gate 	   ** in the file.  If a match is found then return the path to
169*0Sstevel@tonic-gate 	   ** the specified directory that contains the configuration files
170*0Sstevel@tonic-gate 	   ** for that specific domain.  If a match is not found, or an invalid
171*0Sstevel@tonic-gate 	   ** directory path is encountered like above, return standard paths.
172*0Sstevel@tonic-gate 	   **
173*0Sstevel@tonic-gate 	   ** As usual, comments and blanklines are ignored.
174*0Sstevel@tonic-gate 	 */
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	/* get our address */
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	virtual_len = sizeof(virtual_addr);
179*0Sstevel@tonic-gate 	if (getsockname(0, (struct sockaddr *) &virtual_addr, &virtual_len) == 0) {
180*0Sstevel@tonic-gate 	    while (read_servers_line(svrfp, hostaddress, sizeof(hostaddress),
181*0Sstevel@tonic-gate 		   configdir, sizeof(configdir)) == 1) {
182*0Sstevel@tonic-gate 		if (!strcmp(hostaddress, inet_stop(&virtual_addr))) {
183*0Sstevel@tonic-gate 		    if (debug)
184*0Sstevel@tonic-gate 			syslog(LOG_DEBUG, "VirtualFTP Connect to: %s", hostaddress);
185*0Sstevel@tonic-gate 		    (void) strlcpy(virtual_address, hostaddress,
186*0Sstevel@tonic-gate 				   MAXHOSTNAMELEN);
187*0Sstevel@tonic-gate 		    if (hostname != NULL) {
188*0Sstevel@tonic-gate 			/* reset hostname to this virtual name */
189*0Sstevel@tonic-gate 			wu_gethostbyaddr(&virtual_addr, hostname, MAXHOSTNAMELEN);
190*0Sstevel@tonic-gate 			(void) strlcpy(virtual_hostname, hostname,
191*0Sstevel@tonic-gate 				       MAXHOSTNAMELEN);
192*0Sstevel@tonic-gate 		    }
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 		    /* get rid of trailing slash */
195*0Sstevel@tonic-gate 		    sp = configdir + (strlen(configdir) - 1);
196*0Sstevel@tonic-gate 		    if (*sp == '/')
197*0Sstevel@tonic-gate 			*sp = '\0';
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 		    /*
200*0Sstevel@tonic-gate 		       ** check to see that a valid directory value was
201*0Sstevel@tonic-gate 		       ** supplied and not something such as "INTERNAL"
202*0Sstevel@tonic-gate 		     */
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate 		    if ((stat(configdir, &st) == 0) &&
205*0Sstevel@tonic-gate 			((st.st_mode & S_IFMT) == S_IFDIR)) {
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate 			(void) snprintf(filepath, sizeof(filepath),
208*0Sstevel@tonic-gate 					"%s/ftpaccess", configdir);
209*0Sstevel@tonic-gate 			if (access(filepath, R_OK) == 0) {
210*0Sstevel@tonic-gate 			    (void) strlcpy(_path_ftpaccess, filepath,
211*0Sstevel@tonic-gate 					   sizeof(_path_ftpaccess));
212*0Sstevel@tonic-gate 			    virtual_mode = 1;
213*0Sstevel@tonic-gate 			    virtual_ftpaccess = 1;
214*0Sstevel@tonic-gate 			}
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 			(void) snprintf(filepath, sizeof(filepath),
217*0Sstevel@tonic-gate 					"%s/ftpusers", configdir);
218*0Sstevel@tonic-gate 			if (access(filepath, R_OK) == 0)
219*0Sstevel@tonic-gate 			    (void) strlcpy(_path_ftpusers, filepath,
220*0Sstevel@tonic-gate 					   sizeof(_path_ftpusers));
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 			(void) snprintf(filepath, sizeof(filepath),
223*0Sstevel@tonic-gate 					"%s/ftpgroups", configdir);
224*0Sstevel@tonic-gate 			if (access(filepath, R_OK) == 0)
225*0Sstevel@tonic-gate 			    (void) strlcpy(_path_private, filepath,
226*0Sstevel@tonic-gate 					   sizeof(_path_private));
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 			(void) snprintf(filepath, sizeof(filepath),
229*0Sstevel@tonic-gate 					"%s/ftphosts", configdir);
230*0Sstevel@tonic-gate 			if (access(filepath, R_OK) == 0)
231*0Sstevel@tonic-gate 			    (void) strlcpy(_path_ftphosts, filepath,
232*0Sstevel@tonic-gate 					   sizeof(_path_ftphosts));
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 			(void) snprintf(filepath, sizeof(filepath),
235*0Sstevel@tonic-gate 					"%s/ftpconversions", configdir);
236*0Sstevel@tonic-gate 			if (access(filepath, R_OK) == 0)
237*0Sstevel@tonic-gate 			    (void) strlcpy(_path_cvt, filepath,
238*0Sstevel@tonic-gate 					   sizeof(_path_cvt));
239*0Sstevel@tonic-gate 		    }
240*0Sstevel@tonic-gate 		    (void) fclose(svrfp);
241*0Sstevel@tonic-gate 		    return;
242*0Sstevel@tonic-gate 		}
243*0Sstevel@tonic-gate 	    }
244*0Sstevel@tonic-gate 	}
245*0Sstevel@tonic-gate 	(void) fclose(svrfp);
246*0Sstevel@tonic-gate     }
247*0Sstevel@tonic-gate #endif /* VIRTUAL */
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate     return;
250*0Sstevel@tonic-gate }
251