Debian Codegraph @

contrib/mod_quotatab_file.c

raw symbols SCIP
1 /*
2 * ProFTPD: mod_quotatab_file -- a mod_quotatab sub-module for managing quota
3 * data via file-based tables
4 * Copyright (c) 2002-2024 TJ Saunders
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
19 *
20 * As a special exemption, TJ Saunders gives permission to link this program
21 * with OpenSSL, and distribute the resulting executable, without including
22 * the source code for OpenSSL in the source distribution.
23 */
24
25 #include "mod_quotatab.h"
26
27 #include <sys/types.h>
28 #include <sys/uio.h>
29 #include <unistd.h>
30
31 /* bloody lack of consistency... */
32 #if defined(FREEBSD4)
33 # define QUOTATAB_IOV_BASE_TYPE (char *)
34 #elif defined(LINUX)
35 # define QUOTATAB_IOV_BASE_TYPE (__ptr_t)
36 #elif defined(SOLARIS2)
37 # define QUOTATAB_IOV_BASE_TYPE (caddr_t)
38 #else
39 # define QUOTATAB_IOV_BASE_TYPE (void *)
40 #endif
41
42 module quotatab_file_module;
43
44 static const char *trace_channel = "quotatab.file";
45
46 static int filetab_close(quota_table_t *filetab) {
47 int res;
48
49 res = close(filetab->tab_handle);
50 filetab->tab_handle = -1;
51
52 return res;
53 }
54
55 static int filetab_create(quota_table_t *filetab, void *ptr) {
56 int res = -1;
57 struct iovec quotav[8];
58 off_t current_pos = 0;
59 quota_tally_t *tally = ptr;
60
61 /* Use writev() to make this more efficient. It is done piecewise, rather
62 * than doing a normal write(2) directly from the struct pointer, to avoid
63 * alignment/padding issues.
64 */
65
66 quotav[0].iov_base = QUOTATAB_IOV_BASE_TYPE tally->name;
67 quotav[0].iov_len = sizeof(tally->name);
68
69 quotav[1].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->quota_type);
70 quotav[1].iov_len = sizeof(tally->quota_type);
71
72 quotav[2].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_in_used);
73 quotav[2].iov_len = sizeof(tally->bytes_in_used);
74
75 quotav[3].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_out_used);
76 quotav[3].iov_len = sizeof(tally->bytes_out_used);
77
78 quotav[4].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_xfer_used);
79 quotav[4].iov_len = sizeof(tally->bytes_xfer_used);
80
81 quotav[5].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_in_used);
82 quotav[5].iov_len = sizeof(tally->files_in_used);
83
84 quotav[6].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_out_used);
85 quotav[6].iov_len = sizeof(tally->files_out_used);
86
87 quotav[7].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_xfer_used);
88 quotav[7].iov_len = sizeof(tally->files_xfer_used);
89
90 /* Seek to the end of the table */
91 current_pos = lseek(filetab->tab_handle, (off_t) 0, SEEK_END);
92 if (current_pos < 0) {
93 return -1;
94 }
95
96 while ((res = writev(filetab->tab_handle, quotav, 8)) < 0) {
97 if (errno == EINTR) {
98 pr_signals_handle();
99 continue;
100 }
101
102 return -1;
103 }
104
105 if (res > 0) {
106 /* Rewind to the start of the entry. */
107 if (lseek(filetab->tab_handle, current_pos, SEEK_SET) < 0) {
108 int xerrno = errno;
109
110 quotatab_log("error rewinding to start of tally entry: %s",
111 strerror(xerrno));
112
113 errno = xerrno;
114 return -1;
115 }
116
117 } else if (res == 0) {
118 /* If no bytes were written, it's an error. */
119
120 quotatab_log("error: writev(2) returned zero when creating tally entry, "
121 "returning EPERM");
122 errno = EPERM;
123 res = -1;
124 }
125
126 return res;
127 }
128
129 static unsigned char filetab_lookup(quota_table_t *filetab, void *ptr,
130 const char *name, quota_type_t quota_type) {
131
132 /* Make sure the table pointer is positioned at the start of the table,
133 * skipping the magic header value of the table.
134 */
135 if (lseek(filetab->tab_handle, (off_t) sizeof(unsigned int), SEEK_SET) < 0) {
136 int xerrno = errno;
137
138 quotatab_log("error seeking past table header: %s", strerror(xerrno));
139 return FALSE;
140 }
141
142 /* Handle tally and limit tables differently... */
143
144 if (filetab->tab_type == TYPE_TALLY) {
145 quota_tally_t *tally = ptr;
146
147 while (filetab->tab_read(filetab, ptr) >= 0) {
148 pr_signals_handle();
149
150 /* Compare quota types. If the quota type is ALL_QUOTA, don't
151 * worry about the name.
152 */
153 if (quota_type == tally->quota_type) {
154 /* Match names if need be */
155 if (name != NULL &&
156 strcmp(name, tally->name) == 0) {
157 return TRUE;
158 }
159
160 if (quota_type == ALL_QUOTA) {
161 return TRUE;
162 }
163 }
164
165 /* Undo the auto-rewind of a single record's length done by
166 * filetab_read(), so that the while loop actually does iterate through
167 * all the available records.
168 */
169 if (lseek(filetab->tab_handle, filetab->tab_quotalen, SEEK_CUR) < 0) {
170 quotatab_log("error seeking past tally record: %s", strerror(errno));
171 }
172 }
173
174 } else if (filetab->tab_type == TYPE_LIMIT) {
175 quota_limit_t *limit = ptr;
176
177 while (filetab->tab_read(filetab, ptr) >= 0) {
178 pr_signals_handle();
179
180 if (quota_type == limit->quota_type) {
181 if (name != NULL &&
182 strcmp(name, limit->name) == 0) {
183 return TRUE;
184 }
185
186 if (quota_type == ALL_QUOTA) {
187 return TRUE;
188 }
189 }
190
191 if (lseek(filetab->tab_handle, filetab->tab_quotalen, SEEK_CUR) < 0) {
192 quotatab_log("error seeking past limit record: %s", strerror(errno));
193 }
194 }
195 }
196
197 /* Default return value */
198 return FALSE;
199 }
200
201 static int filetab_read(quota_table_t *filetab, void *ptr) {
202 int res = -1;
203 struct iovec quotav[10];
204 off_t current_pos;
205
206 /* Mark the current file position. */
207 current_pos = lseek(filetab->tab_handle, (off_t) 0, SEEK_CUR);
208 if (current_pos < 0) {
209 return - 1;
210 }
211
212 /* Use readv() to make this more efficient. It is done piecewise, rather
213 * than doing a normal read(2) directly into the struct pointer, to avoid
214 * alignment/padding issues.
215 */
216
217 /* Handle the limit and tally tables differently. */
218
219 if (filetab->tab_type == TYPE_TALLY) {
220 quota_tally_t *tally = ptr;
221
222 quotav[0].iov_base = QUOTATAB_IOV_BASE_TYPE tally->name;
223 quotav[0].iov_len = sizeof(tally->name);
224
225 quotav[1].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->quota_type);
226 quotav[1].iov_len = sizeof(tally->quota_type);
227
228 quotav[2].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_in_used);
229 quotav[2].iov_len = sizeof(tally->bytes_in_used);
230
231 quotav[3].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_out_used);
232 quotav[3].iov_len = sizeof(tally->bytes_out_used);
233
234 quotav[4].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_xfer_used);
235 quotav[4].iov_len = sizeof(tally->bytes_xfer_used);
236
237 quotav[5].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_in_used);
238 quotav[5].iov_len = sizeof(tally->files_in_used);
239
240 quotav[6].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_out_used);
241 quotav[6].iov_len = sizeof(tally->files_out_used);
242
243 quotav[7].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_xfer_used);
244 quotav[7].iov_len = sizeof(tally->files_xfer_used);
245
246 while ((res = readv(filetab->tab_handle, quotav, 8)) < 0) {
247 if (errno == EINTR) {
248 pr_signals_handle();
249 continue;
250 }
251
252 return -1;
253 }
254
255 if (res > 0) {
256 /* Always rewind after reading a record. */
257 if (lseek(filetab->tab_handle, current_pos, SEEK_SET) < 0) {
258 int xerrno = errno;
259
260 quotatab_log("error rewinding to start of tally entry: %s",
261 strerror(xerrno));
262
263 errno = xerrno;
264 return -1;
265 }
266
267 } else if (res == 0) {
268 /* Assume end-of-file. */
269 errno = EOF;
270 res = -1;
271 }
272
273 return res;
274
275 } else if (filetab->tab_type == TYPE_LIMIT) {
276 quota_limit_t *limit = ptr;
277
278 quotav[0].iov_base = QUOTATAB_IOV_BASE_TYPE limit->name;
279 quotav[0].iov_len = sizeof(limit->name);
280
281 quotav[1].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->quota_type);
282 quotav[1].iov_len = sizeof(limit->quota_type);
283
284 quotav[2].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->quota_per_session);
285 quotav[2].iov_len = sizeof(limit->quota_per_session);
286
287 quotav[3].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->quota_limit_type);
288 quotav[3].iov_len = sizeof(limit->quota_limit_type);
289
290 quotav[4].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->bytes_in_avail);
291 quotav[4].iov_len = sizeof(limit->bytes_in_avail);
292
293 quotav[5].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->bytes_out_avail);
294 quotav[5].iov_len = sizeof(limit->bytes_out_avail);
295
296 quotav[6].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->bytes_xfer_avail);
297 quotav[6].iov_len = sizeof(limit->bytes_xfer_avail);
298
299 quotav[7].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->files_in_avail);
300 quotav[7].iov_len = sizeof(limit->files_in_avail);
301
302 quotav[8].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->files_out_avail);
303 quotav[8].iov_len = sizeof(limit->files_out_avail);
304
305 quotav[9].iov_base = QUOTATAB_IOV_BASE_TYPE &(limit->files_xfer_avail);
306 quotav[9].iov_len = sizeof(limit->files_xfer_avail);
307
308 while ((res = readv(filetab->tab_handle, quotav, 10)) < 0) {
309 if (errno == EINTR) {
310 pr_signals_handle();
311 continue;
312 }
313
314 return -1;
315 }
316
317 if (res > 0) {
318 /* Always rewind after reading a record. */
319 if (lseek(filetab->tab_handle, current_pos, SEEK_SET) < 0) {
320 int xerrno = errno;
321
322 quotatab_log("error rewinding to start of limit entry: %s",
323 strerror(xerrno));
324
325 errno = xerrno;
326 return -1;
327 }
328
329 } else if (res == 0) {
330 /* Assume end-of-file. */
331 errno = EOF;
332 res = -1;
333 }
334
335 return res;
336 }
337
338 /* default */
339 errno = EINVAL;
340 return -1;
341 }
342
343 static unsigned char filetab_verify(quota_table_t *filetab) {
344 unsigned int magic = 0L;
345
346 /* Make sure we are positioned at the start of the table. */
347 if (lseek(filetab->tab_handle, (off_t) 0, SEEK_SET) < 0) {
348 quotatab_log("error seeking to start of table: %s", strerror(errno));
349 return FALSE;
350 }
351
352 /* Check the header of this table, to make sure it's valid. */
353 if (read(filetab->tab_handle, &magic, sizeof(magic)) != sizeof(magic)) {
354 return FALSE;
355 }
356
357 if (magic == filetab->tab_magic) {
358 return TRUE;
359 }
360
361 return FALSE;
362 }
363
364 static int filetab_write(quota_table_t *filetab, void *ptr) {
365 int res = -1;
366 struct iovec quotav[8];
367 quota_tally_t *tally = ptr;
368 off_t current_pos;
369
370 /* Mark the current file position. */
371 current_pos = lseek(filetab->tab_handle, (off_t) 0, SEEK_CUR);
372 if (current_pos < 0) {
373 return -1;
374 }
375
376 /* Use writev() to make this more efficient. It is done piecewise, rather
377 * than doing a normal write(2) directly from the struct pointer, to avoid
378 * alignment/padding issues.
379 */
380
381 quotav[0].iov_base = QUOTATAB_IOV_BASE_TYPE tally->name;
382 quotav[0].iov_len = sizeof(tally->name);
383
384 quotav[1].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->quota_type);
385 quotav[1].iov_len = sizeof(tally->quota_type);
386
387 quotav[2].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_in_used);
388 quotav[2].iov_len = sizeof(tally->bytes_in_used);
389
390 quotav[3].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_out_used);
391 quotav[3].iov_len = sizeof(tally->bytes_out_used);
392
393 quotav[4].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->bytes_xfer_used);
394 quotav[4].iov_len = sizeof(tally->bytes_xfer_used);
395
396 quotav[5].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_in_used);
397 quotav[5].iov_len = sizeof(tally->files_in_used);
398
399 quotav[6].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_out_used);
400 quotav[6].iov_len = sizeof(tally->files_out_used);
401
402 quotav[7].iov_base = QUOTATAB_IOV_BASE_TYPE &(tally->files_xfer_used);
403 quotav[7].iov_len = sizeof(tally->files_xfer_used);
404
405 while ((res = writev(filetab->tab_handle, quotav, 8)) < 0) {
406 if (errno == EINTR) {
407 pr_signals_handle();
408 continue;
409 }
410
411 return -1;
412 }
413
414 if (res > 0) {
415 /* Rewind to the start of the entry. */
416 if (lseek(filetab->tab_handle, current_pos, SEEK_SET) < 0) {
417 int xerrno = errno;
418
419 quotatab_log("error rewinding to start of tally entry: %s",
420 strerror(xerrno));
421
422 errno = xerrno;
423 return -1;
424 }
425
426 } else if (res == 0) {
427 /* If no bytes were written, it's an error. */
428
429 quotatab_log("error: writev(2) returned zero when updating tally entry, "
430 "returning EPERM");
431 errno = EPERM;
432 res = -1;
433 }
434
435 return res;
436 }
437
438 static int filetab_rlock(quota_table_t *filetab) {
439 filetab->tab_lock.l_type = F_RDLCK;
440 return fcntl(filetab->tab_handle, F_SETLK, &filetab->tab_lock);
441 }
442
443 static int filetab_unlock(quota_table_t *filetab) {
444 filetab->tab_lock.l_type = F_UNLCK;
445 return fcntl(filetab->tab_handle, F_SETLK, &filetab->tab_lock);
446 }
447
448 static int filetab_wlock(quota_table_t *filetab) {
449 filetab->tab_lock.l_type = F_WRLCK;
450 return fcntl(filetab->tab_handle, F_SETLK, &filetab->tab_lock);
451 }
452
453 static quota_table_t *filetab_open(pool *parent_pool,
454 quota_tabtype_t tab_type, const char *srcinfo) {
455 quota_table_t *tab = NULL;
456 pool *tab_pool = make_sub_pool(parent_pool);
457
458 tab = (quota_table_t *) pcalloc(tab_pool, sizeof(quota_table_t));
459 tab->tab_pool = tab_pool;
460 tab->tab_type = tab_type;
461
462 if (tab->tab_type == TYPE_TALLY) {
463 /* File-based tally table magic number */
464 tab->tab_magic = 0x07644;
465
466 /* File-based tally record length, manually defined to avoid alignment/
467 * padding issues when using sizeof().
468 */
469 tab->tab_quotalen = 121;
470
471 tab->tab_lock.l_whence = SEEK_CUR;
472 tab->tab_lock.l_start = 0;
473 tab->tab_lock.l_len = tab->tab_quotalen;
474
475 /* Open the table handle */
476 tab->tab_handle = open(srcinfo, O_RDWR);
477 if (tab->tab_handle < 0) {
478 int xerrno = errno;
479
480 pr_trace_msg(trace_channel, 7, "error opening tally table '%s': %s",
481 srcinfo, strerror(xerrno));
482
483 destroy_pool(tab->tab_pool);
484 errno = xerrno;
485 return NULL;
486 }
487
488 } else if (tab->tab_type == TYPE_LIMIT) {
489 /* File-based limit table magic number */
490 tab->tab_magic = 0x07626;
491
492 /* File-based limit record length, manually defined to avoid alignment/
493 * padding issues when using sizeof().
494 */
495 tab->tab_quotalen = 126;
496 tab->tab_lock.l_whence = SEEK_CUR;
497 tab->tab_lock.l_start = 0;
498 tab->tab_lock.l_len = tab->tab_quotalen;
499
500 /* Open the table handle */
501 tab->tab_handle = open(srcinfo, O_RDONLY);
502 if (tab->tab_handle < 0) {
503 int xerrno = errno;
504
505 pr_trace_msg(trace_channel, 7, "error opening limit table '%s': %s",
506 srcinfo, strerror(xerrno));
507
508 destroy_pool(tab->tab_pool);
509 errno = xerrno;
510 return NULL;
511 }
512 }
513
514 /* Set all the necessary function pointers. */
515 tab->tab_close = filetab_close;
516 tab->tab_create = filetab_create;
517 tab->tab_lookup = filetab_lookup;
518 tab->tab_read = filetab_read;
519 tab->tab_verify = filetab_verify;
520 tab->tab_write = filetab_write;
521
522 tab->tab_rlock = filetab_rlock;
523 tab->tab_unlock = filetab_unlock;
524 tab->tab_wlock = filetab_wlock;
525
526 return tab;
527 }
528
529 /* Event handlers
530 */
531
532 #if defined(PR_SHARED_MODULE)
533 static void filetab_mod_unload_ev(const void *event_data, void *user_data) {
534 if (strcmp("mod_quotatab_file.c", (const char *) event_data) == 0) {
535 pr_event_unregister(&quotatab_file_module, NULL, NULL);
536 quotatab_unregister_backend("file", QUOTATAB_LIMIT_SRC|QUOTATAB_TALLY_SRC);
537 }
538 }
539 #endif /* PR_SHARED_MODULE */
540
541 /* Initialization routines
542 */
543
544 static int filetab_init(void) {
545
546 /* Initialize the quota source objects for type "file".
547 */
548 quotatab_register_backend("file", filetab_open,
549 QUOTATAB_LIMIT_SRC|QUOTATAB_TALLY_SRC);
550
551 #if defined(PR_SHARED_MODULE)
552 pr_event_register(&quotatab_file_module, "core.module-unload",
553 filetab_mod_unload_ev, NULL);
554 #endif /* PR_SHARED_MODULE */
555
556 return 0;
557 }
558
559 module quotatab_file_module = {
560 NULL, NULL,
561
562 /* Module API version 2.0 */
563 0x20,
564
565 /* Module name */
566 "quotatab_file",
567
568 /* Module configuration handler table */
569 NULL,
570
571 /* Module command handler table */
572 NULL,
573
574 /* Module authentication handler table */
575 NULL,
576
577 /* Module initialization function */
578 filetab_init,
579
580 /* Module child initialization function */
581 NULL
582 };