forked from Radmind/radmind
-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.c
1643 lines (1423 loc) · 40.4 KB
/
command.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
/*
* Copyright (c) 2003, 2007 Regents of The University of Michigan.
* All Rights Reserved. See COPYRIGHT.
*/
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <ctype.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#ifdef HAVE_LIBPAM
#ifdef HAVE_PAM_PAM_APPL_H
#include <pam/pam_appl.h>
#elif HAVE_SECURITY_PAM_APPL_H
#include <security/pam_appl.h>
#else /* HAVE_PAM_ not defined */
die die die
#endif /* HAVE_PAM_* */
#endif /* HAVE_LIBPAM */
extern SSL_CTX *ctx;
#ifdef HAVE_ZLIB
#include <zlib.h>
#endif /* HAVE_ZLIB */
#include <snet.h>
#include "applefile.h"
#include "base64.h"
#include "command.h"
#include "argcargv.h"
#include "cksum.h"
#include "code.h"
#include "list.h"
#include "wildcard.h"
#include "largefile.h"
#include "mkdirs.h"
#include "connect.h"
#define RADMIND_MAX_INCLUDE_DEPTH 10
#define DEFAULT_MODE 0444
#define DEFAULT_UID 0
#define DEFAULT_GID 0
#define K_COMMAND 1
#define K_TRANSCRIPT 2
#define K_SPECIAL 3
#define K_FILE 4
int read_kfile( SNET *sn, char *kfile );
int f_quit( SNET *, int, char *[] );
int f_noop( SNET *, int, char *[] );
int f_help( SNET *, int, char *[] );
int f_stat( SNET *, int, char *[] );
int f_retr( SNET *, int, char *[] );
int f_stor( SNET *, int, char *[] );
int f_noauth( SNET *, int, char *[] );
int f_notls( SNET *, int, char *[] );
int f_starttls( SNET *, int, char *[] );
int f_repo( SNET *, int, char *[] );
#ifdef HAVE_LIBPAM
int f_login( SNET *, int, char *[] );
int exchange( int num_msg, struct pam_message **msgm,
struct pam_response **response, void *appdata_ptr );
#endif /* HAVE_LIBPAM */
#ifdef HAVE_ZLIB
int f_compress( SNET *, int, char *[] );
#endif /* HAVE_ZLIB */
char *user = NULL;
char *password = NULL;
char *remote_host = NULL;
char *remote_addr = NULL;
char *remote_cn = NULL;
char special_dir[ MAXPATHLEN ];
char command_file[ MAXPATHLEN ];
char upload_xscript[ MAXPATHLEN ];
const EVP_MD *md = NULL;
struct list *access_list = NULL;
int ncommands = 0;
int authorized = 0;
int prevstor = 0;
int case_sensitive = 1;
char hostname[ MAXHOSTNAMELEN ];
#ifdef HAVE_ZLIB
int max_zlib_level = 0;
#endif /* HAVE_ZLIB */
extern int debug;
extern int authlevel;
extern int checkuser;
struct command notls[] = {
{ "QUIT", f_quit },
{ "NOOP", f_noop },
{ "HELP", f_help },
{ "STATus", f_notls },
{ "RETRieve", f_notls },
{ "STORe", f_notls },
{ "STARttls", f_starttls },
{ "REPOrt", f_notls },
#ifdef HAVE_LIBPAM
{ "LOGIn", f_notls },
#endif /* HAVE_LIBPAM */
#ifdef HAVE_ZLIB
{ "COMPress", f_notls },
#endif /* HAVE_ZLIB */
};
struct command noauth[] = {
{ "QUIT", f_quit },
{ "NOOP", f_noop },
{ "HELP", f_help },
{ "STATus", f_noauth },
{ "RETRieve", f_noauth },
{ "STORe", f_noauth },
{ "REPOrt", f_noauth },
#ifdef HAVE_LIBPAM
{ "LOGIn", f_noauth },
#endif /* HAVE_LIBPAM */
#ifdef HAVE_ZLIB
{ "COMPress", f_noauth },
#endif /* HAVE_ZLIB */
};
struct command auth[] = {
{ "QUIT", f_quit },
{ "NOOP", f_noop },
{ "HELP", f_help },
{ "STATus", f_stat },
{ "RETRieve", f_retr },
{ "STORe", f_stor },
{ "STARttls", f_starttls },
{ "REPOrt", f_repo },
#ifdef HAVE_LIBPAM
{ "LOGIn", f_login },
#endif /* HAVE_LIBPAM */
#ifdef HAVE_ZLIB
{ "COMPress", f_compress },
#endif /* HAVE_ZLIB */
};
struct command *commands = NULL;
int
f_quit( SNET *sn, int ac, char **av )
{
snet_writef( sn, "%d QUIT OK, closing connection\r\n", 201 );
#ifdef HAVE_ZLIB
if ( debug && max_zlib_level > 0 ) print_stats( sn );
#endif /* HAVE_ZLIB */
exit( 0 );
}
int
f_noop( SNET *sn, int ac, char **av )
{
snet_writef( sn, "%d NOOP OK\r\n", 202 );
return( 0 );
}
int
f_help( SNET *sn, int ac, char **av )
{
snet_writef( sn, "%d What is this, SMTP?\r\n", 203 );
return( 0 );
}
int
f_noauth( SNET *sn, int ac, char **av )
{
snet_writef( sn, "%d No access for %s\r\n", 500, remote_host );
exit( 1 );
}
int
f_notls( SNET *sn, int ac, char **av )
{
snet_writef( sn, "%d Must issue a STARTTLS command first\r\n", 530 );
exit( 1 );
}
int
keyword( int ac, char *av[] )
{
/*
* For now let's always check the biggest max,
* and later we can worry about command specific
* checks or no. remote_host is global. +5 is for
* "/tmp\0"
*/
int rc;
if ( ac < 2 ) {
return( -1 );
}
if ( strcasecmp( av[ 1 ], "COMMAND" ) == 0 ) {
if ( ac > 3 ) {
return( -1 );
}
if ( ac == 2 ) {
if ( strlen( command_file + 5 ) > MAXPATHLEN ) {
syslog( LOG_WARNING, "[tmp]/%s longer than MAXPATHLEN",
command_file );
return( -1 );
}
}
return( K_COMMAND );
}
if ( strcasecmp( av[ 1 ], "SPECIAL" ) == 0 ) {
if ( ac != 3 ) {
return( -1 );
}
if ( strlen( av[ 1 ] ) + strlen( av[ 2 ] ) +
strlen( remote_host ) + 5 > MAXPATHLEN ) {
syslog( LOG_WARNING,
"Overflow attempt: %s/%s-%s longer than MAXPATHLEN",
av[ 1 ], av[ 2 ], remote_host );
return( -1 );
}
rc = K_SPECIAL;
} else if ( strcasecmp( av[ 1 ], "TRANSCRIPT" ) == 0 ) {
if ( ac != 3 ) {
return( -1 );
}
if ( strlen( av[ 1 ] ) + strlen( av[ 2 ] ) + 5 > MAXPATHLEN ) {
syslog( LOG_WARNING, "[tmp]/%s/%s longer than MAXPATHLEN",
av[ 1 ], av [ 2 ] );
return( -1 );
}
rc = K_TRANSCRIPT;
} else if ( strcasecmp( av[ 1 ], "FILE" ) == 0 ) {
if ( ac != 4 ) {
return( -1 );
}
/* Check for leading ../ */
if ( strncmp( av[ 3 ], "../", strlen( "../" )) == 0 ) {
syslog( LOG_WARNING | LOG_AUTH, "attempt to access: %s", av[ 3 ] );
return( -1 );
}
/* Check for internal /../ */
if ( strstr( av[ 3 ], "/../" ) != NULL ) {
syslog( LOG_WARNING | LOG_AUTH, "attempt to access: %s", av[ 3 ] );
return( -1 );
}
if ( strlen( av[ 1 ] ) + strlen( av[ 2 ] ) +
strlen( av[ 3 ] ) + 5 > MAXPATHLEN ) {
syslog( LOG_WARNING,
"Overflow attempt: %s/%s/%s longer than MAXPATHLEN",
av[ 1 ], av[ 2 ], av[ 3 ] );
return( -1 );
}
rc = K_FILE;
} else {
return( -1 );
}
if ( strstr( av[ 2 ], "../" ) != NULL ) {
syslog( LOG_WARNING | LOG_AUTH, "attempt to access: %s", av[ 2 ] );
return( -1 );
}
return( rc );
}
int
f_retr( SNET *sn, int ac, char **av )
{
ssize_t readlen;
struct stat st;
struct timeval tv;
char buf[8192];
char path[ MAXPATHLEN ];
char *d_path, *d_tran;
int fd;
switch ( keyword( ac, av )) {
case K_COMMAND:
if ( ac == 2 ) {
if ( snprintf( path, MAXPATHLEN, "command/%s", command_file )
>= MAXPATHLEN ) {
syslog( LOG_ERR, "f_retr: command/%s: path too long",
command_file );
snet_writef( sn, "%d Path too long\r\n", 540 );
return( 1 );
}
} else {
if (( d_path = decode( av[ 2 ] )) == NULL ) {
syslog( LOG_ERR, "f_retr: decode: buffer too small" );
snet_writef( sn, "%d Line too long\r\n", 540 );
return( 1 );
}
/* Check for access */
if ( !list_check( access_list, d_path )) {
syslog( LOG_WARNING | LOG_AUTH, "attempt to access: %s",
d_path );
snet_writef( sn, "%d No access for %s\r\n", 540, d_path );
return( 1 );
}
if ( snprintf( path, MAXPATHLEN, "command/%s", d_path )
>= MAXPATHLEN ) {
syslog( LOG_ERR, "f_retr: command path too long" );
snet_writef( sn, "%d Path too long\r\n", 540 );
return( 1 );
}
}
break;
case K_TRANSCRIPT:
if (( d_tran = decode( av[ 2 ] )) == NULL ) {
syslog( LOG_ERR, "f_retr: decode: buffer too small" );
snet_writef( sn, "%d Line too long\r\n", 540 );
return( 1 );
}
/* Check for access */
if ( !list_check( access_list, d_tran )) {
syslog( LOG_WARNING | LOG_AUTH, "attempt to access: %s", d_tran );
snet_writef( sn, "%d No access for %s\r\n", 540, d_tran );
return( 1 );
}
if ( snprintf( path, MAXPATHLEN, "transcript/%s", d_tran )
>= MAXPATHLEN ) {
syslog( LOG_ERR, "f_retr: transcript path too long" );
snet_writef( sn, "%d Path too long\r\n", 540 );
return( 1 );
}
break;
case K_SPECIAL:
if (( d_path = decode( av[ 2 ] )) == NULL ) {
syslog( LOG_ERR, "f_retr: decode: buffer too small" );
snet_writef( sn, "%d Line too long\r\n", 540 );
return( 1 );
}
if ( snprintf( path, MAXPATHLEN, "%s/%s", special_dir, d_path )
>= MAXPATHLEN ) {
syslog( LOG_ERR, "f_retr: special path too long" );
snet_writef( sn, "%d Path too long\r\n", 540 );
return( 1 );
}
break;
case K_FILE:
if (( d_path = decode( av[ 3 ] )) == NULL ) {
syslog( LOG_ERR, "f_retr: decode: buffer too small" );
snet_writef( sn, "%d Line too long\r\n", 540 );
return( 1 );
}
if (( d_path = strdup( d_path )) == NULL ) {
syslog( LOG_ERR, "f_retr: strdup: %s: %m", d_path );
return( -1 );
}
if (( d_tran = decode( av[ 2 ] )) == NULL ) {
syslog( LOG_ERR, "f_retr: decode: buffer too small" );
snet_writef( sn, "%d Line too long\r\n", 540 );
return( 1 );
}
/* Check for access */
if ( !list_check( access_list, d_tran )) {
syslog( LOG_WARNING | LOG_AUTH, "attempt to access: %s", d_tran );
snet_writef( sn, "%d No access for %s:%s\r\n", 540, d_tran,
d_path );
return( 1 );
}
if ( snprintf( path, MAXPATHLEN, "file/%s/%s", d_tran, d_path )
>= MAXPATHLEN ) {
syslog( LOG_ERR, "f_retr: file path too long" );
snet_writef( sn, "%d Path too long\r\n", 540 );
return( 1 );
}
free( d_path );
break;
default:
snet_writef( sn, "%d RETR Syntax error\r\n", 540 );
return( 1 );
}
if (( fd = open( path, O_RDONLY, 0 )) < 0 ) {
syslog( LOG_ERR, "open: %s: %m", path );
snet_writef( sn, "%d Unable to access %s.\r\n", 543, path );
return( 1 );
}
/* dump file info */
if ( fstat( fd, &st ) < 0 ) {
syslog( LOG_ERR, "f_retr: fstat: %m" );
snet_writef( sn, "%d Access Error: %s\r\n", 543, path );
if ( close( fd ) < 0 ) {
syslog( LOG_ERR, "close: %m" );
return( -1 );
}
return( 1 );
}
/*
* Here's a problem. Do we need to add long long support to
* snet_writef?
*/
snet_writef( sn, "240 Retrieving file\r\n%" PRIofft "d\r\n", st.st_size );
/* dump file */
while (( readlen = read( fd, buf, sizeof( buf ))) > 0 ) {
tv.tv_sec = 60 ;
tv.tv_usec = 0;
if ( snet_write( sn, buf, readlen, &tv ) != readlen ) {
syslog( LOG_ERR, "snet_write: %m" );
return( -1 );
}
}
if ( readlen < 0 ) {
syslog( LOG_ERR, "read: %m" );
return( -1 );
}
snet_writef( sn, ".\r\n" );
if ( close( fd ) < 0 ) {
syslog( LOG_ERR, "close: %m" );
return( -1 );
}
syslog( LOG_DEBUG, "f_retr: 'file' %s retrieved", path );
return( 0 );
}
/* looks for special file info in transcripts */
char **
special_t( char *sp_path, char *remote_path )
{
FILE *fs = NULL;
int i, ac, len, ln;
char **av = NULL;
char *paths[ 4 ] = { NULL };
char *p;
char sp_t[ MAXPATHLEN ];
static char line[ MAXPATHLEN ];
/*
* in order, we look for special file transcript lines in the
* following locations:
*
* - A transcript in the same directory and with the same name
* as the special file, but with a ".T" extension.
*
* - A transcript named "<remote_id>.T" in the same directory as
* the client's special file directory root.
*
* - /var/radmind/transcript/special.T
*
* if no matching transcript line is found, default metadata is
* returned to the client (type: f; mode: 0444; owner: 0; group: 0).
*/
paths[ 0 ] = sp_path;
paths[ 1 ] = special_dir;
paths[ 2 ] = "transcript/special.T";
paths[ 3 ] = NULL;
for ( i = 0; paths[ i ] != NULL; i++ ) {
if (( p = strrchr( paths[ i ], '.' )) != NULL
&& strcmp( p, ".T" ) == 0 ) {
if ( strlen( paths[ i ] ) >= MAXPATHLEN ) {
syslog( LOG_WARNING, "special_t: path \"%s\" too long",
paths[ i ] );
continue;
}
strcpy( sp_t, paths[ i ] );
} else if ( snprintf( sp_t, MAXPATHLEN, "%s.T",
paths[ i ] ) >= MAXPATHLEN ) {
syslog( LOG_WARNING, "special_t: path \"%s.T\" too long", sp_path );
continue;
}
if (( fs = fopen( sp_t, "r" )) == NULL ) {
continue;
}
ln = 0;
while ( fgets( line, MAXPATHLEN, fs ) != NULL ) {
ln++;
len = strlen( line );
if (( line[ len - 1 ] ) != '\n' ) {
syslog( LOG_ERR, "special_t: %s: line %d too long", sp_t, ln );
break;
}
/* only files and applefiles allowed */
if ( strncmp( line, "f ", strlen( "f " )) != 0 &&
strncmp( line, "a ", strlen( "a " )) != 0 ) {
continue;
}
if (( ac = argcargv( line, &av )) != 8 ) {
syslog( LOG_WARNING, "special_t: %s: line %d: "
"bad transcript line", sp_t, ln );
continue;
}
if ( strcmp( av[ 1 ], remote_path ) == 0 ) {
(void)fclose( fs );
return( av );
}
}
if ( fclose( fs ) != 0 ) {
syslog( LOG_WARNING, "special_t: fclose %s: %m", sp_t );
}
fs = NULL;
}
if ( fs != NULL ) {
(void)fclose( fs );
}
return( NULL );
}
int
f_stat( SNET *sn, int ac, char *av[] )
{
char path[ MAXPATHLEN ];
char cksum_b64[ SZ_BASE64_E( EVP_MAX_MD_SIZE ) ];
struct stat st;
int key;
char *enc_file, *d_tran, *d_path;
switch ( key = keyword( ac, av )) {
case K_COMMAND:
if ( ac == 2 ) {
if ( snprintf( path, MAXPATHLEN, "command/%s", command_file )
>= MAXPATHLEN ) {
syslog( LOG_ERR, "f_stat: command/%s: path too long",
command_file );
snet_writef( sn, "%d Path too long\r\n", 540 );
return( 1 );
}
} else {
if (( d_path = decode( av[ 2 ] )) == NULL ) {
syslog( LOG_ERR, "f_stat: decode: buffer too small" );
snet_writef( sn, "%d Line too long\r\n", 540 );
return( 1 );
}
/* Check for access */
if ( !list_check( access_list, d_path )) {
syslog( LOG_WARNING | LOG_AUTH, "attempt to access: %s",
d_path );
snet_writef( sn, "%d No access for %s\r\n", 540, d_path );
return( 1 );
}
if ( snprintf( path, MAXPATHLEN, "command/%s", d_path )
>= MAXPATHLEN ) {
syslog( LOG_ERR, "f_stat: command path too long" );
snet_writef( sn, "%d Path too long\r\n", 540 );
return( 1 );
}
}
break;
case K_TRANSCRIPT:
if (( d_tran = decode( av[ 2 ] )) == NULL ) {
syslog( LOG_ERR, "f_stat: decode: buffer too small" );
snet_writef( sn, "%d Line too long\r\n", 540 );
return( 1 );
}
/* Check for access */
if ( !list_check( access_list, d_tran )) {
syslog( LOG_WARNING | LOG_AUTH, "attempt to access: %s", d_tran );
snet_writef( sn, "%d No access for %s\r\n", 540, d_tran );
return( 1 );
}
if ( snprintf( path, MAXPATHLEN, "transcript/%s", d_tran )
>= MAXPATHLEN ) {
syslog( LOG_ERR, "f_stat: transcript path too long" );
snet_writef( sn, "%d Path too long\r\n", 540 );
return( 1 );
}
break;
case K_SPECIAL:
if (( d_path = decode( av[ 2 ] )) == NULL ) {
syslog( LOG_ERR, "f_stat: decode: buffer too small" );
snet_writef( sn, "%d Line too long\r\n", 540 );
return( 1 );
}
if ( snprintf( path, MAXPATHLEN, "%s/%s", special_dir, d_path)
>= MAXPATHLEN ) {
syslog( LOG_ERR, "f_stat: special path too long" );
snet_writef( sn, "%d Path too long\r\n", 540 );
return( 1 );
}
break;
default:
snet_writef( sn, "%d STAT Syntax error\r\n", 530 );
return( 1 );
}
syslog( LOG_DEBUG, "f_stat: returning infomation for %s", path );
if ( stat( path, &st ) < 0 ) {
syslog( LOG_ERR, "f_stat: stat: %m" );
snet_writef( sn, "%d Access Error: %s\r\n", 531, path );
return( 1 );
}
/* XXX cksums here, totally the wrong place to do this! */
OpenSSL_add_all_digests();
md = EVP_get_digestbyname( "sha1" );
if ( !md ) {
/* XXX */
fprintf( stderr, "%s: unsupported checksum\n", "sha1" );
exit( 1 );
}
if ( do_cksum( path, cksum_b64 ) < 0 ) {
syslog( LOG_ERR, "do_cksum: %s: %m", path );
snet_writef( sn, "%d Checksum Error: %s: %m\r\n", 500, path );
return( 1 );
}
snet_writef( sn, "%d Returning STAT information\r\n", 230 );
switch ( key ) {
case K_COMMAND:
if ( ac == 2 ) {
snet_writef( sn, RADMIND_STAT_FMT,
"f", "command", DEFAULT_MODE, DEFAULT_UID, DEFAULT_GID,
st.st_mtime, st.st_size, cksum_b64 );
} else {
snet_writef( sn, RADMIND_STAT_FMT,
"f", av[ 2 ], DEFAULT_MODE, DEFAULT_UID, DEFAULT_GID,
st.st_mtime, st.st_size, cksum_b64 );
}
return( 0 );
case K_TRANSCRIPT:
snet_writef( sn, RADMIND_STAT_FMT,
"f", av[ 2 ],
DEFAULT_MODE, DEFAULT_UID, DEFAULT_GID,
st.st_mtime, st.st_size, cksum_b64 );
return( 0 );
case K_SPECIAL:
/*
* store value of av[ 2 ], because argcargv will be called
* from special_t(), and that will blow away the current values
* for av[ 2 ].
*/
if (( enc_file = strdup( av[ 2 ] )) == NULL ) {
syslog( LOG_ERR, "f_stat: strdup: %s %m", av[ 2 ] );
return( -1 );
}
if (( av = special_t( path, enc_file )) == NULL ) {
/* no special transcript match found, return defaults. */
snet_writef( sn, RADMIND_STAT_FMT,
"f", enc_file,
DEFAULT_MODE, DEFAULT_UID, DEFAULT_GID,
st.st_mtime, st.st_size, cksum_b64 );
free( enc_file );
return( 0 );
}
/*
* Cannot use RADMIND_STAT_FMT shorthand here, since custom
* permission, user and group information are strings.
*/
snet_writef( sn, "%s %s %s %s %s %" PRItimet "d %" PRIofft "d %s\r\n",
av[ 0 ], enc_file,
av[ 2 ], av[ 3 ], av[ 4 ],
st.st_mtime, st.st_size, cksum_b64 );
free( enc_file );
return( 0 );
default:
return( 1 );
}
}
int
f_stor( SNET *sn, int ac, char *av[] )
{
char *sizebuf;
char xscriptdir[ MAXPATHLEN ];
char upload[ MAXPATHLEN ];
char buf[ 8192 ];
char *line;
char *d_tran, *d_path;
int fd;
int zero = 0;
off_t len;
ssize_t rc;
struct timeval tv;
struct protoent *proto;
if ( !prevstor ) {
/* Turn off TCP_NODELAY for stores */
if (( proto = getprotobyname( "tcp" )) == NULL ) {
syslog( LOG_ERR, "f_stor: getprotobyname: %m" );
return( -1 );
}
if ( setsockopt( snet_fd( sn ), proto->p_proto, TCP_NODELAY, &zero,
sizeof( zero )) != 0 ) {
syslog( LOG_ERR, "f_stor: snet_setopt: %m" );
return( -1 );
}
prevstor = 1;
}
if ( checkuser && ( !authorized )) {
snet_writef( sn, "%d Not logged in\r\n", 551 );
exit( 1 );
}
/* decode() uses static mem, so strdup() */
if (( d_tran = decode( av[ 2 ] )) == NULL ) {
syslog( LOG_ERR, "f_stor: decode: buffer too small" );
snet_writef( sn, "%d Line too long\r\n", 540 );
return( 1 );
}
if (( d_tran = strdup( d_tran )) == NULL ) {
syslog( LOG_ERR, "f_stor: strdup: %s: %m", d_tran );
return( -1 );
}
switch ( keyword( ac, av )) {
case K_TRANSCRIPT:
if ( snprintf( xscriptdir, MAXPATHLEN, "tmp/file/%s", d_tran )
>= MAXPATHLEN ) {
syslog( LOG_ERR, "f_stor: xscriptdir path too long" );
snet_writef( sn, "%d Path too long\r\n", 540 );
return( 1 );
}
if ( snprintf( upload, MAXPATHLEN, "tmp/transcript/%s", d_tran )
>= MAXPATHLEN ) {
syslog( LOG_ERR, "f_stor: upload path too long" );
snet_writef( sn, "%d Path too long\r\n", 540 );
return( 1 );
}
/* keep encoded transcript name, since it will just be
* used later to compare in a stor file.
*/
if ( strlen( av[ 2 ] ) >= MAXPATHLEN ) {
syslog( LOG_ERR, "f_stor: upload_xscript path too long" );
snet_writef( sn, "%d Path too long\r\n", 540 );
return( 1 );
}
strcpy( upload_xscript, av[ 2 ] );
/* make the directory for the files of this xscript to live in. */
if ( mkdir( xscriptdir, 0777 ) < 0 ) {
if ( errno == EEXIST ) {
snet_writef( sn, "%d Transcript exists\r\n", 551 );
exit( 1 );
}
snet_writef( sn, "%d %s: %s\r\n",
551, xscriptdir, strerror( errno ));
exit( 1 );
}
break;
case K_FILE:
/* client must have provided a transcript name before giving
* files in that transcript
*/
if (( strcmp( upload_xscript, av[ 2 ] ) != 0 )) {
snet_writef( sn, "%d Incorrect Transcript %s\r\n", 552, av[ 2 ] );
exit( 1 );
}
/* decode() uses static mem, so strdup() */
if (( d_path = decode( av[ 3 ] )) == NULL ) {
syslog( LOG_ERR, "f_stor: decode: buffer too small" );
snet_writef( sn, "%d Line too long\r\n", 540 );
return( 1 );
}
if (( d_path = strdup( d_path )) == NULL ) {
syslog( LOG_ERR, "f_stor: strdup: %s: %m", d_path );
return( -1 );
}
if ( d_path[ 0 ] == '/' ) {
if ( snprintf( upload, MAXPATHLEN, "tmp/file/%s%s", d_tran,
d_path ) >= MAXPATHLEN ) {
syslog( LOG_ERR, "f_stor: upload path too long" );
snet_writef( sn, "%d Path too long\r\n", 540 );
return( 1 );
}
} else {
if ( snprintf( upload, MAXPATHLEN, "tmp/file/%s/%s", d_tran,
d_path ) >= MAXPATHLEN ) {
syslog( LOG_ERR, "f_stor: upload path too long" );
snet_writef( sn, "%d Path too long\r\n", 540 );
return( 1 );
}
}
free( d_path );
free( d_tran );
break;
default:
snet_writef( sn, "%d STOR Syntax error\r\n", 550 );
exit( 1 );
}
if (( fd = open( upload, O_CREAT|O_EXCL|O_WRONLY, 0666 )) < 0 ) {
if ( mkdirs( upload ) < 0 ) {
syslog( LOG_ERR, "f_stor: mkdir: %s: %m", upload );
snet_writef( sn, "%d %s: %s\r\n", 555, upload, strerror( errno ));
exit( 1 );
}
if (( fd = open( upload, O_CREAT|O_EXCL|O_WRONLY, 0666 )) < 0 ) {
syslog( LOG_ERR, "f_stor: open: %s: %m", upload );
snet_writef( sn, "%d %s: %s\r\n", 555, upload, strerror( errno ));
exit( 1 );
}
}
snet_writef( sn, "%d Storing file\r\n", 350 );
tv.tv_sec = 60;
tv.tv_usec = 0;
if ( ( sizebuf = snet_getline( sn, &tv ) ) == NULL ) {
syslog( LOG_ERR, "f_stor: snet_getline: %m" );
return( -1 );
}
/* Will there be a limit? */
len = strtoofft( sizebuf, NULL, 10 );
for ( ; len > 0; len -= rc ) {
tv.tv_sec = 60;
tv.tv_usec = 0;
if (( rc = snet_read(
sn, buf, MIN( len, sizeof( buf )), &tv )) <= 0 ) {
if ( snet_eof( sn )) {
syslog( LOG_ERR, "f_stor: snet_read: eof" );
} else {
syslog( LOG_ERR, "f_stor: snet_read: %m" );
}
return( -1 );
}
if ( write( fd, buf, rc ) != rc ) {
snet_writef( sn, "%d %s: %s\r\n", 555, upload, strerror( errno ));
exit( 1 );
}
}
if ( len != 0 ) {
syslog( LOG_ERR, "f_stor: len is %" PRIofft "d", len );
snet_writef( sn, "%d %s: internal error!\r\n", 555, upload );
exit( 1 );
}
if ( close( fd ) < 0 ) {
snet_writef( sn, "%d %s: %s\r\n", 555, upload, strerror( errno ));
exit( 1 );
}
syslog( LOG_DEBUG, "f_stor: file %s stored", upload );
tv.tv_sec = 60;
tv.tv_usec = 0;
if (( line = snet_getline( sn, &tv )) == NULL ) {
syslog( LOG_ERR, "f_stor: snet_getline: %m" );
return( -1 );
}
/* make sure client agrees we're at the end */
if ( strcmp( line, "." ) != 0 ) {
syslog( LOG_ERR, "f_stor: line is: %s", line );
snet_writef( sn, "%d Length doesn't match sent data %s\r\n",
555, upload );
(void)unlink( upload );
exit( 1 );
}
snet_writef( sn, "%d File stored\r\n", 250 );
return( 0 );
}
int
f_repo( SNET *sn, int ac, char **av )
{
char *cn = "-";
char *d_msg;
if ( ac != 3 ) {
snet_writef( sn, "%d Syntax error (invalid parameters)\r\n", 501 );
return( 1 );
}
if (( d_msg = decode( av[ 2 ] )) == NULL ) {
syslog( LOG_ERR, "f_repo: decode: buffer too small" );
snet_writef( sn, "%d Syntax error (invalid parameter)\r\n", 501 );
return( 1 );
}
if ( remote_cn != NULL ) {
cn = remote_cn;
}
syslog( LOG_NOTICE, "report %s %s %s %s %s %s",
remote_host, remote_addr,
cn, "-", /* reserve for user specified ID, e.g. sasl */
av[ 1 ], d_msg );
snet_writef( sn, "%d Report successful\r\n", 215 );
return( 0 );
}
int
f_starttls( SNET *sn, int ac, char **av )
{
int rc;
X509 *peer;
char buf[ 1024 ];
if ( ac != 1 ) {
snet_writef( sn, "%d Syntax error (no parameters allowed)\r\n", 501 );
return( 1 );
} else {
snet_writef( sn, "%d Ready to start TLS\r\n", 220 );
}
/* We get here when the client asks for TLS with the STARTTLS verb */
/*
* Client MUST NOT attempt to start a TLS session if a TLS
* session is already active. No mention of what to do if it does...
*
* Once STARTTLS has succeeded, the STARTTLS verb is no longer valid
*/
/*
* Begin TLS
*/
/* This is where the TLS start */
/* At this point the client is also starting TLS */
/* 1 is for server, 0 is client */
if (( rc = snet_starttls( sn, ctx, 1 )) != 1 ) {
syslog( LOG_ERR, "f_starttls: snet_starttls: %s",
ERR_error_string( ERR_get_error(), NULL ) );