d9898ee8 |
1 | /* |
2 | ** Copyright 1998 - 2005 Double Precision, Inc. See COPYING for |
3 | ** distribution information. |
4 | */ |
5 | |
6 | #include "auth.h" |
7 | #include "courierauthdebug.h" |
8 | #include <stdio.h> |
9 | #include <string.h> |
10 | #include <stdlib.h> |
11 | #include <ctype.h> |
12 | #include <errno.h> |
13 | |
14 | static const char rcsid[]="$Id: authmoduser3.c,v 1.6 2005/07/02 15:40:28 mrsam Exp $"; |
15 | |
16 | extern int auth_generic(const char *service, |
17 | const char *authtype, |
18 | char *authdata, |
19 | int (*callback_func)(struct authinfo *, void *), |
20 | void *callback_arg); |
21 | |
22 | static int badstr(const char *p) |
23 | { |
24 | while (p && *p) |
25 | { |
26 | if ((int)(unsigned char)*p < ' ') |
27 | return 1; |
28 | ++p; |
29 | } |
30 | return 0; |
31 | } |
32 | |
33 | /* Create a new string consisting of: |
34 | ** - username |
35 | ** - DEFDOMAIN if username does not contain any characters from DOMAINSEP |
36 | ** (or if DOMAINSEP not set, then if username does not contain |
37 | ** the first char of DEFDOMAIN) |
38 | ** - strings s1, s2, s3 |
39 | */ |
40 | |
41 | char *strdupdefdomain(const char *userid, const char *s1, const char *s2, |
42 | const char *s3) |
43 | { |
44 | char *p, *q, *r; |
45 | |
46 | q=getenv("DEFDOMAIN"); |
47 | if (q && q[0]) |
48 | { |
49 | r=getenv("DOMAINSEP"); |
50 | if (r ? strpbrk(userid, r) : strchr(userid, q[0])) q = ""; |
51 | } |
52 | else |
53 | q = ""; |
54 | |
55 | p=malloc(strlen(userid)+strlen(q)+strlen(s1)+strlen(s2)+strlen(s3)+1); |
56 | if (p) |
57 | strcat(strcat(strcat(strcat(strcpy(p, userid), q), s1), s2), s3); |
58 | return p; |
59 | } |
60 | |
61 | int auth_login(const char *service, |
62 | const char *userid, |
63 | const char *passwd, |
64 | int (*callback_func)(struct authinfo *, void *), |
65 | void *callback_arg) |
66 | |
67 | { |
68 | char *p; |
69 | int rc; |
70 | |
71 | if (badstr(userid) || badstr(passwd)) |
72 | { |
73 | errno=EINVAL; |
74 | return -1; |
75 | } |
76 | |
77 | courier_authdebug_login_init(); |
78 | courier_authdebug_login( 1, "username=%s", userid ); |
79 | courier_authdebug_login( 2, "password=%s", passwd ); |
80 | |
81 | p = strdupdefdomain(userid, "\n", passwd, "\n"); |
82 | if (!p) |
83 | return (-1); |
84 | |
85 | rc=auth_generic(service, AUTHTYPE_LOGIN, p, |
86 | callback_func, |
87 | callback_arg); |
88 | free(p); |
89 | return rc; |
90 | } |