Merge from vendor branch GDB:
[dragonfly.git] / contrib / lukemftpd / src / extern.h
1 /*      $NetBSD: extern.h,v 1.43 2001/12/04 13:54:12 lukem Exp $        */
2
3 /*-
4  * Copyright (c) 1992, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by the University of
18  *      California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *      @(#)extern.h    8.2 (Berkeley) 4/4/94
36  */
37
38 /*-
39  * Copyright (c) 1997-2001 The NetBSD Foundation, Inc.
40  * All rights reserved.
41  *
42  * This code is derived from software contributed to The NetBSD Foundation
43  * by Luke Mewburn.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. All advertising materials mentioning features or use of this software
54  *    must display the following acknowledgement:
55  *        This product includes software developed by the NetBSD
56  *        Foundation, Inc. and its contributors.
57  * 4. Neither the name of The NetBSD Foundation nor the names of its
58  *    contributors may be used to endorse or promote products derived
59  *    from this software without specific prior written permission.
60  *
61  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
62  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
63  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
64  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
65  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
66  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
67  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
68  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
69  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
70  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
71  * POSSIBILITY OF SUCH DAMAGE.
72  */
73
74 /*
75  * Copyright (C) 1997 and 1998 WIDE Project.
76  * All rights reserved.
77  * 
78  * Redistribution and use in source and binary forms, with or without
79  * modification, are permitted provided that the following conditions
80  * are met:
81  * 1. Redistributions of source code must retain the above copyright
82  *    notice, this list of conditions and the following disclaimer.
83  * 2. Redistributions in binary form must reproduce the above copyright
84  *    notice, this list of conditions and the following disclaimer in the
85  *    documentation and/or other materials provided with the distribution.
86  * 3. Neither the name of the project nor the names of its contributors
87  *    may be used to endorse or promote products derived from this software
88  *    without specific prior written permission.
89  * 
90  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
91  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
92  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
93  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
94  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
95  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
96  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
97  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
98  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
99  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
100  * SUCH DAMAGE.
101  */
102
103 #ifdef NO_LONG_LONG
104 # define LLF            "%ld"
105 # define LLFP(x)        "%" x "ld"
106 # define LLT            long
107 # define ULLF           "%lu"
108 # define ULLFP(x)       "%" x "lu"
109 # define ULLT           unsigned long
110 # define STRTOLL(x,y,z) strtol(x,y,z)
111 #else
112 #if HAVE_PRINTF_QD
113 # define LLF            "%qd"
114 # define LLFP(x)        "%" x "qd"
115 # define LLT            long long
116 # define ULLF           "%qu"
117 # define ULLFP(x)       "%" x "qu"
118 # define ULLT           unsigned long long
119 # define STRTOLL(x,y,z) strtoll(x,y,z)
120 #else
121 # define LLF            "%lld"
122 # define LLFP(x)        "%" x "lld"
123 # define LLT            long long
124 # define ULLF           "%llu"
125 # define ULLFP(x)       "%" x "llu"
126 # define ULLT           unsigned long long
127 # define STRTOLL(x,y,z) strtoll(x,y,z)
128 #endif
129 #endif
130
131 #define FTP_BUFLEN      512
132
133 void    abor(void);
134 void    blkfree(char **);
135 void    closedataconn(FILE *);
136 char   *conffilename(const char *);
137 char  **copyblk(char **);
138 void    count_users(void);
139 void    cprintf(FILE *, const char *, ...)
140             ;
141 void    cwd(const char *);
142 FILE   *dataconn(const char *, off_t, const char *);
143 void    delete(const char *);
144 int     display_file(const char *, int);
145 char  **do_conversion(const char *);
146 void    dologout(int);
147 void    fatal(const char *);
148 void    feat(void);
149 void    format_path(char *, const char *);
150 int     ftpd_pclose(FILE *);
151 FILE   *ftpd_popen(char *[], const char *, int);
152 char   *getline(char *, int, FILE *);
153 void    init_curclass(void);
154 void    logxfer(const char *, off_t, const char *, const char *,
155             const struct timeval *, const char *);
156 #if 0
157 void    logwtmp(const char *, const char *, const char *);
158 #endif
159 struct tab *lookup(struct tab *, const char *);
160 void    makedir(const char *);
161 void    mlsd(const char *);
162 void    mlst(const char *);
163 void    opts(const char *);
164 void    parse_conf(const char *);
165 void    pass(const char *);
166 void    passive(void);
167 int     lpsvproto2af(int);
168 int     af2lpsvproto(int);
169 int     epsvproto2af(int);
170 int     af2epsvproto(int);
171 void    long_passive(char *, int);
172 int     extended_port(const char *);
173 void    epsv_protounsupp(const char *);
174 void    perror_reply(int, const char *);
175 void    pwd(void);
176 void    removedir(const char *);
177 void    renamecmd(const char *, const char *);
178 char   *renamefrom(const char *);
179 void    reply(int, const char *, ...)
180             ;
181 void    retrieve(char *[], const char *);
182 void    send_file_list(const char *);
183 void    show_chdir_messages(int);
184 void    sizecmd(const char *);
185 void    statcmd(void);
186 void    statfilecmd(const char *);
187 void    statxfer(void);
188 void    store(const char *, const char *, int);
189 LLT     strsuftoll(const char *);
190 void    user(const char *);
191 char   *xstrdup(const char *);
192 void    yyerror(char *);
193
194 #include <netinet/in.h>
195
196 #if defined(__NetBSD__)
197 # define HAVE_SETPROCTITLE      1
198 # define HAVE_SOCKADDR_SA_LEN   1
199 #endif
200
201 struct sockinet {
202         union sockunion {
203                 struct sockaddr_in  su_sin;
204 #ifdef INET6
205                 struct sockaddr_in6 su_sin6;
206 #endif
207         } si_su;
208 #if !HAVE_SOCKADDR_SA_LEN
209         int     si_len;
210 #endif
211 };
212
213 #if !HAVE_SOCKADDR_SA_LEN
214 # define su_len         si_len
215 #else
216 # define su_len         si_su.su_sin.sin_len
217 #endif
218 #define su_addr         si_su.su_sin.sin_addr
219 #define su_family       si_su.su_sin.sin_family
220 #define su_port         si_su.su_sin.sin_port
221 #ifdef INET6
222 # define su_6addr       si_su.su_sin6.sin6_addr
223 # define su_scope_id    si_su.su_sin6.sin6_scope_id
224 #endif
225
226 struct tab {
227         char    *name;
228         short    token;
229         short    state;
230         short    flags; /* 1 if command implemented, 2 if has options,
231                            4 if can occur OOB */
232         char    *help;
233         char    *options;
234 };
235
236 struct ftpconv {
237         struct ftpconv  *next;
238         char            *suffix;        /* Suffix of requested name */
239         char            *types;         /* Valid file types */
240         char            *disable;       /* File to disable conversions */
241         char            *command;       /* Command to do the conversion */
242 };
243
244 typedef enum {
245         CLASS_GUEST,
246         CLASS_CHROOT,
247         CLASS_REAL
248 } class_ft;
249
250 typedef enum {
251         FLAG_checkportcmd =     1<<0,   /* Check port commands */
252         FLAG_denyquick =        1<<1,   /* Check ftpusers(5) before PASS */
253         FLAG_modify =           1<<2,   /* Allow CHMOD, DELE, MKD, RMD, RNFR,
254                                            UMASK */
255         FLAG_passive =          1<<3,   /* Allow PASV mode */
256         FLAG_private =          1<<4,   /* Don't publish class info in STAT */
257         FLAG_sanenames =        1<<5,   /* Restrict names of uploaded files */ 
258         FLAG_upload =           1<<6,   /* As per modify, but also allow
259                                            APPE, STOR, STOU */
260 } classflag_t;
261
262 #define CURCLASS_FLAGS_SET(x)   (curclass.flags |=  (FLAG_ ## x))
263 #define CURCLASS_FLAGS_CLR(x)   (curclass.flags &= ~(FLAG_ ## x))
264 #define CURCLASS_FLAGS_ISSET(x) (curclass.flags &   (FLAG_ ## x))
265
266 struct ftpclass {
267         struct sockinet  advertise;     /* PASV address to advertise as */
268         char            *chroot;        /* Directory to chroot(2) to at login */
269         char            *classname;     /* Current class */
270         struct ftpconv  *conversions;   /* List of conversions */
271         char            *display;       /* File to display upon chdir */
272         char            *homedir;       /* Directory to chdir(2) to at login */
273         classflag_t      flags;         /* Flags; see classflag_t above */
274         int              limit;         /* Max connections (-1 = unlimited) */
275         char            *limitfile;     /* File to display if limit reached */
276         LLT              maxfilesize;   /* Maximum file size of uploads */
277         LLT              maxrateget;    /* Maximum get transfer rate throttle */
278         LLT              maxrateput;    /* Maximum put transfer rate throttle */
279         unsigned int     maxtimeout;    /* Maximum permitted timeout */
280         char            *motd;          /* MotD file to display after login */
281         char            *notify;        /* Files to notify about upon chdir */
282         int              portmin;       /* Minumum port for passive mode */
283         int              portmax;       /* Maximum port for passive mode */
284         LLT              rateget;       /* Get (RETR) transfer rate throttle */
285         LLT              rateput;       /* Put (STOR) transfer rate throttle */
286         unsigned int     timeout;       /* Default timeout */
287         class_ft         type;          /* Class type */
288         mode_t           umask;         /* Umask to use */
289 };
290
291 extern void             ftp_loop(void);
292 extern void             ftp_handle_line(char *);
293
294 #ifndef GLOBAL
295 #define GLOBAL  extern
296 #endif
297
298
299 GLOBAL  struct sockinet ctrl_addr;
300 GLOBAL  struct sockinet data_dest;
301 GLOBAL  struct sockinet data_source;
302 GLOBAL  struct sockinet his_addr;
303 GLOBAL  struct sockinet pasv_addr;
304 GLOBAL  int             connections;
305 GLOBAL  struct ftpclass curclass;
306 GLOBAL  int             debug;
307 GLOBAL  jmp_buf         errcatch;
308 GLOBAL  char            *emailaddr;
309 GLOBAL  int             form;
310 GLOBAL  int             gidcount;       /* number of entries in gidlist[] */
311 GLOBAL  gid_t           gidlist[NGROUPS_MAX];
312 GLOBAL  int             hasyyerrored;
313 GLOBAL  char            hostname[MAXHOSTNAMELEN+1];
314 GLOBAL  char            homedir[MAXPATHLEN];
315 #ifdef KERBEROS5
316 GLOBAL  krb5_context    kcontext;
317 #endif
318 GLOBAL  int             logged_in;
319 GLOBAL  int             logging;
320 GLOBAL  int             pdata;                  /* for passive mode */
321 #if HAVE_SETPROCTITLE
322 GLOBAL  char            proctitle[BUFSIZ];      /* initial part of title */
323 #endif
324 GLOBAL  struct passwd  *pw;
325 GLOBAL  int             quietmessages;
326 GLOBAL  char            remotehost[MAXHOSTNAMELEN+1];
327 GLOBAL  off_t           restart_point;
328 GLOBAL  char            tmpline[FTP_BUFLEN];
329 GLOBAL  sig_atomic_t    transflag;
330 GLOBAL  int             type;
331 GLOBAL  int             usedefault;             /* for data transfers */
332 GLOBAL  const char     *version;
333 GLOBAL  int             is_oob;
334
335                                                 /* total file data bytes */
336 GLOBAL  off_t           total_data_in,  total_data_out,  total_data;
337                                                 /* total number of data files */
338 GLOBAL  off_t           total_files_in, total_files_out, total_files;
339                                                 /* total bytes */
340 GLOBAL  off_t           total_bytes_in, total_bytes_out, total_bytes;
341                                                 /* total number of xfers */
342 GLOBAL  off_t           total_xfers_in, total_xfers_out, total_xfers;
343
344 extern  struct tab      cmdtab[];
345
346 #define INTERNAL_LS     "/bin/ls"
347
348
349 #define CMD_IMPLEMENTED(x)      ((x)->flags != 0)
350 #define CMD_HAS_OPTIONS(x)      ((x)->flags & 0x2)
351 #define CMD_OOB(x)              ((x)->flags & 0x4)
352
353 #define CPUTC(c, f)     do { \
354                                 putc(c, f); total_bytes++; total_bytes_out++; \
355                         } while (0);
356
357 #define CURCLASSTYPE    curclass.type == CLASS_GUEST  ? "GUEST"  : \
358                         curclass.type == CLASS_CHROOT ? "CHROOT" : \
359                         curclass.type == CLASS_REAL   ? "REAL"   : \
360                         "<unknown>"
361
362 #define ISDOTDIR(x)     (x[0] == '.' && x[1] == '\0')
363 #define ISDOTDOTDIR(x)  (x[0] == '.' && x[1] == '.' && x[2] == '\0')
364
365 #define EMPTYSTR(p)     ((p) == NULL || *(p) == '\0')
366 #define NEXTWORD(P, W)  do { \
367                                 (W) = strsep(&(P), " \t"); \
368                         } while ((W) != NULL && *(W) == '\0')
369 #define PLURAL(s)       ((s) == 1 ? "" : "s")
370 #define REASSIGN(X,Y)   do { if (X) free(X); (X)=(Y); } while (/*CONSTCOND*/0)
371
372 #ifndef IPPORT_ANONMAX
373 # define IPPORT_ANONMAX 65535
374 #endif