| 1 | /* |
| 2 | ** Copyright 1998 - 2008 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 "courierauth.h" |
| 18 | #include "courierauthstaticlist.h" |
| 19 | #include "libhmac/hmac.h" |
| 20 | |
| 21 | |
| 22 | static int auth_custom_login(const char *service, char *authdata, |
| 23 | int (*callback_func)(struct authinfo *, void *), |
| 24 | int *callback_arg) |
| 25 | { |
| 26 | const char *user, *pass; |
| 27 | |
| 28 | if ((user=strtok(authdata, "\n")) == 0 || |
| 29 | (pass=strtok(0, "\n")) == 0) |
| 30 | { |
| 31 | errno=EPERM; |
| 32 | return (-1); |
| 33 | } |
| 34 | |
| 35 | return authcustomcommon(user, pass, callback_func, callback_arg); |
| 36 | } |
| 37 | |
| 38 | static int auth_custom_cram(const char *service, |
| 39 | const char *authtype, |
| 40 | char *authdata, |
| 41 | int (*callback_func)(struct authinfo *, void *), |
| 42 | void *callback_arg) |
| 43 | { |
| 44 | struct cram_callback_info cci; |
| 45 | |
| 46 | if (auth_get_cram(authtype, authdata, &cci)) |
| 47 | return (-1); |
| 48 | |
| 49 | cci.callback_func=callback_func; |
| 50 | cci.callback_arg=callback_arg; |
| 51 | |
| 52 | return authcustomcommon(cci.user, 0, &auth_cram_callback, &cci); |
| 53 | } |
| 54 | |
| 55 | int auth_custom(const char *service, const char *authtype, char *authdata, |
| 56 | int (*callback_func)(struct authinfo *, void *), |
| 57 | void *callback_arg) |
| 58 | { |
| 59 | if (strcmp(authtype, AUTHTYPE_LOGIN) == 0) |
| 60 | return (auth_custom_login(service, authdata, |
| 61 | callback_func, callback_arg)); |
| 62 | |
| 63 | return (auth_custom_cram(service, authtype, authdata, |
| 64 | callback_func, callback_arg)); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | extern int auth_custom_pre(const char *userid, const char *service, |
| 69 | int (*callback)(struct authinfo *, void *), |
| 70 | void *arg); |
| 71 | |
| 72 | static int auth_custom_chgpwd(const char *service, |
| 73 | const char *uid, |
| 74 | const char *oldpwd, |
| 75 | const char *newpwd) |
| 76 | { |
| 77 | /* |
| 78 | ** Insert code to change the account's password here. |
| 79 | ** |
| 80 | ** return 0 if changed. |
| 81 | ** |
| 82 | ** return 1 if failed. |
| 83 | ** Set errno to EPERM if we had a temporary failure (such as invalid |
| 84 | ** old pwd). |
| 85 | ** |
| 86 | ** Set errno to EINVAL if we failed because we did not recognize uid. |
| 87 | */ |
| 88 | |
| 89 | errno=EINVAL; |
| 90 | return (-1); |
| 91 | } |
| 92 | |
| 93 | static void auth_custom_idle() |
| 94 | { |
| 95 | /* |
| 96 | ** Insert code to temporarily deallocate resources after remaining |
| 97 | ** idle (as part of authdaemond) for more than 5 minutes. |
| 98 | */ |
| 99 | } |
| 100 | |
| 101 | static struct authstaticinfo authcustom_info={ |
| 102 | "authcustom", |
| 103 | auth_custom, |
| 104 | auth_custom_pre, |
| 105 | authcustomclose, |
| 106 | auth_custom_chgpwd, |
| 107 | auth_custom_idle}; |
| 108 | |
| 109 | |
| 110 | struct authstaticinfo *courier_authcustom_init() |
| 111 | { |
| 112 | return &authcustom_info; |
| 113 | } |