aboutsummaryrefslogtreecommitdiff
path: root/src/authmode.c
blob: 950b1ec8686f121965d1873bb2fb548d1b315baa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
   authmode.c

   This file is part of GNU Anubis.
   Copyright (C) 2003-2014 The Anubis Team.

   GNU Anubis is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   GNU Anubis is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with GNU Anubis.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "headers.h"
#include "extern.h"
#include "rcfile.h"

#ifdef WITH_GSASL

static char *smtp_greeting_message;
static ANUBIS_LIST smtp_help_message;
static int helo_count; /* report possible SMTP attack */



void
make_help_message (char *text)
{
  char *p;

  if (smtp_help_message)
    list_destroy (&smtp_help_message, anubis_free_list_item, NULL);
  smtp_help_message = list_create ();

  p = strtok (text, "\n");
  do
    list_append (smtp_help_message, strdup (p));
  while ((p = strtok (NULL, "\n")));
}


enum asmtp_state
{
  state_init,
  state_ehlo,
  state_auth,
  state_quit,
};

#define KW_HELO      0
#define KW_EHLO      1
#define KW_XELO      2
#define KW_AUTH      3
#define KW_QUIT      4
#define KW_HELP      5
#define KW_STARTTLS  6
#define KW_MAIL      7
#define KW_RCPT      8
#define KW_XDATABASE 9

static int
asmtp_kw (const char *name)
{
  static struct anubis_keyword kw[] = {
    { "helo", KW_HELO },
    { "ehlo", KW_EHLO },
    { "xelo", KW_XELO },
    { "auth", KW_AUTH },
    { "help", KW_HELP },
    { "quit", KW_QUIT },
    { "starttls", KW_STARTTLS },
    { "mail", KW_MAIL },
    { "rcpt", KW_RCPT },
    { "xdatabase", KW_XDATABASE },
    { NULL },
  };
  struct anubis_keyword *kwp = anubis_keyword_lookup_ci (kw, name);
  if (kwp)
    return kwp->tok;
  return -1;
}

#define R_CONT     0x8000
#define R_CODEMASK 0xfff

void
asmtp_reply (int code, char *fmt, ...)
{
  va_list ap;
  int cont = code & R_CONT ? '-' : ' ';
  static char obuf[512];
  int n;

  va_start (ap, fmt);
  n = snprintf (obuf, sizeof obuf, "%d%c", code & R_CODEMASK, cont);
  n += vsnprintf (obuf + n, sizeof obuf - n, fmt, ap);
  va_end (ap);
  n += snprintf (obuf + n, sizeof obuf - n, "\r\n");
  swrite (SERVER, remote_client, obuf);
}

char *
get_command_word (char *command)
{
  return strtok (command, " \t\r\n");
}

char *
get_command_arg ()
{
  return strtok (NULL, " \t\r\n");
}

static void
asmtp_greet (void)
{
  char *name = get_localname ();
  asmtp_reply (220, "%s %s", name, smtp_greeting_message);
}

static ANUBIS_LIST asmtp_capa;

void
asmtp_capa_add (char *name)
{
  list_append (asmtp_capa, strdup (name));
}

void
asmtp_capa_add_prefix (char *prefix, char *name)
{
  char *str = malloc (strlen (prefix) + 1 + strlen (name) + 1);
  if (!str)
    abort ();
  sprintf (str, "%s %s", prefix, name);
  list_append (asmtp_capa, str);
}

static int
name_cmp (void *a, void *b)
{
  return strcmp (a, b);
}

void
asmtp_capa_remove (char *name)
{
  char *p = list_remove (asmtp_capa, strdup (name), name_cmp);
  if (p)
    free (p);
}

static void
asmtp_capa_init (void)
{
  asmtp_capa = list_create ();
#ifdef USE_SSL
  asmtp_capa_add ("STARTTLS");
#endif
  auth_gsasl_init ();
  asmtp_capa_add ("HELP");
}

static void
asmtp_capa_report (void)
{
  ITERATOR itr = iterator_create (asmtp_capa);
  char *p = iterator_first (itr);

  while (p)
    {
      char *next = iterator_next (itr);
      asmtp_reply ((next ? R_CONT : 0) | 250, "%s", p);
      p = next;
    }
  iterator_destroy (&itr);
}

static enum asmtp_state
asmtp_helo_reply (char *args)
{
  char *domain = get_command_arg ();

  if (!domain)
    {
      asmtp_reply (501, "HELO requires domain address");
      return state_init;
    }
  else if (get_command_arg ())
    {
      asmtp_reply (501, "Syntax error");
      return state_init;
    }

  helo_count++;
  asmtp_reply (250, "Anubis is pleased to meet you.");
  return state_ehlo;
}

static enum asmtp_state
asmtp_ehlo_reply (char *args)
{
  char *domain = get_command_arg (args);

  if (!domain)
    {
      asmtp_reply (501, "EHLO requires domain address");
      return state_init;
    }
  else if (get_command_arg ())
    {
      asmtp_reply (501, "Syntax error");
      return state_init;
    }
  
  set_ehlo_domain (domain, strlen (domain));
  
  helo_count++;
  asmtp_reply (R_CONT | 250, "Anubis is pleased to meet you.");
  asmtp_capa_report ();
  return state_ehlo;
}

static enum asmtp_state
asmtp_xelo_reply (char *args)
{
  char *domain = get_command_arg (args);

  if (!domain)
    {
      asmtp_reply (501, "XELO requires domain address");
      return state_init;
    }
  else if (get_command_arg ())
    {
      asmtp_reply (501, "Syntax error");
      return state_init;
    }

  helo_count++;
  topt |= T_XELO;

  asmtp_reply (R_CONT | 250, "Anubis is pleased to meet you.");
  asmtp_capa_report ();
  return state_ehlo;
}

static void
asmtp_help (void)
{
  if (smtp_help_message)
    {
      char *s;
      ITERATOR itr = iterator_create (smtp_help_message);
      for (s = iterator_first (itr); s; s = iterator_next (itr))
	asmtp_reply (R_CONT | 214, "%s", s);
      iterator_destroy (&itr);
    }
  asmtp_reply (214, "End of HELP info");
}

static enum asmtp_state
asmtp_init (enum asmtp_state state)
{
  char *command = NULL;
  size_t s = 0;

  recvline (SERVER, remote_client, &command, &s);

  switch (asmtp_kw (get_command_word (command))) {
  case KW_EHLO:
    state = asmtp_ehlo_reply (command);
    break;
    
  case KW_XELO:
    state = asmtp_xelo_reply (command);
    break;
    
  case KW_HELO:
    state = asmtp_helo_reply (command);
    break;
    
  case KW_HELP:
    asmtp_help ();
    break;
    
  case KW_AUTH:
  case KW_STARTTLS:
    asmtp_reply (503, "Polite people say EHLO first");
    break;
    
  case KW_QUIT:
    asmtp_reply (221, "Closing connection");
    state = state_quit;
    break;
    
  case KW_MAIL:
  case KW_RCPT:
    asmtp_reply (550, "Command disabled. Proper authentication required.");
    break;
    
  default:
    asmtp_reply (500, "Unknown command");
  }
  free (command);
  return state;
}

static enum asmtp_state
asmtp_ehlo (enum asmtp_state state, ANUBIS_USER * usr)
{
  char *command = NULL;
  size_t s = 0;
  char *mech;
  char *init_input;

  if (recvline (SERVER, remote_client, &command, &s) <= 0)
    exit (1);

  switch (asmtp_kw (get_command_word (command))) {
  case KW_EHLO:
    state = asmtp_ehlo_reply (command);
    break;
    
#ifdef USE_SSL
  case KW_STARTTLS:
    if (topt & T_SSL_FINISHED)
      asmtp_reply(503, "TLS already started");
    else
      {
	NET_STREAM stream;
	
	if (!secure.cert)
	  secure.cert = xstrdup (DEFAULT_SSL_PEM);
	if (!check_filename (secure.cert, NULL))
	  {
	    asmtp_reply (454, "TLS not available due to temporary reason");
	    break;
	  }

	if (!secure.key)
	  secure.key = xstrdup (secure.cert);
	else if (!check_filename (secure.key, NULL))
	  {
	    asmtp_reply (454, "TLS not available due to temporary reason");
	    break;
	  }

	asmtp_reply (220, "Ready to start TLS");
	stream = start_ssl_server (remote_client, options.termlevel > NORMAL);
	if (!stream)
	  {
	    asmtp_reply (454, "TLS not available" CRLF);
	    break;
	  }
	remote_client = stream;
	asmtp_capa_remove ("STARTTLS");
	topt |= T_SSL_FINISHED;
    
	state = state_ehlo;
      }
    break;
#endif /* USE_SSL */
    
  case KW_AUTH:
    mech = get_command_arg ();
    init_input = get_command_arg ();
    if (anubis_auth_gsasl (mech, init_input, usr) == 0)
      state = state_auth;
    break;
    
  case KW_QUIT:
    asmtp_reply (221, "Closing connection");
    state = state_quit;
    break;
    
  case KW_MAIL:
  case KW_RCPT:
    asmtp_reply (550, "Command disabled. Proper authentication required.");
    break;
    
  case KW_HELP:
    asmtp_help ();
    break;
    
  default:
    asmtp_reply (500, "Unknown command");
  }

  free (command);
  return state;
}

static int
anubis_smtp (ANUBIS_USER * usr)
{
  enum asmtp_state state;

  asmtp_capa_init ();
  asmtp_greet ();

  for (state = state_init; state != state_auth;)
    {
      switch (state) {
      case state_init:
	state = asmtp_init (state);
	break;
	
      case state_ehlo:
	state = asmtp_ehlo (state, usr);
	break;
	
      case state_quit:
	return EXIT_FAILURE;
	
      case state_auth:
	break;
      }
    }

  if (topt & T_SSL_FINISHED)
    {
      /* If `ssl yes' is requested, convert it to `ssl-oneway' for
	 the mechanics of tunnel.c:handle_ehlo() to work properly. */
	 
      topt &= ~T_SSL_FINISHED;
      if (topt & T_SSL)
	{
	  topt &= ~T_SSL;
	  topt |= T_SSL_ONEWAY;
	}
    }
  xdatabase_enable ();
  
  return 0;
}

static void
xdb_loop (void)
{
  char *command = NULL;
  size_t s = 0;

  info (VERBOSE, _("Entering XDB loop..."));

  asmtp_capa_add ("XDATABASE");
  while (recvline (SERVER, remote_client, &command, &s) > 0)
    {
      switch (asmtp_kw (get_command_word (command))) {
      case KW_HELP:
	asmtp_help ();
	break;
    
      case KW_QUIT:
	asmtp_reply (221, "Closing connection");
	info (VERBOSE, _("Exiting XDB loop..."));
	return;

      case KW_XDATABASE:
	xdatabase (make_lowercase (get_command_arg ()));
	break;

      case KW_EHLO:
	asmtp_ehlo_reply (command);
	break;
	
      case KW_AUTH:      
      case KW_STARTTLS:  
      case KW_MAIL:
      case KW_RCPT:
	asmtp_reply (550, "Command disabled.");
	break;
    
      default:
	asmtp_reply (500, "Unknown command");
      }
    }
  info (VERBOSE, _("Exiting XDB loop..."));
}



static char *anubis_dbarg;

void
anubis_set_password_db (char *arg)
{
  free (anubis_dbarg);
  anubis_dbarg = strdup (arg);
}

int
anubis_get_db_record (const char *username, ANUBIS_USER * usr)
{
  void *db;
  int rc;
  char *errtext;

  if (!anubis_dbarg)
    {
      anubis_error (0, 0, _("Database not specified"));
      return ANUBIS_DB_FAIL;
    }

  if (anubis_db_open (anubis_dbarg, anubis_db_rdonly,
		      &db, &errtext) != ANUBIS_DB_SUCCESS)
    {
      anubis_error (0, 0,
		    _("Cannot open database %s: %s"), anubis_dbarg, errtext);
      return ANUBIS_DB_FAIL;
    }

  rc = anubis_db_get_record (db, username, usr);
  switch (rc) {
  case ANUBIS_DB_SUCCESS:
    info (VERBOSE, _("Found record for `%s'."), username);
    break;
    
  case ANUBIS_DB_FAIL:
    anubis_error (0, 0,
		  _("Cannot retrieve data from the SASL database: %s"),
		  anubis_db_strerror (db));
    break;
    
  case ANUBIS_DB_NOT_FOUND:
    info (VERBOSE, _("Record for `%s' not found."), username);
    break;
  }

  anubis_db_close (&db);
  return rc;
}


int
anubis_authenticate_mode (struct sockaddr_in *addr)
{
  ANUBIS_USER usr;

  remote_server = remote_client;
  alarm (900);

  if (anubis_smtp (&usr))
    return EXIT_FAILURE;

  if (usr.username)
    {
      if (check_username (usr.username))
	{
	  anubis_changeowner (usr.username);
	  assign_string (&session.clientname, usr.username);
	}
      else
	set_unprivileged_user ();
    }
  else
    set_unprivileged_user ();

  if (usr.rcfile_name)
    session.rcfile_name = usr.rcfile_name;

  open_rcfile (CF_CLIENT);
  process_rcfile (CF_CLIENT);
  
  if (topt & T_XELO)
    {
      xdb_loop ();
    }
  else
    {
      session_prologue ();
      smtp_session ();
      alarm (0);
    }
  
  net_close_stream (&remote_client);
  net_close_stream (&remote_server);
  
  info (NORMAL, _("Connection closed successfully."));

#ifdef HAVE_PAM
  if (pamh)
    {
      int pam_retval = pam_close_session (pamh, 0);
      if (pam_retval == PAM_SUCCESS)
	info (VERBOSE, _("PAM: Session closed."));
      if (pam_end (pamh, pam_retval) != PAM_SUCCESS)
	{
	  pamh = NULL;
	  info (NORMAL, _("PAM: failed to release authenticator."));
	  return EXIT_FAILURE;
	}
    }
#endif /* HAVE_PAM */
  return 0;
}

#define KW_SASL_PASSWORD_DB      1
#define KW_SASL_ALLOWED_MECH     2
#define KW_SASL_SERVICE          3
#define KW_SASL_REALM            4
#define KW_SASL_HOSTNAME         5
#define KW_SMTP_GREETING_MESSAGE 6
#define KW_SMTP_HELP_MESSAGE     7

static void
rc_parser (EVAL_ENV env, int key, ANUBIS_LIST arglist, void *inv_data)
{
  char *arg = list_item (arglist, 0);

  switch (key)
    {
    case KW_SMTP_GREETING_MESSAGE:
      if (list_count (arglist) != 1)
	eval_error (0, env, _("invalid number of arguments"));
      xfree (smtp_greeting_message);
      smtp_greeting_message = strdup (arg);
      break;
      
    case KW_SMTP_HELP_MESSAGE:
      if (list_count (arglist) != 1)
	eval_error (0, env, _("invalid number of arguments"));
      make_help_message (arg);
      break;
      
    case KW_SASL_PASSWORD_DB:
      if (list_count (arglist) != 1)
	eval_error (0, env, _("invalid number of arguments"));
      anubis_set_password_db (arg);
      break;
      
    case KW_SASL_ALLOWED_MECH:
      anubis_set_server_mech_list (arglist);
      break;
      
    case KW_SASL_SERVICE:
      if (list_count (arglist) != 1)
	eval_error (0, env, _("invalid number of arguments"));
      xfree (anubis_sasl_service);
      anubis_sasl_service = strdup (arg);
      break;
      
    case KW_SASL_REALM:
      if (list_count (arglist) != 1)
	eval_error (0, env, _("invalid number of arguments"));
      xfree (anubis_sasl_realm);
      anubis_sasl_realm = strdup (arg);
      break;
      
    case KW_SASL_HOSTNAME:
      if (list_count (arglist) != 1)
	eval_error (0, env, _("invalid number of arguments"));
      xfree (anubis_sasl_hostname);
      anubis_sasl_hostname = strdup (arg);
      break;
      
    default:
      eval_error (2, env,
		  _("INTERNAL ERROR at %s:%d: unhandled key %d; "
		    "please report"),
		  __FILE__, __LINE__,
		  key);
    }
}

static struct rc_kwdef init_authmode_kw[] = {
  { "smtp-greeting-message", KW_SMTP_GREETING_MESSAGE },
  { "smtp-help-message",     KW_SMTP_HELP_MESSAGE },
  { "sasl-password-db",      KW_SASL_PASSWORD_DB },
  { "sasl-allowed-mech",     KW_SASL_ALLOWED_MECH },
  { "sasl-service",          KW_SASL_SERVICE },
  { "sasl-hostname",         KW_SASL_HOSTNAME },
  { "sasl-realm",            KW_SASL_REALM },
  { NULL }
};

static struct rc_secdef_child init_authmode_child = {
  NULL,
  CF_INIT,
  init_authmode_kw,
  rc_parser,
  NULL
};

void
authmode_section_init (void)
{
  struct rc_secdef *sp = anubis_add_section ("AUTH");
  rc_secdef_add_child (sp, &init_authmode_child);
  smtp_greeting_message = strdup ("GNU Anubis ESMTP; Identify yourself");
  smtp_help_message = list_create ();
  list_append (smtp_help_message,
	       strdup
	       ("Run 'info anubis' or visit http://www.gnu.org/software/anubis/manual/"));
}

#endif /* WITH_GSASL */

/* EOF */

Return to:

Send suggestions and report system problems to the System administrator.