Debian Codegraph @

contrib/mod_quotatab_ldap.c

raw symbols SCIP
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-2025 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 ldap_cmdtab = pr_stash_get_symbol2(PR_SYM_HOOK, "ldap_quota_lookup", NULL,
55 NULL, NULL);
56 if (ldap_cmdtab == NULL) {
57 quotatab_log("error: unable to find LDAP hook symbol 'ldap_quota_lookup'");
58 return FALSE;
59 }
60
61 /* Allocate a temporary pool for the duration of this lookup. */
62 tmp_pool = make_sub_pool(ldaptab->tab_pool);
63
64 /* Prepare the command and call the handler. */
65 ldap_cmd = pr_cmd_alloc(tmp_pool, 1, name);
66 ldap_res = pr_module_call(ldap_cmdtab->m, ldap_cmdtab->handler, ldap_cmd);
67
68 /* Check the results. */
69 if (!ldap_res || MODRET_ISERROR(ldap_res)) {
70 quotatab_log("error performing LDAP search");
71
72 destroy_pool(tmp_pool);
73 return FALSE;
74 }
75
76 ldap_data = (array_header *) ldap_res->data;
77 if (ldap_data->nelts != 9) {
78 quotatab_log("LDAP search returned wrong number of elements");
79
80 destroy_pool(tmp_pool);
81 return FALSE;
82 }
83
84 values = (char **) ldap_data->elts;
85
86 /* Retrieve the limit record (9 values):
87 * name
88 * per_session
89 * limit_type
90 * bytes_{in,out,xfer}_avail
91 * files_{in,out,xfer}_avail
92 */
93
94 sstrncpy(limit->name, values[0], sizeof(limit->name)-1);
95 limit->quota_type = USER_QUOTA;
96
97 if (!strcasecmp(values[1], "false"))
98 limit->quota_per_session = FALSE;
99 else if (!strcasecmp(values[1], "true"))
100 limit->quota_per_session = TRUE;
101
102 if (!strcasecmp(values[2], "soft"))
103 limit->quota_limit_type = SOFT_LIMIT;
104 else if (!strcasecmp(values[2], "hard"))
105 limit->quota_limit_type = HARD_LIMIT;
106
107 limit->bytes_in_avail = atof(values[3]);
108 limit->bytes_out_avail = atof(values[4]);
109 limit->bytes_xfer_avail = atof(values[5]);
110 limit->files_in_avail = atoi(values[6]);
111 limit->files_out_avail = atoi(values[7]);
112 limit->files_xfer_avail = atoi(values[8]);
113
114 destroy_pool(tmp_pool);
115 return TRUE;
116 }
117
118 static unsigned char ldaptab_verify(quota_table_t *ldaptab) {
119
120 /* Always TRUE. */
121 return TRUE;
122 }
123
124 static quota_table_t *ldaptab_open(pool *parent_pool, quota_tabtype_t tab_type,
125 const char *srcinfo) {
126
127 pool *tab_pool = make_sub_pool(parent_pool);
128 quota_table_t *tab = NULL;
129
130 tab = (quota_table_t *) pcalloc(tab_pool, sizeof(quota_table_t));
131 tab->tab_pool = tab_pool;
132 tab->tab_type = tab_type;
133
134 /* Set all the necessary function pointers. */
135 tab->tab_close = ldaptab_close;
136 tab->tab_lookup = ldaptab_lookup;
137 tab->tab_verify = ldaptab_verify;
138
139 return tab;
140 }
141
142 /* Event handlers
143 */
144
145 #if defined(PR_SHARED_MODULE)
146 static void ldaptab_mod_unload_ev(const void *event_data, void *user_data) {
147 if (strcmp("mod_quotatab_ldap.c", (const char *) event_data) == 0) {
148 pr_event_unregister(&quotatab_ldap_module, NULL, NULL);
149 quotatab_unregister_backend("ldap", QUOTATAB_LIMIT_SRC);
150 }
151 }
152 #endif /* PR_SHARED_MODULE */
153
154 /* Initialization routines
155 */
156
157 static int ldaptab_init(void) {
158 quotatab_register_backend("ldap", ldaptab_open, QUOTATAB_LIMIT_SRC);
159
160 #if defined(PR_SHARED_MODULE)
161 pr_event_register(&quotatab_ldap_module, "core.module-unload",
162 ldaptab_mod_unload_ev, NULL);
163 #endif /* PR_SHARED_MODULE */
164
165 return 0;
166 }
167
168 module quotatab_ldap_module = {
169 NULL, NULL,
170
171 /* Module API version 2.0 */
172 0x20,
173
174 /* Module name */
175 "quotatab_ldap",
176
177 /* Module configuration handler table */
178 NULL,
179
180 /* Module command handler table */
181 NULL,
182
183 /* Module authentication handler table */
184 NULL,
185
186 /* Module initialization function */
187 ldaptab_init,
188
189 /* Module child initialization function */
190 NULL
191 };