Coverage Report

Created: 2025-08-24 06:03

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