| 1 | /* |
|---|
| 2 | * ProFTPD: mod_quotatab_ldap -- a mod_quotatab sub-module for obtaining |
|---|
| 3 | * quota information from an LDAP directory. |
|---|
| 4 | * |
|---|
| 5 | * Copyright (c) 2002-2009 TJ Saunders |
|---|
| 6 | * Copyright (c) 2002-3 John Morrissey |
|---|
| 7 | * |
|---|
| 8 | * This program is free software; you can redistribute it and/or modify |
|---|
| 9 | * it under the terms of the GNU General Public License as published by |
|---|
| 10 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 11 | * (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with this program; if not, write to the Free Software |
|---|
| 20 | * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA. |
|---|
| 21 | * |
|---|
| 22 | * As a special exemption, the respective copyright holders give permission |
|---|
| 23 | * to link this program with OpenSSL, and distribute the resulting |
|---|
| 24 | * executable, without including the source code for OpenSSL in the source |
|---|
| 25 | * distribution. |
|---|
| 26 | */ |
|---|
| 27 | |
|---|
| 28 | #include "mod_quotatab.h" |
|---|
| 29 | |
|---|
| 30 | module quotatab_ldap_module; |
|---|
| 31 | |
|---|
| 32 | static int ldaptab_close(quota_table_t *ldaptab) { |
|---|
| 33 | |
|---|
| 34 | /* Nothing really needs to be done here. */ |
|---|
| 35 | return 0; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | static unsigned char ldaptab_lookup(quota_table_t *ldaptab, void *ptr, |
|---|
| 39 | const char *name, quota_type_t quota_type) { |
|---|
| 40 | char **values = NULL; |
|---|
| 41 | array_header *ldap_data = NULL; |
|---|
| 42 | pool *tmp_pool = NULL; |
|---|
| 43 | cmdtable *ldap_cmdtab = NULL; |
|---|
| 44 | cmd_rec *ldap_cmd = NULL; |
|---|
| 45 | modret_t *ldap_res = NULL; |
|---|
| 46 | quota_limit_t *limit = ptr; |
|---|
| 47 | |
|---|
| 48 | if (quota_type != USER_QUOTA) { |
|---|
| 49 | quotatab_log("error: mod_quotatab_ldap only supports user quotas"); |
|---|
| 50 | return FALSE; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | /* Find the cmdtable for the ldap_quota_lookup command. */ |
|---|
| 54 | if ((ldap_cmdtab = pr_stash_get_symbol(PR_SYM_HOOK, "ldap_quota_lookup", NULL, NULL)) == NULL) { |
|---|
| 55 | quotatab_log("error: unable to find LDAP hook symbol 'ldap_quota_lookup'"); |
|---|
| 56 | return FALSE; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /* Allocate a temporary pool for the duration of this lookup. */ |
|---|
| 60 | tmp_pool = make_sub_pool(ldaptab->tab_pool); |
|---|
| 61 | |
|---|
| 62 | /* Prepare the command and call the handler. */ |
|---|
| 63 | ldap_cmd = pr_cmd_alloc(tmp_pool, 1, name); |
|---|
| 64 | ldap_res = pr_module_call(ldap_cmdtab->m, ldap_cmdtab->handler, ldap_cmd); |
|---|
| 65 | |
|---|
| 66 | destroy_pool(tmp_pool); |
|---|
| 67 | |
|---|
| 68 | /* Check the results. */ |
|---|
| 69 | if (!ldap_res || MODRET_ISERROR(ldap_res)) { |
|---|
| 70 | quotatab_log("error performing LDAP search"); |
|---|
| 71 | return FALSE; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | ldap_data = (array_header *) ldap_res->data; |
|---|
| 75 | if (ldap_data->nelts != 9) { |
|---|
| 76 | quotatab_log("LDAP search returned wrong number of elements"); |
|---|
| 77 | return FALSE; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | values = (char **) ldap_data->elts; |
|---|
| 81 | |
|---|
| 82 | /* Retrieve the limit record (9 values): |
|---|
| 83 | * name |
|---|
| 84 | * per_session |
|---|
| 85 | * limit_type |
|---|
| 86 | * bytes_{in,out,xfer}_avail |
|---|
| 87 | * files_{in,out,xfer}_avail |
|---|
| 88 | */ |
|---|
| 89 | |
|---|
| 90 | memmove(limit->name, values[0], strlen(values[0]) + 1); |
|---|
| 91 | limit->quota_type = USER_QUOTA; |
|---|
| 92 | |
|---|
| 93 | if (!strcasecmp(values[1], "false")) |
|---|
| 94 | limit->quota_per_session = FALSE; |
|---|
| 95 | else if (!strcasecmp(values[1], "true")) |
|---|
| 96 | limit->quota_per_session = TRUE; |
|---|
| 97 | |
|---|
| 98 | if (!strcasecmp(values[2], "soft")) |
|---|
| 99 | limit->quota_limit_type = SOFT_LIMIT; |
|---|
| 100 | else if (!strcasecmp(values[2], "hard")) |
|---|
| 101 | limit->quota_limit_type = HARD_LIMIT; |
|---|
| 102 | |
|---|
| 103 | limit->bytes_in_avail = atof(values[3]); |
|---|
| 104 | limit->bytes_out_avail = atof(values[4]); |
|---|
| 105 | limit->bytes_xfer_avail = atof(values[5]); |
|---|
| 106 | limit->files_in_avail = atoi(values[6]); |
|---|
| 107 | limit->files_out_avail = atoi(values[7]); |
|---|
| 108 | limit->files_xfer_avail = atoi(values[8]); |
|---|
| 109 | |
|---|
| 110 | return TRUE; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | static unsigned char ldaptab_verify(quota_table_t *ldaptab) { |
|---|
| 114 | |
|---|
| 115 | /* Always TRUE. */ |
|---|
| 116 | return TRUE; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | static quota_table_t *ldaptab_open(pool *parent_pool, quota_tabtype_t tab_type, |
|---|
| 120 | const char *srcinfo) { |
|---|
| 121 | |
|---|
| 122 | pool *tab_pool = make_sub_pool(parent_pool); |
|---|
| 123 | quota_table_t *tab = NULL; |
|---|
| 124 | |
|---|
| 125 | tab = (quota_table_t *) pcalloc(tab_pool, sizeof(quota_table_t)); |
|---|
| 126 | tab->tab_pool = tab_pool; |
|---|
| 127 | tab->tab_type = tab_type; |
|---|
| 128 | |
|---|
| 129 | /* Set all the necessary function pointers. */ |
|---|
| 130 | tab->tab_close = ldaptab_close; |
|---|
| 131 | tab->tab_lookup = ldaptab_lookup; |
|---|
| 132 | tab->tab_verify = ldaptab_verify; |
|---|
| 133 | |
|---|
| 134 | return tab; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | /* Event handlers |
|---|
| 138 | */ |
|---|
| 139 | |
|---|
| 140 | #if defined(PR_SHARED_MODULE) |
|---|
| 141 | static void ldaptab_mod_unload_ev(const void *event_data, void *user_data) { |
|---|
| 142 | if (strcmp("mod_quotatab_ldap.c", (const char *) event_data) == 0) { |
|---|
| 143 | pr_event_unregister("atab_ldap_module, NULL, NULL); |
|---|
| 144 | quotatab_unregister_backend("ldap", QUOTATAB_LIMIT_SRC); |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | #endif /* PR_SHARED_MODULE */ |
|---|
| 148 | |
|---|
| 149 | /* Initialization routines |
|---|
| 150 | */ |
|---|
| 151 | |
|---|
| 152 | static int ldaptab_init(void) { |
|---|
| 153 | quotatab_register_backend("ldap", ldaptab_open, QUOTATAB_LIMIT_SRC); |
|---|
| 154 | |
|---|
| 155 | #if defined(PR_SHARED_MODULE) |
|---|
| 156 | pr_event_register("atab_ldap_module, "core.module-unload", |
|---|
| 157 | ldaptab_mod_unload_ev, NULL); |
|---|
| 158 | #endif /* PR_SHARED_MODULE */ |
|---|
| 159 | |
|---|
| 160 | return 0; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | module quotatab_ldap_module = { |
|---|
| 164 | NULL, NULL, |
|---|
| 165 | |
|---|
| 166 | /* Module API version 2.0 */ |
|---|
| 167 | 0x20, |
|---|
| 168 | |
|---|
| 169 | /* Module name */ |
|---|
| 170 | "quotatab_ldap", |
|---|
| 171 | |
|---|
| 172 | /* Module configuration handler table */ |
|---|
| 173 | NULL, |
|---|
| 174 | |
|---|
| 175 | /* Module command handler table */ |
|---|
| 176 | NULL, |
|---|
| 177 | |
|---|
| 178 | /* Module authentication handler table */ |
|---|
| 179 | NULL, |
|---|
| 180 | |
|---|
| 181 | /* Module initialization function */ |
|---|
| 182 | ldaptab_init, |
|---|
| 183 | |
|---|
| 184 | /* Module child initialization function */ |
|---|
| 185 | NULL |
|---|
| 186 | }; |
|---|