sql.c
19.6 KB
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2002, 2003, 2004, 2005, 2006,
2007 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#ifdef HAVE_SHADOW_H
# include <shadow.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#else
# include <string.h>
#endif
#ifdef HAVE_CRYPT_H
# include <crypt.h>
#endif
#include <mailutils/assoc.h>
#include <mailutils/list.h>
#include <mailutils/iterator.h>
#include <mailutils/mailbox.h>
#include <mailutils/argp.h>
#include <mailutils/mu_auth.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/nls.h>
#include <mailutils/mutil.h>
#include <mailutils/sql.h>
#ifdef USE_SQL
int sql_interface = 0;
char *mu_sql_getpwnam_query;
char *mu_sql_getpass_query;
char *mu_sql_getpwuid_query;
char *mu_sql_host = NULL; /* Hostname to connect to. NULL for UNIX
socket connection */
char *mu_sql_user = "accounts"; /* Username for mysql access */
char *mu_sql_passwd = "yurpass"; /* Password for mysql access */
char *mu_sql_db = "accounts"; /* Database Name */
int mu_sql_port = 0; /* Port number to connect to.
0 means default port */
enum mu_password_type mu_sql_password_type = password_hash;
static mu_assoc_t sql_field_map;
static char *
sql_escape_string (const char *ustr)
{
char *str, *q;
const unsigned char *p;
size_t len = strlen (ustr);
#define ESCAPABLE_CHAR "\\'\""
for (p = (const unsigned char *) ustr; *p; p++)
{
if (strchr (ESCAPABLE_CHAR, *p))
len++;
}
str = malloc (len + 1);
if (!str)
return NULL;
for (p = (const unsigned char *) ustr, q = str; *p; p++)
{
if (strchr (ESCAPABLE_CHAR, *p))
*q++ = '\\';
*q++ = *p;
}
*q = 0;
return str;
}
char *
mu_sql_expand_query (const char *query, const char *ustr)
{
char *p, *q, *res;
int len;
char *esc_ustr;
if (!query)
return NULL;
esc_ustr = sql_escape_string (ustr);
/* Compute resulting query length */
for (len = 0, p = (char *) query; *p; )
{
if (*p == '%')
{
if (p[1] == 'u')
{
len += strlen (esc_ustr);
p += 2;
}
else if (p[1] == '%')
{
len++;
p += 2;
}
else
{
len++;
p++;
}
}
else
{
len++;
p++;
}
}
res = malloc (len + 1);
if (!res)
{
free (esc_ustr);
return res;
}
for (p = (char *) query, q = res; *p; )
{
if (*p == '%')
{
switch (*++p)
{
case 'u':
strcpy (q, esc_ustr);
q += strlen (q);
p++;
break;
case '%':
*q++ = *p++;
break;
default:
*q++ = *p++;
}
}
else
*q++ = *p++;
}
*q = 0;
free (esc_ustr);
return res;
}
static int
set_sql_password_type (const char *arg)
{
if (strcmp (arg, "plain") == 0)
mu_sql_password_type = password_plaintext;
else if (strcmp (arg, "hash") == 0)
mu_sql_password_type = password_hash;
else if (strcmp (arg, "scrambled") == 0)
mu_sql_password_type = password_scrambled;
else
return 1;
return 0;
}
/* Command-line configuration */
# define ARG_SQL_INTERFACE 256
# define ARG_SQL_GETPWNAM 257
# define ARG_SQL_GETPWUID 258
# define ARG_SQL_GETPASS 259
# define ARG_SQL_HOST 260
# define ARG_SQL_USER 261
# define ARG_SQL_PASSWD 262
# define ARG_SQL_DB 263
# define ARG_SQL_PORT 264
# define ARG_SQL_MU_PASSWORD_TYPE 265
# define ARG_SQL_FIELD_MAP 266
static struct argp_option mu_sql_argp_option[] = {
{"sql-interface", ARG_SQL_INTERFACE, N_("NAME"), OPTION_HIDDEN,
N_("Type of SQL interface to use"), },
{"sql-getpwnam", ARG_SQL_GETPWNAM, N_("QUERY"), OPTION_HIDDEN,
N_("SQL query to retrieve a passwd entry based on username"), 0},
{"sql-getpwuid", ARG_SQL_GETPWUID, N_("QUERY"), OPTION_HIDDEN,
N_("SQL query to retrieve a passwd entry based on UID"), 0},
{"sql-getpass", ARG_SQL_GETPASS, N_("QUERY"), OPTION_HIDDEN,
N_("SQL query to retrieve a password from the database"), 0},
{"sql-host", ARG_SQL_HOST, N_("HOSTNAME"), OPTION_HIDDEN,
N_("Name or IP of MySQL server to connect to"), 0},
{"sql-user", ARG_SQL_USER, N_("NAME"), OPTION_HIDDEN,
N_("SQL user name"), 0},
{"sql-passwd", ARG_SQL_PASSWD, N_("STRING"), OPTION_HIDDEN,
N_("SQL connection password"), 0},
{"sql-db", ARG_SQL_DB, N_("STRING"), OPTION_HIDDEN,
N_("Name of the database to connect to"), 0},
{"sql-port", ARG_SQL_PORT, N_("NUMBER"), OPTION_HIDDEN,
N_("Port to use"), 0},
{"sql-password-type", ARG_SQL_MU_PASSWORD_TYPE, N_("STRING"), OPTION_HIDDEN,
N_("Type of password returned by --sql-getpass query. STRING is one of: plain, hash, scrambled"), 0},
{"sql-field-map", ARG_SQL_FIELD_MAP, N_("MAP"), OPTION_HIDDEN,
N_("Declare a name translation map for SQL fields in results of sql-getpwnam and "
"sql-getpwuid queries"), 0},
{ NULL, 0, NULL, 0, NULL, 0 }
};
static error_t
mu_sql_argp_parser (int key, char *arg, struct argp_state *state)
{
int rc, err;
switch (key)
{
case ARG_SQL_INTERFACE:
sql_interface = mu_sql_interface_index (arg);
if (sql_interface == 0)
argp_error (state, _("Unknown SQL interface `%s'"), arg);
break;
case ARG_SQL_GETPWNAM:
mu_sql_getpwnam_query = arg;
break;
case ARG_SQL_GETPWUID:
mu_sql_getpwuid_query = arg;
break;
case ARG_SQL_GETPASS:
mu_sql_getpass_query = arg;
break;
case ARG_SQL_HOST:
mu_sql_host = arg;
break;
case ARG_SQL_USER:
mu_sql_user = arg;
break;
case ARG_SQL_PASSWD:
mu_sql_passwd = arg;
break;
case ARG_SQL_DB:
mu_sql_db = arg;
break;
case ARG_SQL_PORT:
mu_sql_port = strtoul (arg, NULL, 0);
break;
case ARG_SQL_MU_PASSWORD_TYPE:
if (set_sql_password_type (arg))
argp_error (state, _("Unknown password type `%s'"), arg);
break;
case ARG_SQL_FIELD_MAP:
rc = mutil_parse_field_map (arg, &sql_field_map, &err);
if (rc)
argp_error (state, _("Error near element %d: %s"),
err, mu_strerror (rc));
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp mu_sql_argp = {
mu_sql_argp_option,
mu_sql_argp_parser,
};
/* Resource file configuration */
static int
cb_iface (mu_cfg_locus_t *locus, void *data, char *arg)
{
sql_interface = mu_sql_interface_index (arg);
if (sql_interface == 0)
mu_error (_("%s:%d: Unknown SQL interface `%s'"),
locus->file, locus->line, arg);
return 0;
}
static int
cb_password_type (mu_cfg_locus_t *locus, void *data, char *arg)
{
if (set_sql_password_type (arg))
mu_error (_("%s:%d: Unknown password type `%s'"),
locus->file, locus->line, arg);
return 0;
}
static int
cb_field_map (mu_cfg_locus_t *locus, void *data, char *arg)
{
int err;
int rc = mutil_parse_field_map (arg, &sql_field_map, &err);
if (rc)
mu_error (_("%s:%d: Error near element %d: %s"),
locus->file, locus->line, err, mu_strerror (rc));
return 0;
}
static struct mu_cfg_param mu_sql_param[] = {
{ "interface", mu_cfg_callback, NULL, cb_iface },
{ "getwpnam", mu_cfg_string, &mu_sql_getpwnam_query },
{ "getpwuid", mu_cfg_string, &mu_sql_getpwuid_query },
{ "getpass", mu_cfg_string, &mu_sql_getpass_query },
{ "host", mu_cfg_string, &mu_sql_host },
{ "user", mu_cfg_string, &mu_sql_user },
{ "passwd", mu_cfg_string, &mu_sql_passwd },
{ "db", mu_cfg_string, &mu_sql_db },
{ "port", mu_cfg_int, &mu_sql_port },
{ "password-type", mu_cfg_callback, NULL, cb_password_type },
{ "field-map", mu_cfg_callback, NULL, cb_field_map },
{ NULL }
};
static int
decode_tuple_v1_0 (mu_sql_connection_t conn, int n,
struct mu_auth_data **return_data)
{
int rc;
char *mailbox_name = NULL;
char *name;
if (mu_sql_get_column (conn, 0, 0, &name))
return MU_ERR_FAILURE;
if (n == 7)
{
char *tmp;
if (mu_sql_get_column (conn, 0, 6, &tmp))
return MU_ERR_FAILURE;
if ((mailbox_name = strdup (tmp)) == NULL)
return ENOMEM;
}
else if (mu_construct_user_mailbox_url (&mailbox_name, name))
return MU_ERR_FAILURE;
if (mailbox_name)
{
char *passwd, *suid, *sgid, *dir, *shell;
if (mu_sql_get_column (conn, 0, 1, &passwd)
|| !passwd
|| mu_sql_get_column (conn, 0, 2, &suid)
|| !suid
|| mu_sql_get_column (conn, 0, 3, &sgid)
|| !sgid
|| mu_sql_get_column (conn, 0, 4, &dir)
|| !dir
|| mu_sql_get_column (conn, 0, 5, &shell)
|| !shell)
return MU_ERR_FAILURE;
rc = mu_auth_data_alloc (return_data,
name,
passwd,
atoi (suid),
atoi (sgid),
"SQL User",
dir,
shell,
mailbox_name,
1);
}
else
rc = MU_ERR_AUTH_FAILURE;
free (mailbox_name);
return rc;
}
static int
get_field (mu_sql_connection_t conn, const char *id, char **ret, int mandatory)
{
const char **name = mu_assoc_ref (sql_field_map, id);
int rc = mu_sql_get_field (conn, 0, name ? *name : id, ret);
if (rc)
{
if (mandatory || rc != MU_ERR_NOENT)
mu_error (_("Cannot get SQL field `%s' (`%s'): %s"),
id, name ? *name : id, mu_strerror (rc));
}
else if (!*ret)
{
mu_error (_("SQL field `%s' (`%s') has NULL value"),
id, name ? *name : id);
rc = MU_READ_ERROR;
}
return rc;
}
static int
decode_tuple_new (mu_sql_connection_t conn, int n,
struct mu_auth_data **return_data)
{
int rc;
char *mailbox_name = NULL;
char *name;
char *passwd, *suid, *sgid, *dir, *shell, *gecos, *squota;
mu_off_t quota = 0;
char *p;
uid_t uid;
gid_t gid;
if (get_field (conn, MU_AUTH_NAME, &name, 1)
|| get_field (conn, MU_AUTH_PASSWD, &passwd, 1)
|| get_field (conn, MU_AUTH_UID, &suid, 1)
|| get_field (conn, MU_AUTH_GID, &sgid, 1)
|| get_field (conn, MU_AUTH_DIR, &dir, 1)
|| get_field (conn, MU_AUTH_SHELL, &shell, 1))
return MU_ERR_FAILURE;
if (get_field (conn, MU_AUTH_GECOS, &gecos, 0))
gecos = "SQL user";
uid = strtoul (suid, &p, 0);
if (*p)
{
mu_error (_("Invalid value for uid: %s"), suid);
return MU_ERR_FAILURE;
}
gid = strtoul (sgid, &p, 0);
if (*p)
{
mu_error (_("Invalid value for gid: %s"), sgid);
return MU_ERR_FAILURE;
}
rc = get_field (conn, MU_AUTH_MAILBOX, &mailbox_name, 0);
switch (rc)
{
case 0:
mailbox_name = strdup (mailbox_name);
break;
case MU_ERR_NOENT:
if (mu_construct_user_mailbox_url (&mailbox_name, name))
return MU_ERR_FAILURE;
break;
default:
return MU_ERR_FAILURE;
}
rc = get_field (conn, MU_AUTH_QUOTA, &squota, 0);
if (rc == 0)
{
if (strcasecmp (squota, "none") == 0)
quota = 0;
else
{
quota = strtoul (squota, &p, 10);
switch (*p)
{
case 0:
break;
case 'k':
case 'K':
quota *= 1024;
break;
case 'm':
case 'M':
quota *= 1024*1024;
break;
default:
mu_error (_("Invalid value for quota: %s"), squota);
free (mailbox_name);
return MU_ERR_FAILURE;
}
}
}
else if (rc == MU_ERR_NOENT)
quota = 0;
else
{
free (mailbox_name);
return MU_ERR_FAILURE;
}
rc = mu_auth_data_alloc (return_data,
name,
passwd,
uid,
gid,
gecos,
dir,
shell,
mailbox_name,
1);
if (rc == 0)
mu_auth_data_set_quota (*return_data, quota);
free (mailbox_name);
return rc;
}
static int
decode_tuple (mu_sql_connection_t conn, int n,
struct mu_auth_data **return_data)
{
if (sql_field_map)
return decode_tuple_new (conn, n, return_data);
else
return decode_tuple_v1_0 (conn, n, return_data);
}
static int
mu_auth_sql_by_name (struct mu_auth_data **return_data,
const void *key,
void *func_data MU_ARG_UNUSED,
void *call_data MU_ARG_UNUSED)
{
int status, rc;
char *query_str = NULL;
mu_sql_connection_t conn;
size_t n;
if (!key)
return EINVAL;
query_str = mu_sql_expand_query (mu_sql_getpwnam_query, key);
if (!query_str)
return MU_ERR_FAILURE;
status = mu_sql_connection_init (&conn,
sql_interface,
mu_sql_host,
mu_sql_port,
mu_sql_user, mu_sql_passwd,
mu_sql_db);
if (status)
{
mu_error ("%s: %s", mu_strerror (status), mu_sql_strerror (conn));
mu_sql_connection_destroy (&conn);
free (query_str);
return MU_ERR_FAILURE;
}
status = mu_sql_connect (conn);
if (status)
{
mu_error ("%s: %s", mu_strerror (status), mu_sql_strerror (conn));
mu_sql_connection_destroy (&conn);
free (query_str);
return EAGAIN;
}
status = mu_sql_query (conn, query_str);
free (query_str);
if (status)
{
mu_error (_("SQL Query failed: %s"),
(status == MU_ERR_SQL) ? mu_sql_strerror (conn) :
mu_strerror (status));
mu_sql_connection_destroy (&conn);
return MU_ERR_FAILURE;
}
status = mu_sql_store_result (conn);
if (status)
{
mu_error (_("Cannot store SQL result: %s"),
(status == MU_ERR_SQL) ? mu_sql_strerror (conn) :
mu_strerror (status));
mu_sql_connection_destroy (&conn);
return MU_ERR_FAILURE;
}
mu_sql_num_tuples (conn, &n);
if (n == 0)
rc = MU_ERR_AUTH_FAILURE;
else
rc = decode_tuple (conn, n, return_data);
mu_sql_release_result (conn);
mu_sql_disconnect (conn);
mu_sql_connection_destroy (&conn);
return rc;
}
static int
mu_auth_sql_by_uid (struct mu_auth_data **return_data,
const void *key,
void *func_data MU_ARG_UNUSED,
void *call_data MU_ARG_UNUSED)
{
char uidstr[64];
int status, rc;
char *query_str = NULL;
mu_sql_connection_t conn;
size_t n;
if (!key)
return EINVAL;
snprintf (uidstr, sizeof (uidstr), "%u", *(uid_t*)key);
query_str = mu_sql_expand_query (mu_sql_getpwuid_query, uidstr);
if (!query_str)
return ENOMEM;
status = mu_sql_connection_init (&conn,
sql_interface,
mu_sql_host,
mu_sql_port,
mu_sql_user, mu_sql_passwd,
mu_sql_db);
if (status)
{
mu_error ("%s: %s", mu_strerror (status), mu_sql_strerror (conn));
mu_sql_connection_destroy (&conn);
free (query_str);
return MU_ERR_FAILURE;
}
status = mu_sql_connect (conn);
if (status)
{
mu_error ("%s: %s", mu_strerror (status), mu_sql_strerror (conn));
mu_sql_connection_destroy (&conn);
free (query_str);
return EAGAIN;
}
status = mu_sql_query (conn, query_str);
free (query_str);
if (status)
{
mu_error (_("SQL Query failed: %s"),
(status == MU_ERR_SQL) ? mu_sql_strerror (conn) :
mu_strerror (status));
mu_sql_connection_destroy (&conn);
return MU_ERR_FAILURE;
}
status = mu_sql_store_result (conn);
if (status)
{
mu_error (_("Cannot store SQL result: %s"),
(status == MU_ERR_SQL) ? mu_sql_strerror (conn) :
mu_strerror (status));
mu_sql_connection_destroy (&conn);
return MU_ERR_FAILURE;
}
mu_sql_num_tuples (conn, &n);
if (n == 0)
rc = MU_ERR_AUTH_FAILURE;
else
rc = decode_tuple (conn, n, return_data);
mu_sql_release_result (conn);
mu_sql_disconnect (conn);
mu_sql_connection_destroy (&conn);
return rc;
}
int
mu_sql_getpass (const char *username, char **passwd)
{
mu_sql_connection_t conn;
char *query_str;
int status;
char *sql_pass;
query_str = mu_sql_expand_query (mu_sql_getpass_query, username);
if (!query_str)
return MU_ERR_FAILURE;
status = mu_sql_connection_init (&conn,
sql_interface,
mu_sql_host,
mu_sql_port,
mu_sql_user, mu_sql_passwd,
mu_sql_db);
if (status)
{
mu_error ("%s: %s", mu_strerror (status), mu_sql_strerror (conn));
mu_sql_connection_destroy (&conn);
free (query_str);
return MU_ERR_FAILURE;
}
status = mu_sql_connect (conn);
if (status)
{
mu_error ("%s: %s", mu_strerror (status), mu_sql_strerror (conn));
mu_sql_connection_destroy (&conn);
free (query_str);
return EAGAIN;
}
status = mu_sql_query (conn, query_str);
free (query_str);
if (status)
{
mu_error (_("SQL Query failed: %s"),
(status == MU_ERR_SQL) ? mu_sql_strerror (conn) :
mu_strerror (status));
mu_sql_connection_destroy (&conn);
return MU_ERR_FAILURE;
}
status = mu_sql_store_result (conn);
if (status)
{
mu_error (_("Cannot store SQL result: %s"),
(status == MU_ERR_SQL) ? mu_sql_strerror (conn) :
mu_strerror (status));
mu_sql_connection_destroy (&conn);
return MU_ERR_FAILURE;
}
status = mu_sql_get_column (conn, 0, 0, &sql_pass);
if (status)
{
mu_error (_("Cannot get password from SQL: %s"),
(status == MU_ERR_SQL) ? mu_sql_strerror (conn) :
mu_strerror (status));
mu_sql_release_result (conn);
mu_sql_connection_destroy (&conn);
return MU_ERR_FAILURE;
}
*passwd = strdup (sql_pass);
mu_sql_disconnect (conn);
mu_sql_connection_destroy (&conn);
if (!*passwd)
return ENOMEM;
return 0;
}
static int
mu_sql_authenticate (struct mu_auth_data **return_data MU_ARG_UNUSED,
const void *key,
void *func_data MU_ARG_UNUSED, void *call_data)
{
const struct mu_auth_data *auth_data = key;
char *pass = call_data;
char *sql_pass;
int rc;
if (!auth_data)
return EINVAL;
if ((rc = mu_sql_getpass (auth_data->name, &sql_pass)))
return rc;
switch (mu_sql_password_type)
{
case password_hash:
rc = strcmp (sql_pass, crypt (pass, sql_pass));
break;
case password_scrambled:
/* FIXME: Should this call be implementation-independent? I mean,
should we have mu_sql_check_scrambled() that will match the
password depending on the exact type of the underlying database,
just as the rest of mu_sql_.* functions do */
#ifdef HAVE_MYSQL
rc = mu_check_mysql_scrambled_password (sql_pass, pass);
#else
rc = 1;
#endif
break;
case password_plaintext:
rc = strcmp (sql_pass, pass);
break;
}
free (sql_pass);
return rc == 0 ? 0 : MU_ERR_AUTH_FAILURE;
}
#else
# define mu_sql_authenticate mu_auth_nosupport
# define mu_auth_sql_by_name mu_auth_nosupport
# define mu_auth_sql_by_uid mu_auth_nosupport
#endif
struct mu_auth_module mu_auth_sql_module = {
"sql",
#ifdef USE_SQL
&mu_sql_argp,
mu_sql_param,
#else
NULL,
NULL,
#endif
mu_sql_authenticate,
NULL,
mu_auth_sql_by_name,
NULL,
mu_auth_sql_by_uid,
NULL
};