/
pure-pw.c
1667 lines (1569 loc) · 48.5 KB
/
pure-pw.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
#include <config.h>
#include "ftpd.h"
#include "pure-pw.h"
#include "safe_rw.h"
#include "alt_arc4random.h"
#include "../puredb/src/puredb_write.h"
#include "utils.h"
#ifdef HAVE_POSIX_TERMIOS
# include <termios.h>
#elif defined(HAVE_TERMIO_H)
# include <termio.h>
#elif defined(HAVE_SGTTY_H)
# include <sgtty.h>
#endif
#ifndef HAVE_GETOPT_LONG
# include "bsd-getopt_long.h"
#else
# include <getopt.h>
#endif
#ifdef HAVE_LIBSODIUM
# include <sodium.h>
#endif
#ifdef WITH_DMALLOC
# include <dmalloc.h>
#endif
#ifdef PROBE_RANDOM_AT_RUNTIME
static const char *random_device;
#endif
#ifdef crypto_pwhash_MEMLIMIT_MIN
# define MIN_AUTH_MEMORY crypto_pwhash_MEMLIMIT_MIN
#elif defined(crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN)
# define MIN_AUTH_MEMORY crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN
#else
# define MIN_AUTH_MEMORY 8192ULL
#endif
#ifdef crypto_pwhash_OPSLIMIT_MIN
# define MIN_AUTH_OPS crypto_pwhash_OPSLIMIT_MIN
#elif defined(crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN)
# define MIN_AUTH_OPS crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN
#else
# define MIN_AUTH_OPS 64ULL
#endif
#ifndef DEFAULT_TOTAL_AUTH_MEMORY
# define DEFAULT_TOTAL_AUTH_MEMORY (64ULL * 1024ULL * 1024ULL)
# if DEFAULT_TOTAL_AUTH_MEMORY < MIN_AUTH_MEMORY
# undef DEFAULT_TOTAL_AUTH_MEMORY
# define DEFAULT_TOTAL_AUTH_MEMORY MIN_AUTH_MEMORY
# endif
#endif
#ifndef DEFAULT_MAX_CONCURRENT_LOGINS
# define DEFAULT_MAX_CONCURRENT_LOGINS 8UL
#endif
#ifndef DEFAULT_AUTH_TIME_MS
# define DEFAULT_AUTH_TIME_MS 1000
#endif
#ifndef MIN_AUTH_TIME_MS
# define MIN_AUTH_TIME_MS 250
#endif
#ifndef AUTH_CORES
# define AUTH_CORES 8
#endif
static void disable_echo(void)
{
if (!isatty(0)) {
return;
}
#ifdef ECHO
# ifdef HAVE_POSIX_TERMIOS
{
struct termios p;
if (tcgetattr(0, &p) != 0) {
return;
}
p.c_lflag &= ~ECHO;
# if defined(TCSASOFT) && defined(TCSAFLUSH)
tcsetattr(0, TCSASOFT | TCSAFLUSH, &p);
# elif defined(TCSAFLUSH)
tcsetattr(0, TCSAFLUSH, &p);
# endif
}
# elif defined(HAVE_TERMIO_H) && defined(TCGETA)
{
struct termio tty;
if (ioctl(0, TCGETA, &tty) != 0) {
return;
}
tty.c_lflag &= ~ECHO;
ioctl(0, TCSETA, &tty);
}
# else
{
struct sgttyb tty;
if (ioctl(0, TIOCGETP, &tty) != 0) {
return;
}
tty.sg_flags &= ~ECHO;
ioctl(0, TIOCSETN, &tty);
}
# endif
#endif
}
static void enable_echo(void)
{
if (!isatty(0)) {
return;
}
#ifdef ECHO
# ifdef HAVE_POSIX_TERMIOS
{
struct termios p;
if (tcgetattr(0, &p) != 0) {
return;
}
p.c_lflag |= ECHO;
# if defined(TCSASOFT) && defined(TCSAFLUSH)
tcsetattr(0, TCSASOFT | TCSAFLUSH, &p);
# elif defined(TCSAFLUSH)
tcsetattr(0, TCSAFLUSH, &p);
# endif
tcsetattr(0, TCSAFLUSH, &p);
}
# elif defined(HAVE_TERMIO_H) && defined(TCGETA)
{
struct termio tty;
if (ioctl(0, TCGETA, &tty) != 0) {
return;
}
tty.c_lflag |= ECHO;
ioctl(0, TCSETA, &tty);
}
# else
{
struct sgttyb tty;
if (ioctl(0, TIOCGETP, &tty) != 0) {
return;
}
tty.sg_flags |= ECHO;
ioctl(0, TIOCSETN, &tty);
}
# endif
#endif
}
/*
* The difference between this strtok() and the libc's one is that
* this one doesn't skip empty fields, and takes a char instead of a
* string as a delimiter.
* This strtok2() variant leaves zeroes.
*/
static char *my_strtok2(char *str, const char delim)
{
static char *s;
static char save;
if (str != NULL) {
if (*str == 0) {
return NULL;
}
s = str;
scan:
while (*s != 0 && *s != delim) {
s++;
}
save = *s;
*s = 0;
return str;
}
if (s == NULL || save == 0) {
return NULL;
}
s++;
str = s;
goto scan;
}
static void filter_pw_line_sep(char *str)
{
if (str == NULL) {
return;
}
while (*str != 0) {
if (*str == *PW_LINE_SEP) {
*str = '_';
}
str++;
}
}
static void help(void)
{
puts("\nUsage :\n\n"
"pure-pw useradd <login> [-f <passwd file>] -u <uid> [-g <gid>]\n"
" -D/-d <home directory> [-c <gecos>]\n"
" [-t <download bandwidth>] [-T <upload bandwidth>]\n"
" [-n <max number of files>] [-N <max Mbytes>]\n"
" [-q <upload ratio>] [-Q <download ratio>]\n"
" [-r <allow client ip>/<mask>] [-R <deny client ip>/<mask>]\n"
" [-i <allow local ip>/<mask>] [-I <deny local ip>/<mask>]\n"
" [-y <max number of concurrent sessions>]\n"
" [-C <max number of concurrent login attempts>]\n"
" [-M <total memory (in MB) to reserve for password hashing>]\n"
" [-z <hhmm>-<hhmm>] [-m]\n"
"\n"
"pure-pw usermod <login> -f <passwd file> -u <uid> [-g <gid>]\n"
" -D/-d <home directory> -[c <gecos>]\n"
" [-t <download bandwidth>] [-T <upload bandwidth>]\n"
" [-n <max number of files>] [-N <max Mbytes>]\n"
" [-q <upload ratio>] [-Q <download ratio>]\n"
" [-r <allow client ip>/<mask>] [-R <deny client ip>/<mask>]\n"
" [-i <allow local ip>/<mask>] [-I <deny local ip>/<mask>]\n"
" [-y <max number of concurrent sessions>]\n"
" [-C <max number of concurrent login attempts>]\n"
" [-M <total memory (in MB) to reserve for password hashing>]\n"
" [-z <hhmm>-<hhmm>] [-m]\n"
"\n"
"pure-pw userdel <login> [-f <passwd file>] [-m]\n"
"\n"
"pure-pw passwd <login> [-f <passwd file>] [-m]\n"
"\n"
"pure-pw show <login> [-f <passwd file>]\n"
"\n"
"pure-pw mkdb [<puredb database file> [-f <passwd file>]]\n"
" [-F <puredb file>]\n"
"\n"
"pure-pw list [-f <passwd file>]\n"
"\n"
"-d <home directory> : chroot user (recommended)\n"
"-D <home directory> : don't chroot user\n"
"-<option> '' : set this option to unlimited\n"
"-m : also update the " DEFAULT_PW_DB " database\n"
"For a 1:10 ratio, use -q 1 -Q 10\n"
"To allow access only between 9 am and 6 pm, use -z 0900-1800\n"
"\n");
#ifndef WITH_PUREDB
puts("*WARNING* : that pure-ftpd server hasn't been compiled with puredb support\n");
#endif
exit(EXIT_SUCCESS);
}
static void no_mem(void)
{
fprintf(stderr, "Out of memory : [%s]\n", strerror(errno));
exit(EXIT_FAILURE);
}
static char *best_crypt(const char * const pwd,
const unsigned long max_concurrent_logins,
const unsigned long long max_auth_memory)
{
#if defined(crypto_pwhash_STRBYTES) || defined(crypto_pwhash_scryptsalsa208sha256_STRBYTES)
# ifndef crypto_pwhash_STRBYTES
# define crypto_pwhash_STRBYTES crypto_pwhash_scryptsalsa208sha256_STRBYTES
# define crypto_pwhash_str crypto_pwhash_scryptsalsa208sha256_str
# endif
static char hash[crypto_pwhash_STRBYTES];
struct timeval tv_start, tv_now;
struct timezone tz;
unsigned long long probe_ops = MIN_AUTH_OPS;
unsigned long long ops = 0ULL;
unsigned long long auth_memory =
max_auth_memory / (unsigned long long) max_concurrent_logins;
unsigned long long elapsed;
unsigned long long auth_time_ms = DEFAULT_AUTH_TIME_MS;
if (max_concurrent_logins > AUTH_CORES) {
auth_time_ms /= (unsigned long long) max_concurrent_logins / AUTH_CORES;
}
if (auth_time_ms < MIN_AUTH_TIME_MS) {
auth_time_ms = MIN_AUTH_TIME_MS;
}
if (auth_memory < MIN_AUTH_MEMORY) {
auth_memory = MIN_AUTH_MEMORY;
}
gettimeofday(&tv_start, &tz);
for (;;) {
if (crypto_pwhash_str(hash, pwd, strlen(pwd),
probe_ops, auth_memory) != 0) {
no_mem();
}
ops += probe_ops;
probe_ops *= 2;
gettimeofday(&tv_now, &tz);
elapsed = (tv_now.tv_sec * 1000ULL + tv_now.tv_usec / 1000ULL) -
(tv_start.tv_sec * 1000ULL + tv_start.tv_usec / 1000ULL);
if (elapsed >= auth_time_ms) {
break;
}
}
if (ops < MIN_AUTH_OPS) {
ops = MIN_AUTH_OPS;
}
if (crypto_pwhash_str(hash, pwd, strlen(pwd), ops, auth_memory) != 0) {
no_mem();
}
return hash;
#else
static const char crcars[64] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
const char *crypted;
if ((crypted = (const char *) /* bcrypt */
crypt("test", "$2a$08$1234567890123456789012")) != NULL &&
strcmp(crypted,
"$2a$08$123456789012345678901uBdmsfIXjJcWQwz1wT/IZrWhimJ6xy6a")
== 0) {
char salt[] = "$2a$10$0000000000000000000000";
int c = 28;
do {
c--;
salt[c] = crcars[alt_arc4random() & 63];
} while (c > 7);
return (char *) crypt(pwd, salt);
} else if ((crypted = (const char *) /* SHA-512 */
crypt("test", "$6$1234567890123456$")) != NULL &&
strcmp(crypted,
"$6$1234567890123456$d.pgKQFaiD8bRiExg5NesbGR/"
"3u51YvxeYaQXPzx4C6oSYREw8VoReiuYZjx0V9OhGVTZF"
"qhc6emAxT1RC5BV.") == 0) {
char salt[] = "$6$0000000000000000";
int c = 18;
do {
c--;
salt[c] = crcars[alt_arc4random() & 63];
} while (c > 3);
return (char *) crypt(pwd, salt);
} else if ((crypted = (const char *) /* MD5 */
crypt("test", "$1$12345678$")) != NULL &&
strcmp(crypted, "$1$12345678$oEitTZYQtRHfNGmsFvTBA/") == 0) {
char salt[] = "$1$00000000";
int c = 10;
do {
c--;
salt[c] = crcars[alt_arc4random() & 63];
} while (c > 3);
return (char *) crypt(pwd, salt);
} else {
fprintf(stderr, "No useable password hashing function found\n"
"Please install libsodium (https://libsodium.org) and recompile pure-ftpd.\n");
exit(EXIT_FAILURE);
}
#endif
}
char *newpasswd_filename(const char * const file)
{
size_t sizeof_file2;
char *file2;
sizeof_file2 = strlen(file) + sizeof NEWPASSWD_SUFFIX;
if ((file2 = malloc(sizeof_file2)) == NULL) {
return NULL;
}
(void) snprintf(file2, sizeof_file2, "%s%s", file, NEWPASSWD_SUFFIX);
return file2;
}
static void strip_lf(char *str)
{
char *f;
if (str == NULL) {
return;
}
if ((f = strchr(str, '\r')) != NULL) {
*f = 0;
}
if ((f = strchr(str, '\n')) != NULL) {
*f = 0;
}
}
static int parse_pw_line(char *line, PWInfo * const pwinfo)
{
pwinfo->login = NULL;
pwinfo->pwd = NULL;
pwinfo->gecos = NULL;
pwinfo->home = NULL;
pwinfo->allow_local_ip = pwinfo->deny_local_ip = NULL;
pwinfo->allow_client_ip = pwinfo->deny_client_ip = NULL;
pwinfo->has_bw_dl = 0;
pwinfo->has_bw_ul = 0;
pwinfo->has_quota_files = 0;
pwinfo->has_quota_size = 0;
pwinfo->has_ul_ratio = 0;
pwinfo->has_dl_ratio = 0;
pwinfo->has_time = 0;
pwinfo->time_begin = pwinfo->time_end = 0U;
pwinfo->uid = (uid_t) 0;
pwinfo->gid = (gid_t) 0;
pwinfo->has_per_user_max = 0;
pwinfo->per_user_max = 0U;
if ((line = my_strtok2(line, *PW_LINE_SEP)) == NULL || *line == 0) { /* account */
return -1;
}
pwinfo->login = line;
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL || *line == 0) { /* pwd */
return -1;
}
pwinfo->pwd = line;
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL || *line == 0) { /* uid */
return -1;
}
pwinfo->uid = (uid_t) strtoul(line, NULL, 10);
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL || *line == 0) { /* gid */
return -1;
}
pwinfo->gid = (gid_t) strtoul(line, NULL, 10);
if (pwinfo->uid <= (uid_t) 0 || pwinfo->gid <= (gid_t) 0) {
return -1;
}
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* gecos */
return -1;
}
pwinfo->gecos = line;
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL || *line == 0) { /* home */
return -1;
}
if (*line != '/') {
return -1;
}
pwinfo->home = line;
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* bw_ul */
return 0;
}
if (*line != 0) {
pwinfo->has_bw_ul = 1;
pwinfo->bw_ul = strtoul(line, NULL, 10);
}
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* bw_dl */
return 0;
}
if (*line != 0) {
pwinfo->has_bw_dl = 1;
pwinfo->bw_dl = strtoul(line, NULL, 10);
}
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* ratio up */
return 0;
}
if (*line != 0) {
pwinfo->ul_ratio = (unsigned int) strtoul(line, NULL, 10);
if (pwinfo->ul_ratio > 0U) {
pwinfo->has_ul_ratio = 1;
}
}
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* ratio down */
return 0;
}
if (*line != 0) {
pwinfo->dl_ratio = (unsigned int) strtoul(line, NULL, 10);
if (pwinfo->dl_ratio > 0U) {
pwinfo->has_dl_ratio = 1;
}
}
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* max cnx */
return 0;
}
if (*line != 0) {
pwinfo->per_user_max = (unsigned int) strtoul(line, NULL, 10);
if (pwinfo->per_user_max > 0U) {
pwinfo->has_per_user_max = 1;
}
}
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* files quota */
return 0;
}
if (*line != 0) {
pwinfo->has_quota_files = 1;
pwinfo->quota_files = strtoull(line, NULL, 10);
}
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* size quota */
return 0;
}
if (*line != 0) {
pwinfo->has_quota_size = 1;
pwinfo->quota_size = strtoull(line, NULL, 10);
}
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* allowed local ip */
return 0;
}
pwinfo->allow_local_ip = line;
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* denied local ip */
return 0;
}
pwinfo->deny_local_ip = line;
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* allowed client ip */
return 0;
}
pwinfo->allow_client_ip = line;
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* denied client ip */
return 0;
}
pwinfo->deny_client_ip = line;
if ((line = my_strtok2(NULL, *PW_LINE_SEP)) == NULL) { /* time */
return 0;
}
if (sscanf(line, "%u-%u", &pwinfo->time_begin, &pwinfo->time_end) == 2 &&
pwinfo->time_begin < 2360 && (pwinfo->time_begin % 100) < 60 &&
pwinfo->time_end < 2360 && (pwinfo->time_end % 100) < 60) {
pwinfo->has_time = 1;
}
return 0;
}
static int fetch_pw_account(const char * const file, PWInfo * const pwinfo,
char * const line, const size_t sizeof_line,
const char * const login)
{
FILE *fp;
int ret = -1;
if (file == NULL || pwinfo == NULL || line == NULL ||
sizeof_line < (size_t) 2U || login == NULL ||
*login == 0) {
fprintf(stderr, "bad arguments to fetch account\n");
return -1;
}
if ((fp = fopen(file, "r")) == NULL) {
perror("Unable to open the passwd file");
return -1;
}
while (fgets(line, (int) sizeof_line - 1U, fp) != NULL) {
strip_lf(line);
if (*line == 0 || *line == PW_LINE_COMMENT) {
continue;
}
if (parse_pw_line(line, pwinfo) != 0) {
fprintf(stderr, "Warning: invalid line [%s]\n", line);
continue;
}
if (strcmp(login, pwinfo->login) != 0) {
continue;
}
ret = 0;
break;
}
fclose(fp);
return ret;
}
static FILE *create_newpasswd(const char * const file,
const char * const file2,
const char * const skip_login,
int error_if_user_exists,
int error_if_not_found)
{
FILE *fp;
FILE *fp2;
int fd2;
int found = 0;
size_t skip_login_len;
char line[LINE_MAX];
fp = fopen(file, "r");
if ((fd2 = open(file2, O_EXCL | O_NOFOLLOW |
O_CREAT | O_WRONLY, (mode_t) 0700)) == -1) {
if (fp != NULL) {
fclose(fp);
}
return NULL;
}
if ((fp2 = fdopen(fd2, "w")) == NULL) {
if (fp != NULL) {
fclose(fp);
}
close(fd2);
return NULL;
}
if (fp != NULL) {
if (skip_login != NULL) {
skip_login_len = strlen(skip_login);
} else {
skip_login_len = (size_t) 0U;
}
while (fgets(line, (int) sizeof line - 1U, fp) != NULL) {
if (skip_login_len > (size_t) 0U) {
if (strncmp(line, skip_login, skip_login_len) == 0 &&
line[skip_login_len] == *PW_LINE_SEP) {
if (error_if_user_exists != 0) {
goto err;
}
found = 1;
continue;
}
}
if (fputs(line, fp2) < 0) {
err:
fclose(fp2);
unlink(file2);
if (fp != NULL) {
fclose(fp);
}
return NULL;
}
}
fflush(fp2);
fsync(fd2);
}
if (error_if_not_found != 0 && found == 0) {
goto err;
}
if (fp != NULL) {
fclose(fp);
}
return fp2;
}
static int add_new_pw_line(FILE * const fp2, const PWInfo * const pwinfo)
{
if (fp2 == NULL) {
return -1;
}
if (fprintf(fp2,
"%s" PW_LINE_SEP /* account */
"%s" PW_LINE_SEP /* password */
"%lu" PW_LINE_SEP /* uid */
"%lu" PW_LINE_SEP /* gid */
"%s" PW_LINE_SEP /* gecos */
"%s" PW_LINE_SEP /* home */
, pwinfo->login, pwinfo->pwd,
(unsigned long) pwinfo->uid, (unsigned long) pwinfo->gid,
pwinfo->gecos, pwinfo->home) < 0) {
return -1;
}
if (pwinfo->has_bw_ul != 0) {
if (fprintf(fp2, "%lu", /* bw_ul */
(unsigned long) pwinfo->bw_ul) < 0) {
return -1;
}
}
if (fprintf(fp2, PW_LINE_SEP) < 0) {
return -1;
}
if (pwinfo->has_bw_dl != 0) {
if (fprintf(fp2, "%lu", /* bw_dl */
(unsigned long) pwinfo->bw_dl) < 0) {
return -1;
}
}
if (fprintf(fp2, PW_LINE_SEP) < 0) {
return -1;
}
if (pwinfo->has_ul_ratio != 0) {
if (fprintf(fp2, "%u", /* ratio up */
pwinfo->ul_ratio) < 0) {
return -1;
}
}
if (fprintf(fp2, PW_LINE_SEP) < 0) {
return -1;
}
if (pwinfo->has_dl_ratio != 0) {
if (fprintf(fp2, "%u", /* ratio down */
pwinfo->dl_ratio) < 0) {
return -1;
}
}
if (fprintf(fp2, PW_LINE_SEP) < 0) {
return -1;
}
if (pwinfo->has_per_user_max != 0) {
if (fprintf(fp2, "%u", pwinfo->per_user_max) < 0) {
return -1;
}
}
if (fprintf(fp2, PW_LINE_SEP) < 0) {
return -1;
}
if (pwinfo->has_quota_files != 0) {
if (fprintf(fp2, "%llu", /* files quota */
pwinfo->quota_files) < 0) {
return -1;
}
}
if (fprintf(fp2, PW_LINE_SEP) < 0) {
return -1;
}
if (pwinfo->has_quota_size != 0) {
if (fprintf(fp2, "%llu", /* size quota */
pwinfo->quota_size) < 0) {
return -1;
}
}
if (fprintf(fp2, PW_LINE_SEP) < 0) {
return -1;
}
if (pwinfo->allow_local_ip != NULL) {
fprintf(fp2, "%s", pwinfo->allow_local_ip); /* allowed local ip */
}
if (fprintf(fp2, PW_LINE_SEP) < 0) {
return -1;
}
if (pwinfo->deny_local_ip != NULL) {
fprintf(fp2, "%s", pwinfo->deny_local_ip); /* denied local ip */
}
if (fprintf(fp2, PW_LINE_SEP) < 0) {
return -1;
}
if (pwinfo->allow_client_ip != NULL) {
fprintf(fp2, "%s", pwinfo->allow_client_ip); /* allowed client ip */
}
if (fprintf(fp2, PW_LINE_SEP) < 0) {
return -1;
}
if (pwinfo->deny_client_ip != NULL) {
fprintf(fp2, "%s", pwinfo->deny_client_ip); /* denied local ip */
}
if (fprintf(fp2, PW_LINE_SEP) < 0) {
return -1;
}
if (pwinfo->has_time != 0) {
if (fprintf(fp2, "%u-%u", /* time restrictions */
pwinfo->time_begin, pwinfo->time_end) < 0) {
return -1;
}
}
if (fprintf(fp2, "\n") < 0) {
return -1;
}
return 0;
}
static char *do_get_passwd(void)
{
static char pwd[LINE_MAX];
char pwd2[LINE_MAX];
int tries = MAX_PASSWD_CHANGE_TRIES;
*pwd = 0;
*pwd2 = 0;
again:
printf("Password: ");
fflush(stdout);
disable_echo();
if (fgets(pwd, (int) (sizeof pwd - 1U), stdin) == NULL) {
enable_echo();
return NULL;
}
strip_lf(pwd);
printf("\nEnter it again: ");
fflush(stdout);
disable_echo();
if (fgets(pwd2, (int) (sizeof pwd2 - 1U), stdin) == NULL) {
enable_echo();
return NULL;
}
strip_lf(pwd2);
puts("");
if (strcmp(pwd, pwd2) != 0) {
if (*pwd2 != 0) {
pure_memzero(pwd2, strlen(pwd2));
}
if (*pwd != 0) {
pure_memzero(pwd, strlen(pwd));
}
puts("You didn't enter the same password");
if (--tries > 0) {
goto again;
}
enable_echo();
return NULL;
}
if (*pwd2 != 0) {
pure_memzero(pwd2, strlen(pwd2));
}
enable_echo();
return pwd;
}
static int do_list(const char * const file)
{
FILE *fp;
PWInfo pwinfo;
char line[LINE_MAX];
if (file == NULL) {
fprintf(stderr, "missing file to list accounts\n");
return PW_ERROR_MISSING_PASSWD_FILE;
}
if ((fp = fopen(file, "r")) == NULL) {
perror("Unable to open the passwd file");
return PW_ERROR_MISSING_PASSWD_FILE;
}
while (fgets(line, (int) sizeof line - 1U, fp) != NULL) {
strip_lf(line);
if (*line == 0 || *line == PW_LINE_COMMENT) {
continue;
}
if (parse_pw_line(line, &pwinfo) != 0) {
fprintf(stderr, "Warning: invalid line [%s]\n", line);
continue;
}
if (isatty(1)) {
printf("%-19s %-39s %-19s\n", pwinfo.login, pwinfo.home, pwinfo.gecos);
} else {
printf("%s\t%s\t%s\n", pwinfo.login, pwinfo.home, pwinfo.gecos);
}
}
fclose(fp);
return 0;
}
static int do_useradd(const char * const file,
const PWInfo * const pwinfo_,
const unsigned long max_concurrent_logins,
const unsigned long long max_auth_memory)
{
char *file2;
FILE *fp2;
PWInfo pwinfo = *pwinfo_;
if (pwinfo.login == NULL || *(pwinfo.login) == 0) {
fprintf(stderr, "Missing login\n");
return PW_ERROR_MISSING_LOGIN;
}
if (file == NULL) {
fprintf(stderr, "Missing passwd file\n");
return PW_ERROR_MISSING_PASSWD_FILE;
}
#ifndef ACCEPT_ROOT_VIRTUAL_USERS
if (pwinfo.uid <= (uid_t) 0 || pwinfo.gid <= (gid_t) 0) {
fprintf(stderr, "You must give (non-root) uid and gid\n");
return PW_ERROR_USERADD_NOT_ROOT;
}
#endif
if (pwinfo.home == NULL) {
fprintf(stderr, "Missing home directory\n");
return PW_ERROR_USERADD_MISSING_HOME_DIR;
}
if (pwinfo.gecos == NULL) {
if ((pwinfo.gecos = strdup("")) == NULL) {
no_mem();
}
}
if ((pwinfo.pwd = do_get_passwd()) == NULL) {
fprintf(stderr, "Error with entering password - aborting\n");
return PW_ERROR_ENTER_PASSWD_PW_ERROR;
}
{
char *cleartext = pwinfo.pwd;
pwinfo.pwd = best_crypt(cleartext,
max_concurrent_logins, max_auth_memory);
if (*cleartext != 0) {
pure_memzero(cleartext, strlen(cleartext));
}
}
if ((file2 = newpasswd_filename(file)) == NULL) {
no_mem();
}
if ((fp2 = create_newpasswd(file, file2, pwinfo.login, 1, 0)) == NULL) {
fprintf(stderr, "Error.\n"
"Check that [%s] doesn't already exist,\n"
"and that [%s] can be written.\n",
pwinfo.login, file2);
free(file2);
return PW_ERROR_USER_ALREADY_EXIST;
}
if (add_new_pw_line(fp2, &pwinfo) != 0) {
fprintf(stderr, "Unable to append a line\n");
goto bye;
}
fflush(fp2);
#ifdef HAVE_FILENO
fsync(fileno(fp2));
#endif
if (fclose(fp2) != 0) {
perror("Unable to close the file");
goto bye2;
}
if (rename(file2, file) != 0) {
perror("Unable to rename the file");
goto bye2;
}
free(file2);
return 0;
bye:
fclose(fp2);
bye2:
unlink(file2);
free(file2);
return PW_ERROR_UNEXPECTED_ERROR;
}
static int do_usermod(const char * const file,
const PWInfo *pwinfo,
const unsigned long max_concurrent_logins,
const unsigned long long max_auth_memory)
{
char *file2;
FILE *fp2;
PWInfo fetched_info;
static char line[LINE_MAX];
if (pwinfo->login == NULL || *(pwinfo->login) == 0) {
fprintf(stderr, "Missing login\n");
return PW_ERROR_MISSING_LOGIN;
}
if (file == NULL) {
fprintf(stderr, "Missing passwd file\n");
return PW_ERROR_MISSING_PASSWD_FILE;
}
if (fetch_pw_account(file, &fetched_info, line, sizeof line,
pwinfo->login) != 0) {
fprintf(stderr, "Unable to fetch info about user [%s] in file [%s]\n",
pwinfo->login, file);
return PW_ERROR_UNABLE_TO_FETCH;
}
if (pwinfo->pwd != NULL) {
char *cleartext = pwinfo->pwd;
fetched_info.pwd = best_crypt(cleartext,
max_concurrent_logins, max_auth_memory);
if (*cleartext != 0) {
pure_memzero(cleartext, strlen(cleartext));
}
}
if (pwinfo->uid > (uid_t) 0) {
fetched_info.uid = pwinfo->uid;
}
if (pwinfo->gid > (gid_t) 0) {
fetched_info.gid = pwinfo->gid;
}
if (pwinfo->home != NULL) {
fetched_info.home = pwinfo->home;
}
if (pwinfo->gecos != NULL) {
fetched_info.gecos = pwinfo->gecos;
}
if (pwinfo->has_bw_dl != 0) {
if (pwinfo->has_bw_dl < 0) {
fetched_info.has_bw_dl = 0;
} else {
fetched_info.has_bw_dl = pwinfo->has_bw_dl;
fetched_info.bw_dl = pwinfo->bw_dl;
}
}
if (pwinfo->has_bw_ul != 0) {
if (pwinfo->has_bw_ul < 0) {
fetched_info.has_bw_ul = 0;
} else {
fetched_info.has_bw_ul = pwinfo->has_bw_ul;
fetched_info.bw_ul = pwinfo->bw_ul;
}
}
if (pwinfo->has_quota_files != 0) {
if (pwinfo->has_quota_files < 0) {
fetched_info.has_quota_files = 0;
} else {
fetched_info.has_quota_files = pwinfo->has_quota_files;
fetched_info.quota_files = pwinfo->quota_files;
}
}
if (pwinfo->has_quota_size != 0) {
if (pwinfo->has_quota_size < 0) {
fetched_info.has_quota_size = 0;
} else {
fetched_info.has_quota_size = pwinfo->has_quota_size;
fetched_info.quota_size = pwinfo->quota_size;