1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
/*
* ProFTPD: mod_wrap2_file -- a mod_wrap2 sub-module for supplying IP-based
* access control data via file-based tables
* Copyright (c) 2002-2016 TJ Saunders
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, TJ Saunders gives permission to link this program
* with OpenSSL, and distribute the resulting executable, without including
* the source code for OpenSSL in the source distribution.
*/
#include "mod_wrap2.h"
#define MOD_WRAP2_FILE_VERSION "mod_wrap2_file/1.3"
module wrap2_file_module;
static const char *filetab_service_name = NULL;
static array_header *filetab_clients_list = NULL;
static array_header *filetab_daemons_list = NULL;
static array_header *filetab_options_list = NULL;
#ifndef MOD_WRAP2_FILE_BUFFER_SIZE
# define MOD_WRAP2_FILE_BUFFER_SIZE PR_TUNABLE_BUFFER_SIZE
#endif
static void filetab_parse_table(wrap2_table_t *filetab) {
unsigned int lineno = 0;
char buf[MOD_WRAP2_FILE_BUFFER_SIZE] = {'\0'};
while (pr_fsio_getline(buf, sizeof(buf), (pr_fh_t *) filetab->tab_handle,
&lineno) != NULL) {
char *ptr, *res = NULL, *service = NULL;
size_t buflen = strlen(buf);
if (buf[buflen-1] != '\n') {
wrap2_log("file '%s': missing newline or line too long (%u) at line %u",
filetab->tab_name, (unsigned int) buflen, lineno);
continue;
}
if (buf[0] == '#' || buf[strspn(buf, " \t\r\n")] == 0) {
continue;
}
buf[buflen-1] = '\0';
/* The list of daemons is from the start of the line to a ':' delimiter.
* This list is assumed to be space-delimited; failure to match this
* syntax will result in lack of desired results when doing the access
* checks.
*/
ptr = strchr(buf, ':');
if (ptr == NULL) {
wrap2_log("file '%s': badly formatted list of daemon/service names at "
"line %u", filetab->tab_name, lineno);
continue;
}
service = pstrndup(filetab->tab_pool, buf, (ptr - buf));
if (filetab_service_name &&
(strcasecmp(filetab_service_name, service) == 0 ||
strncasecmp("ALL", service, 4) == 0)) {
if (filetab_daemons_list == NULL) {
filetab_daemons_list = make_array(filetab->tab_pool, 0, sizeof(char *));
}
*((char **) push_array(filetab_daemons_list)) = service;
res = wrap2_strsplit(buf, ':');
if (res == NULL) {
wrap2_log("file '%s': missing \":\" separator at %u",
filetab->tab_name, lineno);
continue;
}
if (filetab_clients_list == NULL) {
filetab_clients_list = make_array(filetab->tab_pool, 0, sizeof(char *));
}
/* Check for another ':' delimiter. If present, anything following that
* delimiter is an option/shell command (as per the hosts_access(5) man
* page syntax description).
*
* If there are commas or whitespace in the line, parse them as separate
* client names. Otherwise, a comma- or space-delimited list of names
* will be treated as a single name, and violate the principle of least
* surprise for the site admin.
*
* NOTE: Disable support for options in the file syntax if IPv6 addresses
* are present, since the parsing code below is not sufficient for
* handling both IPv6 addresses AND options, e.g.:
*
* proftpd: [::1] [::2]: <options>
*/
ptr = strchr(res, ':');
if (ptr != NULL) {
char *clients;
size_t clients_len;
clients_len = (ptr - res);
clients = pstrndup(filetab->tab_pool, res, clients_len);
if (strcspn(clients, "[]") == clients_len) {
ptr = wrap2_strsplit(res, ':');
if (filetab_options_list == NULL) {
filetab_options_list = make_array(filetab->tab_pool, 0,
sizeof(char *));
}
/* Skip redundant whitespaces */
while (*ptr == ' ' ||
*ptr == '\t') {
pr_signals_handle();
ptr++;
}
*((char **) push_array(filetab_options_list)) =
pstrdup(filetab->tab_pool, ptr);
} else {
/* Ignoring options and IPv6 addresses (Bug#4090) for now. */
}
} else {
/* No options present. */
ptr = res;
}
ptr = strpbrk(res, ", \t");
if (ptr != NULL) {
char *dup_opts, *word;
dup_opts = pstrdup(filetab->tab_pool, res);
while ((word = pr_str_get_token(&dup_opts, ", \t")) != NULL) {
size_t wordlen;
pr_signals_handle();
wordlen = strlen(word);
if (wordlen == 0) {
continue;
}
/* Remove any trailing comma */
if (word[wordlen-1] == ',') {
word[wordlen-1] = '\0';
wordlen--;
}
*((char **) push_array(filetab_clients_list)) = word;
/* Skip redundant whitespaces */
while (*dup_opts == ' ' ||
*dup_opts == '\t') {
pr_signals_handle();
dup_opts++;
}
}
} else {
*((char **) push_array(filetab_clients_list)) =
pstrdup(filetab->tab_pool, res);
}
} else {
wrap2_log("file '%s': skipping irrevelant daemon/service ('%s') line %u",
filetab->tab_name, service, lineno);
}
}
return;
}
static int filetab_close_cb(wrap2_table_t *filetab) {
int res = pr_fsio_close((pr_fh_t *) filetab->tab_handle);
filetab->tab_handle = NULL;
filetab_clients_list = NULL;
filetab_daemons_list = NULL;
filetab_options_list = NULL;
filetab_service_name = NULL;
return res;
}
static array_header *filetab_fetch_clients_cb(wrap2_table_t *filetab,
const char *name) {
/* If this table/file has not yet been parsed, parse it. */
if (*((unsigned char *) filetab->tab_data) == FALSE) {
filetab_parse_table(filetab);
*((unsigned char *) filetab->tab_data) = TRUE;
}
return filetab_clients_list;
}
static array_header *filetab_fetch_daemons_cb(wrap2_table_t *filetab,
const char *name) {
filetab_service_name = name;
/* If this table/file has not yet been parsed, parse it. */
if (*((unsigned char *) filetab->tab_data) == FALSE) {
filetab_parse_table(filetab);
*((unsigned char *) filetab->tab_data) = TRUE;
}
return filetab_daemons_list;
}
static array_header *filetab_fetch_options_cb(wrap2_table_t *filetab,
const char *name) {
/* If this table/file has not yet been parsed, parse it. */
if (*((unsigned char *) filetab->tab_data) == FALSE) {
filetab_parse_table(filetab);
*((unsigned char *) filetab->tab_data) = TRUE;
}
return filetab_options_list;
}
static wrap2_table_t *filetab_open_cb(pool *parent_pool, const char *srcinfo) {
struct stat st;
wrap2_table_t *tab = NULL;
pool *tab_pool = make_sub_pool(parent_pool);
/* Do not allow relative paths. */
if (*srcinfo != '/' &&
*srcinfo != '~') {
wrap2_log("error: table relative paths are forbidden: '%s'", srcinfo);
destroy_pool(tab_pool);
errno = EINVAL;
return NULL;
}
/* If the path starts with a tilde, expand it out. */
if (srcinfo[0] == '~' &&
srcinfo[1] == '/') {
char *path = NULL;
PRIVS_USER
path = dir_realpath(tab_pool, srcinfo);
PRIVS_RELINQUISH
if (path) {
srcinfo = path;
wrap2_log("resolved tilde: path now '%s'", srcinfo);
}
}
/* If the path contains a %U variable, interpolate it. */
if (strstr(srcinfo, "%U") != NULL) {
const char *orig_user;
orig_user = pr_table_get(session.notes, "mod_auth.orig-user", NULL);
if (orig_user != NULL) {
const char *interp_path;
interp_path = sreplace(tab_pool, srcinfo, "%U", orig_user, NULL);
if (interp_path != NULL) {
srcinfo = interp_path;
wrap2_log("resolved %%U: path now '%s'", srcinfo);
}
}
}
tab = (wrap2_table_t *) pcalloc(tab_pool, sizeof(wrap2_table_t));
tab->tab_pool = tab_pool;
/* Open the table handle */
while ((tab->tab_handle = (void *) pr_fsio_open(srcinfo, O_RDONLY)) == NULL) {
int xerrno = errno;
if (xerrno == EINTR) {
pr_signals_handle();
continue;
}
destroy_pool(tab->tab_pool);
errno = xerrno;
return NULL;
}
/* Stat the opened file to determine the optimal buffer size for IO. */
memset(&st, 0, sizeof(st));
if (pr_fsio_fstat((pr_fh_t *) tab->tab_handle, &st) < 0) {
int xerrno = errno;
destroy_pool(tab->tab_pool);
pr_fsio_close((pr_fh_t *) tab->tab_handle);
tab->tab_handle = NULL;
errno = xerrno;
return NULL;
}
if (S_ISDIR(st.st_mode)) {
int xerrno = EISDIR;
destroy_pool(tab->tab_pool);
pr_fsio_close((pr_fh_t *) tab->tab_handle);
tab->tab_handle = NULL;
errno = xerrno;
return NULL;
}
((pr_fh_t *) tab->tab_handle)->fh_iosz = st.st_blksize;
tab->tab_name = pstrdup(tab->tab_pool, srcinfo);
/* Set the necessary callbacks. */
tab->tab_close = filetab_close_cb;
tab->tab_fetch_clients = filetab_fetch_clients_cb;
tab->tab_fetch_daemons = filetab_fetch_daemons_cb;
tab->tab_fetch_options = filetab_fetch_options_cb;
/* Use the tab_data member as a Boolean flag. */
tab->tab_data = pcalloc(tab->tab_pool, sizeof(unsigned char));
*((unsigned char *) tab->tab_data) = FALSE;
return tab;
}
/* Event handlers
*/
#if defined(PR_SHARED_MODULE)
static void filetab_mod_unload_ev(const void *event_data, void *user_data) {
if (strcmp("mod_wrap2_file.c", (const char *) event_data) == 0) {
pr_event_unregister(&wrap2_file_module, NULL, NULL);
wrap2_unregister("file");
}
}
#endif /* PR_SHARED_MODULE */
/* Initialization routines
*/
static int filetab_init(void) {
/* Initialize the wrap source objects for type "file". */
wrap2_register("file", filetab_open_cb);
#if defined(PR_SHARED_MODULE)
pr_event_register(&wrap2_file_module, "core.module-unload",
filetab_mod_unload_ev, NULL);
#endif /* PR_SHARED_MODULE */
return 0;
}
module wrap2_file_module = {
NULL, NULL,
/* Module API version 2.0 */
0x20,
/* Module name */
"wrap2_file",
/* Module configuration handler table */
NULL,
/* Module command handler table */
NULL,
/* Module authentication handler table */
NULL,
/* Module initialization function */
filetab_init,
/* Session initialization function */
NULL,
/* Module version */
MOD_WRAP2_FILE_VERSION
};
|