-
Notifications
You must be signed in to change notification settings - Fork 45
/
openarc.c
4931 lines (4168 loc) · 100 KB
/
openarc.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) 2009-2017, The Trusted Domain Project. All rights reserved.
*/
#include "build-config.h"
#ifndef _POSIX_PTHREAD_SEMANTICS
# define _POSIX_PTHREAD_SEMANTICS
#endif /* ! _POSIX_PTHREAD_SEMANTICS */
/* system includes */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/wait.h>
#ifdef HAVE_ISO_LIMITS_ISO_H
# include <iso/limits_iso.h>
#endif /* HAVE_ISO_LIMITS_ISO_H */
#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif /* HAVE_LIMITS_H */
#ifdef __linux__
# include <sys/prctl.h>
#endif /* __linux__ */
#include <sys/queue.h>
#ifdef USE_LUA
# include <netinet/in.h>
# include <arpa/inet.h>
#endif /* USE_LUA */
#ifdef AF_INET6
# include <arpa/inet.h>
#endif /* AF_INET6 */
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#endif /* HAVE_STDBOOL_H */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <sysexits.h>
#include <errno.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <math.h>
#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
#include <pthread.h>
#include <netdb.h>
#include <signal.h>
#include <regex.h>
#include <openssl/sha.h>
#include <openssl/err.h>
#ifdef HAVE_PATHS_H
# include <paths.h>
#endif /* HAVE_PATHS_H */
#ifndef _PATH_DEVNULL
# define _PATH_DEVNULL "/dev/null"
#endif /* ! _PATH_DEVNULL */
/* libmilter includes */
#include "libmilter/mfapi.h"
/* libopenarc includes */
#include "arc.h"
/* libbsd if found */
#ifdef USE_BSD_H
# include <bsd/string.h>
#endif /* USE_BSD_H */
/* libstrl if needed */
#ifdef USE_STRL_H
# include <strl.h>
#endif /* USE_STRL_H */
/* openarc includes */
#include "config.h"
#include "openarc-ar.h"
#include "openarc-config.h"
#include "openarc-crypto.h"
#include "openarc-test.h"
#include "openarc.h"
#include "util.h"
/* macros */
#define CMDLINEOPTS "Ac:fhlnp:P:r:t:u:vV"
#define AR_HEADER_NAME "Authentication-Results"
/*
** CONFIGVALUE -- a list of configuration values
*/
struct configvalue
{
u_char * value;
LIST_ENTRY(configvalue) entries;
};
LIST_HEAD(conflist, configvalue);
/*
** CONFIG -- configuration data
*/
struct arcf_config
{
_Bool conf_dolog; /* syslog interesting stuff? */
_Bool conf_milterv2; /* using milter v2? */
_Bool conf_disablecryptoinit; /* disable crypto lib init */
_Bool conf_enablecores; /* enable coredumps */
_Bool conf_reqhdrs; /* enforce RFC5322 */
_Bool conf_addswhdr; /* add software header field */
_Bool conf_safekeys; /* require safe keys */
_Bool conf_keeptmpfiles; /* keep temp files */
u_int conf_refcnt; /* reference count */
u_int conf_mode; /* mode flags */
arc_canon_t conf_canonhdr; /* canonicalization for header */
arc_canon_t conf_canonbody; /* canonicalization for body */
arc_alg_t conf_signalg; /* signing algorithm */
uint64_t conf_fixedtime; /* fixed timestamp */
char * conf_selector; /* signing selector */
char * conf_keyfile; /* key file */
char * conf_tmpdir; /* temp file directory */
char * conf_authservid; /* ID for A-R fields */
char * conf_peerfile; /* peer hosts table */
char * conf_domain; /* domain */
char * conf_signhdrs_raw; /* headers to sign (raw) */
const char ** conf_signhdrs; /* headers to sign (array) */
char * conf_oversignhdrs_raw; /* fields to over-sign (raw) */
const char ** conf_oversignhdrs; /* fields to over-sign (array) */
u_char * conf_keydata; /* binary key data */
size_t conf_keylen; /* key length */
ssize_t conf_maxhdrsz; /* max. header size */
struct config * conf_data; /* configuration data */
ARC_LIB * conf_libopenarc; /* shared library instance */
struct conflist conf_peers; /* peers hosts */
struct conflist conf_internal; /* internal hosts */
};
/*
** MSGCTX -- message context, containing transaction-specific data
*/
typedef struct msgctx * msgctx;
struct msgctx
{
_Bool mctx_peer; /* peer source? */
ssize_t mctx_hdrbytes; /* count of header bytes */
u_char * mctx_jobid; /* job ID */
struct Header * mctx_hqhead; /* header queue head */
struct Header * mctx_hqtail; /* header queue tail */
ARC_MESSAGE * mctx_arcmsg; /* libopenarc message */
struct arcf_dstring * mctx_tmpstr; /* temporary string */
};
/*
** CONNCTX -- connection context, containing thread-specific data
*/
typedef struct connctx * connctx;
struct connctx
{
_Bool cctx_milterv2; /* milter v2 available */
_Bool cctx_noleadspc; /* no leading spaces */
unsigned int cctx_mode; /* operating mode */
char cctx_host[ARC_MAXHOSTNAMELEN + 1];
/* hostname */
struct sockaddr_storage cctx_ip; /* IP info */
struct arcf_config * cctx_config; /* configuration in use */
struct msgctx * cctx_msg; /* message context */
};
/*
** LOOKUP -- generic lookup table
*/
struct lookup
{
char *str;
int code;
};
struct lookup log_facilities[] =
{
{ "auth", LOG_AUTH },
{ "cron", LOG_CRON },
{ "daemon", LOG_DAEMON },
{ "kern", LOG_KERN },
{ "lpr", LOG_LPR },
{ "mail", LOG_MAIL },
{ "news", LOG_NEWS },
{ "security", LOG_AUTH }, /* DEPRECATED */
{ "syslog", LOG_SYSLOG },
{ "user", LOG_USER },
{ "uucp", LOG_UUCP },
{ "local0", LOG_LOCAL0 },
{ "local1", LOG_LOCAL1 },
{ "local2", LOG_LOCAL2 },
{ "local3", LOG_LOCAL3 },
{ "local4", LOG_LOCAL4 },
{ "local5", LOG_LOCAL5 },
{ "local6", LOG_LOCAL6 },
{ "local7", LOG_LOCAL7 },
{ NULL, -1 }
};
struct lookup arcf_canonicalizations[] = {
{ "simple", ARC_CANON_SIMPLE },
{ "relaxed", ARC_CANON_RELAXED },
{ NULL, -1 }
};
struct lookup arcf_signalgorithms[] = {
{ "rsa-sha1", ARC_SIGN_RSASHA1 },
{ "rsa-sha256", ARC_SIGN_RSASHA256 },
{ NULL, -1 }
};
struct lookup arcf_chainstates[] = {
{ "none", ARC_CHAIN_NONE },
{ "pass", ARC_CHAIN_PASS },
{ "fail", ARC_CHAIN_FAIL },
{ NULL, -1 }
};
/* PROTOTYPES */
sfsistat mlfi_abort __P((SMFICTX *));
sfsistat mlfi_close __P((SMFICTX *));
sfsistat mlfi_connect __P((SMFICTX *, char *, _SOCK_ADDR *));
sfsistat mlfi_envfrom __P((SMFICTX *, char **));
sfsistat mlfi_eoh __P((SMFICTX *));
sfsistat mlfi_body __P((SMFICTX *, u_char *, size_t));
sfsistat mlfi_eom __P((SMFICTX *));
sfsistat mlfi_header __P((SMFICTX *, char *, char *));
sfsistat mlfi_negotiate __P((SMFICTX *, unsigned long, unsigned long,
unsigned long, unsigned long,
unsigned long *, unsigned long *,
unsigned long *, unsigned long *));
static Header arcf_findheader __P((msgctx, char *, int));
/* GLOBALS */
_Bool dolog; /* logging? (exported) */
_Bool reload; /* reload requested */
_Bool no_i_whine; /* noted ${i} is undefined */
_Bool die; /* global "die" flag */
_Bool testmode; /* test mode */
int diesig; /* signal to distribute */
char *progname; /* program name */
char *sock; /* listening socket */
char *conffile; /* configuration file */
struct arcf_config *curconf; /* current configuration */
pthread_mutex_t conf_lock; /* config lock */
pthread_mutex_t pwdb_lock; /* passwd/group lock */
char myhostname[MAXHOSTNAMELEN + 1]; /* local host's name */
/* Other useful definitions */
#define CRLF "\r\n" /* CRLF */
#define SUPERUSER "root" /* superuser name */
/* MACROS */
#define BITSET(b, s) (((b) & (s)) == (b))
#define JOBID(x) ((x) == NULL ? JOBIDUNKNOWN : (char *) (x))
#define TRYFREE(x) do { \
if ((x) != NULL) \
{ \
free(x); \
(x) = NULL; \
} \
} while (0)
#define ARCF_EOHMACROS "i {daemon_name} {auth_type}"
/*
** ==================================================================
** BEGIN private section
*/
#ifndef HAVE_SMFI_INSHEADER
/*
** SMFI_INSHEADER -- stub for smfi_insheader() which didn't exist before
** sendmail 8.13.0
**
** Parameters:
** ctx -- milter context
** idx -- insertion index
** hname -- header name
** hvalue -- header value
**
** Return value:
** An sfsistat.
*/
sfsistat
smfi_insheader(SMFICTX *ctx, int idx, char *hname, char *hvalue)
{
assert(ctx != NULL);
assert(hname != NULL);
assert(hvalue != NULL);
return smfi_addheader(ctx, hname, hvalue);
}
#endif /* ! HAVE_SMFI_INSHEADER */
/*
** ARCF_GETPRIV -- wrapper for smfi_getpriv()
**
** Parameters:
** ctx -- milter (or test) context
**
** Return value:
** The stored private pointer, or NULL.
*/
void *
arcf_getpriv(SMFICTX *ctx)
{
assert(ctx != NULL);
if (testmode)
return arcf_test_getpriv((void *) ctx);
else
return smfi_getpriv(ctx);
}
/*
** ARCF_SETPRIV -- wrapper for smfi_setpriv()
**
** Parameters:
** ctx -- milter (or test) context
**
** Return value:
** An sfsistat.
*/
sfsistat
arcf_setpriv(SMFICTX *ctx, void *ptr)
{
assert(ctx != NULL);
if (testmode)
return arcf_test_setpriv((void *) ctx, ptr);
else
return smfi_setpriv(ctx, ptr);
}
/*
** ARCF_INSHEADER -- wrapper for smfi_insheader()
**
** Parameters:
** ctx -- milter (or test) context
** idx -- index at which to insert
** hname -- header name
** hvalue -- header value
**
** Return value:
** An sfsistat.
*/
sfsistat
arcf_insheader(SMFICTX *ctx, int idx, char *hname, char *hvalue)
{
assert(ctx != NULL);
assert(hname != NULL);
assert(hvalue != NULL);
if (testmode)
return arcf_test_insheader(ctx, idx, hname, hvalue);
else
#ifdef HAVE_SMFI_INSHEADER
return smfi_insheader(ctx, idx, hname, hvalue);
#else /* HAVE_SMFI_INSHEADER */
return smfi_addheader(ctx, hname, hvalue);
#endif /* HAVE_SMFI_INSHEADER */
}
/*
** ARCF_CHGHEADER -- wrapper for smfi_chgheader()
**
** Parameters:
** ctx -- milter (or test) context
** hname -- header name
** idx -- index of header to be changed
** hvalue -- header value
**
** Return value:
** An sfsistat.
*/
sfsistat
arcf_chgheader(SMFICTX *ctx, char *hname, int idx, char *hvalue)
{
assert(ctx != NULL);
assert(hname != NULL);
if (testmode)
return arcf_test_chgheader(ctx, hname, idx, hvalue);
else
return smfi_chgheader(ctx, hname, idx, hvalue);
}
/*
** ARCF_QUARANTINE -- wrapper for smfi_quarantine()
**
** Parameters:
** ctx -- milter (or test) context
** reason -- quarantine reason
**
** Return value:
** An sfsistat.
*/
sfsistat
arcf_quarantine(SMFICTX *ctx, char *reason)
{
assert(ctx != NULL);
if (testmode)
return arcf_test_quarantine(ctx, reason);
#ifdef SMFIF_QUARANTINE
else
return smfi_quarantine(ctx, reason);
#endif /* SMFIF_QUARANTINE */
}
/*
** ARCF_ADDHEADER -- wrapper for smfi_addheader()
**
** Parameters:
** ctx -- milter (or test) context
** hname -- header name
** hvalue -- header value
**
** Return value:
** An sfsistat.
*/
sfsistat
arcf_addheader(SMFICTX *ctx, char *hname, char *hvalue)
{
assert(ctx != NULL);
assert(hname != NULL);
assert(hvalue != NULL);
if (testmode)
return arcf_test_addheader(ctx, hname, hvalue);
else
return smfi_addheader(ctx, hname, hvalue);
}
/*
** ARCF_ADDRCPT -- wrapper for smfi_addrcpt()
**
** Parameters:
** ctx -- milter (or test) context
** addr -- address to add
**
** Return value:
** An sfsistat.
*/
sfsistat
arcf_addrcpt(SMFICTX *ctx, char *addr)
{
assert(ctx != NULL);
assert(addr != NULL);
if (testmode)
return arcf_test_addrcpt(ctx, addr);
else
return smfi_addrcpt(ctx, addr);
}
/*
** ARCF_DELRCPT -- wrapper for smfi_delrcpt()
**
** Parameters:
** ctx -- milter (or test) context
** addr -- address to delete
**
** Return value:
** An sfsistat.
*/
sfsistat
arcf_delrcpt(SMFICTX *ctx, char *addr)
{
assert(ctx != NULL);
assert(addr != NULL);
if (testmode)
return arcf_test_delrcpt(ctx, addr);
else
return smfi_delrcpt(ctx, addr);
}
/*
** ARCF_SETREPLY -- wrapper for smfi_setreply()
**
** Parameters:
** ctx -- milter (or test) context
** rcode -- SMTP reply code
** xcode -- SMTP enhanced status code
** replytxt -- reply text
**
** Return value:
** An sfsistat.
*/
sfsistat
arcf_setreply(SMFICTX *ctx, char *rcode, char *xcode, char *replytxt)
{
assert(ctx != NULL);
if (testmode)
return arcf_test_setreply(ctx, rcode, xcode, replytxt);
else
return smfi_setreply(ctx, rcode, xcode, replytxt);
}
/*
** ARCF_GETSYMVAL -- wrapper for smfi_getsymval()
**
** Parameters:
** ctx -- milter (or test) context
** sym -- symbol to retrieve
**
** Return value:
** Pointer to the value of the requested MTA symbol.
*/
char *
arcf_getsymval(SMFICTX *ctx, char *sym)
{
assert(ctx != NULL);
assert(sym != NULL);
if (testmode)
return arcf_test_getsymval(ctx, sym);
else
return smfi_getsymval(ctx, sym);
}
/*
** ARCF_INIT_SYSLOG -- initialize syslog()
**
** Parameters:
** facility -- name of the syslog facility to use when logging;
** can be NULL to request the default
**
** Return value:
** None.
*/
static void
arcf_init_syslog(char *facility)
{
#ifdef LOG_MAIL
int code;
struct lookup *p = NULL;
closelog();
code = LOG_MAIL;
if (facility != NULL)
{
for (p = log_facilities; p != NULL; p++)
{
if (strcasecmp(p->str, facility) == 0)
{
code = p->code;
break;
}
}
}
openlog(progname, LOG_PID, code);
#else /* LOG_MAIL */
closelog();
openlog(progname, LOG_PID);
#endif /* LOG_MAIL */
}
/*
** ARCF_RESTART_CHECK -- initialize/check restart rate information
**
** Parameters:
** n -- size of restart rate array to initialize/enforce
** t -- maximum time range for restarts (0 == init)
**
** Return value:
** TRUE -- OK to continue
** FALSE -- error
*/
static _Bool
arcf_restart_check(int n, time_t t)
{
static int idx; /* last filled slot */
static int alen; /* allocated length */
static time_t *list;
if (t == 0)
{
alen = n * sizeof(time_t);
list = (time_t *) malloc(alen);
if (list == NULL)
return FALSE;
memset(list, '\0', alen);
idx = 0;
alen = n;
return TRUE;
}
else
{
int which;
time_t now;
(void) time(&now);
which = (idx - 1) % alen;
if (which == -1)
which = alen - 1;
if (list[which] != 0 &&
list[which] + t > now)
return FALSE;
list[which] = now;
idx++;
return TRUE;
}
}
/*
** ARCF_CHECKFSNODE -- check a filesystem node for safety
**
** Parameters:
** path -- path of the node to check
** myuid -- executing user's effective uid
** myname -- executing user's login
** ino -- evaluated inode (returned)
** err -- error buffer
** errlen -- error buffer length
**
** Return value:
** 1 -- node is safe to use
** 0 -- node is not safe to use
** -1 -- error (check errno)
**
** Notes:
** "Safe" here means the target file cannot be read or written by anyone
** other than the executing user and the superuser. The entire directory
** tree is checked from the root down after resolution of symlinks and
** references to "." and ".." looking for errant "write" bits on
** directories and the file itself.
**
** To prevent attacks through symbolic links, this function also returns
** the inode of the object it evaluated if that object was a file. Thus,
** if the caller first opens the file but doesn't read from it, then the
** returned inode can be compared to the inode of the opened descriptor
** to ensure that what was opened was safe at the time open() was called.
** An inode of -1 is reported if some directory above the target was
** sufficiently locked down that the inode comparison isn't necessary.
**
** This still isn't bulletproof; there's a race between the time of the
** open() call and the result returned by this function. I'm not sure if
** that can be improved.
*/
static int
arcf_checkfsnode(const char *path, uid_t myuid, char *myname, ino_t *ino,
char *err, size_t errlen)
{
int status;
struct passwd *pw;
struct group *gr;
struct stat s;
assert(path != NULL);
assert(myname != NULL);
assert(ino != NULL);
status = stat(path, &s);
if (status != 0)
return -1;
if (S_ISREG(s.st_mode))
{
/* owned by root or by me */
if (s.st_uid != 0 && s.st_uid != myuid)
{
if (err != NULL)
{
snprintf(err, errlen,
"%s is not owned by the executing uid (%d)%s",
path, myuid,
myuid != 0 ? " or the superuser"
: "");
}
return 0;
}
/* if group read/write, the group is only me and/or root */
if ((s.st_mode & (S_IRGRP|S_IWGRP)) != 0)
{
int c;
/* check if anyone else has this file's gid */
pthread_mutex_lock(&pwdb_lock);
setpwent();
for (pw = getpwent(); pw != NULL; pw = getpwent())
{
if (pw->pw_uid != myuid &&
pw->pw_uid != 0 &&
s.st_gid == pw->pw_gid)
{
if (err != NULL)
{
snprintf(err, errlen,
"%s is in group %u which has multiple users (e.g. \"%s\")",
path, s.st_gid,
pw->pw_name);
}
pthread_mutex_unlock(&pwdb_lock);
return 0;
}
}
endpwent();
/* check if this group contains anyone else */
gr = getgrgid(s.st_gid);
if (gr == NULL)
{
pthread_mutex_unlock(&pwdb_lock);
return -1;
}
for (c = 0; gr->gr_mem[c] != NULL; c++)
{
if (strcmp(gr->gr_mem[c], myname) != 0 &&
strcmp(gr->gr_mem[c], SUPERUSER) != 0)
{
if (err != NULL)
{
snprintf(err, errlen,
"%s is in group %u which has multiple users (e.g., \"%s\")",
path, s.st_gid,
gr->gr_mem[c]);
}
pthread_mutex_unlock(&pwdb_lock);
return 0;
}
}
pthread_mutex_unlock(&pwdb_lock);
}
/* not read/write by others */
if ((s.st_mode & (S_IROTH|S_IWOTH)) != 0)
{
if (err != NULL)
{
snprintf(err, errlen,
"%s can be read or written by other users",
path);
}
return 0;
}
*ino = s.st_ino;
}
else if (S_ISDIR(s.st_mode))
{
/* other write needs to be off */
if ((s.st_mode & S_IWOTH) != 0)
{
if (err != NULL)
{
snprintf(err, errlen,
"%s can be read or written by other users",
path);
}
return 0;
}
/* group write needs to be super-user or me only */
if ((s.st_mode & S_IWGRP) != 0)
{
int c;
/* check if anyone else has this file's gid */
pthread_mutex_lock(&pwdb_lock);
setpwent();
for (pw = getpwent(); pw != NULL; pw = getpwent())
{
if (pw->pw_uid != myuid &&
pw->pw_uid != 0 &&
s.st_gid == pw->pw_gid)
{
if (err != NULL)
{
snprintf(err, errlen,
"%s is in group %u which has multiple users (e.g., \"%s\")",
myname, s.st_gid,
pw->pw_name);
}
pthread_mutex_unlock(&pwdb_lock);
return 0;
}
}
/* check if this group contains anyone else */
gr = getgrgid(s.st_gid);
if (gr == NULL)
{
pthread_mutex_unlock(&pwdb_lock);
return -1;
}
for (c = 0; gr->gr_mem[c] != NULL; c++)
{
if (strcmp(gr->gr_mem[c], myname) != 0 &&
strcmp(gr->gr_mem[c], SUPERUSER) != 0)
{
if (err != NULL)
{
snprintf(err, errlen,
"%s is in group %u which has multiple users (e.g., \"%s\")",
myname, s.st_gid,
gr->gr_mem[c]);
}
pthread_mutex_unlock(&pwdb_lock);
return 0;
}
}
pthread_mutex_unlock(&pwdb_lock);
}
/* owner write needs to be super-user or me only */
if ((s.st_mode & S_IWUSR) != 0 &&
(s.st_uid != 0 && s.st_uid != myuid))
{
if (err != NULL)
{
snprintf(err, errlen,
"%s is writeable and owned by uid %u which is not the executing uid (%u)%s",
path, s.st_uid, myuid,
myuid != 0 ? " or the superuser"
: "");
}
return 0;
}
/* if nobody else can execute below here, that's good enough */
if ((s.st_mode & (S_IXGRP|S_IXOTH)) == 0)
{
*ino = (ino_t) -1;
return 1;
}
}
return 1;
}
/*
** ARCF_SECUREFILE -- determine whether a file at a specific path is "safe"
**
** Parameters:
** path -- path to evaluate
** ino -- inode of evaluated object
** myuid -- user to impersonate (-1 means "me")
** err -- error buffer
** errlen -- bytes available at "err"
**
** Return value:
** As for arcf_checkfsnode().
**
** Notes:
** If realpath() is available, this function checks the entire resolved
** filesystem tree from the root to the target file to ensure there are no
** permissions that would allow someone else on the system to either read
** or replace the file being evaluated. (It's designed to check private
** key files.) Without realpath(), only the target filename is checked.
*/
int
arcf_securefile(const char *path, ino_t *ino, uid_t myuid, char *err,
size_t errlen)
{
int status;
struct passwd *pw;
#ifdef HAVE_REALPATH
char *p;
char *q;
char real[MAXPATHLEN + 1];
char partial[MAXPATHLEN + 1];
char myname[BUFRSZ + 1];
#endif /* HAVE_REALPATH */
assert(path != NULL);
assert(ino != NULL);
/* figure out who I am */
pthread_mutex_lock(&pwdb_lock);
if (myuid == (uid_t) -1)
pw = getpwuid(geteuid());
else
pw = getpwuid(myuid);
if (pw == NULL)
{
pthread_mutex_unlock(&pwdb_lock);
return -1;
}
if (myuid == (uid_t) -1)
myuid = pw->pw_uid;
pthread_mutex_unlock(&pwdb_lock);
#ifdef HAVE_REALPATH
strlcpy(myname, pw->pw_name, sizeof myname);
p = realpath(path, real);
if (p == NULL)
return -1;
/*
** Check each node in the tree to ensure that:
** 1) The file itself is read-write only by the executing user and the
** super-user;
** 2) No directory above the file is writeable by anyone other than
** the executing user and the super-user.
*/
partial[0] = '/';
partial[1] = '\0';
# ifdef HAVE_STRSEP
q = real;
while ((p = strsep(&q, "/")) != NULL)
# else /* HAVE_STRSEP */
q = NULL;
for (p = strtok_r(real, "/", &q);
p != NULL;
p = strtok_r(NULL, "/", &q))
# endif /* HAVE_STRSEP */
{
strlcat(partial, p, sizeof partial);
status = arcf_checkfsnode((const char *) partial,
myuid, myname, ino, err, errlen);
if (status != 1)
return status;
if (partial[1] != '\0')
strlcat(partial, "/", sizeof partial);
}
return 1;
#else /* HAVE_REALPATH */
struct stat s;
status = stat(path, &s);
if (status != 0)
return -1;
/* we don't own it and neither does the super-user; bad */
if (s.st_uid != myuid && s.st_uid != 0)
return 0;
/* world readable/writeable; bad */
if ((s.st_node & (S_IROTH|S_IWOTH)) != 0)
return 0;
/* group read/write is bad if others are in that group */
if ((s.st_mode & (S_IRGRP|S_IWGRP)) != 0)
{
int c;
struct group *gr;