-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_counter.c
1318 lines (1032 loc) · 33.2 KB
/
mod_counter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ProFTPD: mod_counter -- a module for using counters to enforce per-file usage
* Copyright (c) 2004-2018 TJ Saunders
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* As a special exemption, TJ Saunders and other respective copyright holders
* give permission to link this program with OpenSSL, and distribute the
* resulting executable, without including the source code for OpenSSL in the
* source distribution.
*
* This is mod_counter, contrib software for proftpd 1.3.x and above.
* For more information contact TJ Saunders <tj@castaglia.org>.
*/
#include "conf.h"
#include "privs.h"
#include "mod_ctrls.h"
#include <sys/ipc.h>
#include <sys/sem.h>
#define MOD_COUNTER_VERSION "mod_counter/0.6.2"
#if PROFTPD_VERSION_NUMBER < 0x0001030201
# error "ProFTPD 1.3.2rc1 or later required"
#endif
#define COUNTER_PROJ_ID 247
#define COUNTER_NSEMS 3
#define COUNTER_READER_SEMNO 0
#define COUNTER_WRITER_SEMNO 1
#define COUNTER_NPROCS_SEMNO 2
#define COUNTER_DEFAULT_MAX_READERS 0
#define COUNTER_DEFAULT_MAX_WRITERS 1
module counter_module;
struct counter_fh {
struct counter_fh *next, *prev;
const char *area;
size_t arealen;
int isglob;
pr_fh_t *fh;
};
static pool *counter_pool = NULL;
static xaset_t *counter_fhs = NULL;
static const char *counter_chroot_path = NULL;
static const char *counter_curr_path = NULL;
static int counter_curr_semid = -1;
static int counter_engine = FALSE;
static int counter_max_readers = COUNTER_DEFAULT_MAX_READERS;
static int counter_max_writers = COUNTER_DEFAULT_MAX_WRITERS;
static int counter_logfd = -1;
static int counter_pending = 0;
#define COUNTER_HAVE_READER 0x01
#define COUNTER_HAVE_WRITER 0x02
#if (defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED)) || \
defined(DARWIN9) || defined(DARWIN10) || defined(DARWIN11) || \
defined(DARWIN12) || defined(DARWIN13) || defined(DARWIN14) || \
defined(DARWIN15) || defined(DARWIN16) || defined(DARWIN17)
#else
union semun {
int val;
struct semid_ds *buf;
unsigned short int *array;
struct seminfo *__buf;
};
#endif
#ifndef HAVE_FLOCK
# define LOCK_SH 1
# define LOCK_EX 2
# define LOCK_UN 8
# define LOCK_NB 4
#endif /* HAVE_FLOCK */
static int counter_file_lock(pr_fh_t *, int);
static array_header *counter_file_read(pr_fh_t *);
static int counter_file_write(pr_fh_t *, array_header *);
static int counter_set_procs(int);
static int counter_set_readers(int);
static int counter_set_writers(int);
/* Support routines
*/
static int counter_add_reader(int semid) {
struct sembuf s[2];
s[0].sem_num = COUNTER_READER_SEMNO;
s[0].sem_op = -1;
s[0].sem_flg = IPC_NOWAIT|SEM_UNDO;
s[1].sem_num = COUNTER_NPROCS_SEMNO;
s[1].sem_op = -1;
s[1].sem_flg = IPC_NOWAIT|SEM_UNDO;
return semop(semid, s, 2);
}
static int counter_add_writer(int semid) {
struct sembuf s[2];
s[0].sem_num = COUNTER_WRITER_SEMNO;
s[0].sem_op = -1;
s[0].sem_flg = IPC_NOWAIT|SEM_UNDO;
s[1].sem_num = COUNTER_NPROCS_SEMNO;
s[1].sem_op = -1;
s[1].sem_flg = IPC_NOWAIT|SEM_UNDO;
return semop(semid, s, 2);
}
static int counter_file_add_id(pr_fh_t *fh, int semid) {
int res;
array_header *ids;
if (counter_file_lock(fh, LOCK_EX) < 0) {
return -1;
}
ids = counter_file_read(fh);
if (ids == NULL) {
int xerrno = errno;
counter_file_lock(fh, LOCK_UN);
errno = xerrno;
return -1;
}
*((int *) push_array(ids)) = semid;
res = counter_file_write(fh, ids);
counter_file_lock(fh, LOCK_UN);
return res;
}
static int counter_file_lock(pr_fh_t *fh, int op) {
static int counter_have_lock = FALSE;
#ifdef HAVE_FLOCK
int res;
#else
int flag;
struct flock lock;
#endif /* HAVE_FLOCK */
if (counter_have_lock &&
((op & LOCK_SH) || (op & LOCK_EX))) {
return 0;
}
if (!counter_have_lock &&
(op & LOCK_UN)) {
return 0;
}
#ifdef HAVE_FLOCK
res = flock(fh->fh_fd, op);
if (res == 0) {
if ((op & LOCK_SH) ||
(op & LOCK_EX)) {
counter_have_lock = TRUE;
} else if (op & LOCK_UN) {
counter_have_lock = FALSE;
}
}
return res;
#else
flag = F_SETLKW;
lock.l_whence = 0;
lock.l_start = lock.l_len = 0;
if (op & LOCK_SH) {
lock.l_type = F_RDLCK;
} else if (op & LOCK_EX) {
lock.l_type = F_WRLCK;
} else if (op & LOCK_UN) {
lock.l_type= F_UNLCK;
} else {
errno = EINVAL;
return -1;
}
if (op & LOCK_NB) {
flag = F_SETLK;
}
while (fcntl(fh->fh_fd, flag, &lock) < 0) {
if (errno == EINTR) {
pr_signals_handle();
continue;
}
return -1;
}
if ((op & LOCK_SH) ||
(op & LOCK_EX)) {
counter_have_lock = TRUE;
} else if (op & LOCK_UN) {
counter_have_lock = FALSE;
}
return 0;
#endif /* HAVE_FLOCK */
}
static array_header *counter_file_read(pr_fh_t *fh) {
char buf[PR_TUNABLE_BUFFER_SIZE];
array_header *ids = make_array(counter_pool, 0, sizeof(int));
/* Read the list of IDs in the CounterFile into an array. */
if (counter_file_lock(fh, LOCK_SH) < 0) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"error read-locking CounterFile '%s': %s", fh->fh_path, strerror(errno));
}
if (pr_fsio_lseek(fh, 0, SEEK_SET) < 0) {
int xerrno = errno;
counter_file_lock(fh, LOCK_UN);
errno = xerrno;
return NULL;
}
memset(buf, '\0', sizeof(buf));
while (pr_fsio_gets(buf, sizeof(buf), fh) != NULL) {
int id;
pr_signals_handle();
id = atoi(buf);
if (id < 0) {
continue;
}
*((int *) push_array(ids)) = id;
}
if (counter_file_lock(fh, LOCK_UN) < 0) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"error unlocking CounterFile '%s': %s", fh->fh_path, strerror(errno));
}
return ids;
}
static int counter_file_remove_id(pr_fh_t *fh, int semid) {
register unsigned int i;
int res;
array_header *ids;
int *semids;
if (counter_file_lock(fh, LOCK_EX) < 0) {
return -1;
}
ids = counter_file_read(fh);
if (ids == NULL) {
int xerrno = errno;
counter_file_lock(fh, LOCK_UN);
errno = xerrno;
return -1;
}
semids = (int *) ids->elts;
for (i = 0; i < ids->nelts; i++) {
if (semids[i] == semid) {
semids[i] = -1;
break;
}
}
res = counter_file_write(fh, ids);
counter_file_lock(fh, LOCK_UN);
return res;
}
static int counter_file_write(pr_fh_t *fh, array_header *ids) {
register unsigned int i;
int *elts;
/* Write the list of IDs in the given array to the CounterFile,
* overwriting any previous values.
*/
if (counter_file_lock(fh, LOCK_EX) < 0) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"error write-locking CounterFile '%s': %s", fh->fh_path, strerror(errno));
}
if (pr_fsio_lseek(fh, 0, SEEK_SET) < 0) {
int xerrno = errno;
counter_file_lock(fh, LOCK_UN);
errno = xerrno;
return -1;
}
elts = (int *) ids->elts;
for (i = 0; i < ids->nelts; i++) {
char buf[32];
pr_signals_handle();
/* Skip any negative IDs. This small hack allows for IDs to be
* effectively removed from the list.
*/
if (elts[i] < 0) {
continue;
}
memset(buf, '\0', sizeof(buf));
snprintf(buf, sizeof(buf), "%d\n", elts[i]);
buf[sizeof(buf)-1] = '\0';
buf[strlen(buf)-1] = '\0';
if (pr_fsio_puts(buf, fh) < 0) {
int xerrno = errno;
counter_file_lock(fh, LOCK_UN);
errno = xerrno;
return -1;
}
}
if (pr_fsio_ftruncate(fh, 0) < 0) {
int xerrno = errno;
counter_file_lock(fh, LOCK_UN);
errno = xerrno;
return -1;
}
if (counter_file_lock(fh, LOCK_SH) < 0) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"error unlocking CounterFile '%s': %s", fh->fh_path, strerror(errno));
}
return 0;
}
static char *counter_abs_path(pool *p, const char *path, int interpolate) {
const char *chroot_path;
char *abs_path;
chroot_path = session.chroot_path;
if (counter_chroot_path != NULL) {
session.chroot_path = counter_chroot_path;
}
abs_path = dir_abs_path(p, path, interpolate);
session.chroot_path = chroot_path;
return abs_path;
}
static pr_fh_t *counter_get_fh(pool *p, const char *path) {
struct counter_fh *iter, *cfh = NULL;
const char *abs_path;
/* Find the CounterFile handle to use for the given path, if any. */
if (counter_fhs == NULL) {
errno = ENOENT;
return NULL;
}
if (session.chroot_path) {
abs_path = counter_abs_path(p, path, FALSE);
} else {
abs_path = path;
}
/* In order to handle globs, we do two passes. On the first pass,
* we look for the closest-matching glob area. On the second pass,
* we look for any closest-matching non-glob area. This means that
* exact matches override glob matches (as they should).
*/
for (iter = (struct counter_fh *) counter_fhs->xas_list; iter;
iter = iter->next) {
pr_signals_handle();
if (!iter->isglob) {
continue;
}
if (cfh == NULL) {
/* Haven't found anything matching yet. */
if (pr_fnmatch(iter->area, abs_path, 0) == 0) {
cfh = iter;
}
} else {
/* Have a previous match. Is this a closer matching area? */
if (iter->arealen > cfh->arealen &&
pr_fnmatch(iter->area, abs_path, 0) == 0) {
cfh = iter;
}
}
}
for (iter = (struct counter_fh *) counter_fhs->xas_list; iter;
iter = iter->next) {
if (iter->isglob) {
continue;
}
if (cfh == NULL) {
/* Haven't found anything matching yet. */
if (strncmp(iter->area, abs_path, iter->arealen) == 0) {
cfh = iter;
}
} else {
/* Have a previous match. Is this a closer matching area? */
if (iter->arealen > cfh->arealen &&
strncmp(iter->area, abs_path, iter->arealen) == 0) {
cfh = iter;
}
}
}
if (cfh != NULL) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"using CounterFile '%s' covering area '%s' for path '%s'",
cfh->fh->fh_path, cfh->area, path);
return cfh->fh;
}
errno = ENOENT;
return NULL;
}
/* Use Perl's hashing algorithm by default.
*
* Here's a good article about this hashing algorithm, and about hashing
* functions in general:
*
* http://www.perl.com/pub/2002/10/01/hashes.html
*/
static unsigned int key_hash(const void *key, size_t keysz) {
unsigned int i = 0;
size_t sz = !keysz ? strlen((const char *) key) : keysz;
while (sz--) {
const char *k = key;
unsigned int c;
c = k[sz];
/* Always handle signals in potentially long-running while loops. */
pr_signals_handle();
i = (i * 33) + c;
}
return i;
}
static key_t counter_get_key(const char *path) {
size_t pathlen;
unsigned int h;
key_t key;
/* ftok() uses stat(2) on the given path, which means that it needs to exist.
* So we AVOID using ftok(3), and instead generate the key value ourselves.
*/
pathlen = strlen(path);
h = key_hash(path, pathlen);
key = h & COUNTER_PROJ_ID;
return key;
}
static int counter_get_sem(pr_fh_t *fh, const char *path) {
int semid;
key_t key;
/* Obtain a key for this path. */
key = counter_get_key(path);
if (key == (key_t) -1) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"unable to get key for '%s': %s", path, strerror(errno));
return -1;
}
/* Try first using IPC_CREAT|IPC_EXCL, to check if there is an existing
* semaphore set for this key. If there is, try again, using a flag of
* zero.
*/
semid = semget(key, COUNTER_NSEMS, IPC_CREAT|IPC_EXCL|0666);
if (semid < 0) {
if (errno == EEXIST) {
semid = semget(key, 0, 0);
} else {
return -1;
}
} else {
/* Set the values of the newly created semaphore to the configured
* CounterMaxReaders and CounterMaxWriters.
*/
if (counter_set_readers(semid) < 0) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"error setting readers (semaphore ID %d): %s", semid, strerror(errno));
}
if (counter_set_writers(semid) < 0) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"error setting writers (semaphore ID %d): %s", semid, strerror(errno));
}
if (counter_set_procs(semid) < 0) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"error setting procs (semaphore ID %d): %s", semid, strerror(errno));
}
/* Record the ID of the created semaphore in the CounterFile. */
if (counter_file_add_id(fh, semid) < 0) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"error recording semaphore (semaphore ID %d) in CounterFile '%s': %s",
semid, fh->fh_path, strerror(errno));
}
}
return semid;
}
static int counter_remove_reader(pr_fh_t *fh, int semid) {
struct sembuf s[2];
s[0].sem_num = COUNTER_READER_SEMNO;
s[0].sem_op = 1;
s[0].sem_flg = IPC_NOWAIT|SEM_UNDO;
s[1].sem_num = COUNTER_NPROCS_SEMNO;
s[1].sem_op = 1;
s[1].sem_flg = IPC_NOWAIT|SEM_UNDO;
if (semop(semid, s, 2) < 0) {
return -1;
}
if (semctl(semid, 0, IPC_RMID, s) < 0) {
return -1;
}
return counter_file_remove_id(fh, semid);
}
static int counter_remove_writer(pr_fh_t *fh, int semid) {
struct sembuf s[2];
s[0].sem_num = COUNTER_WRITER_SEMNO;
s[0].sem_op = 1;
s[0].sem_flg = IPC_NOWAIT|SEM_UNDO;
s[1].sem_num = COUNTER_NPROCS_SEMNO;
s[1].sem_op = 1;
s[1].sem_flg = IPC_NOWAIT|SEM_UNDO;
if (semop(semid, s, 2) < 0) {
return -1;
}
if (semctl(semid, 0, IPC_RMID, s) < 0) {
return -1;
}
return counter_file_remove_id(fh, semid);
}
static int counter_set_procs(int semid) {
union semun arg;
arg.val = counter_max_readers + counter_max_writers;
return semctl(semid, COUNTER_NPROCS_SEMNO, SETVAL, arg);
}
static int counter_set_readers(int semid) {
union semun arg;
arg.val = counter_max_readers;
return semctl(semid, COUNTER_READER_SEMNO, SETVAL, arg);
}
static int counter_set_writers(int semid) {
union semun arg;
arg.val = counter_max_writers;
return semctl(semid, COUNTER_WRITER_SEMNO, SETVAL, arg);
}
/* Configuration handlers
*/
/* usage: CounterEngine on|off */
MODRET set_counterengine(cmd_rec *cmd) {
int engine = -1;
config_rec *c;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
engine = get_boolean(cmd, 1);
if (engine == -1) {
CONF_ERROR(cmd, "expected Boolean parameter");
}
c = add_config_param(cmd->argv[0], 1, NULL);
c->argv[0] = pcalloc(c->pool, sizeof(int));
*((int *) c->argv[0]) = engine;
return PR_HANDLED(cmd);
}
/* usage: CounterFile path */
MODRET set_counterfile(cmd_rec *cmd) {
config_rec *c;
const char *path;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON|CONF_DIR);
path = cmd->argv[1];
if (*path != '/') {
CONF_ERROR(cmd, "must be an absolute path");
}
/* In theory, we could open a filehandle on the configured path right
* here, and fail if the file couldn't be created/opened. Then we
* could just stash that filehandle in the cmd_rec. Easy.
*
* However, that would mean that we would have open descriptors for
* vhosts to which the client may not connect. We would also need to
* use pr_fs_get_usable_fd() so that these filehandles don't use the wrong
* fds. Instead, then, we wait to open the filehandles in sess_init(),
* where we know vhost to which the client connected.
*/
c = add_config_param_str(cmd->argv[0], 1, path);
c->flags |= CF_MERGEDOWN;
return PR_HANDLED(cmd);
}
/* usage: CounterLog path|"none" */
MODRET set_counterlog(cmd_rec *cmd) {
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
if (pr_fs_valid_path(cmd->argv[1]) < 0) {
CONF_ERROR(cmd, "must be an absolute path");
}
add_config_param_str(cmd->argv[0], 1, cmd->argv[1]);
return PR_HANDLED(cmd);
}
/* usage:
* CounterMaxReaders max
* CounterMaxWriters max
*/
MODRET set_countermaxreaderswriters(cmd_rec *cmd) {
int count;
config_rec *c;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL|CONF_ANON|CONF_DIR);
/* A count of zero means that an unlimited number of readers (or writers),
* as is the default without this module, is in effect.
*/
count = atoi(cmd->argv[1]);
if (count < 0 ||
count > INT_MAX) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "invalid number: ", cmd->argv[1],
NULL));
}
c = add_config_param(cmd->argv[0], 1, NULL);
c->argv[0] = pcalloc(c->pool, sizeof(int));
*((int *) c->argv[0]) = count;
c->flags |= CF_MERGEDOWN;
return PR_HANDLED(cmd);
}
/* Command handlers
*/
MODRET counter_retr(cmd_rec *cmd) {
config_rec *c;
int res;
pr_fh_t *fh;
const char *path;
if (counter_engine == FALSE) {
return PR_DECLINED(cmd);
}
c = find_config(CURRENT_CONF, CONF_PARAM, "CounterMaxReaders", FALSE);
counter_max_readers = c ? *((int *) c->argv[0]) : COUNTER_DEFAULT_MAX_READERS;
if (counter_max_readers == 0) {
return PR_DECLINED(cmd);
}
path = pr_table_get(cmd->notes, "mod_xfer.retr-path", NULL);
if (path == NULL) {
return PR_DECLINED(cmd);
}
/* Note that for purposes of our semaphores, we need to use the absolute
* path.
*/
counter_curr_path = counter_abs_path(cmd->pool, path, FALSE);
fh = counter_get_fh(cmd->tmp_pool, counter_curr_path);
if (fh == NULL) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"%s: no CounterFile found for path '%s'", (char *) cmd->argv[0],
counter_curr_path);
/* No CounterFile configured/available for this path. */
return PR_DECLINED(cmd);
}
counter_curr_semid = counter_get_sem(fh, counter_curr_path);
if (counter_curr_semid < 0) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"unable to get semaphore for '%s': %s", counter_curr_path,
strerror(errno));
return PR_DECLINED(cmd);
}
/* Add a reader to this file by decrementing the reader counter value.
* This functions as a sort of "lock".
*/
res = counter_add_reader(counter_curr_semid);
if (res < 0 &&
errno == EAGAIN) {
/* The lock acquisition failed, which means the file is busy.
* The download should be failed.
*/
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"%s: max number of readers (%d) reached for '%s'", (char *) cmd->argv[0],
counter_max_readers, counter_curr_path);
pr_response_add_err(R_450, _("%s: File busy"), cmd->arg);
return PR_ERROR(cmd);
}
counter_pending |= COUNTER_HAVE_READER;
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"%s: added reader counter for '%s' (semaphore ID %d)",
(char *) cmd->argv[0], counter_curr_path, counter_curr_semid);
return PR_DECLINED(cmd);
}
/* Handles the DELE, RNFR, and RNTO commands. */
MODRET counter_alter(cmd_rec *cmd) {
config_rec *c;
int res;
pr_fh_t *fh;
const char *path;
if (counter_engine == FALSE) {
return PR_DECLINED(cmd);
}
c = find_config(CURRENT_CONF, CONF_PARAM, "CounterMaxWriters", FALSE);
counter_max_writers = c ? *((int *) c->argv[0]) : COUNTER_DEFAULT_MAX_WRITERS;
if (counter_max_writers == 0) {
return PR_DECLINED(cmd);
}
path = pr_fs_decode_path(cmd->tmp_pool, cmd->arg);
if (!exists((char *) path)) {
return PR_DECLINED(cmd);
}
/* The semaphores operate using dir_best_path(). */
path = dir_best_path(cmd->tmp_pool, path);
if (path == NULL) {
return PR_DECLINED(cmd);
}
counter_curr_path = path;
fh = counter_get_fh(cmd->tmp_pool, counter_curr_path);
if (fh == NULL) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"%s: no CounterFile found for path '%s'", (char *) cmd->argv[0],
counter_curr_path);
/* No CounterFile configured/available for this path. */
return PR_DECLINED(cmd);
}
counter_curr_semid = counter_get_sem(fh, counter_curr_path);
if (counter_curr_semid < 0) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"unable to get semaphore for '%s': %s", counter_curr_path,
strerror(errno));
return PR_DECLINED(cmd);
}
/* Add a writer to this file by decrementing the writer counter value.
* This functions as a sort of "lock".
*/
res = counter_add_writer(counter_curr_semid);
if (res < 0 &&
errno == EAGAIN) {
/* The lock acquisition failed, which means the file is busy.
* The upload should be failed.
*/
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"%s: max number of writers (%d) reached for '%s'", (char *) cmd->argv[0],
counter_max_writers, counter_curr_path);
pr_response_add_err(R_450, _("%s: File busy"), cmd->arg);
return PR_ERROR(cmd);
}
counter_pending |= COUNTER_HAVE_WRITER;
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"%s: added writer counter for '%s' (semaphore ID %d)",
(char *) cmd->argv[0], counter_curr_path, counter_curr_semid);
return PR_DECLINED(cmd);
}
MODRET counter_stor(cmd_rec *cmd) {
config_rec *c;
int res;
pr_fh_t *fh;
const char *path;
if (counter_engine == FALSE) {
return PR_DECLINED(cmd);
}
c = find_config(CURRENT_CONF, CONF_PARAM, "CounterMaxWriters", FALSE);
counter_max_writers = c ? *((int *) c->argv[0]) : COUNTER_DEFAULT_MAX_WRITERS;
if (counter_max_writers == 0) {
return PR_DECLINED(cmd);
}
path = pr_table_get(cmd->notes, "mod_xfer.store-path", NULL);
if (path == NULL) {
return PR_DECLINED(cmd);
}
/* Note that for purposes of our semaphores, we need to use the absolute
* path.
*/
counter_curr_path = counter_abs_path(cmd->pool, path, FALSE);
fh = counter_get_fh(cmd->tmp_pool, counter_curr_path);
if (fh == NULL) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"%s: no CounterFile found for path '%s'", (char *) cmd->argv[0],
counter_curr_path);
/* No CounterFile configured/available for this path. */
return PR_DECLINED(cmd);
}
counter_curr_semid = counter_get_sem(fh, counter_curr_path);
if (counter_curr_semid < 0) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"unable to get semaphore for '%s': %s", counter_curr_path,
strerror(errno));
return PR_DECLINED(cmd);
}
/* Add a writer to this file by decrementing the writer counter value.
* This functions as a sort of "lock".
*/
res = counter_add_writer(counter_curr_semid);
if (res < 0 &&
errno == EAGAIN) {
/* The lock acquisition failed, which means the file is busy.
* The upload should be failed.
*/
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"%s: max number of writers (%d) reached for '%s'", (char *) cmd->argv[0],
counter_max_writers, counter_curr_path);
pr_response_add_err(R_450, _("%s: File busy"), cmd->arg);
return PR_ERROR(cmd);
}
counter_pending |= COUNTER_HAVE_WRITER;
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"%s: added writer counter for '%s' (semaphore ID %d)",
(char *) cmd->argv[0], counter_curr_path, counter_curr_semid);
return PR_DECLINED(cmd);
}
MODRET counter_reader_done(cmd_rec *cmd) {
pr_fh_t *fh;
if (counter_engine == FALSE) {
return PR_DECLINED(cmd);
}
if (!(counter_pending & COUNTER_HAVE_READER)) {
return PR_DECLINED(cmd);
}
fh = counter_get_fh(cmd->tmp_pool, counter_curr_path);
if (fh == NULL) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"%s: no CounterFile found for path '%s'", (char *) cmd->argv[0],
counter_curr_path);
/* No CounterFile configured/available for this path. */
return PR_DECLINED(cmd);
}
if (counter_curr_semid == -1) {
counter_curr_semid = counter_get_sem(fh, counter_curr_path);
if (counter_curr_semid < 0) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"unable to get semaphore for '%s': %s", counter_curr_path,
strerror(errno));
return PR_DECLINED(cmd);
}
}
if (counter_remove_reader(fh, counter_curr_semid) < 0) {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"error removing reader for '%s': %s", counter_curr_path,
strerror(errno));
} else {
(void) pr_log_writefile(counter_logfd, MOD_COUNTER_VERSION,
"removed reader counter for '%s' (semaphore ID %d)", counter_curr_path,
counter_curr_semid);
counter_curr_path = NULL;
counter_curr_semid = -1;
counter_pending &= ~COUNTER_HAVE_READER;
}
return PR_DECLINED(cmd);
}
MODRET counter_writer_done(cmd_rec *cmd) {