Coverage Report

Created: 2026-02-23 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/proftpd/modules/mod_auth.c
Line
Count
Source
1
/*
2
 * ProFTPD - FTP server daemon
3
 * Copyright (c) 1997, 1998 Public Flood Software
4
 * Copyright (c) 1999, 2000 MacGyver aka Habeeb J. Dihu <macgyver@tos.net>
5
 * Copyright (c) 2001-2025 The ProFTPD Project team
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
20
 *
21
 * As a special exemption, Public Flood Software/MacGyver aka Habeeb J. Dihu
22
 * and other respective copyright holders give permission to link this program
23
 * with OpenSSL, and distribute the resulting executable, without including
24
 * the source code for OpenSSL in the source distribution.
25
 */
26
27
/* Authentication module for ProFTPD */
28
29
#include "conf.h"
30
#include "privs.h"
31
32
#ifdef HAVE_USERSEC_H
33
# include <usersec.h>
34
#endif
35
36
#ifdef HAVE_SYS_AUDIT_H
37
# include <sys/audit.h>
38
#endif
39
40
extern pid_t mpid;
41
42
module auth_module;
43
44
#ifdef PR_USE_LASTLOG
45
static unsigned char lastlog = FALSE;
46
#endif /* PR_USE_LASTLOG */
47
48
static unsigned char mkhome = FALSE;
49
static unsigned char authenticated_without_pass = FALSE;
50
static int TimeoutLogin = PR_TUNABLE_TIMEOUTLOGIN;
51
static int logged_in = FALSE;
52
static int auth_anon_allow_robots = FALSE;
53
static int auth_anon_allow_robots_enabled = FALSE;
54
static int auth_client_connected = FALSE;
55
static int auth_tries = 0;
56
static char *auth_pass_resp_code = R_230;
57
static pr_fh_t *displaylogin_fh = NULL;
58
static int TimeoutSession = 0;
59
60
static int saw_first_user_cmd = FALSE;
61
static const char *timing_channel = "timing";
62
63
static int auth_count_scoreboard(cmd_rec *, const char *);
64
static int auth_scan_scoreboard(void);
65
static int auth_sess_init(void);
66
67
/* auth_cmd_chk_cb() is hooked into the main server's auth_hook function,
68
 * so that we can deny all commands until authentication is complete.
69
 *
70
 * Note: Once this function returns true (i.e. client has authenticated),
71
 * it will ALWAYS return true.  At least until REIN is implemented.  Thus
72
 * we have a flag for such a situation, to save on redundant lookups for
73
 * the "authenticated" record.
74
 */
75
static int auth_have_authenticated = FALSE;
76
77
0
static int auth_cmd_chk_cb(cmd_rec *cmd) {
78
0
  if (auth_have_authenticated == FALSE) {
79
0
    unsigned char *authd;
80
81
0
    authd = get_param_ptr(cmd->server->conf, "authenticated", FALSE);
82
83
0
    if (authd == NULL ||
84
0
        *authd == FALSE) {
85
0
      const void *already_checked = NULL;
86
87
      /* Note that the core dispatching routines could check the same
88
       * unauthenticated cmd_rec multiple times (see Issue #2003).  We thus
89
       * only want to add the error response once per cmd_rec, and avoid
90
       * desynchronizing the client with multiple duplicate error responses.
91
       */
92
93
0
      already_checked = pr_table_get(cmd->notes, "mod_auth.checked-auth", NULL);
94
0
      if (already_checked == NULL) {
95
0
        int checked = TRUE;
96
97
0
        pr_response_add_err(R_530, _("Please login with USER and PASS"));
98
0
        pr_table_add(cmd->notes, "mod_auth.checked-auth", &checked, 0);
99
0
      }
100
101
0
      return FALSE;
102
0
    }
103
104
0
    auth_have_authenticated = TRUE;
105
0
  }
106
107
0
  return TRUE;
108
0
}
109
110
0
static int auth_login_timeout_cb(CALLBACK_FRAME) {
111
0
  pr_response_send_async(R_421,
112
0
    _("Login timeout (%d %s): closing control connection"), TimeoutLogin,
113
0
    TimeoutLogin != 1 ? "seconds" : "second");
114
115
  /* It's possible that any listeners of this event might terminate the
116
   * session process themselves (e.g. mod_ban).  So write out that the
117
   * TimeoutLogin has been exceeded to the log here, in addition to the
118
   * scheduled session exit message.
119
   */
120
0
  pr_log_pri(PR_LOG_INFO, "%s", "Login timeout exceeded, disconnected");
121
0
  pr_event_generate("core.timeout-login", NULL);
122
123
0
  pr_session_disconnect(&auth_module, PR_SESS_DISCONNECT_TIMEOUT,
124
0
    "TimeoutLogin");
125
126
  /* Do not restart the timer (should never be reached). */
127
0
  return 0;
128
0
}
129
130
0
static int auth_session_timeout_cb(CALLBACK_FRAME) {
131
0
  pr_event_generate("core.timeout-session", NULL);
132
0
  pr_response_send_async(R_421,
133
0
    _("Session Timeout (%d seconds): closing control connection"),
134
0
    TimeoutSession);
135
136
0
  pr_log_pri(PR_LOG_INFO, "%s", "FTP session timed out, disconnected");
137
0
  pr_session_disconnect(&auth_module, PR_SESS_DISCONNECT_TIMEOUT,
138
0
    "TimeoutSession");
139
140
  /* no need to restart the timer -- session's over */
141
0
  return 0;
142
0
}
143
144
/* Event listeners
145
 */
146
147
0
static void auth_exit_ev(const void *event_data, void *user_data) {
148
0
  pr_auth_cache_clear();
149
150
  /* Close the scoreboard descriptor that we opened. */
151
0
  (void) pr_close_scoreboard(FALSE);
152
0
}
153
154
0
static void auth_sess_reinit_ev(const void *event_data, void *user_data) {
155
0
  int res;
156
157
  /* A HOST command changed the main_server pointer, reinitialize ourselves. */
158
159
0
  pr_event_unregister(&auth_module, "core.exit", auth_exit_ev);
160
0
  pr_event_unregister(&auth_module, "core.session-reinit", auth_sess_reinit_ev);
161
162
0
  pr_timer_remove(PR_TIMER_LOGIN, &auth_module);
163
164
  /* Reset the CreateHome setting. */
165
0
  mkhome = FALSE;
166
167
  /* Reset any MaxPasswordSize setting. */
168
0
  (void) pr_auth_set_max_password_len(session.pool, 0);
169
170
#if defined(PR_USE_LASTLOG)
171
  lastlog = FALSE;
172
#endif /* PR_USE_LASTLOG */
173
0
  mkhome = FALSE;
174
175
0
  res = auth_sess_init();
176
0
  if (res < 0) {
177
0
    pr_session_disconnect(&auth_module,
178
0
      PR_SESS_DISCONNECT_SESSION_INIT_FAILED, NULL);
179
0
  }
180
0
}
181
182
/* Initialization functions
183
 */
184
185
0
static int auth_init(void) {
186
  /* Add the commands handled by this module to the HELP list. */
187
0
  pr_help_add(C_USER, _("<sp> username"), TRUE);
188
0
  pr_help_add(C_PASS, _("<sp> password"), TRUE);
189
0
  pr_help_add(C_ACCT, _("is not implemented"), FALSE);
190
0
  pr_help_add(C_REIN, _("is not implemented"), FALSE);
191
192
  /* By default, enable auth checking */
193
0
  set_auth_check(auth_cmd_chk_cb);
194
195
0
  return 0;
196
0
}
197
198
0
static int auth_sess_init(void) {
199
0
  config_rec *c = NULL;
200
0
  unsigned char *tmp = NULL;
201
202
0
  pr_event_register(&auth_module, "core.session-reinit", auth_sess_reinit_ev,
203
0
    NULL);
204
205
  /* Check for any MaxPasswordSize. */
206
0
  c = find_config(main_server->conf, CONF_PARAM, "MaxPasswordSize", FALSE);
207
0
  if (c != NULL) {
208
0
    size_t len;
209
210
0
    len = *((size_t *) c->argv[0]);
211
0
    (void) pr_auth_set_max_password_len(session.pool, len);
212
0
  }
213
214
  /* Check for a server-specific TimeoutLogin */
215
0
  c = find_config(main_server->conf, CONF_PARAM, "TimeoutLogin", FALSE);
216
0
  if (c != NULL) {
217
0
    TimeoutLogin = *((int *) c->argv[0]);
218
0
  }
219
220
  /* Start the login timer */
221
0
  if (TimeoutLogin) {
222
0
    pr_timer_remove(PR_TIMER_LOGIN, &auth_module);
223
0
    pr_timer_add(TimeoutLogin, PR_TIMER_LOGIN, &auth_module,
224
0
      auth_login_timeout_cb, "TimeoutLogin");
225
0
  }
226
227
0
  if (auth_client_connected == FALSE) {
228
0
    int res = 0;
229
230
0
    PRIVS_ROOT
231
0
    res = pr_open_scoreboard(O_RDWR);
232
0
    PRIVS_RELINQUISH
233
234
0
    if (res < 0) {
235
0
      switch (res) {
236
0
        case PR_SCORE_ERR_BAD_MAGIC:
237
0
          pr_log_debug(DEBUG0, "error opening scoreboard: bad/corrupted file");
238
0
          break;
239
240
0
        case PR_SCORE_ERR_OLDER_VERSION:
241
0
          pr_log_debug(DEBUG0,
242
0
            "error opening scoreboard: bad version (too old)");
243
0
          break;
244
245
0
        case PR_SCORE_ERR_NEWER_VERSION:
246
0
          pr_log_debug(DEBUG0,
247
0
            "error opening scoreboard: bad version (too new)");
248
0
          break;
249
250
0
        default:
251
0
          pr_log_debug(DEBUG0, "error opening scoreboard: %s", strerror(errno));
252
0
          break;
253
0
      }
254
0
    }
255
0
  }
256
257
0
  pr_event_register(&auth_module, "core.exit", auth_exit_ev, NULL);
258
259
0
  if (auth_client_connected == FALSE) {
260
0
    unsigned int scoreboard_opts = 0UL;
261
262
0
    c = find_config(main_server->conf, CONF_PARAM, "ScoreboardOptions", FALSE);
263
0
    while (c != NULL) {
264
0
      unsigned long opts;
265
266
0
      pr_signals_handle();
267
268
0
      opts = *((unsigned long *) c->argv[0]);
269
0
      scoreboard_opts |= opts;
270
271
0
      c = find_config_next(c, c->next, CONF_PARAM, "ScoreboardOptions", FALSE);
272
0
    }
273
274
    /* Create an entry in the scoreboard for this session, if we don't already
275
     * have one.
276
     */
277
0
    if (pr_scoreboard_entry_get(PR_SCORE_CLIENT_ADDR) == NULL) {
278
0
      if (pr_scoreboard_entry_add() < 0) {
279
280
0
        if (scoreboard_opts & PR_SCOREBOARD_OPT_ALLOW_MISSING_ENTRY) {
281
          /* In this case, we simply log the error, but allow the session to
282
           * continue while lacking a Scoreboard entry.
283
           */
284
0
          pr_log_pri(PR_LOG_NOTICE,
285
0
            "notice: unable to add scoreboard entry: %s", strerror(errno));
286
287
0
        } else {
288
0
          pr_log_pri(PR_LOG_ERR,
289
0
            "error: unable to add scoreboard entry: %s", strerror(errno));
290
0
          pr_session_disconnect(&auth_module,
291
0
            PR_SESS_DISCONNECT_SESSION_INIT_FAILED, "No ScoreboardFile entry");
292
0
        }
293
0
      }
294
295
0
      pr_scoreboard_entry_update(session.pid,
296
0
        PR_SCORE_USER, "(none)",
297
0
        PR_SCORE_SERVER_PORT, main_server->ServerPort,
298
0
        PR_SCORE_SERVER_ADDR, session.c->local_addr, session.c->local_port,
299
0
        PR_SCORE_SERVER_LABEL, main_server->ServerName,
300
0
        PR_SCORE_CLIENT_ADDR, session.c->remote_addr,
301
0
        PR_SCORE_CLIENT_NAME, session.c->remote_name,
302
0
        PR_SCORE_CLASS, session.conn_class ? session.conn_class->cls_name : "",
303
0
        PR_SCORE_PROTOCOL, "ftp",
304
0
        PR_SCORE_BEGIN_SESSION, time(NULL),
305
0
        NULL);
306
0
    }
307
308
0
  } else {
309
    /* We're probably handling a HOST command, and the server changed; just
310
     * update the SERVER_LABEL field.
311
     */
312
0
    pr_scoreboard_entry_update(session.pid,
313
0
      PR_SCORE_SERVER_LABEL, main_server->ServerName,
314
0
      NULL);
315
0
  }
316
317
  /* Should we create the home for a user, if they don't have one? */
318
0
  tmp = get_param_ptr(main_server->conf, "CreateHome", FALSE);
319
0
  if (tmp != NULL &&
320
0
      *tmp == TRUE) {
321
0
    mkhome = TRUE;
322
323
0
  } else {
324
0
    mkhome = FALSE;
325
0
  }
326
327
#ifdef PR_USE_LASTLOG
328
  /* Use the lastlog file, if supported and requested. */
329
  tmp = get_param_ptr(main_server->conf, "UseLastlog", FALSE);
330
  if (tmp &&
331
      *tmp == TRUE) {
332
    lastlog = TRUE;
333
334
  } else {
335
    lastlog = FALSE;
336
  }
337
#endif /* PR_USE_LASTLOG */
338
339
  /* Scan the scoreboard now, in order to tally up certain values for
340
   * substituting in any of the Display* file variables.  This function
341
   * also performs the MaxConnectionsPerHost enforcement.
342
   */
343
0
  auth_scan_scoreboard();
344
345
0
  auth_client_connected = TRUE;
346
0
  return 0;
347
0
}
348
349
0
static int do_auth(pool *p, xaset_t *conf, const char *u, char *pw) {
350
0
  char *cpw = NULL;
351
352
0
  if (conf != NULL) {
353
0
    config_rec *c;
354
355
0
    c = find_config(conf, CONF_PARAM, "UserPassword", FALSE);
356
0
    while (c != NULL) {
357
0
      pr_signals_handle();
358
359
0
      if (strcmp(c->argv[0], u) == 0) {
360
0
        cpw = (char *) c->argv[1];
361
0
        break;
362
0
      }
363
364
0
      c = find_config_next(c, c->next, CONF_PARAM, "UserPassword", FALSE);
365
0
    }
366
0
  }
367
368
0
  if (cpw != NULL) {
369
0
    if (pr_auth_getpwnam(p, u) == NULL) {
370
0
      int xerrno = errno;
371
372
0
      if (xerrno == ENOENT) {
373
0
        pr_log_pri(PR_LOG_NOTICE, "no such user '%s'", u);
374
0
      }
375
376
0
      errno = xerrno;
377
0
      return PR_AUTH_NOPWD;
378
0
    }
379
380
0
    return pr_auth_check(p, cpw, u, pw);
381
0
  }
382
383
0
  return pr_auth_authenticate(p, u, pw);
384
0
}
385
386
/* Command handlers
387
 */
388
389
0
static void login_failed(pool *p, const char *user) {
390
0
  const char *host, *sess_ttyname;
391
#if defined(HAVE_LOGINFAILED)
392
  int res, xerrno;
393
#endif /* HAVE_LOGINFAILED */
394
395
0
  host = pr_netaddr_get_dnsstr(session.c->remote_addr);
396
0
  sess_ttyname = pr_session_get_ttyname(p);
397
398
0
  pr_trace_msg("auth", 19, "mod_auth handling failed login for "
399
0
    "user = '%s', host = '%s', tty = '%s'", user, host, sess_ttyname);
400
#if defined(HAVE_LOGINFAILED)
401
  PRIVS_ROOT
402
  res = loginfailed((char *) user, (char *) host, (char *) sess_ttyname,
403
    AUDIT_FAIL);
404
  xerrno = errno;
405
  PRIVS_RELINQUISH
406
407
  if (res < 0) {
408
    pr_trace_msg("auth", 3, "AIX loginfailed() error for user '%s', "
409
      "host '%s', tty '%s', reason %d: %s", user, host, sess_ttyname,
410
      AUDIT_FAIL, strerror(xerrno));
411
  }
412
#endif /* HAVE_LOGINFAILED */
413
0
}
414
415
0
MODRET auth_err_pass(cmd_rec *cmd) {
416
0
  const char *user;
417
418
0
  user = pr_table_get(session.notes, "mod_auth.orig-user", NULL);
419
0
  if (user != NULL) {
420
0
    const void *hint;
421
422
    /* Look for any notes/hints attached to this command which might indicate
423
     * that it is not a real PASS command error, but rather a fake command
424
     * dispatched for e.g. logging/handling by other modules.  We pay attention
425
     * to this here due to e.g. AIX loginfailed(3) semantics (Issue #693).
426
     */
427
0
    hint = pr_table_get(cmd->notes, "mod_sftp.nonfatal-attempt", NULL);
428
0
    if (hint == NULL) {
429
0
      login_failed(cmd->tmp_pool, user);
430
431
0
    } else {
432
0
      pr_trace_msg("auth", 19,
433
0
        "ignoring non-fatal %s auth attempt for user '%s' from mod_sftp",
434
0
        (const char *) hint, user);
435
0
    }
436
0
  }
437
438
  /* Remove the stashed original USER name here in a LOG_CMD_ERR handler, so
439
   * that other modules, who may want to lookup the original USER parameter on
440
   * a failed login in an earlier command handler phase, have a chance to do
441
   * so.  This removal of the USER parameter on failure was happening directly
442
   * in the CMD handler previously, thus preventing POST_CMD_ERR handlers from
443
   * using USER.
444
   */
445
0
  pr_table_remove(session.notes, "mod_auth.orig-user", NULL);
446
447
  /* If auth_tries = -1, that means we reached the max login attempts and
448
   * should disconnect the session.
449
   */
450
0
  if (auth_tries == -1) {
451
0
    pr_session_disconnect(&auth_module, PR_SESS_DISCONNECT_CONFIG_ACL,
452
0
      "Denied by MaxLoginAttempts");
453
0
  }
454
455
0
  return PR_HANDLED(cmd);
456
0
}
457
458
0
MODRET auth_log_pass(cmd_rec *cmd) {
459
460
  /* Only log, to the syslog, that the login has succeeded here, where we
461
   * know that the login has definitely succeeded.
462
   */
463
0
  pr_log_auth(PR_LOG_INFO, "%s %s: Login successful.",
464
0
    (session.anon_config != NULL) ? "ANON" : C_USER, session.user);
465
466
0
  if (cmd->arg != NULL) {
467
0
    size_t passwd_len;
468
469
    /* And scrub the memory holding the password sent by the client, for
470
     * safety/security.
471
     */
472
0
    passwd_len = strlen(cmd->arg);
473
0
    pr_memscrub(cmd->arg, passwd_len);
474
0
  }
475
476
0
  return PR_DECLINED(cmd);
477
0
}
478
479
0
static void login_succeeded(pool *p, const char *user) {
480
0
  const char *host, *sess_ttyname;
481
#if defined(HAVE_LOGINSUCCESS)
482
  char *msg = NULL;
483
  int res, xerrno;
484
#endif /* HAVE_LOGINSUCCESS */
485
486
0
  host = pr_netaddr_get_dnsstr(session.c->remote_addr);
487
0
  sess_ttyname = pr_session_get_ttyname(p);
488
489
0
  pr_trace_msg("auth", 19, "mod_auth handling successful login for "
490
0
    "user = '%s', host = '%s', tty = '%s'", user, host, sess_ttyname);
491
492
#if defined(HAVE_LOGINSUCCESS)
493
  PRIVS_ROOT
494
  res = loginsuccess((char *) user, (char *) host, (char *) sess_ttyname, &msg);
495
  xerrno = errno;
496
  PRIVS_RELINQUISH
497
498
  if (res == 0) {
499
    if (msg != NULL) {
500
      pr_trace_msg("auth", 14, "AIX loginsuccess() report: %s", msg);
501
    }
502
503
  } else {
504
    pr_trace_msg("auth", 3, "AIX loginsuccess() error for user '%s', "
505
      "host '%s', tty '%s': %s", user, host, sess_ttyname, strerror(errno));
506
  }
507
508
  if (msg != NULL) {
509
    free(msg);
510
  }
511
#endif /* HAVE_LOGINSUCCESS */
512
0
}
513
514
0
MODRET auth_post_pass(cmd_rec *cmd) {
515
0
  config_rec *c = NULL;
516
0
  const char *grantmsg = NULL, *user;
517
0
  unsigned int ctxt_precedence = 0;
518
0
  unsigned char have_user_timeout, have_group_timeout, have_class_timeout,
519
0
    have_all_timeout, *authenticated;
520
0
  int root_revoke = TRUE;
521
0
  struct stat st;
522
523
  /* Was there a preceding USER command? Was the client successfully
524
   * authenticated?
525
   */
526
0
  authenticated = get_param_ptr(cmd->server->conf, "authenticated", FALSE);
527
528
  /* Clear the list of auth-only modules. */
529
0
  pr_auth_clear_auth_only_modules();
530
531
0
  if (authenticated != NULL &&
532
0
      *authenticated == TRUE) {
533
534
    /* At this point, we can look up the Protocols config if the client
535
     * has been authenticated, which may have been tweaked via mod_ifsession's
536
     * user/group/class-specific sections.
537
     */
538
0
    c = find_config(main_server->conf, CONF_PARAM, "Protocols", FALSE);
539
0
    if (c != NULL) {
540
0
      array_header *protocols;
541
0
      char **elts;
542
0
      const char *protocol;
543
544
0
      protocols = c->argv[0];
545
0
      elts = protocols->elts;
546
547
0
      protocol = pr_session_get_protocol(PR_SESS_PROTO_FL_LOGOUT);
548
549
      /* We only want to check for 'ftp' in the configured Protocols list
550
       * if a) a RFC2228 mechanism (e.g. SSL or GSS) is not in use, and
551
       *    b) an SSH protocol is not in use.
552
       */
553
0
      if (session.rfc2228_mech == NULL &&
554
0
          strcmp(protocol, "SSH2") != 0) {
555
0
        register unsigned int i;
556
0
        int allow_ftp = FALSE;
557
558
0
        for (i = 0; i < protocols->nelts; i++) {
559
0
          char *proto;
560
561
0
          proto = elts[i];
562
0
          if (proto != NULL) {
563
0
            if (strcasecmp(proto, "ftp") == 0) {
564
0
              allow_ftp = TRUE;
565
0
              break;
566
0
            }
567
0
          }
568
0
        }
569
570
0
        if (allow_ftp == FALSE) {
571
0
          pr_log_debug(DEBUG0, "%s", "ftp protocol denied by Protocols config");
572
0
          pr_response_send(R_530, "%s", _("Login incorrect."));
573
0
          pr_session_disconnect(&auth_module, PR_SESS_DISCONNECT_CONFIG_ACL,
574
0
            "Denied by Protocols setting");
575
0
        }
576
0
      }
577
0
    }
578
0
  }
579
580
0
  user = pr_table_get(session.notes, "mod_auth.orig-user", NULL);
581
582
  /* Count up various quantities in the scoreboard, checking them against
583
   * the Max* limits to see if the session should be barred from going
584
   * any further.
585
   */
586
0
  auth_count_scoreboard(cmd, session.user);
587
588
  /* Check for dynamic configuration.  This check needs to be after the
589
   * setting of any possible anon_config, as that context may be allowed
590
   * or denied .ftpaccess-parsing separately from the containing server.
591
   */
592
0
  if (pr_fsio_stat(session.cwd, &st) != -1) {
593
0
    build_dyn_config(cmd->tmp_pool, session.cwd, &st, TRUE);
594
0
  }
595
596
0
  have_user_timeout = have_group_timeout = have_class_timeout =
597
0
    have_all_timeout = FALSE;
598
599
0
  c = find_config(TOPLEVEL_CONF, CONF_PARAM, "TimeoutSession", FALSE);
600
0
  while (c != NULL) {
601
0
    pr_signals_handle();
602
603
0
    if (c->argc == 3) {
604
0
      if (strcasecmp(c->argv[1], "user") == 0) {
605
0
        if (pr_expr_eval_user_or((char **) &c->argv[2]) == TRUE) {
606
607
0
          if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
608
609
            /* Set the context precedence. */
610
0
            ctxt_precedence = *((unsigned int *) c->argv[1]);
611
612
0
            TimeoutSession = *((int *) c->argv[0]);
613
614
0
            have_group_timeout = have_class_timeout = have_all_timeout = FALSE;
615
0
            have_user_timeout = TRUE;
616
0
          }
617
0
        }
618
619
0
      } else if (strcasecmp(c->argv[1], "group") == 0) {
620
0
        if (pr_expr_eval_group_and((char **) &c->argv[2]) == TRUE) {
621
622
0
          if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
623
624
            /* Set the context precedence. */
625
0
            ctxt_precedence = *((unsigned int *) c->argv[1]);
626
627
0
            TimeoutSession = *((int *) c->argv[0]);
628
629
0
            have_user_timeout = have_class_timeout = have_all_timeout = FALSE;
630
0
            have_group_timeout = TRUE;
631
0
          }
632
0
        }
633
634
0
      } else if (strcasecmp(c->argv[1], "class") == 0) {
635
0
        if (session.conn_class != NULL &&
636
0
            strcmp(session.conn_class->cls_name, c->argv[2]) == 0) {
637
638
0
          if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
639
640
            /* Set the context precedence. */
641
0
            ctxt_precedence = *((unsigned int *) c->argv[1]);
642
643
0
            TimeoutSession = *((int *) c->argv[0]);
644
645
0
            have_user_timeout = have_group_timeout = have_all_timeout = FALSE;
646
0
            have_class_timeout = TRUE;
647
0
          }
648
0
        }
649
0
      }
650
651
0
    } else {
652
0
      if (*((unsigned int *) c->argv[1]) > ctxt_precedence) {
653
654
        /* Set the context precedence. */
655
0
        ctxt_precedence = *((unsigned int *) c->argv[1]);
656
657
0
        TimeoutSession = *((int *) c->argv[0]);
658
659
0
        have_user_timeout = have_group_timeout = have_class_timeout = FALSE;
660
0
        have_all_timeout = TRUE;
661
0
      }
662
0
    }
663
664
0
    c = find_config_next(c, c->next, CONF_PARAM, "TimeoutSession", FALSE);
665
0
  }
666
667
  /* If configured, start a session timer.  The timer ID value for
668
   * session timers will not be #defined, as I think that is a bad approach.
669
   * A better mechanism would be to use the random timer ID generation, and
670
   * store the returned ID in order to later remove the timer.
671
   */
672
673
0
  if (have_user_timeout ||
674
0
      have_group_timeout ||
675
0
      have_class_timeout ||
676
0
      have_all_timeout) {
677
0
    pr_log_debug(DEBUG4, "setting TimeoutSession of %d seconds for current %s",
678
0
      TimeoutSession,
679
0
      have_user_timeout ? "user" : have_group_timeout ? "group" :
680
0
      have_class_timeout ? "class" : "all");
681
0
    pr_timer_add(TimeoutSession, PR_TIMER_SESSION, &auth_module,
682
0
      auth_session_timeout_cb, "TimeoutSession");
683
0
  }
684
685
  /* Handle a DisplayLogin file. */
686
0
  if (displaylogin_fh != NULL) {
687
0
    if (!(session.sf_flags & SF_ANON)) {
688
0
      if (pr_display_fh(displaylogin_fh, NULL, auth_pass_resp_code, 0) < 0) {
689
0
        pr_log_debug(DEBUG6, "unable to display DisplayLogin file '%s': %s",
690
0
          displaylogin_fh->fh_path, strerror(errno));
691
0
      }
692
693
0
      pr_fsio_close(displaylogin_fh);
694
0
      displaylogin_fh = NULL;
695
696
0
    } else {
697
      /* We're an <Anonymous> login, but there was a previous DisplayLogin
698
       * configured which was picked up earlier.  Close that filehandle,
699
       * and look for a new one.
700
       */
701
0
      char *displaylogin;
702
703
0
      pr_fsio_close(displaylogin_fh);
704
0
      displaylogin_fh = NULL;
705
706
0
      displaylogin = get_param_ptr(TOPLEVEL_CONF, "DisplayLogin", FALSE);
707
0
      if (displaylogin != NULL) {
708
0
        if (pr_display_file(displaylogin, NULL, auth_pass_resp_code, 0) < 0) {
709
0
          pr_log_debug(DEBUG6, "unable to display DisplayLogin file '%s': %s",
710
0
            displaylogin, strerror(errno));
711
0
        }
712
0
      }
713
0
    }
714
715
0
  } else {
716
0
    char *displaylogin;
717
718
0
    displaylogin = get_param_ptr(TOPLEVEL_CONF, "DisplayLogin", FALSE);
719
0
    if (displaylogin != NULL) {
720
0
      if (pr_display_file(displaylogin, NULL, auth_pass_resp_code, 0) < 0) {
721
0
        pr_log_debug(DEBUG6, "unable to display DisplayLogin file '%s': %s",
722
0
          displaylogin, strerror(errno));
723
0
      }
724
0
    }
725
0
  }
726
727
0
  grantmsg = get_param_ptr(TOPLEVEL_CONF, "AccessGrantMsg", FALSE);
728
0
  if (grantmsg == NULL) {
729
    /* Append the final greeting lines. */
730
0
    if (session.sf_flags & SF_ANON) {
731
0
      pr_response_add(auth_pass_resp_code, "%s",
732
0
        _("Anonymous access granted, restrictions apply"));
733
734
0
    } else {
735
0
      pr_response_add(auth_pass_resp_code, _("User %s logged in"), user);
736
0
    }
737
738
0
  } else {
739
     /* Handle any AccessGrantMsg directive. */
740
0
     grantmsg = sreplace(cmd->tmp_pool, grantmsg, "%u", user, NULL);
741
0
     pr_response_add(auth_pass_resp_code, "%s", grantmsg);
742
0
  }
743
744
0
  login_succeeded(cmd->tmp_pool, user);
745
746
  /* Should we give up root privs completely here? */
747
0
  c = find_config(main_server->conf, CONF_PARAM, "RootRevoke", FALSE);
748
0
  if (c != NULL) {
749
0
    root_revoke = *((int *) c->argv[0]);
750
751
0
    if (root_revoke == FALSE) {
752
0
      pr_log_debug(DEBUG8, "retaining root privileges per RootRevoke setting");
753
0
    }
754
755
0
  } else {
756
    /* Do a recursive look for any UserOwner directives; honoring that
757
     * configuration also requires root privs.
758
     */
759
0
    c = find_config(main_server->conf, CONF_PARAM, "UserOwner", TRUE);
760
0
    if (c != NULL) {
761
0
      pr_log_debug(DEBUG9, "retaining root privileges per UserOwner setting");
762
0
      root_revoke = FALSE;
763
0
    }
764
0
  }
765
766
0
  if (root_revoke) {
767
0
    pr_signals_block();
768
0
    PRIVS_ROOT
769
0
    PRIVS_REVOKE
770
0
    pr_signals_unblock();
771
772
    /* Disable future attempts at UID/GID manipulation. */
773
0
    session.disable_id_switching = TRUE;
774
775
0
    pr_log_debug(DEBUG2, "RootRevoke in effect, dropped root privs");
776
0
  }
777
778
0
  c = find_config(TOPLEVEL_CONF, CONF_PARAM, "AnonAllowRobots", FALSE);
779
0
  if (c != NULL) {
780
0
    auth_anon_allow_robots = *((int *) c->argv[0]);
781
0
  }
782
783
0
  return PR_DECLINED(cmd);
784
0
}
785
786
/* Determine any applicable chdirs. */
787
0
static const char *get_default_chdir(pool *p, xaset_t *conf) {
788
0
  config_rec *c;
789
0
  const char *dir = NULL;
790
791
0
  c = find_config(conf, CONF_PARAM, "DefaultChdir", FALSE);
792
0
  while (c != NULL) {
793
0
    int res;
794
795
0
    pr_signals_handle();
796
797
    /* Check the groups acl */
798
0
    if (c->argc < 2) {
799
0
      dir = c->argv[0];
800
0
      break;
801
0
    }
802
803
0
    res = pr_expr_eval_group_and(((char **) c->argv)+1);
804
0
    if (res) {
805
0
      dir = c->argv[0];
806
0
      break;
807
0
    }
808
809
0
    c = find_config_next(c, c->next, CONF_PARAM, "DefaultChdir", FALSE);
810
0
  }
811
812
  /* If the directory is relative, concatenate w/ session.cwd. */
813
0
  if (dir != NULL &&
814
0
      *dir != '/' &&
815
0
      *dir != '~') {
816
0
    dir = pdircat(p, session.cwd, dir, NULL);
817
0
  }
818
819
  /* Check for any expandable variables. */
820
0
  if (dir != NULL) {
821
0
    dir = path_subst_uservar(p, &dir);
822
0
  }
823
824
0
  return dir;
825
0
}
826
827
0
static int is_symlink_path(pool *p, const char *path, size_t pathlen) {
828
0
  int res, xerrno = 0;
829
0
  struct stat st;
830
0
  char *ptr;
831
832
0
  if (pathlen == 0) {
833
0
    return 0;
834
0
  }
835
836
0
  pr_fs_clear_cache2(path);
837
0
  res = pr_fsio_lstat(path, &st);
838
0
  xerrno = errno;
839
840
0
  if (res < 0) {
841
0
    pr_log_pri(PR_LOG_WARNING, "error: unable to check %s: %s", path,
842
0
      strerror(xerrno));
843
844
0
    errno = xerrno;
845
0
    return -1;
846
0
  }
847
848
0
  if (S_ISLNK(st.st_mode)) {
849
0
    errno = EPERM;
850
0
    return -1;
851
0
  }
852
853
  /* To handle the case where a component further up the path might be a
854
   * symlink (which lstat(2) will NOT handle), we walk the path backwards,
855
   * calling ourselves recursively.
856
   */
857
858
0
  ptr = strrchr(path, '/');
859
0
  if (ptr != NULL) {
860
0
    char *new_path;
861
0
    size_t new_pathlen;
862
863
0
    pr_signals_handle();
864
865
0
    new_pathlen = ptr - path;
866
867
    /* Make sure our pointer actually changed position. */
868
0
    if (new_pathlen == pathlen) {
869
0
      return 0;
870
0
    }
871
872
0
    new_path = pstrndup(p, path, new_pathlen);
873
874
0
    pr_log_debug(DEBUG10,
875
0
      "AllowChrootSymlink: path '%s' not a symlink, checking '%s'", path,
876
0
      new_path);
877
0
    res = is_symlink_path(p, new_path, new_pathlen);
878
0
    if (res < 0) {
879
0
      return -1;
880
0
    }
881
0
  }
882
883
0
  return 0;
884
0
}
885
886
/* Determine if the user (non-anon) needs a default root dir other than /. */
887
0
static int get_default_root(pool *p, int allow_symlinks, const char **root) {
888
0
  config_rec *c = NULL;
889
0
  const char *dir = NULL;
890
0
  int res;
891
892
0
  c = find_config(main_server->conf, CONF_PARAM, "DefaultRoot", FALSE);
893
0
  while (c != NULL) {
894
0
    pr_signals_handle();
895
896
    /* Check the groups acl */
897
0
    if (c->argc < 2) {
898
0
      dir = c->argv[0];
899
0
      break;
900
0
    }
901
902
0
    res = pr_expr_eval_group_and(((char **) c->argv)+1);
903
0
    if (res) {
904
0
      dir = c->argv[0];
905
0
      break;
906
0
    }
907
908
0
    c = find_config_next(c, c->next, CONF_PARAM, "DefaultRoot", FALSE);
909
0
  }
910
911
0
  if (dir != NULL) {
912
0
    const char *new_dir;
913
914
    /* Check for any expandable variables. */
915
0
    new_dir = path_subst_uservar(p, &dir);
916
0
    if (new_dir != NULL) {
917
0
      dir = new_dir;
918
0
    }
919
920
0
    if (strncmp(dir, "/", 2) == 0) {
921
0
      dir = NULL;
922
923
0
    } else {
924
0
      char *realdir;
925
0
      int xerrno = 0;
926
927
0
      if (allow_symlinks == FALSE) {
928
0
        char *path, target_path[PR_TUNABLE_PATH_MAX + 1];
929
0
        size_t pathlen;
930
931
        /* First, deal with any possible interpolation.  dir_realpath() will
932
         * do this for us, but dir_realpath() ALSO automatically follows
933
         * symlinks, which is what we do NOT want to do here.
934
         */
935
936
0
        path = pstrdup(p, dir);
937
0
        if (*path != '/') {
938
0
          if (*path == '~') {
939
0
            if (pr_fs_interpolate(dir, target_path,
940
0
                sizeof(target_path)-1) < 0) {
941
0
              return -1;
942
0
            }
943
944
0
            path = target_path;
945
0
          }
946
0
        }
947
948
        /* Note: lstat(2) is sensitive to the presence of a trailing slash on
949
         * the path, particularly in the case of a symlink to a directory.
950
         * Thus to get the correct test, we need to remove any trailing slash
951
         * that might be present.  Subtle.
952
         */
953
0
        pathlen = strlen(path);
954
0
        if (pathlen > 1 &&
955
0
            path[pathlen-1] == '/') {
956
0
          path[pathlen-1] = '\0';
957
0
        }
958
959
0
        PRIVS_USER
960
0
        res = is_symlink_path(p, path, pathlen);
961
0
        xerrno = errno;
962
0
        PRIVS_RELINQUISH
963
964
0
        if (res < 0) {
965
0
          if (xerrno == EPERM) {
966
0
            pr_log_pri(PR_LOG_WARNING, "error: DefaultRoot %s is a symlink "
967
0
              "(denied by AllowChrootSymlinks config)", path);
968
0
          }
969
970
0
          errno = EPERM;
971
0
          return -1;
972
0
        }
973
0
      }
974
975
      /* We need to be the final user here so that if the user has their home
976
       * directory with a mode the user proftpd is running (i.e. the User
977
       * directive) as can not traverse down, we can still have the default
978
       * root.
979
       */
980
981
0
      pr_fs_clear_cache2(dir);
982
983
0
      PRIVS_USER
984
0
      realdir = dir_realpath(p, dir);
985
0
      xerrno = errno;
986
0
      PRIVS_RELINQUISH
987
988
0
      if (realdir) {
989
0
        dir = realdir;
990
991
0
      } else {
992
        /* Try to provide a more informative message. */
993
0
        char interp_dir[PR_TUNABLE_PATH_MAX + 1];
994
995
0
        memset(interp_dir, '\0', sizeof(interp_dir));
996
0
        (void) pr_fs_interpolate(dir, interp_dir, sizeof(interp_dir)-1);
997
998
0
        pr_log_pri(PR_LOG_NOTICE,
999
0
          "notice: unable to use DefaultRoot '%s' [resolved to '%s']: %s",
1000
0
          dir, interp_dir, strerror(xerrno));
1001
1002
0
        errno = xerrno;
1003
0
      }
1004
0
    }
1005
0
  }
1006
1007
0
  *root = dir;
1008
0
  return 0;
1009
0
}
1010
1011
0
static struct passwd *passwd_dup(pool *p, struct passwd *pw) {
1012
0
  struct passwd *npw;
1013
1014
0
  npw = pcalloc(p, sizeof(struct passwd));
1015
1016
0
  npw->pw_name = pstrdup(p, pw->pw_name);
1017
0
  npw->pw_passwd = pstrdup(p, pw->pw_passwd);
1018
0
  npw->pw_uid = pw->pw_uid;
1019
0
  npw->pw_gid = pw->pw_gid;
1020
0
  npw->pw_gecos = pstrdup(p, pw->pw_gecos);
1021
0
  npw->pw_dir = pstrdup(p, pw->pw_dir);
1022
0
  npw->pw_shell = pstrdup(p, pw->pw_shell);
1023
1024
0
  return npw;
1025
0
}
1026
1027
0
static void ensure_open_passwd(pool *p) {
1028
  /* Make sure pass/group is open. */
1029
0
  pr_auth_setpwent(p);
1030
0
  pr_auth_setgrent(p);
1031
1032
  /* On some unices the following is necessary to ensure the files
1033
   * are open (BSDI 3.1)
1034
   */
1035
0
  pr_auth_getpwent(p);
1036
0
  pr_auth_getgrent(p);
1037
1038
  /* Per Debian bug report:
1039
   *   https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=717235
1040
   * we might want to do another set{pw,gr}ent(), to play better with
1041
   * some NSS modules.
1042
   */
1043
0
  pr_auth_setpwent(p);
1044
0
  pr_auth_setgrent(p);
1045
0
}
1046
1047
/* Next function (the biggie) handles all authentication, setting
1048
 * up chroot() jail, etc.
1049
 */
1050
0
static int setup_env(pool *p, cmd_rec *cmd, const char *user, char *pass) {
1051
0
  struct passwd *pw;
1052
0
  config_rec *c, *tmpc;
1053
0
  const char *defchdir = NULL, *defroot = NULL, *origuser, *sess_ttyname;
1054
0
  char *ourname = NULL, *anonname = NULL, *anongroup = NULL, *ugroup = NULL;
1055
0
  char *xferlog = NULL;
1056
0
  int aclp, i, res = 0, allow_chroot_symlinks = TRUE, showsymlinks;
1057
0
  unsigned char *wtmp_log = NULL, *anon_require_passwd = NULL;
1058
1059
  /********************* Authenticate the user here *********************/
1060
1061
0
  session.hide_password = TRUE;
1062
1063
0
  origuser = user;
1064
0
  c = pr_auth_get_anon_config(p, &user, &ourname, &anonname);
1065
0
  if (c != NULL) {
1066
0
    pr_trace_msg("auth", 13,
1067
0
      "found <Anonymous> config: login user = %s, config user = %s, "
1068
0
      "anon name = %s", user != NULL ? user : "(null)",
1069
0
      ourname != NULL ? ourname : "(null)",
1070
0
      anonname != NULL ? anonname : "(null)");
1071
0
    session.anon_config = c;
1072
0
  }
1073
1074
0
  if (user == NULL) {
1075
0
    pr_log_auth(PR_LOG_NOTICE, "USER %s: user is not a UserAlias from %s [%s] "
1076
0
      "to %s:%i", origuser, session.c->remote_name,
1077
0
      pr_netaddr_get_ipstr(session.c->remote_addr),
1078
0
      pr_netaddr_get_ipstr(session.c->local_addr), session.c->local_port);
1079
0
    goto auth_failure;
1080
0
  }
1081
1082
0
  pw = pr_auth_getpwnam(p, user);
1083
0
  if (pw == NULL &&
1084
0
      c != NULL &&
1085
0
      ourname != NULL) {
1086
    /* If the client is authenticating using an alias (e.g. "AuthAliasOnly on"),
1087
     * then we need to try checking using the real username, too (Bug#4255).
1088
     */
1089
0
    pr_trace_msg("auth", 16,
1090
0
      "no user entry found for <Anonymous> alias '%s', using '%s'", user,
1091
0
      ourname);
1092
0
    pw = pr_auth_getpwnam(p, ourname);
1093
0
  }
1094
1095
0
  if (pw == NULL) {
1096
0
    int auth_code = PR_AUTH_NOPWD;
1097
1098
0
    pr_log_auth(PR_LOG_NOTICE,
1099
0
      "USER %s: no such user found from %s [%s] to %s:%i",
1100
0
      user, session.c->remote_name,
1101
0
      pr_netaddr_get_ipstr(session.c->remote_addr),
1102
0
      pr_netaddr_get_ipstr(session.c->local_addr), session.c->local_port);
1103
0
    pr_event_generate("mod_auth.authentication-code", &auth_code);
1104
1105
0
    goto auth_failure;
1106
0
  }
1107
1108
  /* Security: other functions perform pw lookups, thus we need to make
1109
   * a local copy of the user just looked up.
1110
   */
1111
0
  pw = passwd_dup(p, pw);
1112
1113
0
  if (pw->pw_uid == PR_ROOT_UID) {
1114
0
    unsigned char *root_allow = NULL;
1115
1116
0
    pr_event_generate("mod_auth.root-login", NULL);
1117
1118
    /* If RootLogin is set to true, we allow this... even though we
1119
     * still log a warning. :)
1120
     */
1121
0
    if ((root_allow = get_param_ptr(c ? c->subset : main_server->conf,
1122
0
        "RootLogin", FALSE)) == NULL || *root_allow != TRUE) {
1123
0
      if (pass) {
1124
0
        pr_memscrub(pass, strlen(pass));
1125
0
      }
1126
1127
0
      pr_log_auth(PR_LOG_NOTICE, "SECURITY VIOLATION: Root login attempted");
1128
0
      return 0;
1129
0
    }
1130
0
  }
1131
1132
0
  session.user = pstrdup(p, pw->pw_name);
1133
0
  session.user_homedir = pstrdup(p, pw->pw_dir);
1134
0
  session.group = pstrdup(p, pr_auth_gid2name(p, pw->pw_gid));
1135
1136
  /* Set the login_uid and login_uid */
1137
0
  session.login_uid = pw->pw_uid;
1138
0
  session.login_gid = pw->pw_gid;
1139
1140
  /* Check for any expandable variables in session.cwd. */
1141
0
  pw->pw_dir = (char *) path_subst_uservar(p, (const char **) &pw->pw_dir);
1142
1143
  /* Before we check for supplemental groups, check to see if the locally
1144
   * resolved name of the user, returned via auth_getpwnam(), is different
1145
   * from the USER argument sent by the client.  The name can change, since
1146
   * auth modules can play all sorts of neat tricks on us.
1147
   *
1148
   * If the names differ, assume that any cached data in the session.gids
1149
   * and session.groups lists are stale, and clear them out.
1150
   */
1151
0
  if (strcmp(pw->pw_name, user) != 0) {
1152
0
    pr_trace_msg("auth", 10, "local user name '%s' differs from client-sent "
1153
0
      "user name '%s', clearing cached group data", pw->pw_name, user);
1154
0
    session.gids = NULL;
1155
0
    session.groups = NULL;
1156
0
  }
1157
1158
0
  if (session.gids == NULL &&
1159
0
      session.groups == NULL) {
1160
    /* Get the supplemental groups.  Note that we only look up the
1161
     * supplemental group credentials if we have not cached the group
1162
     * credentials before, in session.gids and session.groups.
1163
     *
1164
     * Those credentials may have already been retrieved, as part of the
1165
     * pr_auth_get_anon_config() call.
1166
     */
1167
0
     res = pr_auth_getgroups(p, pw->pw_name, &session.gids, &session.groups);
1168
0
     if (res < 1) {
1169
       /* If no supplemental groups are provided, default to using the process
1170
        * primary GID as the supplemental group.  This prevents access
1171
        * regressions as seen in Issue #1830.
1172
        */
1173
0
       pr_log_debug(DEBUG5, "no supplemental groups found for user '%s', "
1174
0
         "using primary group %s (GID %lu)", pw->pw_name, session.group,
1175
0
         (unsigned long) session.login_gid);
1176
1177
0
       session.gids = make_array(p, 2, sizeof(gid_t));
1178
0
       session.groups = make_array(p, 2, sizeof(char *));
1179
1180
0
       *((gid_t *) push_array(session.gids)) = session.login_gid;
1181
0
       *((char **) push_array(session.groups)) = pstrdup(p, session.group);
1182
0
     }
1183
0
  }
1184
1185
0
  tmpc = find_config(main_server->conf, CONF_PARAM, "AllowChrootSymlinks",
1186
0
    FALSE);
1187
0
  if (tmpc != NULL) {
1188
0
    allow_chroot_symlinks = *((int *) tmpc->argv[0]);
1189
0
  }
1190
1191
  /* If c != NULL from this point on, we have an anonymous login */
1192
0
  aclp = login_check_limits(main_server->conf, FALSE, TRUE, &i);
1193
1194
0
  if (c != NULL) {
1195
0
    anongroup = get_param_ptr(c->subset, "GroupName", FALSE);
1196
0
    if (anongroup == NULL) {
1197
0
      anongroup = get_param_ptr(main_server->conf, "GroupName",FALSE);
1198
0
    }
1199
1200
0
#ifdef PR_USE_REGEX
1201
    /* Check for configured AnonRejectPasswords regex here, and fail the login
1202
     * if the given password matches the regex.
1203
     */
1204
0
    tmpc = find_config(c->subset, CONF_PARAM, "AnonRejectPasswords", FALSE);
1205
0
    if (tmpc != NULL) {
1206
0
      int re_notmatch;
1207
0
      pr_regex_t *pw_regex;
1208
1209
0
      pw_regex = (pr_regex_t *) tmpc->argv[0];
1210
0
      re_notmatch = *((int *) tmpc->argv[1]);
1211
1212
0
      if (pw_regex != NULL &&
1213
0
          pass != NULL) {
1214
0
        int re_res;
1215
1216
0
        re_res = pr_regexp_exec(pw_regex, pass, 0, NULL, 0, 0, 0);
1217
0
        if (re_res == 0 ||
1218
0
            (re_res != 0 && re_notmatch == TRUE)) {
1219
0
          char errstr[200] = {'\0'};
1220
1221
0
          pr_regexp_error(re_res, pw_regex, errstr, sizeof(errstr));
1222
0
          pr_log_auth(PR_LOG_NOTICE,
1223
0
            "ANON %s: AnonRejectPasswords denies login", origuser);
1224
1225
0
          pr_event_generate("mod_auth.anon-reject-passwords", session.c);
1226
0
          goto auth_failure;
1227
0
        }
1228
0
      }
1229
0
    }
1230
0
#endif
1231
1232
0
    if (!login_check_limits(c->subset, FALSE, TRUE, &i) || (!aclp && !i) ){
1233
0
      pr_log_auth(PR_LOG_NOTICE, "ANON %s (Login failed): Limit access denies "
1234
0
        "login", origuser);
1235
0
      goto auth_failure;
1236
0
    }
1237
0
  }
1238
1239
0
  if (c == NULL &&
1240
0
      aclp == 0) {
1241
0
    pr_log_auth(PR_LOG_NOTICE,
1242
0
      "USER %s (Login failed): Limit access denies login", origuser);
1243
0
    goto auth_failure;
1244
0
  }
1245
1246
0
  if (c != NULL) {
1247
0
    anon_require_passwd = get_param_ptr(c->subset, "AnonRequirePassword",
1248
0
      FALSE);
1249
0
  }
1250
1251
0
  if (c == NULL ||
1252
0
      (anon_require_passwd != NULL &&
1253
0
       *anon_require_passwd == TRUE)) {
1254
0
    int auth_code;
1255
0
    const char *user_name = user;
1256
1257
0
    if (c != NULL &&
1258
0
        origuser != NULL &&
1259
0
        strcasecmp(user, origuser) != 0) {
1260
0
      unsigned char *auth_using_alias;
1261
1262
0
      auth_using_alias = get_param_ptr(c->subset, "AuthUsingAlias", FALSE);
1263
1264
      /* If 'AuthUsingAlias' set and we're logging in under an alias,
1265
       * then auth using that alias.
1266
       */
1267
0
      if (auth_using_alias &&
1268
0
          *auth_using_alias == TRUE) {
1269
0
        user_name = origuser;
1270
0
        pr_log_auth(PR_LOG_INFO,
1271
0
          "ANON AUTH: User %s, authenticating using alias %s", user,
1272
0
          user_name);
1273
0
      }
1274
0
    }
1275
1276
    /* It is possible for the user to have already been authenticated during
1277
     * the handling of the USER command, as by an RFC2228 mechanism.  If
1278
     * that had happened, we won't need to call do_auth() here.
1279
     */
1280
0
    if (!authenticated_without_pass) {
1281
0
      auth_code = do_auth(p, c ? c->subset : main_server->conf, user_name,
1282
0
        pass);
1283
1284
0
    } else {
1285
0
      auth_code = PR_AUTH_OK_NO_PASS;
1286
0
    }
1287
1288
0
    pr_event_generate("mod_auth.authentication-code", &auth_code);
1289
1290
0
    if (pass != NULL) {
1291
0
      pr_memscrub(pass, strlen(pass));
1292
0
    }
1293
1294
0
    if (session.auth_mech != NULL) {
1295
0
      pr_log_debug(DEBUG2, "user '%s' authenticated by %s", user,
1296
0
        session.auth_mech);
1297
0
    }
1298
1299
0
    switch (auth_code) {
1300
0
      case PR_AUTH_OK_NO_PASS:
1301
0
        auth_pass_resp_code = R_232;
1302
0
        break;
1303
1304
0
      case PR_AUTH_OK:
1305
0
        auth_pass_resp_code = R_230;
1306
0
        break;
1307
1308
0
      case PR_AUTH_NOPWD:
1309
0
        pr_log_auth(PR_LOG_NOTICE,
1310
0
          "USER %s (Login failed): No such user found", user);
1311
0
        goto auth_failure;
1312
1313
0
      case PR_AUTH_BADPWD:
1314
0
        pr_log_auth(PR_LOG_NOTICE,
1315
0
          "USER %s (Login failed): Incorrect password", origuser);
1316
0
        goto auth_failure;
1317
1318
0
      case PR_AUTH_AGEPWD:
1319
0
        pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): Password expired",
1320
0
          user);
1321
0
        goto auth_failure;
1322
1323
0
      case PR_AUTH_DISABLEDPWD:
1324
0
        pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): Account disabled",
1325
0
          user);
1326
0
        goto auth_failure;
1327
1328
0
      case PR_AUTH_CRED_INSUFFICIENT:
1329
0
        pr_log_auth(PR_LOG_NOTICE,
1330
0
          "USER %s (Login failed): Insufficient credentials", user);
1331
0
        goto auth_failure;
1332
1333
0
      case PR_AUTH_CRED_UNAVAIL:
1334
0
        pr_log_auth(PR_LOG_NOTICE,
1335
0
          "USER %s (Login failed): Unavailable credentials", user);
1336
0
        goto auth_failure;
1337
1338
0
      case PR_AUTH_CRED_ERROR:
1339
0
        pr_log_auth(PR_LOG_NOTICE,
1340
0
          "USER %s (Login failed): Failure setting credentials", user);
1341
0
        goto auth_failure;
1342
1343
0
      case PR_AUTH_INFO_UNAVAIL:
1344
0
        pr_log_auth(PR_LOG_NOTICE,
1345
0
          "USER %s (Login failed): Unavailable authentication service", user);
1346
0
        goto auth_failure;
1347
1348
0
      case PR_AUTH_MAX_ATTEMPTS_EXCEEDED:
1349
0
        pr_log_auth(PR_LOG_NOTICE,
1350
0
          "USER %s (Login failed): Max authentication service attempts reached",
1351
0
          user);
1352
0
        goto auth_failure;
1353
1354
0
      case PR_AUTH_INIT_ERROR:
1355
0
        pr_log_auth(PR_LOG_NOTICE,
1356
0
          "USER %s (Login failed): Failed initializing authentication service",
1357
0
          user);
1358
0
        goto auth_failure;
1359
1360
0
      case PR_AUTH_NEW_TOKEN_REQUIRED:
1361
0
        pr_log_auth(PR_LOG_NOTICE,
1362
0
          "USER %s (Login failed): New authentication token required", user);
1363
0
        goto auth_failure;
1364
1365
0
      default:
1366
0
        break;
1367
0
    };
1368
1369
    /* Catch the case where we forgot to handle a bad auth code above. */
1370
0
    if (auth_code < 0) {
1371
0
      goto auth_failure;
1372
0
    }
1373
1374
0
    if (pw->pw_uid == PR_ROOT_UID) {
1375
0
      pr_log_auth(PR_LOG_WARNING, "ROOT FTP login successful");
1376
0
    }
1377
1378
0
  } else if (c && (!anon_require_passwd || *anon_require_passwd == FALSE)) {
1379
0
    session.hide_password = FALSE;
1380
0
  }
1381
1382
0
  pr_auth_setgrent(p);
1383
1384
0
  res = pr_auth_is_valid_shell(c ? c->subset : main_server->conf,
1385
0
    pw->pw_shell);
1386
0
  if (res == FALSE) {
1387
0
    pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): Invalid shell: '%s'",
1388
0
      user, pw->pw_shell);
1389
0
    goto auth_failure;
1390
0
  }
1391
1392
0
  res = pr_auth_banned_by_ftpusers(c ? c->subset : main_server->conf,
1393
0
    pw->pw_name);
1394
0
  if (res == TRUE) {
1395
0
    pr_log_auth(PR_LOG_NOTICE, "USER %s (Login failed): User in "
1396
0
      PR_FTPUSERS_PATH, user);
1397
0
    goto auth_failure;
1398
0
  }
1399
1400
0
  if (c != NULL) {
1401
0
    struct group *grp = NULL;
1402
0
    unsigned char *add_userdir = NULL;
1403
0
    const char *u;
1404
0
    char *chroot_dir;
1405
0
    int auth_code = PR_AUTH_OK;
1406
1407
0
    u = pr_table_get(session.notes, "mod_auth.orig-user", NULL);
1408
0
    add_userdir = get_param_ptr(c->subset, "UserDirRoot", FALSE);
1409
1410
    /* If resolving an <Anonymous> user, make sure that user's groups
1411
     * are set properly for the check of the home directory path (which
1412
     * depend on those supplemental group memberships).  Additionally,
1413
     * temporarily switch to the new user's uid.
1414
     */
1415
1416
0
    pr_signals_block();
1417
1418
0
    PRIVS_ROOT
1419
0
    res = set_groups(p, pw->pw_gid, session.gids);
1420
0
    if (res < 0) {
1421
0
      if (errno != ENOSYS) {
1422
0
        pr_log_pri(PR_LOG_WARNING, "error: unable to set groups: %s",
1423
0
          strerror(errno));
1424
0
      }
1425
0
    }
1426
1427
0
#ifndef PR_DEVEL_COREDUMP
1428
# ifdef __hpux
1429
    if (setresuid(0, 0, 0) < 0) {
1430
      pr_log_pri(PR_LOG_ERR, "unable to setresuid(): %s", strerror(errno));
1431
    }
1432
1433
    if (setresgid(0, 0, 0) < 0) {
1434
      pr_log_pri(PR_LOG_ERR, "unable to setresgid(): %s", strerror(errno));
1435
    }
1436
# else
1437
0
    if (setuid(PR_ROOT_UID) < 0) {
1438
0
      pr_log_pri(PR_LOG_ERR, "unable to setuid(): %s", strerror(errno));
1439
0
    }
1440
1441
0
    if (setgid(PR_ROOT_GID) < 0) {
1442
0
      pr_log_pri(PR_LOG_ERR, "unable to setgid(): %s", strerror(errno));
1443
0
    }
1444
0
# endif /* __hpux */
1445
0
#endif /* PR_DEVEL_COREDUMP */
1446
1447
0
    PRIVS_SETUP(pw->pw_uid, pw->pw_gid)
1448
1449
0
    if ((add_userdir && *add_userdir == TRUE) &&
1450
0
        strcmp(u, user) != 0) {
1451
0
      chroot_dir = pdircat(p, c->name, u, NULL);
1452
1453
0
    } else {
1454
0
      chroot_dir = c->name;
1455
0
    }
1456
1457
0
    if (allow_chroot_symlinks == FALSE) {
1458
0
      char *chroot_path, target_path[PR_TUNABLE_PATH_MAX+1];
1459
0
      struct stat st;
1460
1461
0
      chroot_path = chroot_dir;
1462
0
      if (chroot_path[0] != '/') {
1463
0
        if (chroot_path[0] == '~') {
1464
0
          if (pr_fs_interpolate(chroot_path, target_path,
1465
0
              sizeof(target_path)-1) == 0) {
1466
0
            chroot_path = target_path;
1467
1468
0
          } else {
1469
0
            chroot_path = NULL;
1470
0
          }
1471
0
        }
1472
0
      }
1473
1474
0
      if (chroot_path != NULL) {
1475
0
        size_t chroot_pathlen;
1476
1477
        /* Note: lstat(2) is sensitive to the presence of a trailing slash on
1478
         * the path, particularly in the case of a symlink to a directory.
1479
         * Thus to get the correct test, we need to remove any trailing slash
1480
         * that might be present.  Subtle.
1481
         */
1482
0
        chroot_pathlen = strlen(chroot_path);
1483
0
        if (chroot_pathlen > 1 &&
1484
0
            chroot_path[chroot_pathlen-1] == '/') {
1485
0
          chroot_path[chroot_pathlen-1] = '\0';
1486
0
        }
1487
1488
0
        pr_fs_clear_cache2(chroot_path);
1489
0
        res = pr_fsio_lstat(chroot_path, &st);
1490
0
        if (res < 0) {
1491
0
          int xerrno = errno;
1492
1493
0
          pr_log_pri(PR_LOG_WARNING, "error: unable to check %s: %s",
1494
0
            chroot_path, strerror(xerrno));
1495
1496
0
          errno = xerrno;
1497
0
          chroot_path = NULL;
1498
1499
0
        } else {
1500
0
          if (S_ISLNK(st.st_mode)) {
1501
0
            pr_log_pri(PR_LOG_WARNING,
1502
0
              "error: <Anonymous %s> is a symlink (denied by "
1503
0
              "AllowChrootSymlinks config)", chroot_path);
1504
0
            errno = EPERM;
1505
0
            chroot_path = NULL;
1506
0
          }
1507
0
        }
1508
0
      }
1509
1510
0
      if (chroot_path != NULL) {
1511
0
        session.chroot_path = dir_realpath(p, chroot_dir);
1512
1513
0
      } else {
1514
0
        session.chroot_path = NULL;
1515
0
      }
1516
1517
0
      if (session.chroot_path == NULL) {
1518
0
        pr_log_debug(DEBUG8, "error resolving '%s': %s", chroot_dir,
1519
0
          strerror(errno));
1520
0
      }
1521
1522
0
    } else {
1523
0
      session.chroot_path = dir_realpath(p, chroot_dir);
1524
0
      if (session.chroot_path == NULL) {
1525
0
        pr_log_debug(DEBUG8, "error resolving '%s': %s", chroot_dir,
1526
0
          strerror(errno));
1527
0
      }
1528
0
    }
1529
1530
0
    if (session.chroot_path != NULL &&
1531
0
        pr_fsio_access(session.chroot_path, X_OK, session.uid,
1532
0
          session.gid, session.gids) != 0) {
1533
0
      session.chroot_path = NULL;
1534
1535
0
    } else {
1536
0
      session.chroot_path = pstrdup(session.pool, session.chroot_path);
1537
0
    }
1538
1539
0
    pr_event_generate("mod_auth.authentication-code", &auth_code);
1540
1541
    /* Return all privileges back to that of the daemon, for now. */
1542
0
    PRIVS_ROOT
1543
0
    res = set_groups(p, daemon_gid, daemon_gids);
1544
0
    if (res < 0) {
1545
0
      if (errno != ENOSYS) {
1546
0
        pr_log_pri(PR_LOG_ERR, "error: unable to set groups: %s",
1547
0
          strerror(errno));
1548
0
      }
1549
0
    }
1550
1551
0
#ifndef PR_DEVEL_COREDUMP
1552
# ifdef __hpux
1553
    if (setresuid(0, 0, 0) < 0) {
1554
      pr_log_pri(PR_LOG_ERR, "unable to setresuid(): %s", strerror(errno));
1555
    }
1556
1557
    if (setresgid(0, 0, 0) < 0) {
1558
      pr_log_pri(PR_LOG_ERR, "unable to setresgid(): %s", strerror(errno));
1559
    }
1560
# else
1561
0
    if (setuid(PR_ROOT_UID) < 0) {
1562
0
      pr_log_pri(PR_LOG_ERR, "unable to setuid(): %s", strerror(errno));
1563
0
    }
1564
1565
0
    if (setgid(PR_ROOT_GID) < 0) {
1566
0
      pr_log_pri(PR_LOG_ERR, "unable to setgid(): %s", strerror(errno));
1567
0
    }
1568
0
# endif /* __hpux */
1569
0
#endif /* PR_DEVEL_COREDUMP */
1570
1571
0
    PRIVS_SETUP(daemon_uid, daemon_gid)
1572
1573
0
    pr_signals_unblock();
1574
1575
    /* Sanity check, make sure we have daemon_uid and daemon_gid back */
1576
#ifdef HAVE_GETEUID
1577
    if (getegid() != daemon_gid ||
1578
        geteuid() != daemon_uid) {
1579
1580
      PRIVS_RELINQUISH
1581
1582
      pr_log_pri(PR_LOG_WARNING,
1583
        "switching IDs from user %s back to daemon uid/gid failed: %s",
1584
        session.user, strerror(errno));
1585
      pr_session_disconnect(&auth_module, PR_SESS_DISCONNECT_BY_APPLICATION,
1586
        NULL);
1587
    }
1588
#endif /* HAVE_GETEUID */
1589
1590
0
    if (anon_require_passwd &&
1591
0
        *anon_require_passwd == TRUE) {
1592
0
      session.anon_user = pstrdup(session.pool, origuser);
1593
1594
0
    } else {
1595
0
      session.anon_user = pstrdup(session.pool, pass);
1596
0
    }
1597
1598
0
    if (!session.chroot_path) {
1599
0
      pr_log_pri(PR_LOG_NOTICE, "%s: Directory %s is not accessible",
1600
0
        session.user, c->name);
1601
0
      pr_response_add_err(R_530, _("Unable to set anonymous privileges."));
1602
0
      goto auth_failure;
1603
0
    }
1604
1605
0
    sstrncpy(session.cwd, "/", sizeof(session.cwd));
1606
0
    xferlog = get_param_ptr(c->subset, "TransferLog", FALSE);
1607
1608
0
    if (anongroup) {
1609
0
      grp = pr_auth_getgrnam(p, anongroup);
1610
0
      if (grp) {
1611
0
        pw->pw_gid = grp->gr_gid;
1612
0
        session.group = pstrdup(p, grp->gr_name);
1613
0
      }
1614
0
    }
1615
1616
0
  } else {
1617
0
    struct group *grp;
1618
0
    char *homedir;
1619
1620
0
    if (ugroup) {
1621
0
      grp = pr_auth_getgrnam(p, ugroup);
1622
0
      if (grp) {
1623
0
        pw->pw_gid = grp->gr_gid;
1624
0
        session.group = pstrdup(p, grp->gr_name);
1625
0
      }
1626
0
    }
1627
1628
    /* Attempt to resolve any possible symlinks. */
1629
0
    PRIVS_USER
1630
0
    homedir = dir_realpath(p, pw->pw_dir);
1631
0
    PRIVS_RELINQUISH
1632
1633
0
    if (homedir != NULL) {
1634
0
      sstrncpy(session.cwd, homedir, sizeof(session.cwd));
1635
1636
0
    } else {
1637
0
      sstrncpy(session.cwd, pw->pw_dir, sizeof(session.cwd));
1638
0
    }
1639
0
  }
1640
1641
  /* Create the home directory, if need be. */
1642
1643
0
  if (!c && mkhome) {
1644
0
    if (create_home(p, session.cwd, origuser, pw->pw_uid, pw->pw_gid) < 0) {
1645
1646
      /* NOTE: should this cause the login to fail? */
1647
0
      goto auth_failure;
1648
0
    }
1649
0
  }
1650
1651
  /* Get default chdir (if any) */
1652
0
  defchdir = get_default_chdir(p, (c ? c->subset : main_server->conf));
1653
0
  if (defchdir != NULL) {
1654
0
    sstrncpy(session.cwd, defchdir, sizeof(session.cwd));
1655
0
  }
1656
1657
  /* Check limits again to make sure deny/allow directives still permit
1658
   * access.
1659
   */
1660
1661
0
  if (!login_check_limits((c ? c->subset : main_server->conf), FALSE, TRUE,
1662
0
      &i)) {
1663
0
    pr_log_auth(PR_LOG_NOTICE, "%s %s: Limit access denies login",
1664
0
      (c != NULL) ? "ANON" : C_USER, origuser);
1665
0
    goto auth_failure;
1666
0
  }
1667
1668
  /* Perform a directory fixup. */
1669
0
  resolve_deferred_dirs(main_server);
1670
0
  fixup_dirs(main_server, CF_DEFER);
1671
1672
  /* If running under an anonymous context, resolve all <Directory>
1673
   * blocks inside it.
1674
   */
1675
0
  if (c != NULL &&
1676
0
      c->subset != NULL) {
1677
0
    resolve_anonymous_dirs(c->subset);
1678
0
  }
1679
1680
  /* Write the login to wtmp.  This must be done here because we won't
1681
   * have access after we give up root.  This can result in falsified
1682
   * wtmp entries if an error kicks the user out before we get
1683
   * through with the login process.  Oh well.
1684
   */
1685
1686
0
  sess_ttyname = pr_session_get_ttyname(p);
1687
1688
  /* Perform wtmp logging only if not turned off in <Anonymous>
1689
   * or the current server
1690
   */
1691
0
  if (c != NULL) {
1692
0
    wtmp_log = get_param_ptr(c->subset, "WtmpLog", FALSE);
1693
0
  }
1694
1695
0
  if (wtmp_log == NULL) {
1696
0
    wtmp_log = get_param_ptr(main_server->conf, "WtmpLog", FALSE);
1697
0
  }
1698
1699
  /* As per Bug#3482, we need to disable WtmpLog for FreeBSD 9.0, as
1700
   * an interim measure.
1701
   *
1702
   * The issue is that some platforms update multiple files for a single
1703
   * pututxline(3) call; proftpd tries to update those files manually,
1704
   * do to chroots (after which a pututxline(3) call will fail).  A proper
1705
   * solution requires a separate process, running with the correct
1706
   * privileges, which would handle wtmp logging. The proftpd session
1707
   * processes would send messages to this logging daemon (via Unix domain
1708
   * socket, or FIFO, or TCP socket).
1709
   *
1710
   * Also note that this hack to disable WtmpLog may need to be extended
1711
   * to other platforms in the future.
1712
   */
1713
#if defined(HAVE_UTMPX_H) && \
1714
    defined(__FreeBSD_version) && __FreeBSD_version >= 900007
1715
  if (wtmp_log == NULL ||
1716
      *wtmp_log == TRUE) {
1717
    wtmp_log = pcalloc(p, sizeof(unsigned char));
1718
    *wtmp_log = FALSE;
1719
1720
    pr_log_debug(DEBUG5,
1721
      "WtpmLog automatically disabled; see Bug#3482 for details");
1722
  }
1723
#endif
1724
1725
0
  PRIVS_ROOT
1726
1727
0
  if (wtmp_log == NULL ||
1728
0
      *wtmp_log == TRUE) {
1729
0
    log_wtmp(sess_ttyname, session.user, session.c->remote_name,
1730
0
      session.c->remote_addr);
1731
0
    session.wtmp_log = TRUE;
1732
0
  }
1733
1734
#ifdef PR_USE_LASTLOG
1735
  if (lastlog) {
1736
    log_lastlog(pw->pw_uid, session.user, sess_ttyname, session.c->remote_addr);
1737
  }
1738
#endif /* PR_USE_LASTLOG */
1739
1740
  /* Open any TransferLogs */
1741
0
  if (xferlog == NULL) {
1742
0
    if (c != NULL) {
1743
0
      xferlog = get_param_ptr(c->subset, "TransferLog", FALSE);
1744
0
    }
1745
1746
0
    if (xferlog == NULL) {
1747
0
      xferlog = get_param_ptr(main_server->conf, "TransferLog", FALSE);
1748
0
    }
1749
1750
0
    if (xferlog == NULL) {
1751
0
      xferlog = PR_XFERLOG_PATH;
1752
0
    }
1753
0
  }
1754
1755
0
  if (strcasecmp(xferlog, "NONE") == 0) {
1756
0
    xferlog_open(NULL);
1757
1758
0
  } else {
1759
0
    xferlog_open(xferlog);
1760
0
  }
1761
1762
0
  res = set_groups(p, pw->pw_gid, session.gids);
1763
0
  if (res < 0) {
1764
0
    if (errno != ENOSYS) {
1765
0
      pr_log_pri(PR_LOG_ERR, "error: unable to set groups: %s",
1766
0
        strerror(errno));
1767
0
    }
1768
0
  }
1769
1770
0
  PRIVS_RELINQUISH
1771
1772
  /* Now check to see if the user has an applicable DefaultRoot */
1773
0
  if (c == NULL) {
1774
0
    if (get_default_root(session.pool, allow_chroot_symlinks, &defroot) < 0) {
1775
0
      pr_log_pri(PR_LOG_NOTICE,
1776
0
        "error: unable to determine DefaultRoot directory");
1777
0
      pr_response_send(R_530, _("Login incorrect."));
1778
0
      pr_session_end(0);
1779
0
    }
1780
1781
0
    ensure_open_passwd(p);
1782
1783
0
    if (defroot != NULL) {
1784
0
      if (pr_auth_chroot(defroot) == -1) {
1785
0
        pr_log_pri(PR_LOG_NOTICE, "error: unable to set DefaultRoot directory");
1786
0
        pr_response_send(R_530, _("Login incorrect."));
1787
0
        pr_session_end(0);
1788
0
      }
1789
1790
      /* Re-calc the new cwd based on this root dir.  If not applicable
1791
       * place the user in / (of defroot)
1792
       */
1793
1794
0
      if (strncmp(session.cwd, defroot, strlen(defroot)) == 0) {
1795
0
        char *newcwd = &session.cwd[strlen(defroot)];
1796
1797
0
        if (*newcwd == '/') {
1798
0
          newcwd++;
1799
0
        }
1800
0
        session.cwd[0] = '/';
1801
0
        sstrncpy(&session.cwd[1], newcwd, sizeof(session.cwd));
1802
0
      }
1803
0
    }
1804
0
  }
1805
1806
0
  if (c != NULL) {
1807
0
    ensure_open_passwd(p);
1808
0
  }
1809
1810
0
  if (c != NULL &&
1811
0
      pr_auth_chroot(session.chroot_path) == -1) {
1812
0
    pr_log_pri(PR_LOG_NOTICE, "error: unable to set anonymous privileges");
1813
0
    pr_response_send(R_530, _("Login incorrect."));
1814
0
    pr_session_end(0);
1815
0
  }
1816
1817
  /* new in 1.1.x, I gave in and we don't give up root permanently..
1818
   * sigh.
1819
   */
1820
1821
0
  PRIVS_ROOT
1822
1823
0
#ifndef PR_DEVEL_COREDUMP
1824
# ifdef __hpux
1825
    if (setresuid(0, 0, 0) < 0) {
1826
      pr_log_pri(PR_LOG_ERR, "unable to setresuid(): %s", strerror(errno));
1827
    }
1828
1829
    if (setresgid(0, 0, 0) < 0) {
1830
      pr_log_pri(PR_LOG_ERR, "unable to setresgid(): %s", strerror(errno));
1831
    }
1832
# else
1833
0
    if (setuid(PR_ROOT_UID) < 0) {
1834
0
      pr_log_pri(PR_LOG_ERR, "unable to setuid(): %s", strerror(errno));
1835
0
    }
1836
1837
0
    if (setgid(PR_ROOT_GID) < 0) {
1838
0
      pr_log_pri(PR_LOG_ERR, "unable to setgid(): %s", strerror(errno));
1839
0
    }
1840
0
# endif /* __hpux */
1841
0
#endif /* PR_DEVEL_COREDUMP */
1842
1843
0
  PRIVS_SETUP(pw->pw_uid, pw->pw_gid)
1844
1845
#ifdef HAVE_GETEUID
1846
  if (getegid() != pw->pw_gid ||
1847
     geteuid() != pw->pw_uid) {
1848
1849
    PRIVS_RELINQUISH
1850
    pr_log_pri(PR_LOG_ERR, "error: %s setregid() or setreuid(): %s",
1851
      session.user, strerror(errno));
1852
    pr_response_send(R_530, _("Login incorrect."));
1853
    pr_session_end(0);
1854
  }
1855
#endif
1856
1857
  /* If the home directory is NULL or "", reject the login. */
1858
0
  if (pw->pw_dir == NULL ||
1859
0
      strncmp(pw->pw_dir, "", 1) == 0) {
1860
0
    pr_log_pri(PR_LOG_WARNING, "error: user %s home directory is NULL or \"\"",
1861
0
      session.user);
1862
0
    pr_response_send(R_530, _("Login incorrect."));
1863
0
    pr_session_end(0);
1864
0
  }
1865
1866
0
  {
1867
0
    unsigned char *show_symlinks = get_param_ptr(
1868
0
      c ? c->subset : main_server->conf, "ShowSymlinks", FALSE);
1869
1870
0
    if (show_symlinks == NULL ||
1871
0
        *show_symlinks == TRUE) {
1872
0
      showsymlinks = TRUE;
1873
1874
0
    } else {
1875
0
      showsymlinks = FALSE;
1876
0
    }
1877
0
  }
1878
1879
  /* chdir to the proper directory, do this even if anonymous
1880
   * to make sure we aren't outside our chrooted space.
1881
   */
1882
1883
  /* Attempt to change to the correct directory -- use session.cwd first.
1884
   * This will contain the DefaultChdir directory, if configured...
1885
   */
1886
0
  if (pr_fsio_chdir_canon(session.cwd, !showsymlinks) == -1) {
1887
1888
    /* if we've got DefaultRoot or anonymous login, ignore this error
1889
     * and chdir to /
1890
     */
1891
1892
0
    if (session.chroot_path != NULL || defroot) {
1893
1894
0
      pr_log_debug(DEBUG2, "unable to chdir to %s (%s), defaulting to chroot "
1895
0
        "directory %s", session.cwd, strerror(errno),
1896
0
        (session.chroot_path ? session.chroot_path : defroot));
1897
1898
0
      if (pr_fsio_chdir_canon("/", !showsymlinks) == -1) {
1899
0
        pr_log_pri(PR_LOG_NOTICE, "%s chdir(\"/\") failed: %s", session.user,
1900
0
          strerror(errno));
1901
0
        pr_response_send(R_530, _("Login incorrect."));
1902
0
        pr_session_end(0);
1903
0
      }
1904
1905
0
    } else if (defchdir) {
1906
1907
      /* If we've got defchdir, failure is ok as well, simply switch to
1908
       * user's homedir.
1909
       */
1910
0
      pr_log_debug(DEBUG2, "unable to chdir to %s (%s), defaulting to home "
1911
0
        "directory %s", session.cwd, strerror(errno), pw->pw_dir);
1912
1913
0
      if (pr_fsio_chdir_canon(pw->pw_dir, !showsymlinks) == -1) {
1914
0
        pr_log_pri(PR_LOG_NOTICE, "%s chdir(\"%s\") failed: %s", session.user,
1915
0
          session.cwd, strerror(errno));
1916
0
        pr_response_send(R_530, _("Login incorrect."));
1917
0
        pr_session_end(0);
1918
0
      }
1919
1920
0
    } else {
1921
1922
      /* Unable to switch to user's real home directory, which is not
1923
       * allowed.
1924
       */
1925
0
      pr_log_pri(PR_LOG_NOTICE, "%s chdir(\"%s\") failed: %s", session.user,
1926
0
        session.cwd, strerror(errno));
1927
0
      pr_response_send(R_530, _("Login incorrect."));
1928
0
      pr_session_end(0);
1929
0
    }
1930
0
  }
1931
1932
0
  sstrncpy(session.cwd, pr_fs_getcwd(), sizeof(session.cwd));
1933
0
  sstrncpy(session.vwd, pr_fs_getvwd(), sizeof(session.vwd));
1934
1935
  /* Make sure directory config pointers are set correctly */
1936
0
  dir_check_full(p, cmd, G_NONE, session.cwd, NULL);
1937
1938
0
  if (c) {
1939
0
    if (!session.hide_password) {
1940
0
      session.proc_prefix = pstrcat(session.pool, session.c->remote_name,
1941
0
        ": anonymous/", pass, NULL);
1942
1943
0
    } else {
1944
0
      session.proc_prefix = pstrcat(session.pool, session.c->remote_name,
1945
0
        ": anonymous", NULL);
1946
0
    }
1947
1948
0
    session.sf_flags = SF_ANON;
1949
1950
0
  } else {
1951
0
    session.proc_prefix = pstrdup(session.pool, session.c->remote_name);
1952
0
    session.sf_flags = 0;
1953
0
  }
1954
1955
  /* While closing the pointer to the password database would avoid any
1956
   * potential attempt to hijack this information, it is unfortunately needed
1957
   * in a chroot()ed environment.  Otherwise, mappings from UIDs to names,
1958
   * among other things, would fail.
1959
   */
1960
  /* pr_auth_endpwent(p); */
1961
1962
  /* Authentication complete, user logged in, now kill the login
1963
   * timer.
1964
   */
1965
1966
  /* Update the scoreboard entry */
1967
0
  pr_scoreboard_entry_update(session.pid,
1968
0
    PR_SCORE_USER, session.user,
1969
0
    PR_SCORE_CWD, session.cwd,
1970
0
    NULL);
1971
1972
0
  pr_session_set_idle();
1973
1974
0
  pr_timer_remove(PR_TIMER_LOGIN, &auth_module);
1975
1976
  /* These copies are made from the session.pool, instead of the more
1977
   * volatile pool used originally, in order that the copied data maintain
1978
   * its integrity for the lifetime of the session.
1979
   */
1980
0
  session.user = pstrdup(session.pool, session.user);
1981
1982
0
  if (session.user_homedir != NULL) {
1983
0
    session.user_homedir = pstrdup(session.pool, session.user_homedir);
1984
0
  }
1985
1986
0
  if (session.group != NULL) {
1987
0
    session.group = pstrdup(session.pool, session.group);
1988
0
  }
1989
1990
0
  if (session.gids != NULL) {
1991
0
    session.gids = copy_array(session.pool, session.gids);
1992
0
  }
1993
1994
  /* session.groups is an array of strings, so we must copy the string data
1995
   * as well as the pointers.
1996
   */
1997
0
  session.groups = copy_array_str(session.pool, session.groups);
1998
1999
  /* Resolve any deferred-resolution paths in the FS layer */
2000
0
  pr_resolve_fs_map();
2001
2002
0
  return 1;
2003
2004
0
auth_failure:
2005
0
  if (pass != NULL) {
2006
0
    pr_memscrub(pass, strlen(pass));
2007
0
  }
2008
0
  session.user = session.user_homedir = session.group = NULL;
2009
0
  session.gids = session.groups = NULL;
2010
0
  session.wtmp_log = FALSE;
2011
0
  return 0;
2012
0
}
2013
2014
/* This function counts the number of connected users. It only fills in the
2015
 * Class-based counters and an estimate for the number of clients. The primary
2016
 * purpose is to make it so that the %N/%y escapes work in a DisplayConnect
2017
 * greeting.  A secondary purpose is to enforce any configured
2018
 * MaxConnectionsPerHost limit.
2019
 */
2020
0
static int auth_scan_scoreboard(void) {
2021
0
  char *key;
2022
0
  void *v;
2023
0
  config_rec *c = NULL;
2024
0
  pr_scoreboard_entry_t *score = NULL;
2025
0
  unsigned int cur = 0, ccur = 0, hcur = 0;
2026
0
  char curr_server_addr[80] = {'\0'};
2027
0
  const char *client_addr = pr_netaddr_get_ipstr(session.c->remote_addr);
2028
2029
0
  pr_snprintf(curr_server_addr, sizeof(curr_server_addr), "%s:%d",
2030
0
    pr_netaddr_get_ipstr(session.c->local_addr), main_server->ServerPort);
2031
0
  curr_server_addr[sizeof(curr_server_addr)-1] = '\0';
2032
2033
  /* Determine how many users are currently connected */
2034
0
  if (pr_rewind_scoreboard() < 0) {
2035
0
    pr_log_pri(PR_LOG_NOTICE, "error rewinding scoreboard: %s",
2036
0
      strerror(errno));
2037
0
  }
2038
2039
0
  while ((score = pr_scoreboard_entry_read()) != NULL) {
2040
0
    pr_signals_handle();
2041
2042
    /* Make sure it matches our current server */
2043
0
    if (strcmp(score->sce_server_addr, curr_server_addr) == 0) {
2044
0
      cur++;
2045
2046
0
      if (strcmp(score->sce_client_addr, client_addr) == 0) {
2047
0
        hcur++;
2048
0
      }
2049
2050
      /* Only count up authenticated clients, as per the documentation. */
2051
0
      if (strcmp(score->sce_user, "(none)") == 0) {
2052
0
        continue;
2053
0
      }
2054
2055
      /* Note: the class member of the scoreboard entry will never be
2056
       * NULL.  At most, it may be the empty string.
2057
       */
2058
0
      if (session.conn_class != NULL &&
2059
0
          strcasecmp(score->sce_class, session.conn_class->cls_name) == 0) {
2060
0
        ccur++;
2061
0
      }
2062
0
    }
2063
0
  }
2064
0
  pr_restore_scoreboard();
2065
2066
0
  key = "client-count";
2067
0
  (void) pr_table_remove(session.notes, key, NULL);
2068
0
  v = palloc(session.pool, sizeof(unsigned int));
2069
0
  *((unsigned int *) v) = cur;
2070
2071
0
  if (pr_table_add(session.notes, key, v, sizeof(unsigned int)) < 0) {
2072
0
    if (errno != EEXIST) {
2073
0
      pr_log_pri(PR_LOG_WARNING,
2074
0
        "warning: error stashing '%s': %s", key, strerror(errno));
2075
0
    }
2076
0
  }
2077
2078
0
  if (session.conn_class != NULL) {
2079
0
    key = "class-client-count";
2080
0
    (void) pr_table_remove(session.notes, key, NULL);
2081
0
    v = palloc(session.pool, sizeof(unsigned int));
2082
0
    *((unsigned int *) v) = ccur;
2083
2084
0
    if (pr_table_add(session.notes, key, v, sizeof(unsigned int)) < 0) {
2085
0
      if (errno != EEXIST) {
2086
0
        pr_log_pri(PR_LOG_WARNING,
2087
0
          "warning: error stashing '%s': %s", key, strerror(errno));
2088
0
      }
2089
0
    }
2090
0
  }
2091
2092
  /* Lookup any configured MaxConnectionsPerHost. */
2093
0
  c = find_config(main_server->conf, CONF_PARAM, "MaxConnectionsPerHost",
2094
0
    FALSE);
2095
2096
0
  if (c != NULL) {
2097
0
    unsigned int *max = c->argv[0];
2098
2099
0
    if (*max &&
2100
0
        hcur > *max) {
2101
2102
0
      char maxstr[20];
2103
0
      char *msg = "Sorry, the maximum number of connections (%m) for your host "
2104
0
        "are already connected.";
2105
2106
0
      pr_event_generate("mod_auth.max-connections-per-host", session.c);
2107
2108
0
      if (c->argc == 2) {
2109
0
        msg = c->argv[1];
2110
0
      }
2111
2112
0
      memset(maxstr, '\0', sizeof(maxstr));
2113
0
      pr_snprintf(maxstr, sizeof(maxstr), "%u", *max);
2114
0
      maxstr[sizeof(maxstr)-1] = '\0';
2115
2116
0
      pr_response_send(R_530, "%s", sreplace(session.pool, msg,
2117
0
        "%m", maxstr, NULL));
2118
2119
0
      pr_log_auth(PR_LOG_NOTICE,
2120
0
        "Connection refused (MaxConnectionsPerHost %u)", *max);
2121
0
      pr_session_disconnect(&auth_module, PR_SESS_DISCONNECT_CONFIG_ACL,
2122
0
        "Denied by MaxConnectionsPerHost");
2123
0
    }
2124
0
  }
2125
2126
0
  return 0;
2127
0
}
2128
2129
0
static int have_client_limits(cmd_rec *cmd) {
2130
0
  if (find_config(TOPLEVEL_CONF, CONF_PARAM, "MaxClientsPerClass", FALSE) != NULL) {
2131
0
    return TRUE;
2132
0
  }
2133
2134
0
  if (find_config(TOPLEVEL_CONF, CONF_PARAM, "MaxClientsPerHost", FALSE) != NULL) {
2135
0
    return TRUE;
2136
0
  }
2137
2138
0
  if (find_config(TOPLEVEL_CONF, CONF_PARAM, "MaxClientsPerUser", FALSE) != NULL) {
2139
0
    return TRUE;
2140
0
  }
2141
2142
0
  if (find_config(TOPLEVEL_CONF, CONF_PARAM, "MaxClients", FALSE) != NULL) {
2143
0
    return TRUE;
2144
0
  }
2145
2146
0
  if (find_config(TOPLEVEL_CONF, CONF_PARAM, "MaxHostsPerUser", FALSE) != NULL) {
2147
0
    return TRUE;
2148
0
  }
2149
2150
0
  return FALSE;
2151
0
}
2152
2153
0
static int auth_count_scoreboard(cmd_rec *cmd, const char *user) {
2154
0
  char *key;
2155
0
  void *v;
2156
0
  pr_scoreboard_entry_t *score = NULL;
2157
0
  long cur = 0, hcur = 0, ccur = 0, hostsperuser = 1, usersessions = 0;
2158
0
  config_rec *c = NULL, *maxc = NULL;
2159
2160
  /* First, check to see which Max* directives are configured.  If none
2161
   * are configured, then there is no need for us to needlessly scan the
2162
   * ScoreboardFile.
2163
   */
2164
0
  if (have_client_limits(cmd) == FALSE) {
2165
0
    return 0;
2166
0
  }
2167
2168
  /* Determine how many users are currently connected. */
2169
2170
  /* We use this call to get the possibly-changed user name. */
2171
0
  c = pr_auth_get_anon_config(cmd->tmp_pool, &user, NULL, NULL);
2172
2173
  /* Gather our statistics. */
2174
0
  if (user != NULL) {
2175
0
    char curr_server_addr[80] = {'\0'};
2176
2177
0
    pr_snprintf(curr_server_addr, sizeof(curr_server_addr), "%s:%d",
2178
0
      pr_netaddr_get_ipstr(session.c->local_addr), main_server->ServerPort);
2179
0
    curr_server_addr[sizeof(curr_server_addr)-1] = '\0';
2180
2181
0
    if (pr_rewind_scoreboard() < 0) {
2182
0
      pr_log_pri(PR_LOG_NOTICE, "error rewinding scoreboard: %s",
2183
0
        strerror(errno));
2184
0
    }
2185
2186
0
    while ((score = pr_scoreboard_entry_read()) != NULL) {
2187
0
      unsigned char same_host = FALSE;
2188
2189
0
      pr_signals_handle();
2190
2191
      /* Make sure it matches our current server. */
2192
0
      if (strcmp(score->sce_server_addr, curr_server_addr) == 0) {
2193
2194
0
        if ((c != NULL &&
2195
0
             c->config_type == CONF_ANON &&
2196
0
             strcmp(score->sce_user, user) == 0) ||
2197
0
            c == NULL) {
2198
2199
          /* Only count authenticated clients, as per the documentation. */
2200
0
          if (strcmp(score->sce_user, "(none)") == 0) {
2201
0
            continue;
2202
0
          }
2203
2204
0
          cur++;
2205
2206
          /* Count up sessions on a per-host basis. */
2207
2208
0
          if (strcmp(score->sce_client_addr,
2209
0
              pr_netaddr_get_ipstr(session.c->remote_addr)) == 0) {
2210
0
            same_host = TRUE;
2211
0
            hcur++;
2212
0
          }
2213
2214
          /* Take a per-user count of connections. */
2215
0
          if (strcmp(score->sce_user, user) == 0) {
2216
0
            usersessions++;
2217
2218
            /* Count up unique hosts. */
2219
0
            if (same_host == FALSE) {
2220
0
              hostsperuser++;
2221
0
            }
2222
0
          }
2223
0
        }
2224
2225
0
        if (session.conn_class != NULL &&
2226
0
            strcasecmp(score->sce_class, session.conn_class->cls_name) == 0) {
2227
0
          ccur++;
2228
0
        }
2229
0
      }
2230
0
    }
2231
0
    pr_restore_scoreboard();
2232
0
    PRIVS_RELINQUISH
2233
0
  }
2234
2235
0
  key = "client-count";
2236
0
  (void) pr_table_remove(session.notes, key, NULL);
2237
0
  v = palloc(session.pool, sizeof(unsigned int));
2238
0
  *((unsigned int *) v) = cur;
2239
2240
0
  if (pr_table_add(session.notes, key, v, sizeof(unsigned int)) < 0) {
2241
0
    if (errno != EEXIST) {
2242
0
      pr_log_pri(PR_LOG_WARNING,
2243
0
        "warning: error stashing '%s': %s", key, strerror(errno));
2244
0
    }
2245
0
  }
2246
2247
0
  if (session.conn_class != NULL) {
2248
0
    key = "class-client-count";
2249
0
    (void) pr_table_remove(session.notes, key, NULL);
2250
0
    v = palloc(session.pool, sizeof(unsigned int));
2251
0
    *((unsigned int *) v) = ccur;
2252
2253
0
    if (pr_table_add(session.notes, key, v, sizeof(unsigned int)) < 0) {
2254
0
      if (errno != EEXIST) {
2255
0
        pr_log_pri(PR_LOG_WARNING,
2256
0
          "warning: error stashing '%s': %s", key, strerror(errno));
2257
0
      }
2258
0
    }
2259
0
  }
2260
2261
  /* Try to determine what MaxClients/MaxHosts limits apply to this session
2262
   * (if any) and count through the runtime file to see if this limit would
2263
   * be exceeded.
2264
   */
2265
2266
0
  maxc = find_config(cmd->server->conf, CONF_PARAM, "MaxClientsPerClass",
2267
0
    FALSE);
2268
0
  while (session.conn_class != NULL && maxc) {
2269
0
    char *maxstr = "Sorry, the maximum number of clients (%m) from your class "
2270
0
      "are already connected.";
2271
0
    unsigned int *max = maxc->argv[1];
2272
2273
0
    if (strcmp(maxc->argv[0], session.conn_class->cls_name) != 0) {
2274
0
      maxc = find_config_next(maxc, maxc->next, CONF_PARAM,
2275
0
        "MaxClientsPerClass", FALSE);
2276
0
      continue;
2277
0
    }
2278
2279
0
    if (maxc->argc > 2) {
2280
0
      maxstr = maxc->argv[2];
2281
0
    }
2282
2283
0
    if (*max &&
2284
0
        ccur > *max) {
2285
0
      char maxn[20] = {'\0'};
2286
2287
0
      pr_event_generate("mod_auth.max-clients-per-class",
2288
0
        session.conn_class->cls_name);
2289
2290
0
      pr_snprintf(maxn, sizeof(maxn), "%u", *max);
2291
0
      pr_response_send(R_530, "%s", sreplace(cmd->tmp_pool, maxstr, "%m", maxn,
2292
0
        NULL));
2293
0
      (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
2294
2295
0
      pr_log_auth(PR_LOG_NOTICE,
2296
0
        "Connection refused (MaxClientsPerClass %s %u)",
2297
0
        session.conn_class->cls_name, *max);
2298
0
      pr_session_disconnect(&auth_module, PR_SESS_DISCONNECT_CONFIG_ACL,
2299
0
        "Denied by MaxClientsPerClass");
2300
0
    }
2301
2302
0
    break;
2303
0
  }
2304
2305
0
  maxc = find_config(TOPLEVEL_CONF, CONF_PARAM, "MaxClientsPerHost", FALSE);
2306
0
  if (maxc) {
2307
0
    char *maxstr = "Sorry, the maximum number of clients (%m) from your host "
2308
0
      "are already connected.";
2309
0
    unsigned int *max = maxc->argv[0];
2310
2311
0
    if (maxc->argc > 1) {
2312
0
      maxstr = maxc->argv[1];
2313
0
    }
2314
2315
0
    if (*max &&
2316
0
        hcur > *max) {
2317
0
      char maxn[20] = {'\0'};
2318
2319
0
      pr_event_generate("mod_auth.max-clients-per-host", session.c);
2320
2321
0
      pr_snprintf(maxn, sizeof(maxn), "%u", *max);
2322
0
      pr_response_send(R_530, "%s", sreplace(cmd->tmp_pool, maxstr, "%m", maxn,
2323
0
        NULL));
2324
0
      (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
2325
2326
0
      pr_log_auth(PR_LOG_NOTICE,
2327
0
        "Connection refused (MaxClientsPerHost %u)", *max);
2328
0
      pr_session_disconnect(&auth_module, PR_SESS_DISCONNECT_CONFIG_ACL,
2329
0
        "Denied by MaxClientsPerHost");
2330
0
    }
2331
0
  }
2332
2333
  /* Check for any configured MaxClientsPerUser. */
2334
0
  maxc = find_config(TOPLEVEL_CONF, CONF_PARAM, "MaxClientsPerUser", FALSE);
2335
0
  if (maxc) {
2336
0
    char *maxstr = "Sorry, the maximum number of clients (%m) for this user "
2337
0
      "are already connected.";
2338
0
    unsigned int *max = maxc->argv[0];
2339
2340
0
    if (maxc->argc > 1) {
2341
0
      maxstr = maxc->argv[1];
2342
0
    }
2343
2344
0
    if (*max &&
2345
0
        usersessions > *max) {
2346
0
      char maxn[20] = {'\0'};
2347
2348
0
      pr_event_generate("mod_auth.max-clients-per-user", user);
2349
2350
0
      pr_snprintf(maxn, sizeof(maxn), "%u", *max);
2351
0
      pr_response_send(R_530, "%s", sreplace(cmd->tmp_pool, maxstr, "%m", maxn,
2352
0
        NULL));
2353
0
      (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
2354
2355
0
      pr_log_auth(PR_LOG_NOTICE,
2356
0
        "Connection refused (MaxClientsPerUser %u)", *max);
2357
0
      pr_session_disconnect(&auth_module, PR_SESS_DISCONNECT_CONFIG_ACL,
2358
0
        "Denied by MaxClientsPerUser");
2359
0
    }
2360
0
  }
2361
2362
0
  maxc = find_config(TOPLEVEL_CONF, CONF_PARAM, "MaxClients", FALSE);
2363
0
  if (maxc) {
2364
0
    char *maxstr = "Sorry, the maximum number of allowed clients (%m) are "
2365
0
      "already connected.";
2366
0
    unsigned int *max = maxc->argv[0];
2367
2368
0
    if (maxc->argc > 1) {
2369
0
      maxstr = maxc->argv[1];
2370
0
    }
2371
2372
0
    if (*max &&
2373
0
        cur > *max) {
2374
0
      char maxn[20] = {'\0'};
2375
2376
0
      pr_event_generate("mod_auth.max-clients", NULL);
2377
2378
0
      pr_snprintf(maxn, sizeof(maxn), "%u", *max);
2379
0
      pr_response_send(R_530, "%s", sreplace(cmd->tmp_pool, maxstr, "%m", maxn,
2380
0
        NULL));
2381
0
      (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
2382
2383
0
      pr_log_auth(PR_LOG_NOTICE, "Connection refused (MaxClients %u)", *max);
2384
0
      pr_session_disconnect(&auth_module, PR_SESS_DISCONNECT_CONFIG_ACL,
2385
0
        "Denied by MaxClients");
2386
0
    }
2387
0
  }
2388
2389
0
  maxc = find_config(TOPLEVEL_CONF, CONF_PARAM, "MaxHostsPerUser", FALSE);
2390
0
  if (maxc) {
2391
0
    char *maxstr = "Sorry, the maximum number of hosts (%m) for this user are "
2392
0
      "already connected.";
2393
0
    unsigned int *max = maxc->argv[0];
2394
2395
0
    if (maxc->argc > 1) {
2396
0
      maxstr = maxc->argv[1];
2397
0
    }
2398
2399
0
    if (*max && hostsperuser > *max) {
2400
0
      char maxn[20] = {'\0'};
2401
2402
0
      pr_event_generate("mod_auth.max-hosts-per-user", user);
2403
2404
0
      pr_snprintf(maxn, sizeof(maxn), "%u", *max);
2405
0
      pr_response_send(R_530, "%s", sreplace(cmd->tmp_pool, maxstr, "%m", maxn,
2406
0
        NULL));
2407
0
      (void) pr_cmd_dispatch_phase(cmd, LOG_CMD_ERR, 0);
2408
2409
0
      pr_log_auth(PR_LOG_NOTICE, "Connection refused (MaxHostsPerUser %u)",
2410
0
        *max);
2411
0
      pr_session_disconnect(&auth_module, PR_SESS_DISCONNECT_CONFIG_ACL,
2412
0
        "Denied by MaxHostsPerUser");
2413
0
    }
2414
0
  }
2415
2416
0
  return 0;
2417
0
}
2418
2419
0
MODRET auth_pre_user(cmd_rec *cmd) {
2420
2421
0
  if (saw_first_user_cmd == FALSE) {
2422
0
    if (pr_trace_get_level(timing_channel)) {
2423
0
      unsigned long elapsed_ms;
2424
0
      uint64_t finish_ms;
2425
2426
0
      pr_gettimeofday_millis(&finish_ms);
2427
0
      elapsed_ms = (unsigned long) (finish_ms - session.connect_time_ms);
2428
2429
0
      pr_trace_msg(timing_channel, 4, "Time before first USER: %lu ms",
2430
0
        elapsed_ms);
2431
0
    }
2432
0
    saw_first_user_cmd = TRUE;
2433
0
  }
2434
2435
0
  if (logged_in) {
2436
0
    return PR_DECLINED(cmd);
2437
0
  }
2438
2439
  /* Close the passwd and group databases, because libc won't let us see new
2440
   * entries to these files without this (only in PersistentPasswd mode).
2441
   */
2442
0
  pr_auth_endpwent(cmd->tmp_pool);
2443
0
  pr_auth_endgrent(cmd->tmp_pool);
2444
2445
  /* Check for a user name that exceeds PR_TUNABLE_LOGIN_MAX. */
2446
0
  if (strlen(cmd->arg) > PR_TUNABLE_LOGIN_MAX) {
2447
0
    pr_log_pri(PR_LOG_NOTICE, "USER %s (Login failed): "
2448
0
      "maximum USER length exceeded", cmd->arg);
2449
0
    pr_response_add_err(R_501, _("Login incorrect."));
2450
2451
0
    pr_cmd_set_errno(cmd, EPERM);
2452
0
    errno = EPERM;
2453
0
    return PR_ERROR(cmd);
2454
0
  }
2455
2456
0
  return PR_DECLINED(cmd);
2457
0
}
2458
2459
0
MODRET auth_user(cmd_rec *cmd) {
2460
0
  int nopass = FALSE;
2461
0
  config_rec *c;
2462
0
  const char *user, *origuser;
2463
0
  unsigned char *anon_require_passwd = NULL;
2464
2465
0
  if (cmd->argc < 2) {
2466
0
    return PR_ERROR_MSG(cmd, R_500, _("USER: command requires a parameter"));
2467
0
  }
2468
2469
0
  if (logged_in) {
2470
    /* If the client has already authenticated, BUT the given USER command
2471
     * here is for the exact same user name, then allow the command to
2472
     * succeed (Bug#4217).
2473
     */
2474
0
    origuser = pr_table_get(session.notes, "mod_auth.orig-user", NULL);
2475
0
    if (origuser != NULL &&
2476
0
        strcmp(origuser, cmd->arg) == 0) {
2477
0
      pr_response_add(R_230, _("User %s logged in"), origuser);
2478
0
      return PR_HANDLED(cmd);
2479
0
    }
2480
2481
0
    pr_response_add_err(R_501, "%s", _("Reauthentication not supported"));
2482
0
    return PR_ERROR(cmd);
2483
0
  }
2484
2485
0
  user = cmd->arg;
2486
2487
0
  (void) pr_table_remove(session.notes, "mod_auth.orig-user", NULL);
2488
0
  (void) pr_table_remove(session.notes, "mod_auth.anon-passwd", NULL);
2489
2490
0
  if (pr_table_add_dup(session.notes, "mod_auth.orig-user", user, 0) < 0) {
2491
0
    pr_log_debug(DEBUG3, "error stashing 'mod_auth.orig-user' in "
2492
0
      "session.notes: %s", strerror(errno));
2493
0
  }
2494
2495
0
  origuser = user;
2496
0
  c = pr_auth_get_anon_config(cmd->tmp_pool, &user, NULL, NULL);
2497
2498
0
  if (c != NULL) {
2499
0
    anon_require_passwd = get_param_ptr(c->subset, "AnonRequirePassword",
2500
0
      FALSE);
2501
0
  }
2502
2503
0
  if (c && user && (!anon_require_passwd || *anon_require_passwd == FALSE)) {
2504
0
    nopass = TRUE;
2505
0
  }
2506
2507
0
  session.gids = NULL;
2508
0
  session.groups = NULL;
2509
0
  session.user = NULL;
2510
0
  session.user_homedir = NULL;
2511
0
  session.group = NULL;
2512
2513
0
  if (nopass) {
2514
0
    pr_response_add(R_331, _("Anonymous login ok, send your complete email "
2515
0
      "address as your password"));
2516
2517
0
  } else if (pr_auth_requires_pass(cmd->tmp_pool, user) == FALSE) {
2518
    /* Check to see if a password from the client is required.  In the
2519
     * vast majority of cases, a password will be required.
2520
     */
2521
2522
    /* Act as if we received a PASS command from the client. */
2523
0
    cmd_rec *fakecmd = pr_cmd_alloc(cmd->pool, 2, NULL);
2524
2525
    /* We use pstrdup() here, rather than assigning C_PASS directly, since
2526
     * code elsewhere will attempt to modify this buffer, and C_PASS is
2527
     * a string literal.
2528
     */
2529
0
    fakecmd->argv[0] = pstrdup(fakecmd->pool, C_PASS);
2530
0
    fakecmd->argv[1] = NULL;
2531
0
    fakecmd->arg = NULL;
2532
2533
0
    c = add_config_param_set(&cmd->server->conf, "authenticated", 1, NULL);
2534
0
    c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
2535
0
    *((unsigned char *) c->argv[0]) = TRUE;
2536
2537
0
    authenticated_without_pass = TRUE;
2538
0
    pr_log_auth(PR_LOG_NOTICE, "USER %s: Authenticated without password", user);
2539
2540
0
    pr_cmd_dispatch(fakecmd);
2541
2542
0
  } else {
2543
0
    pr_response_add(R_331, _("Password required for %s"),
2544
0
      (char *) cmd->argv[1]);
2545
0
  }
2546
2547
0
  return PR_HANDLED(cmd);
2548
0
}
2549
2550
/* Close the passwd and group databases, similar to auth_pre_user(). */
2551
0
MODRET auth_pre_pass(cmd_rec *cmd) {
2552
0
  const char *user;
2553
0
  char *displaylogin;
2554
2555
0
  pr_auth_endpwent(cmd->tmp_pool);
2556
0
  pr_auth_endgrent(cmd->tmp_pool);
2557
2558
  /* Handle cases where PASS might be sent before USER. */
2559
0
  user = pr_table_get(session.notes, "mod_auth.orig-user", NULL);
2560
0
  if (user != NULL) {
2561
0
    config_rec *c;
2562
2563
0
    c = find_config(main_server->conf, CONF_PARAM, "AllowEmptyPasswords",
2564
0
      FALSE);
2565
0
    if (c == NULL) {
2566
0
      const char *anon_user;
2567
0
      config_rec *anon_config;
2568
2569
      /* Since we have not authenticated yet, we cannot use the TOPLEVEL_CONF
2570
       * macro to handle <Anonymous> sections.  So we do it manually.
2571
       */
2572
0
      anon_user = pstrdup(cmd->tmp_pool, user);
2573
0
      anon_config = pr_auth_get_anon_config(cmd->tmp_pool, &anon_user, NULL,
2574
0
        NULL);
2575
0
      if (anon_config != NULL) {
2576
0
        c = find_config(anon_config->subset, CONF_PARAM, "AllowEmptyPasswords",
2577
0
          FALSE);
2578
0
      }
2579
0
    }
2580
2581
0
    if (c != NULL) {
2582
0
      int allow_empty_passwords;
2583
2584
0
      allow_empty_passwords = *((int *) c->argv[0]);
2585
0
      if (allow_empty_passwords == FALSE) {
2586
0
        const char *proto;
2587
0
        int reject_empty_passwd = FALSE, using_ssh2 = FALSE;
2588
0
        size_t passwd_len = 0;
2589
2590
0
        proto = pr_session_get_protocol(0);
2591
0
        if (strcmp(proto, "ssh2") == 0) {
2592
0
          using_ssh2 = TRUE;
2593
0
        }
2594
2595
0
        if (cmd->argc > 1) {
2596
0
          if (cmd->arg != NULL) {
2597
0
            passwd_len = strlen(cmd->arg);
2598
0
          }
2599
0
        }
2600
2601
0
        if (passwd_len == 0) {
2602
0
          reject_empty_passwd = TRUE;
2603
2604
          /* Make sure to NOT enforce 'AllowEmptyPasswords off' if e.g.
2605
           * the AllowDotLogin TLSOption is in effect, or if the protocol is
2606
           * SSH2 (for mod_sftp uses "fake" PASS commands for the SSH login
2607
           * protocol).
2608
           */
2609
2610
0
          if (session.auth_mech != NULL &&
2611
0
              strcmp(session.auth_mech, "mod_tls.c") == 0) {
2612
0
            pr_log_debug(DEBUG9, "%s", "'AllowEmptyPasswords off' in effect, "
2613
0
              "BUT client authenticated via the AllowDotLogin TLSOption");
2614
0
            reject_empty_passwd = FALSE;
2615
0
          }
2616
2617
0
          if (using_ssh2 == TRUE) {
2618
0
            reject_empty_passwd = FALSE;
2619
0
          }
2620
0
        }
2621
2622
0
        if (reject_empty_passwd == TRUE) {
2623
0
          pr_log_debug(DEBUG5,
2624
0
            "Refusing empty password from user '%s' (AllowEmptyPasswords "
2625
0
            "false)", user);
2626
0
          pr_log_auth(PR_LOG_NOTICE,
2627
0
            "Refusing empty password from user '%s'", user);
2628
2629
0
          pr_event_generate("mod_auth.empty-password", user);
2630
0
          pr_response_add_err(R_501, _("Login incorrect."));
2631
0
          return PR_ERROR(cmd);
2632
0
        }
2633
0
      }
2634
0
    }
2635
0
  }
2636
2637
  /* Look for a DisplayLogin file which has an absolute path.  If we find one,
2638
   * open a filehandle, such that that file can be displayed even if the
2639
   * session is chrooted.  DisplayLogin files with relative paths will be
2640
   * handled after chroot, preserving the old behavior.
2641
   */
2642
2643
0
  displaylogin = get_param_ptr(TOPLEVEL_CONF, "DisplayLogin", FALSE);
2644
0
  if (displaylogin &&
2645
0
      *displaylogin == '/') {
2646
0
    struct stat st;
2647
2648
0
    displaylogin_fh = pr_fsio_open(displaylogin, O_RDONLY);
2649
0
    if (displaylogin_fh == NULL) {
2650
0
      pr_log_debug(DEBUG6, "unable to open DisplayLogin file '%s': %s",
2651
0
        displaylogin, strerror(errno));
2652
2653
0
    } else {
2654
0
      if (pr_fsio_fstat(displaylogin_fh, &st) < 0) {
2655
0
        pr_log_debug(DEBUG6, "unable to stat DisplayLogin file '%s': %s",
2656
0
          displaylogin, strerror(errno));
2657
0
        pr_fsio_close(displaylogin_fh);
2658
0
        displaylogin_fh = NULL;
2659
2660
0
      } else {
2661
0
        if (S_ISDIR(st.st_mode)) {
2662
0
          errno = EISDIR;
2663
0
          pr_log_debug(DEBUG6, "unable to use DisplayLogin file '%s': %s",
2664
0
            displaylogin, strerror(errno));
2665
0
          pr_fsio_close(displaylogin_fh);
2666
0
          displaylogin_fh = NULL;
2667
0
        }
2668
0
      }
2669
0
    }
2670
0
  }
2671
2672
0
  return PR_DECLINED(cmd);
2673
0
}
2674
2675
0
MODRET auth_pass(cmd_rec *cmd) {
2676
0
  const char *user = NULL;
2677
0
  int res = 0;
2678
2679
0
  if (logged_in) {
2680
0
    return PR_ERROR_MSG(cmd, R_503, _("You are already logged in"));
2681
0
  }
2682
2683
0
  user = pr_table_get(session.notes, "mod_auth.orig-user", NULL);
2684
0
  if (user == NULL) {
2685
0
    (void) pr_table_remove(session.notes, "mod_auth.orig-user", NULL);
2686
0
    (void) pr_table_remove(session.notes, "mod_auth.anon-passwd", NULL);
2687
2688
0
    return PR_ERROR_MSG(cmd, R_503, _("Login with USER first"));
2689
0
  }
2690
2691
  /* Clear any potentially cached directory config */
2692
0
  session.anon_config = NULL;
2693
0
  session.dir_config = NULL;
2694
2695
0
  res = setup_env(cmd->tmp_pool, cmd, user, cmd->arg);
2696
0
  if (res == 1) {
2697
0
    config_rec *c = NULL;
2698
2699
0
    c = add_config_param_set(&cmd->server->conf, "authenticated", 1, NULL);
2700
0
    c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
2701
0
    *((unsigned char *) c->argv[0]) = TRUE;
2702
2703
0
    set_auth_check(NULL);
2704
2705
0
    (void) pr_table_remove(session.notes, "mod_auth.anon-passwd", NULL);
2706
2707
0
    if (session.sf_flags & SF_ANON) {
2708
0
      if (pr_table_add_dup(session.notes, "mod_auth.anon-passwd",
2709
0
          pr_fs_decode_path(cmd->server->pool, cmd->arg), 0) < 0) {
2710
0
        pr_log_debug(DEBUG3,
2711
0
          "error stashing anonymous password in session.notes: %s",
2712
0
          strerror(errno));
2713
0
      }
2714
0
    }
2715
2716
0
    logged_in = TRUE;
2717
2718
0
    if (pr_trace_get_level(timing_channel)) {
2719
0
      unsigned long elapsed_ms;
2720
0
      uint64_t finish_ms;
2721
2722
0
      pr_gettimeofday_millis(&finish_ms);
2723
0
      elapsed_ms = (unsigned long) (finish_ms - session.connect_time_ms);
2724
2725
0
      pr_trace_msg(timing_channel, 4,
2726
0
        "Time before successful login (via '%s'): %lu ms", session.auth_mech,
2727
0
        elapsed_ms);
2728
0
    }
2729
2730
0
    return PR_HANDLED(cmd);
2731
0
  }
2732
2733
0
  (void) pr_table_remove(session.notes, "mod_auth.anon-passwd", NULL);
2734
2735
0
  if (res == 0) {
2736
0
    unsigned int max_logins, *max = NULL;
2737
0
    const char *denymsg = NULL;
2738
2739
    /* check for AccessDenyMsg */
2740
0
    if ((denymsg = get_param_ptr((session.anon_config ?
2741
0
        session.anon_config->subset : cmd->server->conf),
2742
0
        "AccessDenyMsg", FALSE)) != NULL) {
2743
2744
0
      if (strstr(denymsg, "%u") != NULL) {
2745
0
        denymsg = sreplace(cmd->tmp_pool, denymsg, "%u", user, NULL);
2746
0
      }
2747
0
    }
2748
2749
0
    max = get_param_ptr(main_server->conf, "MaxLoginAttempts", FALSE);
2750
0
    if (max != NULL) {
2751
0
      max_logins = *max;
2752
2753
0
    } else {
2754
0
      max_logins = 3;
2755
0
    }
2756
2757
0
    if (max_logins > 0 &&
2758
0
        ((unsigned int) ++auth_tries) >= max_logins) {
2759
0
      if (denymsg) {
2760
0
        pr_response_send(R_530, "%s", denymsg);
2761
2762
0
      } else {
2763
0
        pr_response_send(R_530, "%s", _("Login incorrect."));
2764
0
      }
2765
2766
0
      pr_log_auth(PR_LOG_NOTICE,
2767
0
        "Maximum login attempts (%u) exceeded, connection refused", max_logins);
2768
2769
      /* Generate an event about this limit being exceeded. */
2770
0
      pr_event_generate("mod_auth.max-login-attempts", session.c);
2771
2772
      /* Set auth_tries to -1 so that the session is disconnected after
2773
       * POST_CMD_ERR and LOG_CMD_ERR events are processed.
2774
       */
2775
0
      auth_tries = -1;
2776
0
    }
2777
2778
0
    return PR_ERROR_MSG(cmd, R_530, denymsg ? denymsg : _("Login incorrect."));
2779
0
  }
2780
2781
0
  return PR_HANDLED(cmd);
2782
0
}
2783
2784
0
MODRET auth_acct(cmd_rec *cmd) {
2785
0
  pr_response_add_err(R_502, _("ACCT command not implemented"));
2786
0
  return PR_ERROR(cmd);
2787
0
}
2788
2789
0
MODRET auth_rein(cmd_rec *cmd) {
2790
0
  pr_response_add_err(R_502, _("REIN command not implemented"));
2791
0
  return PR_ERROR(cmd);
2792
0
}
2793
2794
/* FSIO callbacks for providing a fake robots.txt file, for the AnonAllowRobots
2795
 * functionality.
2796
 */
2797
2798
0
#define AUTH_ROBOTS_TXT     "User-agent: *\nDisallow: /\n"
2799
0
#define AUTH_ROBOTS_TXT_FD    6742
2800
2801
0
static int robots_fsio_stat(pr_fs_t *fs, const char *path, struct stat *st) {
2802
0
  st->st_dev = (dev_t) 0;
2803
0
  st->st_ino = (ino_t) 0;
2804
0
  st->st_mode = (S_IFREG|S_IRUSR|S_IRGRP|S_IROTH);
2805
0
  st->st_nlink = 0;
2806
0
  st->st_uid = (uid_t) 0;
2807
0
  st->st_gid = (gid_t) 0;
2808
0
  st->st_atime = 0;
2809
0
  st->st_mtime = 0;
2810
0
  st->st_ctime = 0;
2811
0
  st->st_size = strlen(AUTH_ROBOTS_TXT);
2812
0
  st->st_blksize = 1024;
2813
0
  st->st_blocks = 1;
2814
2815
0
  return 0;
2816
0
}
2817
2818
0
static int robots_fsio_fstat(pr_fh_t *fh, int fd, struct stat *st) {
2819
0
  if (fd != AUTH_ROBOTS_TXT_FD) {
2820
0
    errno = EINVAL;
2821
0
    return -1;
2822
0
  }
2823
2824
0
  return robots_fsio_stat(NULL, NULL, st);
2825
0
}
2826
2827
0
static int robots_fsio_lstat(pr_fs_t *fs, const char *path, struct stat *st) {
2828
0
  return robots_fsio_stat(fs, path, st);
2829
0
}
2830
2831
0
static int robots_fsio_unlink(pr_fs_t *fs, const char *path) {
2832
0
  return 0;
2833
0
}
2834
2835
0
static int robots_fsio_open(pr_fh_t *fh, const char *path, int flags) {
2836
0
  if (flags != O_RDONLY) {
2837
0
    errno = EINVAL;
2838
0
    return -1;
2839
0
  }
2840
2841
0
  return AUTH_ROBOTS_TXT_FD;
2842
0
}
2843
2844
0
static int robots_fsio_close(pr_fh_t *fh, int fd) {
2845
0
  if (fd != AUTH_ROBOTS_TXT_FD) {
2846
0
    errno = EINVAL;
2847
0
    return -1;
2848
0
  }
2849
2850
0
  return 0;
2851
0
}
2852
2853
0
static int robots_fsio_read(pr_fh_t *fh, int fd, char *buf, size_t bufsz) {
2854
0
  size_t robots_len;
2855
2856
0
  if (fd != AUTH_ROBOTS_TXT_FD) {
2857
0
    errno = EINVAL;
2858
0
    return -1;
2859
0
  }
2860
2861
0
  robots_len = strlen(AUTH_ROBOTS_TXT);
2862
2863
0
  if (bufsz < robots_len) {
2864
0
    errno = EINVAL;
2865
0
    return -1;
2866
0
  }
2867
2868
0
  memcpy(buf, AUTH_ROBOTS_TXT, robots_len);
2869
0
  return (int) robots_len;
2870
0
}
2871
2872
static int robots_fsio_write(pr_fh_t *fh, int fd, const char *buf,
2873
0
    size_t bufsz) {
2874
0
  if (fd != AUTH_ROBOTS_TXT_FD) {
2875
0
    errno = EINVAL;
2876
0
    return -1;
2877
0
  }
2878
2879
0
  return (int) bufsz;
2880
0
}
2881
2882
static int robots_fsio_access(pr_fs_t *fs, const char *path, int mode,
2883
0
    uid_t uid, gid_t gid, array_header *suppl_gids) {
2884
0
  if (mode != R_OK) {
2885
0
    errno = EACCES;
2886
0
    return -1;
2887
0
  }
2888
2889
0
  return 0;
2890
0
}
2891
2892
static int robots_fsio_faccess(pr_fh_t *fh, int mode, uid_t uid, gid_t gid,
2893
0
    array_header *suppl_gids) {
2894
2895
0
  if (fh->fh_fd != AUTH_ROBOTS_TXT_FD) {
2896
0
    errno = EINVAL;
2897
0
    return -1;
2898
0
  }
2899
2900
0
  if (mode != R_OK) {
2901
0
    errno = EACCES;
2902
0
    return -1;
2903
0
  }
2904
2905
0
  return 0;
2906
0
}
2907
2908
0
MODRET auth_pre_retr(cmd_rec *cmd) {
2909
0
  const char *path;
2910
0
  pr_fs_t *curr_fs = NULL;
2911
0
  struct stat st;
2912
2913
  /* Only apply this for <Anonymous> logins. */
2914
0
  if (session.anon_config == NULL) {
2915
0
    return PR_DECLINED(cmd);
2916
0
  }
2917
2918
0
  if (auth_anon_allow_robots == TRUE) {
2919
0
    return PR_DECLINED(cmd);
2920
0
  }
2921
2922
0
  auth_anon_allow_robots_enabled = FALSE;
2923
2924
0
  path = dir_canonical_path(cmd->tmp_pool, cmd->arg);
2925
0
  if (strcasecmp(path, "/robots.txt") != 0) {
2926
0
    return PR_DECLINED(cmd);
2927
0
  }
2928
2929
  /* If a previous REST command, with a non-zero value, has been sent, then
2930
   * do nothing.  Ugh.
2931
   */
2932
0
  if (session.restart_pos > 0) {
2933
0
    pr_log_debug(DEBUG10, "'AnonAllowRobots off' in effect, but cannot "
2934
0
      "support resumed download (REST %" PR_LU " previously sent by client)",
2935
0
      (pr_off_t) session.restart_pos);
2936
0
    return PR_DECLINED(cmd);
2937
0
  }
2938
2939
0
  pr_fs_clear_cache2(path);
2940
0
  if (pr_fsio_lstat(path, &st) == 0) {
2941
    /* There's an existing REAL "robots.txt" file on disk; use that, and
2942
     * preserve the principle of least surprise.
2943
     */
2944
0
    pr_log_debug(DEBUG10, "'AnonAllowRobots off' in effect, but have "
2945
0
      "real 'robots.txt' file on disk; using that");
2946
0
    return PR_DECLINED(cmd);
2947
0
  }
2948
2949
0
  curr_fs = pr_get_fs(path, NULL);
2950
0
  if (curr_fs != NULL) {
2951
0
    pr_fs_t *robots_fs;
2952
2953
0
    robots_fs = pr_register_fs(cmd->pool, "robots", path);
2954
0
    if (robots_fs == NULL) {
2955
0
      pr_log_debug(DEBUG8, "'AnonAllowRobots off' in effect, but failed to "
2956
0
        "register FS: %s", strerror(errno));
2957
0
      return PR_DECLINED(cmd);
2958
0
    }
2959
2960
    /* Use enough of our own custom FSIO callbacks to be able to provide
2961
     * a fake "robots.txt" file.
2962
     */
2963
0
    robots_fs->stat = robots_fsio_stat;
2964
0
    robots_fs->fstat = robots_fsio_fstat;
2965
0
    robots_fs->lstat = robots_fsio_lstat;
2966
0
    robots_fs->unlink = robots_fsio_unlink;
2967
0
    robots_fs->open = robots_fsio_open;
2968
0
    robots_fs->close = robots_fsio_close;
2969
0
    robots_fs->read = robots_fsio_read;
2970
0
    robots_fs->write = robots_fsio_write;
2971
0
    robots_fs->access = robots_fsio_access;
2972
0
    robots_fs->faccess = robots_fsio_faccess;
2973
2974
    /* For all other FSIO callbacks, use the underlying FS. */
2975
0
    robots_fs->rename = curr_fs->rename;
2976
0
    robots_fs->lseek = curr_fs->lseek;
2977
0
    robots_fs->link = curr_fs->link;
2978
0
    robots_fs->readlink = curr_fs->readlink;
2979
0
    robots_fs->symlink = curr_fs->symlink;
2980
0
    robots_fs->ftruncate = curr_fs->ftruncate;
2981
0
    robots_fs->truncate = curr_fs->truncate;
2982
0
    robots_fs->chmod = curr_fs->chmod;
2983
0
    robots_fs->fchmod = curr_fs->fchmod;
2984
0
    robots_fs->chown = curr_fs->chown;
2985
0
    robots_fs->fchown = curr_fs->fchown;
2986
0
    robots_fs->lchown = curr_fs->lchown;
2987
0
    robots_fs->utimes = curr_fs->utimes;
2988
0
    robots_fs->futimes = curr_fs->futimes;
2989
0
    robots_fs->fsync = curr_fs->fsync;
2990
2991
0
    pr_fs_clear_cache2(path);
2992
0
    auth_anon_allow_robots_enabled = TRUE;
2993
0
  }
2994
2995
0
  return PR_DECLINED(cmd);
2996
0
}
2997
2998
0
MODRET auth_post_retr(cmd_rec *cmd) {
2999
0
  if (auth_anon_allow_robots == TRUE) {
3000
0
    return PR_DECLINED(cmd);
3001
0
  }
3002
3003
0
  if (auth_anon_allow_robots_enabled == TRUE) {
3004
0
    int res;
3005
3006
0
    res = pr_unregister_fs("/robots.txt");
3007
0
    if (res < 0) {
3008
0
      pr_log_debug(DEBUG9, "error removing 'robots' FS for '/robots.txt': %s",
3009
0
        strerror(errno));
3010
0
    }
3011
3012
0
    auth_anon_allow_robots_enabled = FALSE;
3013
0
  }
3014
3015
0
  return PR_DECLINED(cmd);
3016
0
}
3017
3018
/* Configuration handlers
3019
 */
3020
3021
0
MODRET set_accessdenymsg(cmd_rec *cmd) {
3022
0
  config_rec *c = NULL;
3023
3024
0
  CHECK_ARGS(cmd, 1);
3025
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
3026
3027
0
  c = add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
3028
0
  c->flags |= CF_MERGEDOWN;
3029
3030
0
  return PR_HANDLED(cmd);
3031
0
}
3032
3033
0
MODRET set_accessgrantmsg(cmd_rec *cmd) {
3034
0
  config_rec *c = NULL;
3035
3036
0
  CHECK_ARGS(cmd, 1);
3037
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
3038
3039
0
  c = add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
3040
0
  c->flags |= CF_MERGEDOWN;
3041
3042
0
  return PR_HANDLED(cmd);
3043
0
}
3044
3045
/* usage: AllowChrootSymlinks on|off */
3046
0
MODRET set_allowchrootsymlinks(cmd_rec *cmd) {
3047
0
  int allow_chroot_symlinks = -1;
3048
0
  config_rec *c = NULL;
3049
3050
0
  CHECK_ARGS(cmd, 1);
3051
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3052
3053
0
  allow_chroot_symlinks = get_boolean(cmd, 1);
3054
0
  if (allow_chroot_symlinks == -1) {
3055
0
    CONF_ERROR(cmd, "expected Boolean parameter");
3056
0
  }
3057
3058
0
  c = add_config_param(cmd->argv[0], 1, NULL);
3059
0
  c->argv[0] = pcalloc(c->pool, sizeof(int));
3060
0
  *((int *) c->argv[0]) = allow_chroot_symlinks;
3061
3062
0
  return PR_HANDLED(cmd);
3063
0
}
3064
3065
/* usage: AllowEmptyPasswords on|off */
3066
0
MODRET set_allowemptypasswords(cmd_rec *cmd) {
3067
0
  int allow_empty_passwords = -1;
3068
0
  config_rec *c = NULL;
3069
3070
0
  CHECK_ARGS(cmd, 1);
3071
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
3072
3073
0
  allow_empty_passwords = get_boolean(cmd, 1);
3074
0
  if (allow_empty_passwords == -1) {
3075
0
    CONF_ERROR(cmd, "expected Boolean parameter");
3076
0
  }
3077
3078
0
  c = add_config_param(cmd->argv[0], 1, NULL);
3079
0
  c->argv[0] = pcalloc(c->pool, sizeof(int));
3080
0
  *((int *) c->argv[0]) = allow_empty_passwords;
3081
0
  c->flags |= CF_MERGEDOWN;
3082
3083
0
  return PR_HANDLED(cmd);
3084
0
}
3085
3086
/* usage: AnonAllowRobots on|off */
3087
0
MODRET set_anonallowrobots(cmd_rec *cmd) {
3088
0
  int allow_robots = -1;
3089
0
  config_rec *c;
3090
3091
0
  CHECK_ARGS(cmd, 1);
3092
0
  CHECK_CONF(cmd, CONF_ANON);
3093
3094
0
  allow_robots = get_boolean(cmd, 1);
3095
0
  if (allow_robots == -1) {
3096
0
    CONF_ERROR(cmd, "expected Boolean parameter");
3097
0
  }
3098
3099
0
  c = add_config_param(cmd->argv[0], 1, NULL);
3100
0
  c->argv[0] = palloc(c->pool, sizeof(int));
3101
0
  *((int *) c->argv[0]) = allow_robots;
3102
3103
0
  return PR_HANDLED(cmd);
3104
0
}
3105
3106
0
MODRET set_anonrequirepassword(cmd_rec *cmd) {
3107
0
  int anon_require_passwd = -1;
3108
0
  config_rec *c = NULL;
3109
3110
0
  CHECK_ARGS(cmd, 1);
3111
0
  CHECK_CONF(cmd, CONF_ANON);
3112
3113
0
  anon_require_passwd = get_boolean(cmd, 1);
3114
0
  if (anon_require_passwd == -1) {
3115
0
    CONF_ERROR(cmd, "expected Boolean parameter");
3116
0
  }
3117
3118
0
  c = add_config_param(cmd->argv[0], 1, NULL);
3119
0
  c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
3120
0
  *((unsigned char *) c->argv[0]) = anon_require_passwd;
3121
3122
0
  return PR_HANDLED(cmd);
3123
0
}
3124
3125
/* usage: AnonRejectPasswords pattern [flags] */
3126
0
MODRET set_anonrejectpasswords(cmd_rec *cmd) {
3127
0
#ifdef PR_USE_REGEX
3128
0
  config_rec *c;
3129
0
  pr_regex_t *pre = NULL;
3130
0
  int notmatch = FALSE, regex_flags = REG_EXTENDED|REG_NOSUB, res = 0;
3131
0
  char *pattern = NULL;
3132
3133
0
  if (cmd->argc-1 < 1 ||
3134
0
      cmd->argc-1 > 2) {
3135
0
    CONF_ERROR(cmd, "bad number of parameters");
3136
0
  }
3137
3138
0
  CHECK_CONF(cmd, CONF_ANON);
3139
3140
  /* Make sure that, if present, the flags parameter is correctly formatted. */
3141
0
  if (cmd->argc-1 == 2) {
3142
0
    int flags = 0;
3143
3144
    /* We need to parse the flags parameter here, to see if any flags which
3145
     * affect the compilation of the regex (e.g. NC) are present.
3146
     */
3147
3148
0
    flags = pr_filter_parse_flags(cmd->tmp_pool, cmd->argv[2]);
3149
0
    if (flags < 0) {
3150
0
      CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
3151
0
        ": badly formatted flags parameter: '", cmd->argv[2], "'", NULL));
3152
0
    }
3153
3154
0
    if (flags == 0) {
3155
0
      CONF_ERROR(cmd, pstrcat(cmd->tmp_pool,
3156
0
        ": unknown flags '", cmd->argv[2], "'", NULL));
3157
0
    }
3158
3159
0
    regex_flags |= flags;
3160
0
  }
3161
3162
0
  pre = pr_regexp_alloc(&auth_module);
3163
3164
0
  pattern = cmd->argv[1];
3165
0
  if (*pattern == '!') {
3166
0
    notmatch = TRUE;
3167
0
    pattern++;
3168
0
  }
3169
3170
0
  res = pr_regexp_compile(pre, pattern, regex_flags);
3171
0
  if (res != 0) {
3172
0
    char errstr[200] = {'\0'};
3173
3174
0
    pr_regexp_error(res, pre, errstr, 200);
3175
0
    pr_regexp_free(NULL, pre);
3176
3177
0
    CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "Unable to compile regex '",
3178
0
      cmd->argv[1], "': ", errstr, NULL));
3179
0
  }
3180
3181
0
  c = add_config_param(cmd->argv[0], 2, pre, NULL);
3182
0
  c->argv[1] = palloc(c->pool, sizeof(int));
3183
0
  *((int *) c->argv[1]) = notmatch;
3184
0
  return PR_HANDLED(cmd);
3185
3186
#else
3187
  CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "The ", cmd->argv[0], " directive "
3188
    "cannot be used on this system, as you do not have POSIX compliant "
3189
    "regex support", NULL));
3190
#endif
3191
0
}
3192
3193
0
MODRET set_authaliasonly(cmd_rec *cmd) {
3194
0
  int auth_alias_only = -1;
3195
0
  config_rec *c = NULL;
3196
3197
0
  CHECK_ARGS(cmd, 1);
3198
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
3199
3200
0
  auth_alias_only = get_boolean(cmd, 1);
3201
0
  if (auth_alias_only == -1) {
3202
0
    CONF_ERROR(cmd, "expected Boolean parameter");
3203
0
  }
3204
3205
0
  c = add_config_param(cmd->argv[0], 1, NULL);
3206
0
  c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
3207
0
  *((unsigned char *) c->argv[0]) = auth_alias_only;
3208
3209
0
  c->flags |= CF_MERGEDOWN;
3210
0
  return PR_HANDLED(cmd);
3211
0
}
3212
3213
0
MODRET set_authusingalias(cmd_rec *cmd) {
3214
0
  int auth_using_alias = -1;
3215
0
  config_rec *c = NULL;
3216
3217
0
  CHECK_ARGS(cmd, 1);
3218
0
  CHECK_CONF(cmd, CONF_ANON);
3219
3220
0
  auth_using_alias = get_boolean(cmd, 1);
3221
0
  if (auth_using_alias == -1) {
3222
0
    CONF_ERROR(cmd, "expected Boolean parameter");
3223
0
  }
3224
3225
0
  c = add_config_param(cmd->argv[0], 1, NULL);
3226
0
  c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
3227
0
  *((unsigned char *) c->argv[0]) = auth_using_alias;
3228
3229
0
  return PR_HANDLED(cmd);
3230
0
}
3231
3232
0
MODRET set_createhome(cmd_rec *cmd) {
3233
0
  int create_home = -1, start = 2;
3234
0
  mode_t mode = (mode_t) 0700, dirmode = (mode_t) 0711;
3235
0
  char *skel_path = NULL;
3236
0
  config_rec *c = NULL;
3237
0
  uid_t cuid = 0;
3238
0
  gid_t cgid = 0, hgid = -1;
3239
0
  unsigned long flags = 0UL;
3240
3241
0
  if (cmd->argc-1 < 1) {
3242
0
    CONF_ERROR(cmd, "wrong number of parameters");
3243
0
  }
3244
3245
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3246
3247
0
  create_home = get_boolean(cmd, 1);
3248
0
  if (create_home == -1) {
3249
0
    CONF_ERROR(cmd, "expected Boolean parameter");
3250
0
  }
3251
3252
  /* No need to process the rest if bool is FALSE. */
3253
0
  if (create_home == FALSE) {
3254
0
    c = add_config_param(cmd->argv[0], 1, NULL);
3255
0
    c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
3256
0
    *((unsigned char *) c->argv[0]) = create_home;
3257
3258
0
    return PR_HANDLED(cmd);
3259
0
  }
3260
3261
  /* Check the mode parameter, if present */
3262
0
  if (cmd->argc-1 >= 2 &&
3263
0
      strcasecmp(cmd->argv[2], "dirmode") != 0 &&
3264
0
      strcasecmp(cmd->argv[2], "skel") != 0) {
3265
0
    char *tmp = NULL;
3266
3267
0
    mode = strtol(cmd->argv[2], &tmp, 8);
3268
3269
0
    if (tmp && *tmp) {
3270
0
      CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, ": bad mode parameter: '",
3271
0
        cmd->argv[2], "'", NULL));
3272
0
    }
3273
3274
0
    start = 3;
3275
0
  }
3276
3277
0
  if (cmd->argc-1 > 2) {
3278
0
    register unsigned int i;
3279
3280
    /* Cycle through the rest of the parameters */
3281
0
    for (i = start; i < cmd->argc;) {
3282
0
      if (strcasecmp(cmd->argv[i], "skel") == 0) {
3283
0
        struct stat st;
3284
3285
        /* Check that the skel directory, if configured, meets the
3286
         * requirements.
3287
         */
3288
3289
0
        skel_path = cmd->argv[++i];
3290
3291
0
        if (*skel_path != '/') {
3292
0
          CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "skel path '",
3293
0
            skel_path, "' is not a full path", NULL));
3294
0
        }
3295
3296
0
        if (pr_fsio_stat(skel_path, &st) < 0) {
3297
0
          CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unable to stat '",
3298
0
            skel_path, "': ", strerror(errno), NULL));
3299
0
        }
3300
3301
0
        if (!S_ISDIR(st.st_mode)) {
3302
0
          CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "'", skel_path,
3303
0
            "' is not a directory", NULL));
3304
0
        }
3305
3306
        /* Must not be world-writable. */
3307
0
        if (st.st_mode & S_IWOTH) {
3308
0
          CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "'", skel_path,
3309
0
            "' is world-writable", NULL));
3310
0
        }
3311
3312
        /* Move the index past the skel parameter */
3313
0
        i++;
3314
3315
0
      } else if (strcasecmp(cmd->argv[i], "dirmode") == 0) {
3316
0
        char *tmp = NULL;
3317
3318
0
        dirmode = strtol(cmd->argv[++i], &tmp, 8);
3319
3320
0
        if (tmp && *tmp) {
3321
0
          CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "bad mode parameter: '",
3322
0
            cmd->argv[i], "'", NULL));
3323
0
        }
3324
3325
        /* Move the index past the dirmode parameter */
3326
0
        i++;
3327
3328
0
      } else if (strcasecmp(cmd->argv[i], "uid") == 0) {
3329
3330
        /* Check for a "~" parameter. */
3331
0
        if (strcmp(cmd->argv[i+1], "~") != 0) {
3332
0
          uid_t uid;
3333
3334
0
          if (pr_str2uid(cmd->argv[++i], &uid) < 0) {
3335
0
            CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "bad UID parameter: '",
3336
0
              cmd->argv[i], "'", NULL));
3337
0
          }
3338
3339
0
          cuid = uid;
3340
3341
0
        } else {
3342
0
          cuid = (uid_t) -1;
3343
0
          i++;
3344
0
        }
3345
3346
        /* Move the index past the uid parameter */
3347
0
        i++;
3348
3349
0
      } else if (strcasecmp(cmd->argv[i], "gid") == 0) {
3350
3351
        /* Check for a "~" parameter. */
3352
0
        if (strcmp(cmd->argv[i+1], "~") != 0) {
3353
0
          gid_t gid;
3354
3355
0
          if (pr_str2gid(cmd->argv[++i], &gid) < 0) {
3356
0
            CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "bad GID parameter: '",
3357
0
              cmd->argv[i], "'", NULL));
3358
0
          }
3359
3360
0
          cgid = gid;
3361
3362
0
        } else {
3363
0
          cgid = (gid_t) -1;
3364
0
          i++;
3365
0
        }
3366
3367
        /* Move the index past the gid parameter */
3368
0
        i++;
3369
3370
0
      } else if (strcasecmp(cmd->argv[i], "homegid") == 0) {
3371
0
        char *tmp = NULL;
3372
0
        gid_t gid;
3373
3374
0
        gid = strtol(cmd->argv[++i], &tmp, 10);
3375
3376
0
        if (tmp && *tmp) {
3377
0
          CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "bad GID parameter: '",
3378
0
            cmd->argv[i], "'", NULL));
3379
0
        }
3380
3381
0
        hgid = gid;
3382
3383
        /* Move the index past the homegid parameter */
3384
0
        i++;
3385
3386
0
      } else if (strcasecmp(cmd->argv[i], "NoRootPrivs") == 0) {
3387
0
        flags |= PR_MKHOME_FL_USE_USER_PRIVS;
3388
0
        i++;
3389
3390
0
      } else {
3391
0
        CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "unknown parameter: '",
3392
0
          cmd->argv[i], "'", NULL));
3393
0
      }
3394
0
    }
3395
0
  }
3396
3397
0
  c = add_config_param(cmd->argv[0], 8, NULL, NULL, NULL, NULL,
3398
0
    NULL, NULL, NULL, NULL);
3399
3400
0
  c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
3401
0
  *((unsigned char *) c->argv[0]) = create_home;
3402
0
  c->argv[1] = pcalloc(c->pool, sizeof(mode_t));
3403
0
  *((mode_t *) c->argv[1]) = mode;
3404
0
  c->argv[2] = pcalloc(c->pool, sizeof(mode_t));
3405
0
  *((mode_t *) c->argv[2]) = dirmode;
3406
3407
0
  if (skel_path != NULL) {
3408
0
    c->argv[3] = pstrdup(c->pool, skel_path);
3409
0
  }
3410
3411
0
  c->argv[4] = pcalloc(c->pool, sizeof(uid_t));
3412
0
  *((uid_t *) c->argv[4]) = cuid;
3413
0
  c->argv[5] = pcalloc(c->pool, sizeof(gid_t));
3414
0
  *((gid_t *) c->argv[5]) = cgid;
3415
0
  c->argv[6] = pcalloc(c->pool, sizeof(gid_t));
3416
0
  *((gid_t *) c->argv[6]) = hgid;
3417
0
  c->argv[7] = pcalloc(c->pool, sizeof(unsigned long));
3418
0
  *((unsigned long *) c->argv[7]) = flags;
3419
3420
0
  return PR_HANDLED(cmd);
3421
0
}
3422
3423
0
MODRET add_defaultroot(cmd_rec *cmd) {
3424
0
  config_rec *c;
3425
0
  char *dir;
3426
0
  unsigned int argc;
3427
0
  void **argv;
3428
0
  array_header *acl = NULL;
3429
3430
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3431
3432
0
  if (cmd->argc < 2) {
3433
0
    CONF_ERROR(cmd, "syntax: DefaultRoot <directory> [<group-expression>]");
3434
0
  }
3435
3436
0
  argc = cmd->argc - 2;
3437
0
  argv = cmd->argv;
3438
3439
0
  dir = *++argv;
3440
3441
  /* dir must be / or ~. */
3442
0
  if (*dir != '/' &&
3443
0
      *dir != '~') {
3444
0
    CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "(", dir, ") absolute pathname "
3445
0
      "required", NULL));
3446
0
  }
3447
3448
0
  if (strchr(dir, '*')) {
3449
0
    CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "(", dir, ") wildcards not allowed "
3450
0
      "in pathname", NULL));
3451
0
  }
3452
3453
0
  if (*(dir + strlen(dir) - 1) != '/') {
3454
0
    dir = pstrcat(cmd->tmp_pool, dir, "/", NULL);
3455
0
  }
3456
3457
0
  acl = pr_expr_create(cmd->tmp_pool, &argc, (char **) argv);
3458
0
  c = add_config_param(cmd->argv[0], 0);
3459
3460
0
  c->argc = argc + 1;
3461
0
  c->argv = pcalloc(c->pool, (argc + 2) * sizeof(void *));
3462
0
  argv = c->argv;
3463
0
  *argv++ = pstrdup(c->pool, dir);
3464
3465
0
  if (argc && acl) {
3466
0
    while (argc--) {
3467
0
      *argv++ = pstrdup(c->pool, *((char **) acl->elts));
3468
0
      acl->elts = ((char **) acl->elts) + 1;
3469
0
    }
3470
0
  }
3471
3472
0
  *argv = NULL;
3473
0
  return PR_HANDLED(cmd);
3474
0
}
3475
3476
0
MODRET add_defaultchdir(cmd_rec *cmd) {
3477
0
  config_rec *c;
3478
0
  char *dir;
3479
0
  unsigned int argc;
3480
0
  void **argv;
3481
0
  array_header *acl = NULL;
3482
3483
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
3484
3485
0
  if (cmd->argc < 2) {
3486
0
    CONF_ERROR(cmd, "syntax: DefaultChdir <directory> [<group-expression>]");
3487
0
  }
3488
3489
0
  argc = cmd->argc - 2;
3490
0
  argv = cmd->argv;
3491
3492
0
  dir = *++argv;
3493
3494
0
  if (strchr(dir, '*')) {
3495
0
    CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "(", dir, ") wildcards not allowed "
3496
0
      "in pathname", NULL));
3497
0
  }
3498
3499
0
  if (*(dir + strlen(dir) - 1) != '/') {
3500
0
    dir = pstrcat(cmd->tmp_pool, dir, "/", NULL);
3501
0
  }
3502
3503
0
  acl = pr_expr_create(cmd->tmp_pool, &argc, (char **) argv);
3504
0
  c = add_config_param(cmd->argv[0], 0);
3505
3506
0
  c->argc = argc + 1;
3507
0
  c->argv = pcalloc(c->pool, (argc + 2) * sizeof(void *));
3508
0
  argv = c->argv;
3509
0
  *argv++ = pstrdup(c->pool, dir);
3510
3511
0
  if (argc && acl) {
3512
0
    while (argc--) {
3513
0
      *argv++ = pstrdup(c->pool, *((char **) acl->elts));
3514
0
      acl->elts = ((char **) acl->elts) + 1;
3515
0
    }
3516
0
  }
3517
3518
0
  *argv = NULL;
3519
3520
0
  c->flags |= CF_MERGEDOWN;
3521
0
  return PR_HANDLED(cmd);
3522
0
}
3523
3524
0
MODRET set_displaylogin(cmd_rec *cmd) {
3525
0
  config_rec *c = NULL;
3526
3527
0
  CHECK_ARGS(cmd, 1);
3528
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
3529
3530
0
  c = add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
3531
0
  c->flags |= CF_MERGEDOWN;
3532
3533
0
  return PR_HANDLED(cmd);
3534
0
}
3535
3536
/* usage: MaxClientsPerClass class max|"none" ["message"] */
3537
0
MODRET set_maxclientsclass(cmd_rec *cmd) {
3538
0
  int max;
3539
0
  config_rec *c;
3540
3541
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3542
3543
0
  if (strcasecmp(cmd->argv[2], "none") == 0) {
3544
0
    max = 0;
3545
3546
0
  } else {
3547
0
    char *endp = NULL;
3548
3549
0
    max = (int) strtol(cmd->argv[2], &endp, 10);
3550
3551
0
    if ((endp && *endp) || max < 1) {
3552
0
      CONF_ERROR(cmd, "max must be 'none' or a number greater than 0");
3553
0
    }
3554
0
  }
3555
3556
0
  if (cmd->argc == 4) {
3557
0
    c = add_config_param(cmd->argv[0], 3, NULL, NULL, NULL);
3558
0
    c->argv[0] = pstrdup(c->pool, cmd->argv[1]);
3559
0
    c->argv[1] = pcalloc(c->pool, sizeof(unsigned int));
3560
0
    *((unsigned int *) c->argv[1]) = max;
3561
0
    c->argv[2] = pstrdup(c->pool, cmd->argv[3]);
3562
3563
0
  } else {
3564
0
    c = add_config_param(cmd->argv[0], 2, NULL, NULL);
3565
0
    c->argv[0] = pstrdup(c->pool, cmd->argv[1]);
3566
0
    c->argv[1] = pcalloc(c->pool, sizeof(unsigned int));
3567
0
    *((unsigned int *) c->argv[1]) = max;
3568
0
  }
3569
3570
0
  return PR_HANDLED(cmd);
3571
0
}
3572
3573
/* usage: MaxClients max|"none" ["message"] */
3574
0
MODRET set_maxclients(cmd_rec *cmd) {
3575
0
  int max;
3576
0
  config_rec *c = NULL;
3577
3578
0
  if (cmd->argc < 2 ||
3579
0
      cmd->argc > 3) {
3580
0
    CONF_ERROR(cmd, "wrong number of parameters");
3581
0
  }
3582
3583
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
3584
3585
0
  if (strcasecmp(cmd->argv[1], "none") == 0) {
3586
0
    max = 0;
3587
3588
0
  } else {
3589
0
    char *endp = NULL;
3590
3591
0
    max = (int) strtol(cmd->argv[1], &endp, 10);
3592
3593
0
    if ((endp && *endp) || max < 1) {
3594
0
      CONF_ERROR(cmd, "parameter must be 'none' or a number greater than 0");
3595
0
    }
3596
0
  }
3597
3598
0
  if (cmd->argc == 3) {
3599
0
    c = add_config_param(cmd->argv[0], 2, NULL, NULL);
3600
0
    c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
3601
0
    *((unsigned int *) c->argv[0]) = max;
3602
0
    c->argv[1] = pstrdup(c->pool, cmd->argv[2]);
3603
3604
0
  } else {
3605
0
    c = add_config_param(cmd->argv[0], 1, NULL);
3606
0
    c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
3607
0
    *((unsigned int *) c->argv[0]) = max;
3608
0
  }
3609
3610
0
  c->flags |= CF_MERGEDOWN;
3611
3612
0
  return PR_HANDLED(cmd);
3613
0
}
3614
3615
/* usage: MaxClientsPerHost max|"none" ["message"] */
3616
0
MODRET set_maxhostclients(cmd_rec *cmd) {
3617
0
  int max;
3618
0
  config_rec *c = NULL;
3619
3620
0
  if (cmd->argc < 2 ||
3621
0
      cmd->argc > 3) {
3622
0
    CONF_ERROR(cmd, "wrong number of parameters");
3623
0
  }
3624
3625
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
3626
3627
0
  if (strcasecmp(cmd->argv[1], "none") == 0) {
3628
0
    max = 0;
3629
3630
0
  } else {
3631
0
    char *endp = NULL;
3632
3633
0
    max = (int) strtol(cmd->argv[1], &endp, 10);
3634
3635
0
    if ((endp && *endp) || max < 1) {
3636
0
      CONF_ERROR(cmd, "parameter must be 'none' or a number greater than 0");
3637
0
    }
3638
0
  }
3639
3640
0
  if (cmd->argc == 3) {
3641
0
    c = add_config_param(cmd->argv[0], 2, NULL, NULL);
3642
0
    c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
3643
0
    *((unsigned int *) c->argv[0]) = max;
3644
0
    c->argv[1] = pstrdup(c->pool, cmd->argv[2]);
3645
3646
0
  } else {
3647
0
    c = add_config_param(cmd->argv[0], 1, NULL);
3648
0
    c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
3649
0
    *((unsigned int *) c->argv[0]) = max;
3650
0
  }
3651
3652
0
  c->flags |= CF_MERGEDOWN;
3653
3654
0
  return PR_HANDLED(cmd);
3655
0
}
3656
3657
3658
/* usage: MaxClientsPerUser max|"none" ["message"] */
3659
0
MODRET set_maxuserclients(cmd_rec *cmd) {
3660
0
  int max;
3661
0
  config_rec *c = NULL;
3662
3663
0
  if (cmd->argc < 2 ||
3664
0
      cmd->argc > 3) {
3665
0
    CONF_ERROR(cmd, "wrong number of parameters");
3666
0
  }
3667
3668
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
3669
3670
0
  if (strcasecmp(cmd->argv[1], "none") == 0) {
3671
0
    max = 0;
3672
3673
0
  } else {
3674
0
    char *endp = NULL;
3675
3676
0
    max = (int) strtol(cmd->argv[1], &endp, 10);
3677
3678
0
    if ((endp && *endp) || max < 1) {
3679
0
      CONF_ERROR(cmd, "parameter must be 'none' or a number greater than 0");
3680
0
    }
3681
0
  }
3682
3683
0
  if (cmd->argc == 3) {
3684
0
    c = add_config_param(cmd->argv[0], 2, NULL, NULL);
3685
0
    c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
3686
0
    *((unsigned int *) c->argv[0]) = max;
3687
0
    c->argv[1] = pstrdup(c->pool, cmd->argv[2]);
3688
3689
0
  } else {
3690
0
    c = add_config_param(cmd->argv[0], 1, NULL);
3691
0
    c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
3692
0
    *((unsigned int *) c->argv[0]) = max;
3693
0
  }
3694
3695
0
  c->flags |= CF_MERGEDOWN;
3696
3697
0
  return PR_HANDLED(cmd);
3698
0
}
3699
3700
/* usage: MaxConnectionsPerHost max|"none" ["message"] */
3701
0
MODRET set_maxconnectsperhost(cmd_rec *cmd) {
3702
0
  int max;
3703
0
  config_rec *c;
3704
3705
0
  if (cmd->argc < 2 ||
3706
0
      cmd->argc > 3) {
3707
0
    CONF_ERROR(cmd, "wrong number of parameters");
3708
0
  }
3709
3710
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3711
3712
0
  if (strcasecmp(cmd->argv[1], "none") == 0) {
3713
0
    max = 0;
3714
3715
0
  } else {
3716
0
    char *tmp = NULL;
3717
3718
0
    max = (int) strtol(cmd->argv[1], &tmp, 10);
3719
3720
0
    if ((tmp && *tmp) || max < 1) {
3721
0
      CONF_ERROR(cmd, "parameter must be 'none' or a number greater than 0");
3722
0
    }
3723
0
  }
3724
3725
0
  if (cmd->argc == 3) {
3726
0
    c = add_config_param(cmd->argv[0], 2, NULL, NULL);
3727
0
    c->argv[1] = pstrdup(c->pool, cmd->argv[2]);
3728
3729
0
  } else {
3730
0
    c = add_config_param(cmd->argv[0], 1, NULL);
3731
0
  }
3732
3733
0
  c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
3734
0
  *((unsigned int *) c->argv[0]) = max;
3735
3736
0
  return PR_HANDLED(cmd);
3737
0
}
3738
3739
/* usage: MaxHostsPerUser max|"none" ["message"] */
3740
0
MODRET set_maxhostsperuser(cmd_rec *cmd) {
3741
0
  int max;
3742
0
  config_rec *c = NULL;
3743
3744
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
3745
3746
0
  if (cmd->argc < 2 ||
3747
0
      cmd->argc > 3) {
3748
0
    CONF_ERROR(cmd, "wrong number of parameters");
3749
0
  }
3750
3751
0
  if (strcasecmp(cmd->argv[1], "none") == 0) {
3752
0
    max = 0;
3753
3754
0
  } else {
3755
0
    char *endp = NULL;
3756
3757
0
    max = (int) strtol(cmd->argv[1], &endp, 10);
3758
3759
0
    if ((endp && *endp) || max < 1) {
3760
0
      CONF_ERROR(cmd, "parameter must be 'none' or a number greater than 0");
3761
0
    }
3762
0
  }
3763
3764
0
  if (cmd->argc == 3) {
3765
0
    c = add_config_param(cmd->argv[0], 2, NULL, NULL);
3766
0
    c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
3767
0
    *((unsigned int *) c->argv[0]) = max;
3768
0
    c->argv[1] = pstrdup(c->pool, cmd->argv[2]);
3769
3770
0
  } else {
3771
0
    c = add_config_param(cmd->argv[0], 1, NULL);
3772
0
    c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
3773
0
    *((unsigned int *) c->argv[0]) = max;
3774
0
  }
3775
3776
0
  c->flags |= CF_MERGEDOWN;
3777
3778
0
  return PR_HANDLED(cmd);
3779
0
}
3780
3781
0
MODRET set_maxloginattempts(cmd_rec *cmd) {
3782
0
  int max;
3783
0
  config_rec *c = NULL;
3784
3785
0
  CHECK_ARGS(cmd, 1);
3786
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3787
3788
0
  if (strcasecmp(cmd->argv[1], "none") == 0) {
3789
0
    max = 0;
3790
3791
0
  } else {
3792
0
    char *endp = NULL;
3793
0
    max = (int) strtol(cmd->argv[1], &endp, 10);
3794
3795
0
    if ((endp && *endp) || max < 1) {
3796
0
      CONF_ERROR(cmd, "parameter must be 'none' or a number greater than 0");
3797
0
    }
3798
0
  }
3799
3800
0
  c = add_config_param(cmd->argv[0], 1, NULL);
3801
0
  c->argv[0] = pcalloc(c->pool, sizeof(unsigned int));
3802
0
  *((unsigned int *) c->argv[0]) = max;
3803
3804
0
  return PR_HANDLED(cmd);
3805
0
}
3806
3807
/* usage: MaxPasswordSize len */
3808
0
MODRET set_maxpasswordsize(cmd_rec *cmd) {
3809
0
  config_rec *c;
3810
0
  size_t password_len;
3811
0
  char *len, *ptr = NULL;
3812
3813
0
  CHECK_ARGS(cmd, 1);
3814
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3815
3816
0
  len = cmd->argv[1];
3817
0
  if (*len == '-') {
3818
0
    CONF_ERROR(cmd, "badly formatted parameter");
3819
0
  }
3820
3821
0
  password_len = strtoul(len, &ptr, 10);
3822
0
  if (ptr && *ptr) {
3823
0
    CONF_ERROR(cmd, "badly formatted parameter");
3824
0
  }
3825
3826
/* XXX Applies to the following modules, which use crypt(3):
3827
 *
3828
 *  mod_ldap (ldap_auth_check; "check" authtab)
3829
 *    ldap_auth_auth ("auth" authtab) calls pr_auth_check()
3830
 *  mod_sql (sql_auth_crypt, via SQLAuthTypes; cmd_check "check" authtab dispatches here)
3831
 *    cmd_auth ("auth" authtab) calls pr_auth_check()
3832
 *  mod_auth_file (authfile_chkpass, "check" authtab)
3833
 *    authfile_auth ("auth" authtab) calls pr_auth_check()
3834
 *  mod_auth_unix (pw_check, "check" authtab)
3835
 *    pw_auth ("auth" authtab) calls pr_auth_check()
3836
 *
3837
 *  mod_sftp uses pr_auth_authenticate(), which will dispatch into above
3838
 *
3839
 *  mod_radius does NOT use either -- up to RADIUS server policy?
3840
 *
3841
 * Is there a common code path that all of the above go through?
3842
 */
3843
3844
0
  c = add_config_param(cmd->argv[0], 1, NULL);
3845
0
  c->argv[0] = palloc(c->pool, sizeof(size_t));
3846
0
  *((size_t *) c->argv[0]) = password_len;
3847
3848
0
  return PR_HANDLED(cmd);
3849
0
}
3850
3851
0
MODRET set_requirevalidshell(cmd_rec *cmd) {
3852
0
  int require_valid_shell = -1;
3853
0
  config_rec *c = NULL;
3854
3855
0
  CHECK_ARGS(cmd, 1);
3856
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
3857
3858
0
  require_valid_shell = get_boolean(cmd, 1);
3859
0
  if (require_valid_shell == -1) {
3860
0
    CONF_ERROR(cmd, "expected Boolean parameter");
3861
0
  }
3862
3863
0
  c = add_config_param(cmd->argv[0], 1, NULL);
3864
0
  c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
3865
0
  *((unsigned char *) c->argv[0]) = require_valid_shell;
3866
0
  c->flags |= CF_MERGEDOWN;
3867
3868
0
  return PR_HANDLED(cmd);
3869
0
}
3870
3871
/* usage: RewriteHome on|off */
3872
0
MODRET set_rewritehome(cmd_rec *cmd) {
3873
0
  int rewrite_home = -1;
3874
0
  config_rec *c = NULL;
3875
3876
0
  CHECK_ARGS(cmd, 1);
3877
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3878
3879
0
  rewrite_home = get_boolean(cmd, 1);
3880
0
  if (rewrite_home == -1) {
3881
0
    CONF_ERROR(cmd, "expected Boolean parameter");
3882
0
  }
3883
3884
0
  c = add_config_param(cmd->argv[0], 1, NULL);
3885
0
  c->argv[0] = pcalloc(c->pool, sizeof(int));
3886
0
  *((int *) c->argv[0]) = rewrite_home;
3887
3888
0
  return PR_HANDLED(cmd);
3889
0
}
3890
3891
0
MODRET set_rootlogin(cmd_rec *cmd) {
3892
0
  int allow_root_login = -1;
3893
0
  config_rec *c = NULL;
3894
3895
0
  CHECK_ARGS(cmd,1);
3896
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
3897
3898
0
  allow_root_login = get_boolean(cmd, 1);
3899
0
  if (allow_root_login == -1) {
3900
0
    CONF_ERROR(cmd, "expected Boolean parameter");
3901
0
  }
3902
3903
0
  c = add_config_param(cmd->argv[0], 1, NULL);
3904
0
  c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
3905
0
  *((unsigned char *) c->argv[0]) = (unsigned char) allow_root_login;
3906
0
  c->flags |= CF_MERGEDOWN;
3907
3908
0
  return PR_HANDLED(cmd);
3909
0
}
3910
3911
/* usage: RootRevoke on|off|UseNonCompliantActiveTransfer */
3912
0
MODRET set_rootrevoke(cmd_rec *cmd) {
3913
0
  int root_revoke = -1;
3914
0
  config_rec *c = NULL;
3915
3916
0
  CHECK_ARGS(cmd, 1);
3917
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
3918
3919
  /* A RootRevoke value of 0 indicates 'false', 1 indicates 'true', and
3920
   * 2 indicates 'NonCompliantActiveTransfer'.
3921
   */
3922
0
  root_revoke = get_boolean(cmd, 1);
3923
0
  if (root_revoke == -1) {
3924
0
    if (strcasecmp(cmd->argv[1], "UseNonCompliantActiveTransfer") != 0 &&
3925
0
        strcasecmp(cmd->argv[1], "UseNonCompliantActiveTransfers") != 0) {
3926
0
      CONF_ERROR(cmd, "expected Boolean parameter");
3927
0
    }
3928
3929
0
    root_revoke = 2;
3930
0
  }
3931
3932
0
  c = add_config_param(cmd->argv[0], 1, NULL);
3933
0
  c->argv[0] = pcalloc(c->pool, sizeof(int));
3934
0
  *((int *) c->argv[0]) = root_revoke;
3935
3936
0
  c->flags |= CF_MERGEDOWN;
3937
0
  return PR_HANDLED(cmd);
3938
0
}
3939
3940
0
MODRET set_timeoutlogin(cmd_rec *cmd) {
3941
0
  int timeout = -1;
3942
0
  config_rec *c = NULL;
3943
3944
0
  CHECK_ARGS(cmd, 1);
3945
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
3946
3947
0
  if (pr_str_get_duration(cmd->argv[1], &timeout) < 0) {
3948
0
    CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing timeout value '",
3949
0
      cmd->argv[1], "': ", strerror(errno), NULL));
3950
0
  }
3951
3952
0
  c = add_config_param(cmd->argv[0], 1, NULL);
3953
0
  c->argv[0] = pcalloc(c->pool, sizeof(int));
3954
0
  *((int *) c->argv[0]) = timeout;
3955
3956
0
  return PR_HANDLED(cmd);
3957
0
}
3958
3959
0
MODRET set_timeoutsession(cmd_rec *cmd) {
3960
0
  int timeout = 0, precedence = 0;
3961
0
  config_rec *c = NULL;
3962
3963
0
  int ctxt = (cmd->config && cmd->config->config_type != CONF_PARAM ?
3964
0
     cmd->config->config_type : cmd->server->config_type ?
3965
0
     cmd->server->config_type : CONF_ROOT);
3966
3967
  /* this directive must have either 1 or 3 arguments */
3968
0
  if (cmd->argc-1 != 1 &&
3969
0
      cmd->argc-1 != 3) {
3970
0
    CONF_ERROR(cmd, "missing parameters");
3971
0
  }
3972
3973
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
3974
3975
  /* Set the precedence for this config_rec based on its configuration
3976
   * context.
3977
   */
3978
0
  if (ctxt & CONF_GLOBAL) {
3979
0
    precedence = 1;
3980
3981
  /* These will never appear simultaneously */
3982
0
  } else if ((ctxt & CONF_ROOT) ||
3983
0
             (ctxt & CONF_VIRTUAL)) {
3984
0
    precedence = 2;
3985
3986
0
  } else if (ctxt & CONF_ANON) {
3987
0
    precedence = 3;
3988
0
  }
3989
3990
0
  if (pr_str_get_duration(cmd->argv[1], &timeout) < 0) {
3991
0
    CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing timeout value '",
3992
0
      cmd->argv[1], "': ", strerror(errno), NULL));
3993
0
  }
3994
3995
0
  if (timeout == 0) {
3996
    /* do nothing */
3997
0
    return PR_HANDLED(cmd);
3998
0
  }
3999
4000
0
  if (cmd->argc-1 == 3) {
4001
0
    if (strcasecmp(cmd->argv[2], "user") != 0 &&
4002
0
        strcasecmp(cmd->argv[2], "group") != 0 &&
4003
0
        strcasecmp(cmd->argv[2], "class") != 0) {
4004
0
      CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, cmd->argv[0],
4005
0
        ": unknown classifier used: '", cmd->argv[2], "'", NULL));
4006
0
    }
4007
0
  }
4008
4009
0
  if (cmd->argc-1 == 1) {
4010
0
    c = add_config_param(cmd->argv[0], 2, NULL);
4011
0
    c->argv[0] = pcalloc(c->pool, sizeof(int));
4012
0
    *((int *) c->argv[0]) = timeout;
4013
0
    c->argv[1] = pcalloc(c->pool, sizeof(unsigned int));
4014
0
    *((unsigned int *) c->argv[1]) = precedence;
4015
4016
0
  } else if (cmd->argc-1 == 3) {
4017
0
    array_header *acl = NULL;
4018
0
    unsigned int argc;
4019
0
    void **argv;
4020
4021
0
    argc = cmd->argc - 3;
4022
0
    argv = cmd->argv + 2;
4023
4024
0
    acl = pr_expr_create(cmd->tmp_pool, &argc, (char **) argv);
4025
4026
0
    c = add_config_param(cmd->argv[0], 0);
4027
0
    c->argc = argc + 2;
4028
4029
    /* Add 3 to argc for the argv of the config_rec: one for the
4030
     * seconds value, one for the precedence, one for the classifier,
4031
     * and one for the terminating NULL.
4032
     */
4033
0
    c->argv = pcalloc(c->pool, ((argc + 4) * sizeof(void *)));
4034
4035
    /* Capture the config_rec's argv pointer for doing the by-hand
4036
     * population.
4037
     */
4038
0
    argv = c->argv;
4039
4040
    /* Copy in the seconds. */
4041
0
    *argv = pcalloc(c->pool, sizeof(int));
4042
0
    *((int *) *argv++) = timeout;
4043
4044
    /* Copy in the precedence. */
4045
0
    *argv = pcalloc(c->pool, sizeof(unsigned int));
4046
0
    *((unsigned int *) *argv++) = precedence;
4047
4048
    /* Copy in the classifier. */
4049
0
    *argv++ = pstrdup(c->pool, cmd->argv[2]);
4050
4051
    /* now, copy in the expression arguments */
4052
0
    if (argc && acl) {
4053
0
      while (argc--) {
4054
0
        *argv++ = pstrdup(c->pool, *((char **) acl->elts));
4055
0
        acl->elts = ((char **) acl->elts) + 1;
4056
0
      }
4057
0
    }
4058
4059
    /* don't forget the terminating NULL */
4060
0
    *argv = NULL;
4061
4062
0
  } else {
4063
    /* Should never reach here. */
4064
0
    CONF_ERROR(cmd, "wrong number of parameters");
4065
0
  }
4066
4067
0
  c->flags |= CF_MERGEDOWN_MULTI;
4068
0
  return PR_HANDLED(cmd);
4069
0
}
4070
4071
0
MODRET set_useftpusers(cmd_rec *cmd) {
4072
0
  int use_ftpusers = -1;
4073
0
  config_rec *c = NULL;
4074
4075
0
  CHECK_ARGS(cmd, 1);
4076
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
4077
4078
0
  use_ftpusers = get_boolean(cmd, 1);
4079
0
  if (use_ftpusers == -1) {
4080
0
    CONF_ERROR(cmd, "expected Boolean parameter");
4081
0
  }
4082
4083
0
  c = add_config_param(cmd->argv[0], 1, NULL);
4084
0
  c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
4085
0
  *((unsigned char *) c->argv[0]) = use_ftpusers;
4086
0
  c->flags |= CF_MERGEDOWN;
4087
4088
0
  return PR_HANDLED(cmd);
4089
0
}
4090
4091
/* usage: UseLastlog on|off */
4092
0
MODRET set_uselastlog(cmd_rec *cmd) {
4093
#if defined(PR_USE_LASTLOG)
4094
  int use_lastlog = -1;
4095
  config_rec *c;
4096
4097
  CHECK_ARGS(cmd, 1);
4098
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
4099
4100
  use_lastlog = get_boolean(cmd, 1);
4101
  if (use_lastlog == -1) {
4102
    CONF_ERROR(cmd, "expected Boolean parameter");
4103
  }
4104
4105
  c = add_config_param(cmd->argv[0], 1, NULL);
4106
  c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
4107
  *((unsigned char *) c->argv[0]) = use_lastlog;
4108
4109
  return PR_HANDLED(cmd);
4110
#else
4111
0
  CONF_ERROR(cmd, "requires lastlog support (--with-lastlog)");
4112
0
#endif /* PR_USE_LASTLOG */
4113
0
}
4114
4115
/* usage: UserAlias alias real-user */
4116
0
MODRET set_useralias(cmd_rec *cmd) {
4117
0
  char *alias, *real_user;
4118
4119
0
  CHECK_ARGS(cmd, 2);
4120
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
4121
4122
  /* Make sure that the given names differ. */
4123
0
  alias = cmd->argv[1];
4124
0
  real_user = cmd->argv[2];
4125
4126
0
  if (strcmp(alias, real_user) == 0) {
4127
0
    CONF_ERROR(cmd, "alias and real user names must differ");
4128
0
  }
4129
4130
0
  add_config_param_str(cmd->argv[0], 2, alias, real_user);
4131
0
  return PR_HANDLED(cmd);
4132
0
}
4133
4134
0
MODRET set_userdirroot(cmd_rec *cmd) {
4135
0
  int user_dir_root = -1;
4136
0
  config_rec *c = NULL;
4137
4138
0
  CHECK_ARGS(cmd, 1);
4139
0
  CHECK_CONF(cmd, CONF_ANON);
4140
4141
0
  user_dir_root = get_boolean(cmd, 1);
4142
0
  if (user_dir_root == -1) {
4143
0
    CONF_ERROR(cmd, "expected Boolean parameter");
4144
0
  }
4145
4146
0
  c = add_config_param(cmd->argv[0], 1, NULL);
4147
0
  c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
4148
0
  *((unsigned char *) c->argv[0]) = user_dir_root;
4149
4150
0
  return PR_HANDLED(cmd);
4151
0
}
4152
4153
0
MODRET set_userpassword(cmd_rec *cmd) {
4154
0
  config_rec *c = NULL;
4155
4156
0
  CHECK_ARGS(cmd, 2);
4157
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
4158
4159
0
  c = add_config_param_str(cmd->argv[0], 2, cmd->argv[1], cmd->argv[2]);
4160
0
  c->flags |= CF_MERGEDOWN;
4161
4162
0
  return PR_HANDLED(cmd);
4163
0
}
4164
4165
/* usage: WtmpLog on|off */
4166
0
MODRET set_wtmplog(cmd_rec *cmd) {
4167
0
  int use_wtmp = -1;
4168
0
  config_rec *c = NULL;
4169
4170
0
  CHECK_ARGS(cmd, 1);
4171
0
  CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON);
4172
4173
0
  if (strcasecmp(cmd->argv[1], "NONE") == 0) {
4174
0
    use_wtmp = FALSE;
4175
4176
0
  } else {
4177
0
    use_wtmp = get_boolean(cmd, 1);
4178
0
    if (use_wtmp == -1) {
4179
0
      CONF_ERROR(cmd, "expected Boolean parameter");
4180
0
    }
4181
0
  }
4182
4183
0
  c = add_config_param(cmd->argv[0], 1, NULL);
4184
0
  c->argv[0] = pcalloc(c->pool, sizeof(unsigned char));
4185
0
  *((unsigned char *) c->argv[0]) = use_wtmp;
4186
0
  c->flags |= CF_MERGEDOWN;
4187
4188
  return PR_HANDLED(cmd);
4189
0
}
4190
4191
/* Module API tables
4192
 */
4193
4194
static conftable auth_conftab[] = {
4195
  { "AccessDenyMsg",    set_accessdenymsg,    NULL },
4196
  { "AccessGrantMsg",   set_accessgrantmsg,   NULL },
4197
  { "AllowChrootSymlinks",  set_allowchrootsymlinks,  NULL },
4198
  { "AllowEmptyPasswords",  set_allowemptypasswords,  NULL },
4199
  { "AnonAllowRobots",    set_anonallowrobots,    NULL },
4200
  { "AnonRequirePassword",  set_anonrequirepassword,  NULL },
4201
  { "AnonRejectPasswords",  set_anonrejectpasswords,  NULL },
4202
  { "AuthAliasOnly",    set_authaliasonly,    NULL },
4203
  { "AuthUsingAlias",   set_authusingalias,   NULL },
4204
  { "CreateHome",   set_createhome,     NULL },
4205
  { "DefaultChdir",   add_defaultchdir,   NULL },
4206
  { "DefaultRoot",    add_defaultroot,    NULL },
4207
  { "DisplayLogin",   set_displaylogin,   NULL },
4208
  { "MaxClients",   set_maxclients,     NULL },
4209
  { "MaxClientsPerClass", set_maxclientsclass,    NULL },
4210
  { "MaxClientsPerHost",  set_maxhostclients,   NULL },
4211
  { "MaxClientsPerUser",  set_maxuserclients,   NULL },
4212
  { "MaxConnectionsPerHost",  set_maxconnectsperhost,   NULL },
4213
  { "MaxHostsPerUser",    set_maxhostsperuser,    NULL },
4214
  { "MaxLoginAttempts",   set_maxloginattempts,   NULL },
4215
  { "MaxPasswordSize",    set_maxpasswordsize,    NULL },
4216
  { "RequireValidShell",  set_requirevalidshell,    NULL },
4217
  { "RewriteHome",    set_rewritehome,    NULL },
4218
  { "RootLogin",    set_rootlogin,      NULL },
4219
  { "RootRevoke",   set_rootrevoke,     NULL },
4220
  { "TimeoutLogin",   set_timeoutlogin,   NULL },
4221
  { "TimeoutSession",   set_timeoutsession,   NULL },
4222
  { "UseFtpUsers",    set_useftpusers,    NULL },
4223
  { "UseLastlog",   set_uselastlog,     NULL },
4224
  { "UserAlias",    set_useralias,      NULL },
4225
  { "UserDirRoot",    set_userdirroot,    NULL },
4226
  { "UserPassword",   set_userpassword,   NULL },
4227
  { "WtmpLog",      set_wtmplog,      NULL },
4228
4229
  { NULL,     NULL,       NULL }
4230
};
4231
4232
static cmdtable auth_cmdtab[] = {
4233
  { PRE_CMD,  C_USER, G_NONE, auth_pre_user,  FALSE,  FALSE,  CL_AUTH },
4234
  { CMD,  C_USER, G_NONE, auth_user,  FALSE,  FALSE,  CL_AUTH },
4235
  { PRE_CMD,  C_PASS, G_NONE, auth_pre_pass,  FALSE,  FALSE,  CL_AUTH },
4236
  { CMD,  C_PASS, G_NONE, auth_pass,  FALSE,  FALSE,  CL_AUTH },
4237
  { POST_CMD, C_PASS, G_NONE, auth_post_pass, FALSE,  FALSE,  CL_AUTH },
4238
  { LOG_CMD,  C_PASS, G_NONE, auth_log_pass,  FALSE,  FALSE },
4239
  { LOG_CMD_ERR,C_PASS, G_NONE, auth_err_pass,  FALSE,  FALSE },
4240
  { CMD,  C_ACCT, G_NONE, auth_acct,  FALSE,  FALSE,  CL_AUTH },
4241
  { CMD,  C_REIN, G_NONE, auth_rein,  FALSE,  FALSE,  CL_AUTH },
4242
4243
  /* For the automatic robots.txt handling */
4244
  { PRE_CMD,  C_RETR, G_NONE, auth_pre_retr,  FALSE,  FALSE },
4245
  { POST_CMD, C_RETR, G_NONE, auth_post_retr, FALSE,  FALSE },
4246
  { POST_CMD_ERR,C_RETR,G_NONE, auth_post_retr, FALSE,  FALSE },
4247
4248
  { 0, NULL }
4249
};
4250
4251
/* Module interface */
4252
4253
module auth_module = {
4254
  NULL, NULL,
4255
4256
  /* Module API version */
4257
  0x20,
4258
4259
  /* Module name */
4260
  "auth",
4261
4262
  /* Module configuration directive table */
4263
  auth_conftab, 
4264
4265
  /* Module command handler table */
4266
  auth_cmdtab,
4267
4268
  /* Module authentication handler table */
4269
  NULL,
4270
4271
  /* Module initialization function */
4272
  auth_init,
4273
4274
  /* Session initialization function */
4275
  auth_sess_init
4276
};