"Fossies" - the Fresh Open Source Software Archive 
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "ftp_internal.h" see the
Fossies "Dox" file reference documentation.
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /*
18 * Original Copyright (c) 2005 Covalent Technologies
19 *
20 * FTP Protocol module for Apache 2.0
21 */
22
23 #ifndef FTP_INTERNAL_H
24 #define FTP_INTERNAL_H
25
26 #include "mod_ftp.h"
27
28 #ifdef FTP_APXS_BUILD
29 /* Not defined on Win32/Netware since #undef's aren't really needed */
30 #include "ftp_config.h"
31 #endif
32 #ifndef CORE_PRIVATE
33 #define CORE_PRIVATE
34 #endif
35
36 /* FTP header has a couple of defines... on win32
37 rather than including it, we'll just hack em in
38 ourselves
39 */
40 #ifdef HAVE_ARPA_FTP_H
41 #include <arpa/ftp.h>
42 #else
43 #define TYPE_A 1 /* ASCII */
44 #define TYPE_I 3 /* image */
45 #endif
46
47 /* Apache headers */
48 #include "http_log.h"
49 #include "http_main.h"
50 #include "http_core.h"
51 #include "http_connection.h"
52 #include "http_request.h"
53 #include "http_protocol.h"
54
55 #include "ap_config.h"
56 #include "ap_mmn.h"
57
58 #include "util_filter.h"
59 #include "util_ebcdic.h"
60 #include "scoreboard.h"
61
62 #include "apr_buckets.h"
63 #include "apr_strings.h"
64 #include "apr_hooks.h"
65 #include "apr_hash.h"
66 #include "apr_user.h"
67 #include "apr_lib.h"
68 #include "apr_inherit.h"
69 #include "apr_poll.h"
70
71 #ifdef FTP_APXS_BUILD
72 #include "mod_log_config.h"
73 #else
74 #include "../../modules/loggers/mod_log_config.h"
75 #endif
76
77 #if APR_HAVE_UNISTD_H
78 #include <unistd.h>
79 #endif
80
81 #if APR_HAVE_SYS_TYPES_H
82 #include <sys/types.h>
83 #endif
84
85 #ifdef HAVE_SYS_STAT_H
86 #include <sys/stat.h>
87 #endif
88
89 /* SSL Filter name */
90 #define FTP_SSL_FILTER "SSL/TLS Filter"
91
92 /*
93 * FTP_NEED_* - Values used for filter bitmasks
94 *
95 */
96
97 #define FTP_NEED_BYTERANGE (1 << 1)
98 #define FTP_NEED_CONTENT_LENGTH (1 << 2)
99 #define FTP_NEED_CRLF (1 << 4)
100 #define FTP_NEED_DATA_OUT (1 << 5)
101 /*
102 * FTP_MSG_* - Text for sending back to the client. These include
103 * metacharacters to enforce rigidity of layout.
104 */
105
106 #define FTP_MSG_PERM_DENIED "%s: Permission denied"
107 #define FTP_MSG_NOT_A_DIR "%s: Not a directory"
108 #define FTP_MSG_NOSUCHFILE "%s: No such file or directory"
109 #define FTP_MSG_OPENASCII "Opening ASCII mode data connection for file list"
110 #define FTP_MSG_SUCCESS "%s: Command successful"
111 #define FTP_MSG_NOTPLAIN "%s: Not a plain file"
112 #define FTP_MSG_DIR_CREAT "\"%s\" new directory created"
113 #define FTP_MSG_DIR_CUR "\"%s\" is current directory"
114 #define FTP_MSG_HELP_SYNTAX "Syntax: %s %s"
115 #define FTP_MSG_HELP "Direct comments to %s"
116 #define FTP_MSG_NOTIMPL "%s: Command not implemented"
117 #define FTP_MSG_NOTALLOWED "Permission denied: %s not allowed here"
118 #define FTP_MSG_SESSIONLIMIT "Maximum number of concurrent sessions reached," \
119 " closing connection."
120
121 #define FTP_DEFAULT_DBFILE "logs/ftplogins"
122 /*
123 * FTP_OPT_* - FTP server options
124 *
125 */
126 #define FTP_OPT_NONE 0
127 #define FTP_OPT_REQUIRESSL (1 << 0) /* Require AUTH before USER/PASS */
128 #define FTP_OPT_CHECKMAXCLIENTS (1 << 1) /* Check if the server is full */
129 #define FTP_OPT_REMOVEUSERGROUP (1 << 2) /* Fill in user/group information
130 * on directory listings */
131 #define FTP_OPT_NLSTSHOWDIRS (1 << 3) /* Show directories on NLST
132 * requests */
133 #define FTP_OPT_NLSTISLIST (1 << 4) /* Maps NLST directly to LIST */
134 #define FTP_OPT_CREATEHOMEDIRS (1 << 5) /* Create home dirs on the fly */
135 #define FTP_OPT_SHOWUNAUTH (1 << 6) /* Show unauthorized files in LIST */
136 #define FTP_OPT_LISTISNLST (1 << 7) /* Maps LiST directly to NLST */
137 #define FTP_OPT_ALLOWPROXYPORT (1 << 8) /* Data connect to other than client */
138 #define FTP_OPT_ALLOWPROXYPASV (1 << 9) /* Data listens to other than client */
139 #define FTP_OPT_VHOST_BY_USER (1 << 10) /* Parse USER for @hostname vhost */
140 #define FTP_OPT_STRIP_HOSTNAME (1 << 11) /* Strip @hostname from username */
141 #define FTP_OPT_NO_UTF8_FEAT (1 << 12) /* Hide UTF8 from FEAT results */
142
143 /* Supported FTP AUTH mechanisms. */
144 #define FTP_AUTH_NONE 0
145 #define FTP_AUTH_SSL 1
146 #define FTP_AUTH_TLS 2
147
148 /* Data protection levels */
149 #define FTP_PROT_CLEAR 0
150 #define FTP_PROT_SAFE 1
151 #define FTP_PROT_CONFIDENTIAL 2
152 #define FTP_PROT_PRIVATE 3
153
154 typedef enum {
155 FTP_LIMIT_OK,
156 FTP_LIMIT_HIT_PERUSER,
157 FTP_LIMIT_HIT_PERIP,
158 FTP_LIMIT_HIT_PERSERVER,
159 FTP_LIMIT_ERROR
160 } ftp_loginlimit_t;
161
162 /* Directory entry structure. Used for directory listings */
163 typedef struct ftp_direntry
164 {
165 const char *name; /* Just the name of the file (foo.c) */
166 const char *modestring;
167 char datestring[13]; /* Date srings have a fixed length */
168 char *username;
169 char *groupname;
170 apr_off_t size;
171 apr_off_t csize;
172 apr_int32_t nlink;
173 struct ftp_direntry *child; /* For descending */
174 struct ftp_direntry *next;
175 } ftp_direntry;
176
177 #define FTP_DEFAULT_UMASK (APR_GWRITE | APR_WWRITE)
178 #define FTP_UNSPEC -2
179 #define FTP_TIMEOUT_LOGIN 120
180 #define FTP_TIMEOUT_IDLE 600
181 #define FTP_TIMEOUT_DATA 300
182 #define FTP_MAX_LOGINS 3
183 #define FTP_MAX_TRIES 10 /* Maximum number of times to retry on
184 failed bind() and connect() */
185
186 #define FTP_DATA_BLOCK_SIZE 48000 /* Size in bytes to read at a time
187 on the data channel before looking
188 aside for an ABOR */
189
190 /* Wrappers for module_config
191 *
192 * mod_ftp.c
193 */
194
195 extern ap_filter_rec_t *ftp_input_filter_handle;
196
197 /* Routines for handling incoming connections
198 *
199 * ftp_connection.c
200 */
201 int ftp_process_connection(conn_rec *c);
202
203 /* Placeholder of "unknown" username (not valid for ftp_cmd_pass)
204 */
205 extern const char ftp_unknown_username[];
206
207 /* Routines for handling FTP requests
208 *
209 * ftp_request.c
210 */
211 void ftp_process_request(request_rec *r);
212
213 /* Routines specific to the FTP Protocol
214 *
215 * ftp_protocol.c
216 */
217 request_rec *ftp_read_request(ftp_connection *fc);
218 apr_status_t ftp_read_ahead_request(ftp_connection *fc);
219 apr_status_t ftp_reply(ftp_connection *fc, ap_filter_t *out_filter,
220 apr_pool_t *p, int n, int l, const char *fmt, ...);
221 apr_status_t ftp_show_file(ap_filter_t *out_filter, apr_pool_t *p, int code,
222 ftp_connection *fc, const char *file);
223 void ftp_send_response(request_rec *r, int res);
224 apr_status_t ftp_ssl_init(conn_rec *c);
225 int ftp_have_ssl(void);
226
227 /* Routines specific to the FTP data connections
228 *
229 * ftp_data_connection.c
230 */
231 void ftp_reset_dataconn(ftp_connection *fc);
232 conn_rec *ftp_open_dataconn(request_rec *r, int write_not_read);
233
234 /* FTP command handlers registration.
235 */
236 void ftp_register_core_cmds(apr_pool_t *pool);
237
238 /* Routines for handling implementation of login limits.
239 * Note that the actual Mutexing functions and DB file
240 * functions are non-public.
241 *
242 * ftp_limitlogin.c
243 */
244 apr_status_t ftp_mutexdb_init(server_rec *s, apr_pool_t *p);
245 apr_status_t ftp_mutexdb_child_init(server_rec *s, apr_pool_t *p);
246 apr_status_t ftp_mutexdb_cleanup(void *dummy);
247 ftp_loginlimit_t ftp_limitlogin_check(const char *user, request_rec *r);
248 int ftp_limitlogin_loggedout(conn_rec *c);
249
250 /* FTP utilities
251 *
252 * ftp_util.c
253 */
254 int ftp_eprt_decode(apr_int32_t *family, char **addr, apr_port_t *port,
255 char *arg);
256 struct ftp_direntry *ftp_direntry_get(request_rec *r, const char *pattern);
257
258 void ftp_set_authorization(request_rec *r);
259 int ftp_set_uri(request_rec *r, const char *arg);
260
261 #ifdef WIN32
262 #define mode_t int
263 #endif
264
265 mode_t ftp_unix_perms2mode(apr_fileperms_t perms);
266 apr_fileperms_t ftp_unix_mode2perms(mode_t mode);
267
268 char *ftp_toupper(apr_pool_t *p, const char *s);
269 int ftp_check_maxclients(request_rec *r);
270
271 /* FTP filters
272 *
273 * ftp_filters.c
274 */
275 apr_status_t ftp_data_out_filter(ap_filter_t * f, apr_bucket_brigade * bb);
276 apr_status_t ftp_crlf_filter(ap_filter_t *f, apr_bucket_brigade *bb);
277 apr_status_t ftp_protocol_filter(ap_filter_t *f, apr_bucket_brigade *b,
278 ap_input_mode_t mode, apr_read_type_e block,
279 apr_off_t readbytes);
280 apr_bucket *ftp_bucket_datasock_create(ftp_connection *fc,
281 apr_bucket_alloc_t *list);
282 extern const apr_bucket_type_t ftp_bucket_type_datasock;
283
284 /* FTP logging hooks into mod_log_config
285 *
286 * ftp_log.c
287 */
288 const char *ftp_log_transfer_mode(request_rec *r, char *a);
289 const char *ftp_log_action_flags(request_rec *r, char *a);
290 const char *ftp_log_transfer_direction(request_rec *r, char *a);
291 const char *ftp_log_accessed_anonymously(request_rec *r, char *a);
292 const char *ftp_log_service_name(request_rec *r, char *a);
293 const char *ftp_log_auth_method(request_rec *r, char *a);
294 const char *ftp_log_auth_user_id(request_rec *r, char *a);
295
296 /* dirlist stuff */
297 #define FTP_MODESTRING_LEN 11 /* # of chars in a modestring, with \0 */
298 #define FTP_UNKNOWN_MODESTRING "??????????"
299 #define FTP_MAX_MODESTRING ((APR_UREAD | APR_UWRITE | APR_UEXECUTE | \
300 APR_GREAD | APR_GWRITE | APR_GEXECUTE | \
301 APR_WREAD | APR_WWRITE | APR_WEXECUTE | \
302 APR_USETID | APR_GSETID | APR_WSTICKY) + 1)
303
304 /* FTP message functions for dealing with client messages
305 *
306 * ftp_message.c
307 */
308 void ftp_message_generate(ftp_connection *fc, const char *inbuf,
309 char *outbuf, size_t outlen);
310
311 /* inet_pton functionallity ripped straight out of APR
312 *
313 * ftp_inet_pton.c
314 */
315 int ftp_inet_pton(int af, const char *src, void *dst);
316
317 /* FTP hooks */
318
319 /*
320 * This is the function that translates ftp aliased methods.
321 * It will retreive the base command from the cmd hash.
322 */
323 const char *ftp_get_cmd_alias(const char *key);
324
325 /*
326 * This is the function that runs the ftp handler. It will iterate through
327 * the linked list of entries that returned from the cmd hash.
328 */
329 int ftp_run_cmd(request_rec *r, const char *key);
330
331 /*
332 * This function reports (true|false) if a given command should
333 * abort an active data transfer
334 */
335 int ftp_cmd_abort_data(const char *key);
336
337 /* Finalizes ftp_cmd_help and ftp_cmd_feat messages */
338 void ftp_cmd_finalize(apr_pool_t *p, apr_pool_t *ptemp);
339
340 /* FTP low-numbered-port allocation daemon
341 *
342 * ftp_lowportd.c
343 */
344 /* Lone configuration option */
345 const char *lowportd_set_socket(cmd_parms *cmd, void *dummy, const char *arg);
346
347 /* The module is enabled by the presence of unix domain sockets */
348 #if APR_HAVE_SYS_UN_H && (defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS) \
349 || defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL))
350
351 #define HAVE_FTP_LOWPORTD
352
353 /* Initialization */
354 int lowportd_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp);
355 int lowportd_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp,
356 server_rec *main_server);
357
358 /* Query */
359 apr_status_t ftp_request_lowport(apr_socket_t **sock, request_rec *r,
360 apr_sockaddr_t *sa, apr_pool_t *p);
361 #endif
362
363 #endif