Debian Codegraph @

contrib/mod_quotatab_radius.c

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