| 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 | |
| 15 | extern int auth_generic(const char *service, |
| 16 | const char *authtype, |
| 17 | char *authdata, |
| 18 | int (*callback_func)(struct authinfo *, void *), |
| 19 | void *callback_arg); |
| 20 | |
| 21 | static int badstr(const char *p) |
| 22 | { |
| 23 | while (p && *p) |
| 24 | { |
| 25 | if ((int)(unsigned char)*p < ' ') |
| 26 | return 1; |
| 27 | ++p; |
| 28 | } |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | /* Create a new string consisting of: |
| 33 | ** - username |
| 34 | ** - DEFDOMAIN if username does not contain any characters from DOMAINSEP |
| 35 | ** (or if DOMAINSEP not set, then if username does not contain |
| 36 | ** the first char of DEFDOMAIN) |
| 37 | ** - strings s1, s2, s3 |
| 38 | */ |
| 39 | |
| 40 | char *strdupdefdomain(const char *userid, const char *s1, const char *s2, |
| 41 | const char *s3) |
| 42 | { |
| 43 | char *p, *q, *r; |
| 44 | |
| 45 | q=getenv("DEFDOMAIN"); |
| 46 | if (q && q[0]) |
| 47 | { |
| 48 | r=getenv("DOMAINSEP"); |
| 49 | if (r ? strpbrk(userid, r) : strchr(userid, q[0])) q = ""; |
| 50 | } |
| 51 | else |
| 52 | q = ""; |
| 53 | |
| 54 | p=malloc(strlen(userid)+strlen(q)+strlen(s1)+strlen(s2)+strlen(s3)+1); |
| 55 | if (p) |
| 56 | strcat(strcat(strcat(strcat(strcpy(p, userid), q), s1), s2), s3); |
| 57 | return p; |
| 58 | } |
| 59 | |
| 60 | int auth_login(const char *service, |
| 61 | const char *userid, |
| 62 | const char *passwd, |
| 63 | int (*callback_func)(struct authinfo *, void *), |
| 64 | void *callback_arg) |
| 65 | |
| 66 | { |
| 67 | char *p; |
| 68 | int rc; |
| 69 | |
| 70 | if (badstr(userid) || badstr(passwd)) |
| 71 | { |
| 72 | errno=EINVAL; |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | courier_authdebug_login_init(); |
| 77 | courier_authdebug_login( 1, "username=%s", userid ); |
| 78 | courier_authdebug_login( 2, "password=%s", passwd ); |
| 79 | |
| 80 | p = strdupdefdomain(userid, "\n", passwd, "\n"); |
| 81 | if (!p) |
| 82 | return (-1); |
| 83 | |
| 84 | rc=auth_generic(service, AUTHTYPE_LOGIN, p, |
| 85 | callback_func, |
| 86 | callback_arg); |
| 87 | free(p); |
| 88 | return rc; |
| 89 | } |