| 1 | /* |
| 2 | ** Copyright 1998 - 2004 Double Precision, Inc. See COPYING for |
| 3 | ** distribution information. |
| 4 | */ |
| 5 | |
| 6 | #if HAVE_CONFIG_H |
| 7 | #include "courier_auth_config.h" |
| 8 | #endif |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <ctype.h> |
| 12 | #include <string.h> |
| 13 | #include <errno.h> |
| 14 | |
| 15 | #include "auth.h" |
| 16 | #include "authcustom.h" |
| 17 | #include "authstaticlist.h" |
| 18 | |
| 19 | static const char rcsid[]="$Id: authcustom.c,v 1.6 2005/03/20 19:10:30 mrsam Exp $"; |
| 20 | |
| 21 | static int auth_custom_login(const char *service, char *authdata, |
| 22 | int (*callback_func)(struct authinfo *, void *), |
| 23 | int *callback_arg) |
| 24 | { |
| 25 | const char *user, *pass; |
| 26 | |
| 27 | if ((user=strtok(authdata, "\n")) == 0 || |
| 28 | (pass=strtok(0, "\n")) == 0) |
| 29 | { |
| 30 | errno=EPERM; |
| 31 | return (-1); |
| 32 | } |
| 33 | |
| 34 | return authcustomcommon(user, pass, callback_func, callback_arg); |
| 35 | } |
| 36 | |
| 37 | #if HAVE_HMACLIB |
| 38 | |
| 39 | #include "libhmac/hmac.h" |
| 40 | #include "cramlib.h" |
| 41 | |
| 42 | static int auth_custom_cram(const char *service, |
| 43 | const char *authtype, |
| 44 | char *authdata, |
| 45 | int (*callback_func)(struct authinfo *, void *), |
| 46 | void *callback_arg) |
| 47 | { |
| 48 | struct cram_callback_info cci; |
| 49 | |
| 50 | if (auth_get_cram(authtype, authdata, &cci)) |
| 51 | return (-1); |
| 52 | |
| 53 | cci.callback_func=callback_func; |
| 54 | cci.callback_arg=callback_arg; |
| 55 | |
| 56 | return authcustomcommon(cci.user, 0, &auth_cram_callback, &cci); |
| 57 | } |
| 58 | #endif |
| 59 | |
| 60 | int auth_custom(const char *service, const char *authtype, char *authdata, |
| 61 | int (*callback_func)(struct authinfo *, void *), |
| 62 | void *callback_arg) |
| 63 | { |
| 64 | if (strcmp(authtype, AUTHTYPE_LOGIN) == 0) |
| 65 | return (auth_custom_login(service, authdata, |
| 66 | callback_func, callback_arg)); |
| 67 | |
| 68 | #if HAVE_HMACLIB |
| 69 | return (auth_custom_cram(service, authtype, authdata, |
| 70 | callback_func, callback_arg)); |
| 71 | #else |
| 72 | return -1; |
| 73 | #endif |
| 74 | } |
| 75 | |
| 76 | |
| 77 | extern int auth_custom_pre(const char *userid, const char *service, |
| 78 | int (*callback)(struct authinfo *, void *), |
| 79 | void *arg); |
| 80 | |
| 81 | static int auth_custom_chgpwd(const char *service, |
| 82 | const char *uid, |
| 83 | const char *oldpwd, |
| 84 | const char *newpwd) |
| 85 | { |
| 86 | /* |
| 87 | ** Insert code to change the account's password here. |
| 88 | ** |
| 89 | ** return 0 if changed. |
| 90 | ** |
| 91 | ** return 1 if failed. |
| 92 | ** Set errno to EPERM if we had a temporary failure (such as invalid |
| 93 | ** old pwd). |
| 94 | ** |
| 95 | ** Set errno to EINVAL if we failed because we did not recognize uid. |
| 96 | */ |
| 97 | |
| 98 | errno=EINVAL; |
| 99 | return (-1); |
| 100 | } |
| 101 | |
| 102 | static void auth_custom_idle() |
| 103 | { |
| 104 | /* |
| 105 | ** Insert code to temporarily deallocate resources after remaining |
| 106 | ** idle (as part of authdaemond) for more than 5 minutes. |
| 107 | */ |
| 108 | } |
| 109 | |
| 110 | static struct authstaticinfo authcustom_info={ |
| 111 | "authcustom", |
| 112 | auth_custom, |
| 113 | auth_custom_pre, |
| 114 | authcustomclose, |
| 115 | auth_custom_chgpwd, |
| 116 | auth_custom_idle}; |
| 117 | |
| 118 | |
| 119 | struct authstaticinfo *courier_authcustom_init() |
| 120 | { |
| 121 | return &authcustom_info; |
| 122 | } |