-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.c
1602 lines (1413 loc) · 42.7 KB
/
main.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 1991, David Koblas. | */
/* | Permission to use, copy, modify, and distribute this software | */
/* | and its documentation for any purpose and without fee is hereby | */
/* | granted, provided that the above copyright notice appear in all | */
/* | copies and that both that copyright notice and this permission | */
/* | notice appear in supporting documentation. This software is | */
/* | provided "as is" without express or implied warranty. | */
/* +-------------------------------------------------------------------+ */
#include "defs.h"
#include <sys/stat.h>
#include <errno.h>
#include <grp.h>
#include <netdb.h>
#include <pwd.h>
#include <signal.h>
#include <syslog.h>
#include "rplregex.h"
#ifdef __hpux
extern int innetgr(__const char *__netgroup, __const char *__host,
__const char *__user, __const char *__domain);
#endif
#ifdef sun
# if defined(__SVR4) || defined(__svr4__)
# define SOLARIS
# endif
#endif
#if defined(USE_SHADOW) && defined(USE_PAM)
#error USE_SHADOW and USE_PAM are mutually exclusive
#endif
#ifdef USE_SHADOW
#include <shadow.h>
#endif
#ifdef USE_PAM
#include <security/pam_appl.h>
# if defined(__hpux) || defined(SOLARIS) || defined(_AIX)
# define PAM_CONST
# else
# define PAM_CONST const
# endif
#endif
#ifdef SECURID
#include <sdi_athd.h>
#include <sdconf.h>
union config_record configure;
#endif
#ifndef LOG_AUTH
/*
** Pmax's don't have LOG_AUTH
*/
#define LOG_AUTH LOG_WARNING
#endif
#define LOG_PRINT (1UL << 31)
#define MAXARG 1024
#define MAXENV MAXARG
static void Usage(void);
static int FileCompare(const void *a, const void *b);
static int SortCommandList(const void *a, const void *b);
static void ListCommands(void);
static int ReadDir(const char *dir);
static cmd_t *Find(const char *name);
static char *FindOpt(cmd_t * cmd, const char *str);
static char *GetField(char *cp, char *str, size_t len);
#ifdef USE_PAM
static int pam_conversation(int num_msg, PAM_CONST struct pam_message **msg,
struct pam_response **response, void *appdata_ptr);
#endif
static int VerifyPermissions(cmd_t * cmd);
static int Verify(cmd_t * cmd, size_t num, size_t argc, char **argv);
static char *str_replace(const char *source, size_t offset, size_t length, const char *paste);
static int Go(cmd_t * cmd, /* UNUSED */ size_t num, size_t argc, char **argv);
#ifdef NUNUSED
static void output(cmd_t * cmd);
#endif
static char *format_cmd(size_t argc, char **argv, char *retbuf, /* UNUSED */ size_t buflen);
static int vlogger(unsigned level, const char *format, va_list args);
static int rpl_reglog(unsigned level, int error, REGEXP_T * const *prog, const char *str);
cmd_t *First = NULL;
var_t *Variables = NULL;
static char *realuser = NULL;
static int gargc = -1;
static char **gargv = NULL;
static sigset_t sig_mask, old_sig_mask;
static unsigned minimum_logging_level = 99;
static void
Usage(void)
{
fatal(0, "Usage: %s mnemonic [args]\n"
" %s -l List available commands\n"
" %s -V Show op version", gargv[0], gargv[0], gargv[0]);
}
static int
FileCompare(const void *a, const void *b)
{
return strcmp(*(char **)a, *(char **)b);
}
static int
SortCommandList(const void *a, const void *b)
{
return strcmp((*(cmd_t **) a)->name, (*(cmd_t **) b)->name);
}
static void
ListCommands(void)
{
cmd_t *def, *cmd;
array_t *cmds = array_alloc();
size_t length = 0, i;
def = Find("DEFAULT");
/* first pass, get maximum command length and number of commands we have
permission to use */
for (cmd = First; cmd != NULL; cmd = cmd->next) {
if (strcmp(cmd->name, "DEFAULT")) {
cmd_t *new = BuildSingle(def, cmd);
if (VerifyPermissions(new) >= 0) {
/* Flawfinder: ignore (strlen) */
size_t l = strlen(new->name);
for (i = 0; i < cmds->size; ++i)
if (!strcmp(((cmd_t *) cmds->data[i])->name, new->name))
break;
if (i == cmds->size) {
if (l > length)
length = l;
array_push(cmds, new);
}
}
}
}
qsort(cmds->data, cmds->size, sizeof(void *), SortCommandList);
/* second pass, display */
for (i = 0; i < cmds->size; ++i) {
cmd = cmds->data[i];
if (strcmp(cmd->name, "DEFAULT")) {
cmd_t *new = BuildSingle(def, cmd);
if (VerifyPermissions(new) >= 0) {
char *help = FindOpt(new, "help");
if (!help || !*help) {
size_t j, len = 0;
for (j = 0; j < cmd->nargs; ++j)
/* Flawfinder: ignore (strlen) */
len += strlen(cmd->args[j]) + 1;
help = (char *)malloc(len);
/* Flawfinder: fix (strcpy) */
strlcpy(help, cmd->args[0], len);
for (j = 1; j < cmd->nargs; ++j) {
/* Flawfinder: fix (strcat) */
strlcat(help, " ", len);
if (strchr(cmd->args[j], ' ') ||
strchr(cmd->args[j], '\t')) {
/* Flawfinder: fix (strcat) */
strlcat(help, "'", len);
strlcat(help, cmd->args[j], len);
strlcat(help, "'", len);
} else
/* Flawfinder: fix (strcat) */
strlcat(help, cmd->args[j], len);
}
}
printf("%-*s", (int)length + 2, new->name);
printf("%-*.*s", 77 - (int)length, 77 - (int)length, help);
/* Flawfinder: ignore (strlen) */
if (strlen(help) > 77 - length)
printf("...\n");
else
printf("\n");
}
}
}
array_free(cmds);
}
static int
ReadDir(const char *dir)
{
DIR *d;
if ((d = opendir(dir)) != NULL) {
struct dirent *f;
size_t i;
int successes = 0;
array_t *dir_list = array_alloc();
while ((f = readdir(d))) {
if (f->d_name[0] == '.' ||
/* Flawfinder: ignore (strlen) */
(strlen(f->d_name) > 5 &&
/* Flawfinder: ignore (strlen) */
strcmp(f->d_name + strlen(f->d_name) - 5, ".conf")))
continue;
if (!array_push(dir_list, savestr(f->d_name)))
fatal(1, "failed to malloc space for directory entry");
}
closedir(d);
qsort(dir_list->data, dir_list->size, sizeof(void *), FileCompare);
for (i = 0; i < dir_list->size; ++i) {
/* Flawfinder: ignore (char) */
char full_path[PATH_MAX];
snprintf(full_path, PATH_MAX, "%s/%s", OP_ACCESS_DIR,
(char *)dir_list->data[i]);
if (ReadFile(full_path))
successes++;
}
return successes;
}
return 0;
}
int
main(int argc, char *argv[])
{
int num, argStart = 1;
/* Flawfinder: ignore (char) */
char user[MAXSTRLEN];
cmd_t *cmd, *def, *new;
struct passwd *pw;
int lflag = 0, hflag = 0, read_conf = 0, read_conf_dir = 0;
char *uptr = NULL;
/* Flawfinder: ignore (char) */
/* XXX cppcheck unusedVariable:Unused variable: cmd_s, pcmd_s */
/* char cmd_s[MAXSTRLEN]; */
/* char *pcmd_s; */
sigemptyset(&sig_mask);
sigaddset(&sig_mask, SIGINT);
sigaddset(&sig_mask, SIGQUIT);
sigaddset(&sig_mask, SIGTERM);
if (sigprocmask(SIG_BLOCK, &sig_mask, &old_sig_mask))
fatal(1, "could not set signal mask");
gargv = argv;
gargc = argc;
while (1) {
if (argStart >= argc)
break;
if (strcmp("-V", argv[argStart]) == 0) {
printf("%s\n", VERSION);
return 0;
} else if (strcmp("-l", argv[argStart]) == 0) {
lflag++;
argStart++;
} else if (strcmp("-H", argv[argStart]) == 0) {
hflag++;
argStart++;
} else if (strcmp("-u", argv[argStart]) == 0) {
/* Flawfinder: ignore (strlen) */
if (strlen(argv[argStart]) == 2) {
if (argStart + 1 >= argc)
Usage();
argStart++;
uptr = argv[argStart];
}
argStart++;
} else if (strcmp("-uH", argv[argStart]) == 0) {
hflag++;
/* Flawfinder: ignore (strlen) */
if (strlen(argv[argStart]) == 3) {
if (argStart + 1 >= argc)
Usage();
argStart++;
uptr = argv[argStart];
}
argStart++;
} else if (strcmp("-Hu", argv[argStart]) == 0) {
hflag++;
/* Flawfinder: ignore (strlen) */
if (strlen(argv[argStart]) == 3) {
if (argStart + 1 >= argc)
Usage();
argStart++;
uptr = argv[argStart];
}
argStart++;
} else {
break;
}
}
#if OPENLOG_VOID
openlog("op", LOG_PID | LOG_CONS, LOG_AUTH);
#else
if (openlog("op", LOG_PID | LOG_CONS, LOG_AUTH) < 0)
fatal(0, "openlog failed");
#endif
read_conf = ReadFile(OP_ACCESS);
read_conf_dir = ReadDir(OP_ACCESS_DIR);
if (!read_conf && !read_conf_dir)
fatal(1,
"could not open %s or any configuration files in %s"
"(check that file permissions are 600)",
OP_ACCESS, OP_ACCESS_DIR);
if ((pw = getpwuid(getuid())) == NULL)
exit(EXIT_FAILURE);
realuser = (char *)strdup(pw->pw_name);
/* Flawfinder: fix (strncpy) */
strlcpy(user, pw->pw_name, MAXSTRLEN);
if (lflag) {
ListCommands();
exit(EXIT_SUCCESS);
}
if (hflag) {
if (uptr != NULL) {
if (getuid() != 0)
fatal(1, "permission denied for -u option");
}
}
if (uptr != NULL)
Usage();
if (argStart >= argc)
Usage();
def = Find("DEFAULT");
/* Reduce fully qualifed path to basename and see if that is a command */
uptr = strrchr(argv[argStart], '/');
if (uptr == NULL)
uptr = argv[argStart];
else {
uptr++;
/* Flawfinder: ignore (race condition) */
if (access(argv[argStart], F_OK) != 0)
/* Flawfinder: ignore (race condition) */
if (access(argv[argStart], X_OK) != 0)
fatal(1, "unknown or non executable command");
}
cmd = Find(uptr);
if (cmd == NULL)
fatal(1, "no such command %s", argv[1]);
argc -= argStart;
argv += argStart;
new = Build(def, cmd);
num = CountArgs(new);
if ((num < 0) && ((argc - 1) < -num))
fatal(1, "%s: improper number of arguments", cmd->name);
if ((num > 0) && ((argc - 1) != num))
fatal(1, "%s: improper number of arguments", cmd->name);
if (num < 0)
num = -num;
/* XXX cppcheck unreadVariable:Variable 'pcmd_s' is assigned
* a value that is never used */
/* pcmd_s = format_cmd(argc, argv, cmd_s, MAXSTRLEN); */
if (Verify(new, (size_t) num, (size_t) argc, argv) < 0)
fatal(0, "%s: permission denied by op", cmd->name);
return Go(new, (size_t) num, (size_t) argc, argv);
}
static cmd_t *
Find(const char *name)
{
cmd_t *cmd;
for (cmd = First; cmd != NULL; cmd = cmd->next) {
if (strcmp(cmd->name, name) == 0)
break;
}
return cmd;
}
static char *
FindOpt(cmd_t * cmd, const char *str)
{
/* Flawfinder: ignore (char) */
static char nul[1] = "";
size_t i;
char *cp;
for (i = 0; i < cmd->nopts; i++) {
if ((cp = strchr(cmd->opts[i], '=')) == NULL) {
if (strcmp(cmd->opts[i], str) == 0)
return nul;
} else {
size_t l = (size_t) (cp - cmd->opts[i]);
if (strncmp(cmd->opts[i], str, l) == 0)
return cp + 1;
}
}
return NULL;
}
static char *
GetField(char *cp, char *str, size_t len)
{
char *end = str + len - 2;
if (*cp == '\0')
return NULL;
while ((*cp != '\0') && (*cp != ',')) {
if (*cp == '\\')
if (*(cp + 1) == ',') {
*str++ = ',';
cp++;
} else
*str++ = '\\';
else
*str++ = *cp;
cp++;
/* string exceeded target buffer length */
if (str >= end)
return NULL;
}
*str = '\0';
return (*cp == '\0') ? cp : (cp + 1);
}
#ifdef USE_PAM
/* ARGUSED3 */
static int
pam_conversation(int num_msg, PAM_CONST struct pam_message **msg,
struct pam_response **response, void *appdata_ptr)
{
int i;
const struct pam_message *pm;
struct pam_response *pr;
char *pass, *cp;
UNUSED(appdata_ptr);
/*
* Valeur definie sur les principales distributions Linux
* #define PAM_MAX_NUM_MSG 32
*/
if (num_msg <= 0 || num_msg > PAM_MAX_NUM_MSG ||
(*response = malloc((size_t) num_msg *
sizeof(struct pam_response))) == NULL)
return PAM_CONV_ERR;
memset(*response, 0, (size_t) num_msg * sizeof(struct pam_response));
for (i = 0, pm = *msg, pr = *response; i < num_msg; ++i, ++pm, ++pr) {
switch (pm->msg_style) {
case PAM_PROMPT_ECHO_ON:
if (!(pass = malloc(PASS_MAX)))
return PAM_CONV_ERR;
puts(pm->msg);
cp = fgets(pass, PASS_MAX, stdin);
pr->resp = pass;
break;
case PAM_PROMPT_ECHO_OFF:
/* Flawfinder: ignore (getpass) */
if ((pass = getpass(pm->msg)) == NULL) {
for (pr = *response, i = 0; i < num_msg; ++i, ++pr)
if (pr->resp) {
/* Flawfinder: ignore (strlen) */
memset(pr->resp, 0, strlen(pr->resp));
free(pr->resp);
pr->resp = NULL;
}
memset(*response, 0, (size_t) num_msg *
sizeof(struct pam_response));
free(*response);
*response = NULL;
return PAM_CONV_ERR;
}
pr->resp = savestr(pass);
/* Flawfinder: ignore (strlen) */
memset(pass, 0, strlen(pass));
break;
case PAM_TEXT_INFO:
if (pm->msg)
puts(pm->msg);
break;
case PAM_ERROR_MSG:
if (pm->msg) {
fputs(pm->msg, stderr);
fputc('\n', stderr);
}
break;
default:
for (pr = *response, i = 0; i < num_msg; ++i, ++pr)
if (pr->resp) {
/* Flawfinder: ignore (strlen) */
memset(pr->resp, 0, strlen(pr->resp));
free(pr->resp);
pr->resp = NULL;
}
memset(*response, 0, (size_t) num_msg *
sizeof(struct pam_response));
free(*response);
*response = NULL;
return PAM_CONV_ERR;
break;
}
}
return PAM_SUCCESS;
}
#endif
static int
VerifyPermissions(cmd_t * cmd)
{
int gr_fail = 1, uid_fail = 1, netgr_fail = 1, rc;
size_t i;
/* Flawfinder: ignore (char) */
char *cp, str[MAXSTRLEN], hostname[HOST_NAME_MAX];
REGEXP_T *reg1 = NULL;
struct passwd *pw;
/* cppcheck-suppress variableScope */
struct group *gr;
/* root always has access - it is pointless refusing */
if (getuid() == 0)
return 0;
if (gethostname(hostname, HOST_NAME_MAX) == -1)
return logger(LOG_ERR, "Could not get hostname");
if ((pw = getpwuid(getuid())) == NULL)
return logger(LOG_ERR, "Could not get uid of current effective uid");
if ((cp = FindOpt(cmd, "groups")) != NULL) {
/* Flawfinder: ignore (char) */
char grouphost[MAXSTRLEN + HOST_NAME_MAX], regstr[MAXSTRLEN];
for (cp = GetField(cp, str, MAXSTRLEN - 5); cp != NULL;
cp = GetField(cp, str, MAXSTRLEN - 5)) {
/* Flawfinder: fix (strcpy, strcat) */
strlcpy(regstr, "^(", sizeof(regstr));
strlcat(regstr, str, sizeof(regstr));
strlcat(regstr, ")$", sizeof(regstr));
if ((rc = rpl_regcomp(®1, regstr, 0)) != 0)
return rpl_reglog(LOG_ERR, rc, ®1, regstr);
if ((gr = getgrgid(pw->pw_gid)) != NULL) {
char *errstr;
/* Flawfinder: fix (strcpy, strcat) */
strlcpy(grouphost, gr->gr_name, sizeof(grouphost));
strlcat(grouphost, "@", sizeof(grouphost));
strlcat(grouphost, hostname, sizeof(grouphost));
if (rpl_regexec(®1, errstr = gr->gr_name) == 0 ||
rpl_regexec(®1, errstr = grouphost) == 0) {
gr_fail = 0;
break;
}
}
setgrent();
while ((gr = getgrent()) != NULL) {
/* while -> for */
for (i = 0; gr->gr_mem[i] != NULL; i++)
if (strcmp(gr->gr_mem[i], pw->pw_name) == 0)
break;
if (gr->gr_mem[i] != NULL) {
/* Flawfinder: fix (strcpy, strcat) */
strlcpy(grouphost, gr->gr_name, sizeof(grouphost));
strlcat(grouphost, "@", sizeof(grouphost));
strlcat(grouphost, hostname, sizeof(grouphost));
if (rpl_regexec(®1, gr->gr_name) == 0 ||
rpl_regexec(®1, grouphost) == 0) {
gr_fail = 0;
break;
}
}
}
}
}
if (reg1 != NULL)
rpl_regfree(®1);
if (gr_fail && ((cp = FindOpt(cmd, "users")) != NULL)) {
/* Flawfinder: ignore (char) */
char currenttime[13], userhost[MAXSTRLEN + HOST_NAME_MAX],
regstr[MAXSTRLEN];
time_t now = time(NULL);
strftime(currenttime, 13, "%Y%m%d%H%M", localtime(&now));
for (cp = GetField(cp, str, MAXSTRLEN - 5); cp != NULL;
cp = GetField(cp, str, MAXSTRLEN - 5)) {
/* cppcheck-suppress variableScope */
/* Flawfinder: ignore (char) */
char expiretime[13], *expirestart = strchr(str, '/');
if (expirestart)
*expirestart = 0;
/* Flawfinder: fix (strcpy, strcat) */
strlcpy(regstr, "^(", sizeof(regstr));
strlcat(regstr, str, sizeof(regstr));
strlcat(regstr, ")$", sizeof(regstr));
/* Flawfinder: fix (strcpy, strcat) */
strlcpy(userhost, pw->pw_name, sizeof(userhost));
strlcat(userhost, "@", sizeof(userhost));
strlcat(userhost, hostname, sizeof(userhost));
if ((rc = rpl_regcomp(®1, regstr, 0)) != 0)
return rpl_reglog(LOG_ERR, rc, ®1, regstr);
if (rpl_regexec(®1, pw->pw_name) == 0 ||
rpl_regexec(®1, userhost) == 0) {
/* valid user, check expiry (if any) */
if (expirestart) {
++expirestart;
/* ensure at least some sanity in the expiry time */
for (i = 0; expirestart[i]; ++i) {
if (i > 11)
return logger(LOG_ERR,
"Expiry value (%s) has too many digits",
expirestart);
if (!isdigit((int)expirestart[i]))
return logger(LOG_ERR,
"Expiry value (%s) has non-numeric characters",
expirestart);
}
/* Flawfinder: fix (strcpy, strncpy -> strlcpy, strlcat) */
strlcpy(expiretime, expirestart, sizeof(expiretime));
/* YYYYMMDD[HHmm] */
strlcat(expiretime, "000000000000", sizeof(expiretime));
if (strcmp(currenttime, expiretime) >= 0)
return logger(LOG_ERR, "Access expired at %s",
expiretime);
}
uid_fail = 0;
break;
}
}
}
if (reg1 != NULL)
rpl_regfree(®1);
if (uid_fail && (cp = FindOpt(cmd, "netgroups")) != NULL) {
for (cp = GetField(cp, str, MAXSTRLEN); cp != NULL && netgr_fail;
cp = GetField(cp, str, MAXSTRLEN)) {
if (innetgr(str, hostname, pw->pw_name, NULL)) {
netgr_fail = 0;
break;
}
}
}
if (gr_fail && uid_fail && netgr_fail)
return -1;
return 0;
}
static int
Verify(cmd_t * cmd, size_t num, size_t argc, char **argv)
{
size_t i, j, val;
int rc;
/* Flawfinder: ignore (char) */
char *np, *cp, str[MAXSTRLEN], buf[MAXSTRLEN];
REGEXP_T *reg1 = NULL;
REGEXP_T *reg2 = NULL;
struct passwd *pw;
#ifdef USE_SHADOW
struct spwd *spw;
#endif
#ifdef USE_PAM
struct pam_conv pamconv = { pam_conversation, NULL };
pam_handle_t *pam;
#endif
#ifdef SECURID
struct SD_CLIENT sd_dat, *sd;
#endif
if ((pw = getpwuid(getuid())) == NULL)
return -1;
#ifdef SECURID
if ((cp = FindOpt(cmd, "securid")) != NULL) {
memset(&sd_dat, 0, sizeof(sd_dat)); /* clear sd_auth struct */
sd = &sd_dat;
creadcfg(); /* accesses sdconf.rec */
if (sd_init(sd))
return logger(LOG_WARNING | LOG_PRINT, "Cannot contact ACE server");
if (sd_auth(sd))
return -1;
}
#else
if ((cp = FindOpt(cmd, "securid")) != NULL) {
return logger(LOG_ERR | LOG_PRINT,
"SecureID not supported by op. Access denied");
}
#endif
if (getuid() != 0 && (cp = FindOpt(cmd, "password")) != NULL) {
#ifdef USE_PAM
if ((cp = GetField(cp, str, MAXSTRLEN)) != NULL) {
/* Flawfinder: ignore (getpass) */
if ((np = getpass("Password:")) == NULL)
return logger(LOG_ERR, "Could not get user password");
/* Flawfinder: ignore (crypt) */
rc = strcmp(crypt(np, str), str);
/* Flawfinder: ignore (strlen) */
memset(np, 0, strlen(np));
/* Flawfinder: ignore (strlen) */
memset(str, 0, strlen(str));
if (rc != 0)
return logger(LOG_ERR, "Incorrect direct password");
} else {
int resp;
resp = pam_start("op", pw->pw_name, &pamconv, &pam);
if (resp == PAM_SUCCESS)
resp = pam_authenticate(pam, PAM_SILENT);
if (resp == PAM_SUCCESS)
resp = pam_acct_mgmt(pam, 0);
if (resp != PAM_SUCCESS) {
return logger(LOG_ERR, "pam_authenticate: %s",
pam_strerror(pam, resp));
}
pam_end(pam, resp);
}
#else
/* Flawfinder: ignore (getpass) */
if ((np = getpass("Password:")) == NULL)
return logger(LOG_ERR, "Could not get user password");
if ((cp = GetField(cp, str, MAXSTRLEN)) != NULL) {
/* Flawfinder: ignore (crypt) */
rc = strcmp(crypt(np, str), str);
/* Flawfinder: ignore (strlen) */
memset(np, 0, strlen(np));
/* Flawfinder: ignore (strlen) */
memset(str, 0, strlen(str));
if (rc != 0)
return logger(LOG_ERR, "Incorrect direct password");
} else {
#ifdef USE_SHADOW
if (strcmp(pw->pw_passwd, "x") == 0) { /* Shadow passwords */
if ((spw = getspnam(pw->pw_name)) == NULL)
return logger(LOG_ERR, "No shadow entry for '%s'",
pw->pw_name);
pw->pw_passwd = spw->sp_pwdp;
}
#endif
/* Flawfinder: ignore (crypt) */
rc = strcmp(crypt(np, pw->pw_passwd), pw->pw_passwd);
/* Flawfinder: ignore (strlen) */
memset(np, 0, strlen(np));
/* Flawfinder: ignore (strlen) */
memset(pw->pw_passwd, 0, strlen(pw->pw_passwd));
if (rc != 0)
return logger(LOG_ERR, "Invalid user password");
}
#endif
}
/* XXX cppcheck clarifyCondition:
* Suspicious condition (assignment + comparison);
* Clarify expression with parentheses
*/
if (VerifyPermissions(cmd) < 0)
return logger(LOG_ERR,
"Both user, group and netgroup authentication failed");
for (i = 0; i < cmd->nopts; i++) {
if ((cmd->opts[i][0] != '$') ||
((cp = strchr(cmd->opts[i], '=')) == NULL))
continue;
if (cmd->opts[i][1] != '*') {
for (np = cmd->opts[i] + 1; np != cp; np++)
if (!isdigit((int)*np))
break;
if (np != cp)
continue;
} else {
if (cmd->opts[i][2] != '=')
continue;
np = cmd->opts[i] + 3;
for (j = num + 1; j < argc; j++) {
cp = np;
for (cp = GetField(cp, str, MAXSTRLEN - 5); cp != NULL;
cp = GetField(cp, str, MAXSTRLEN - 5)) {
/* Flawfinder: ignore (char) */
char regstr[MAXSTRLEN];
/* Flawfinder: fix (strcpy, strcat) */
strlcpy(regstr, "^(", sizeof(regstr));
strlcat(regstr, str, sizeof(regstr));
strlcat(regstr, ")$", sizeof(regstr));
if ((rc = rpl_regcomp(®1, regstr, 0)) != 0)
return rpl_reglog(LOG_ERR, rc, ®1, regstr);
if (rpl_regexec(®1, argv[j]) == 0)
break;
}
if (cp == NULL)
return logger(LOG_ERR,
"%s: argument %i (%s) did not pass wildcard constraint",
cmd->name, j, argv[j]);
}
}
if (reg1 != NULL)
rpl_regfree(®1);
/* Flawfinder: fix (strncpy) */
strlcpy(str, cmd->opts[i] + 1,
MIN((size_t) (cp - cmd->opts[i]), sizeof(str)));
if (!isdigit((int)*str))
continue;
/* Flawfinder: fix (atoi -> strtolong) */
val = (size_t) strtolong(str, 10);
if (val >= argc)
continue;
cp++;
np = cp;
if (reg2 != NULL) {
for (cp = GetField(cp, str, MAXSTRLEN); cp != NULL;
cp = GetField(cp, str, MAXSTRLEN)) {
rpl_regsub(®2, str, buf, sizeof(buf));
if (strcmp(buf, argv[val]) == 0)
break;
}
if (cp != NULL)
continue;
free(reg2);
reg2 = NULL;
}
if ((reg2 == NULL) || (cp == NULL)) {
cp = np;
for (cp = GetField(cp, str, MAXSTRLEN - 5); cp != NULL;
cp = GetField(cp, str, MAXSTRLEN - 5)) {
/* Flawfinder: ignore (char) */
char regstr[MAXSTRLEN];
/* Flawfinder: fix (strcpy, strcat) */
strlcpy(regstr, "^(", sizeof(regstr));
strlcat(regstr, str, sizeof(regstr));
strlcat(regstr, ")$", sizeof(regstr));
if ((rc = rpl_regcomp(®2, regstr, 0)) != 0)
return rpl_reglog(LOG_ERR, rc, ®2, regstr);
if (rpl_regexec(®2, argv[val]) == 0)
break;
}
}
if (cp == NULL)
return logger(LOG_ERR,
"%s: argument '%s' did not pass constraint '%s'",
cmd->name, argv[val], np);
}
if (reg2 != NULL)
rpl_regfree(®2);
return 0;
}
static char *
str_replace(const char *source, size_t offset, size_t length, const char *paste)
{
/* Flawfinder: ignore (strlen) */
size_t len = strlen(source) - length + strlen(paste) + 1;
char *buffer = malloc(len);
if (!buffer)
fatal(1, "Can't allocate buffer");
/* Flawfinder: fix (strcpy, strcat) */
strlcpy(buffer, source, len);
if (offset <= len)
buffer[offset] = 0; /* expected-warning */
strlcat(buffer, paste, len);
strlcat(buffer, source + offset + length, len);
return buffer;
}
static int
Go(cmd_t * cmd, size_t num, size_t argc, char **argv)
{
extern char **environ;
/* cppcheck-suppress variableScope */
size_t i, j, len, val;
int flag, rc;
/* cppcheck-suppress variableScope */
char *cp, *np;
struct passwd *pw;
struct group *gr;
/* cppcheck-suppress variableScope */
size_t ngroups = 0;
gid_t gidset[NGROUPS_MAX];
size_t curenv = 0, curarg = 0;
/* Flawfinder: ignore (char) */
char *new_envp[MAXENV];
/* Flawfinder: ignore (char) */
char *new_argv[MAXARG];
/* Flawfinder: ignore (char) */
char str[MAXSTRLEN];
struct stat st;
UNUSED(num);
#ifdef XAUTH
/* Flawfinder: ignore (getenv) */
if (getenv("DISPLAY") != NULL && (cp = FindOpt(cmd, "xauth")) != NULL) {
struct passwd *currentpw;
/* cppcheck-suppress variableScope */
/* Flawfinder: ignore (char) */
char tmpxauth[MAXSTRLEN], xauth[MAXSTRLEN], cxauth[MAXSTRLEN], *display;
int status;
uid_t uid;
gid_t gid;
/* We need to find the destination user's info */
if (cp == NULL && (cp = FindOpt(cmd, "uid")) == NULL) {
if ((pw = getpwuid(0)) == NULL)
fatal(1, "Can't get password entry for UID 0");
} else {
if ((pw = getpwnam(cp)) == NULL)
/* Flawfinder: fix (atoi => strtolong) */
if ((pw = getpwuid((uid_t) strtolong(cp, 10))) == NULL)
fatal(1, "Can't get password entry for %s", cp);
}
/* Flawfinder: ignore (getenv) */
if ((display = strchr(getenv("DISPLAY"), ':')) == NULL)
fatal(1, "Could not extract X server from $DISPLAY '%s'",
/* Flawfinder: ignore (getenv) */
getenv("DISPLAY"));
/* Flawfinder: fix (strcpy, strcat) */
strlcpy(xauth, pw->pw_dir, sizeof(xauth));
strlcat(xauth, "/.Xauthority", sizeof(xauth));
uid = pw->pw_uid;
gid = pw->pw_gid;
currentpw = getpwuid(getuid());
/* Now that we know the target user, we can copy the xauth cookies */
/* Flawfinder: ignore (getenv) */
if (getenv("XAUTHORITY") != NULL) {
/* Flawfinder: fix (strcpy) */
strlcpy(cxauth, getenv("XAUTHORITY"), sizeof(cxauth));
} else {
/* Flawfinder: fix (strcpy, strcat) */
strlcpy(cxauth, currentpw->pw_dir, sizeof(cxauth));
strlcat(cxauth, "/.Xauthority", sizeof(cxauth));
}
/* Do not continue if the source .Xauthority does not exist */
if (stat(cxauth, &st) == 0) {
/* Flawfinder: fix (strcpy) */
strlcpy(tmpxauth, "/var/tmp/op-xauth-XXXXXX", sizeof(tmpxauth));
/* Flawfinder: ignore (mkstemp) */
if (mkstemp(tmpxauth) == -1)
fatal(1, "mkstemp(%s) failed with %i", tmpxauth, errno);
/* Flawfinder: ignore (race condition) */