-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsys.c
3150 lines (2715 loc) · 95.4 KB
/
sys.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 2022, Pierre Gentile ([email protected]) */
/* */
/* This Source Code Form is subject to the terms of the Mozilla Public */
/* License, v. 2.0. If a copy of the MPL was not distributed with this */
/* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/* ################################################################### */
#include <glob.h>
#include <grp.h>
#include <fcntl.h>
#include <netdb.h>
#include <pwd.h>
#include <regex.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define XQUOTE(s) QUOTE(s)
#define QUOTE(s) #s
#include "config.h"
#include "xmalloc.h"
#include "list.h"
#include "bst.h"
#include "cachestat.h"
#include "user.h"
#include "types.h"
#include "cache.h"
#include "check.h"
#include "comp.h"
#include "conf.h"
#include "ini.h"
#include "log.h"
#include "parser.h"
#include "sys.h"
#include "strarray.h"
#include "utils.h"
int
egetopt(int nargc, char **nargv, char *ostr);
extern char **environ;
/* External variables used by egetopt */
/* """""""""""""""""""""""""""""""""" */
extern int optneed; /* Character used for mandatory arguments */
extern int optmaybe; /* Character used for optional arguments */
extern int optchar; /* Character which begins a givenargument */
extern int optbad; /* What egetopt() returns for a bad option */
extern int opterrfd; /* Where egetopt() error messages go */
extern char *optstart; /* String which contains valid option *
| start chars */
extern int optind; /* Index of current argv[] */
extern int optopt; /* The actual option pointed to */
extern int opterr; /* Set to 0 to suppress egetopt's error *
| messages */
extern char *optarg; /* The argument of the option */
long path_max; /* Global variable which will contail the maximal *
| path length. This valum will be given by sysconf *
| or set to an arbitrary usual value. */
FILE *log_fh = NULL;
bst_node_t *rule_tree; /* A binary tree of all the successfully parsed ops *
| the tree only contains the latest seen ops */
bst_node_t *var_tree; /* The same for the correctly identified variables. *
| This tree is only used when reading the .dat *
| files to create the final version of the ops so *
| we will be able to put then in the cache */
bst_node_t *env_tree;
size_t env_count;
bst_node_t *new_var_tree;
static ll_t *sys_item_list; /* Item list read from the rule_tree. The list *
| will be used to feed the cache. */
static void
insert_in_cache(void *node, void *arg);
static rule_t *
build_rule_from_cache(char *rule_tag,
char *orig_rule_tag,
char *data,
uint16_t data_len);
/* ===================================================== */
/* This function is made to be called in bst_foreach */
/* The node argument will take all the values in the bst */
/* ----------------------------------------------------- */
/* Appends one element of type elem_t to a linked list */
/* */
/* node (in/out) a node in the bst tree */
/* arg (in/out) a generic pointer not used here but */
/* needed by the callback prototype. */
/* ===================================================== */
static void
insert_in_cache(void *node, void *arg)
{
rule_t *rule;
elem_t *elem;
ll_node_t *param_list_elem;
ll_node_t *val_list_elem;
rule = (rule_t *)node;
if (rule->command == NULL)
return;
/* Allocate some space for the new element to be added in the cache */
/* and start to put data in it. */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
elem = xmalloc(sizeof(elem_t));
elem->str = rule->tag;
elem->data = xstrdup(rule->command);
elem->data_len = strlen(rule->command) + 1;
/* Add the optional list of parameters */
/* """""""""""""""""""""""""""""""""""" */
param_list_elem = rule->param_list->head;
while (param_list_elem)
{
param_t *data = (param_t *)(param_list_elem->data);
char *name = (char *)data->name;
size_t len = strlen(name) + 1;
elem->data = xrealloc(elem->data, elem->data_len + len + 1);
strcpy((char *)(elem->data) + elem->data_len, name);
elem->data_len += len;
*((char *)(elem->data) + elem->data_len - 1) = ':';
/* Process the optional parameter's value list */
/* """"""""""""""""""""""""""""""""""""""""""" */
val_list_elem = data->val_list->head;
if (val_list_elem == NULL)
{
/* We separate the parameter from its list if it has no value */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
elem->data = xrealloc(elem->data, elem->data_len + 1);
*((char *)(elem->data) + elem->data_len) = '\0';
elem->data_len++;
}
else
while (val_list_elem)
{
name = (char *)val_list_elem->data;
len = strlen(name);
elem->data = xrealloc(elem->data, elem->data_len + len + 1);
strcpy((char *)(elem->data) + elem->data_len, name);
elem->data_len += len + 1;
/* The latest comma is to be replaced by a NUL */
/* """"""""""""""""""""""""""""""""""""""""""" */
if (val_list_elem->next != NULL)
*((char *)(elem->data) + elem->data_len - 1) = ',';
else
*((char *)(elem->data) + elem->data_len - 1) = '\0';
val_list_elem = val_list_elem->next;
}
param_list_elem = param_list_elem->next;
}
/* And finally append the new element to the list */
/* """""""""""""""""""""""""""""""""""""""""""""" */
ll_append(sys_item_list, elem);
}
/* ===================================================== */
/* This function is made to be called in bst_foreach */
/* The node argument will take all the values in the bst */
/* ----------------------------------------------------- */
/* Prints the command name followed by its patterns for */
/* an element in the command tree. */
/* ===================================================== */
static void
print_rule_tag_header(void *node, void *arg)
{
ll_t *val_list;
ll_node_t *ll_node;
user_t *user_data = (user_t *)arg;
rule_t *rule;
rule = (rule_t *)node;
if (rule->command == NULL)
return;
/* Make sure variables expansion is done. */
/* """""""""""""""""""""""""""""""""""""" */
update_params_val_lists(rule);
if (check_users_groups_netgroups(rule, user_data))
{
size_t offset = 0;
/* Print the command name and the command line patters if any. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
offset = strcspn(rule->command, " \t");
if (rule->command[offset] == '\0')
printf("%s\n", rule->tag);
else
printf("%s %s\n", rule->tag, rule->command + offset + 1);
/* If the help parameter exists, display its content. */
/* """""""""""""""""""""""""""""""""""""""""""""""""" */
val_list = get_param_val_list(rule, "help");
if (val_list && val_list->len == 0)
trace(LOG_DATA, "[%S] the help message is missing.", rule->tag);
else if (val_list)
{
/* Display a prefix followed by the concatenation of all the */
/* help values separated by a comma. */
/* All occurrences of a newline character trigger the printing */
/* of a new prefix. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
char *tmp = xstrdup("| ");
ll_node = val_list->head;
while (ll_node)
{
tmp = strappend(tmp, (char *)ll_node->data, (char *)0);
if (ll_node->next)
tmp = strappend(tmp, ",", (char *)0);
ll_node = ll_node->next;
}
printf("%s\n", strrep(tmp, "\n", "\n| "));
free(tmp);
}
}
}
/* =========================================================== */
/* Search the first permitted path in which a command is valid */
/* Returns NULL if not found or the path found if found. */
/* =========================================================== */
char *
search_in_paths(rule_t *rule, char *base, char **extra_path_array)
{
ll_t *accept_list, *deny_list;
param_t *accept_param, *deny_param;
char *fullpath;
char *path;
int found = 0;
char *accept_buffer = NULL;
char *deny_buffer = NULL;
glob_t globbuf;
char **path_array;
int n;
/* Get accepted path list. */
/* """"""""""""""""""""""" */
accept_param = get_param(rule, "paths");
if (accept_param == NULL)
{
/* Try to set a default paths. */
/* """"""""""""""""""""""""""" */
accept_list = ll_new();
/* Test for the existence of the _CS_PATH configuration string. */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
n = confstr(_CS_PATH, NULL, (size_t)0);
if (n > 0)
{
/* It exists, so read it */
/* """"""""""""""""""""" */
path = xmalloc(n);
confstr(_CS_PATH, path, n);
/* And add is components to the list of allowed paths. */
/* Also add the extra default directories from the .cfg file. */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
if (strarray(path, &path_array, &n, ":", MERGESEPS | KEEPQUOTES) == 0)
{
int i;
for (i = 0; i < n; i++)
ll_append(accept_list, path_array[i]);
if (extra_path_array != NULL)
{
i = 0;
while (extra_path_array[i] != NULL)
ll_append(accept_list, extra_path_array[i++]);
}
}
else
{
int i;
/* Set an arbitrary default path. */
/* Also add the extra default directories from the .cfg file. */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
ll_append(accept_list, "/bin");
ll_append(accept_list, "/usr/bin");
if (extra_path_array != NULL)
{
i = 0;
while (extra_path_array[i] != NULL)
ll_append(accept_list, extra_path_array[i++]);
}
}
free(path);
}
else
{
int i;
/* Set an arbitrary default path and the extra default directories */
/* from the .cfg file. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
ll_append(accept_list, "/bin");
ll_append(accept_list, "/usr/bin");
if (extra_path_array != NULL)
{
i = 0;
while (extra_path_array[i] != NULL)
ll_append(accept_list, extra_path_array[i++]);
}
}
}
else
accept_list = accept_param->val_list;
/* Get denied path list. */
/* """"""""""""""""""""" */
deny_param = get_param(rule, "!paths");
if (deny_param == NULL)
deny_list = ll_new();
else
deny_list = deny_param->val_list;
/* Check if a full binary path can be built with one of */
/* the accepted path and if its path is not denied. */
/* """""""""""""""""""""""""""""""""""""""""""""""""""" */
if (accept_list && accept_list->len > 0)
{
size_t i;
ll_node_t *node;
FILE *fp;
/* For each path in the "paths" list, try to concatenate the */
/* binary name to it and check if that form a pathname to an */
/* existing file. */
/* If such a pathname is found, proceed directly with the */
/* next part of the code. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""" */
accept_buffer = xmalloc(path_max);
node = accept_list->head;
while (node && !found)
{
strcpy(accept_buffer, (char *)node->data);
if (glob(accept_buffer, 0, NULL, &globbuf) == 0)
{
for (i = 0; i < globbuf.gl_pathc; i++)
{
strcpy(accept_buffer, globbuf.gl_pathv[i]);
strcat(accept_buffer, "/");
strcat(accept_buffer, base);
if ((fp = fopen(accept_buffer, "r")) != NULL)
{
fclose(fp);
found = 1;
path = xstrdup(globbuf.gl_pathv[i]);
break;
}
}
}
globfree(&globbuf);
node = node->next;
}
/* If a potential pathname was found, check if its path is in */
/* the list of forbidden paths regex. If not, returns it else */
/* return NULL */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
if (found)
{
deny_buffer = xmalloc(path_max);
node = deny_list->head;
while (node && found)
{
strcpy(deny_buffer, (char *)node->data);
if (glob(deny_buffer, 0, NULL, &globbuf) == 0)
{
for (i = 0; i < globbuf.gl_pathc; i++)
{
strcpy(deny_buffer, globbuf.gl_pathv[i]);
if (strcmp(path, globbuf.gl_pathv[i]) == 0)
{
/* A pathname was found in the deny paths lists, mark it */
/* as rejected and exits the loop. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""" */
found = 0;
free(path);
break;
}
}
}
globfree(&globbuf);
node = node->next;
}
}
}
if (!found)
fullpath = NULL;
else
fullpath = xstrdup(accept_buffer);
globfree(&globbuf);
free(accept_buffer);
free(deny_buffer);
return fullpath;
}
/* ============================================================ */
/* Sets the uid/username couple from a string containing either */
/* the user id or the user name. */
/* in case of success *username will have to be freed. */
/* returns 0 if OK or -1 if str doesn't contain a valid user. */
/* ============================================================ */
int
str_to_user(char *str, uid_t *uid, char **username)
{
struct passwd *pw;
if (!is_number((int *)uid, str))
{
if ((pw = getpwnam(str)) == NULL)
return -1;
*uid = pw->pw_uid;
*username = xstrdup(str);
}
else
{
if ((pw = getpwuid(atoi(str))) == NULL)
return -1;
*username = xstrdup(pw->pw_name);
}
return 0;
}
/* ============================================================= */
/* Sets the gid/groupname couple from a string containing either */
/* the group id or the group name. */
/* returns 0 if OK or 1 if str doesn't contain a valid group. */
/* ============================================================= */
int
str_to_group(char *str, gid_t *gid, char **groupname)
{
struct group *gr;
if (!is_number((int *)gid, str))
{
if ((gr = getgrnam(str)) == NULL)
return -1;
*gid = gr->gr_gid;
*groupname = xstrdup(str);
}
else
{
if ((gr = getgrgid(atoi(str))) == NULL)
return -1;
*groupname = xstrdup(gr->gr_name);
}
return 0;
}
/* =================================================================== */
/* Ask for password for one of the users in pwd_param using a cache of */
/* already validated users in user_pw_ok_ht. */
/* This function puts a potential error message in error_msg. */
/* Returns 1 if the access has been granted, 0 if not. */
/* =================================================================== */
int
ask_password(rule_t *rule,
param_t *pwd_param,
ht_t *user_pw_ok_ht,
char **error_msg)
{
ll_t *val_list = pwd_param->val_list;
ll_node_t *param_list_node = val_list->head;
char *name;
uid_t uid;
int rc = 0;
while (param_list_node)
{
if (str_to_user((char *)(param_list_node->data), &uid, &name) == -1)
{
trace(LOG_WARN,
"[%s] uid: unknown user \"%s\".",
rule->tag,
(char *)(param_list_node->data));
goto next;
}
/* If the password for this user has already be successfully entered */
/* do not check it anymore. */
/* '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' */
if (ht_search(user_pw_ok_ht, name) != NULL)
{
rc = 1;
free(name);
break;
}
trace(LOG_INFO, "password request for user \"%s\".", name);
if (check_password(name))
{
rc = 1;
ht_insert(user_pw_ok_ht, name, "ok"); /* Here "ok" is a dummy value. */
break;
}
free(name);
next:
param_list_node = param_list_node->next;
}
if (rc)
{
if (*(rule->tag) == '+')
trace(LOG_INFO, "[%s] access granted.", rule->tag + 1);
else
trace(LOG_INFO, "[%s] access granted.", rule->tag);
}
else
{
if (*(rule->tag) == '+')
my_asprintf(error_msg, "[%s] invalid password.", rule->tag + 1);
else
my_asprintf(error_msg, "[%s] invalid password.", rule->tag);
}
return rc;
}
/* ================================ */
/* Daemonize the current process. */
/* returns 1 on success 0 on error. */
/* ================================ */
static int
become_daemon()
{
int maxfd, fd;
/* First fork to change the pid pid but sid and pgid will be */
/* the same as the calling process. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""" */
switch (fork()) /* become background process. */
{
case -1:
return 0;
case 0:
break; /* Child. */
default:
_exit(EXIT_SUCCESS); /* parent terminates. */
}
/* Run the process in a new session without a controlling */
/* terminal. The process group ID will be the process ID */
/* and thus, the process will be the process group leader. */
/* After this call the process will be in a new session, */
/* and it will be the progress group leader in a new */
/* process group. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""" */
if (setsid() == -1) /* Become leader of new session. */
return 0;
/* Fork again. This second fork orphans the process because the */
/* parent will exit. The child process will be adopted by the init */
/* process with process ID 1. The process will be in it's own session */
/* and process group and will have no controlling terminal. Furthermore, */
/* the process will not be the process group leader and thus, cannot */
/* have the controlling terminal if there was one. */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
switch (fork())
{
case -1:
return 0;
case 0:
break; /* Child. */
default:
_exit(EXIT_SUCCESS); /* parent terminates. */
}
chdir("/"); /* Change to root directory. */
maxfd = sysconf(_SC_OPEN_MAX);
if (maxfd == -1)
maxfd = 1024; /* Sane value. */
/* Close potentially open files ignoring errors. */
/* """"""""""""""""""""""""""""""""""""""""""""" */
for (fd = 0; fd < maxfd; fd++)
close(fd);
close(STDIN_FILENO);
/* Redirect stdin,stdout and stderr to /dev/null. */
/* """""""""""""""""""""""""""""""""""""""""""""" */
if (freopen("/dev/null", "r", stdin) == NULL)
return 0;
if (freopen("/dev/null", "r", stdout) == NULL)
return 0;
if (freopen("/dev/null", "r", stderr) == NULL)
return 0;
return 1;
}
/* ========================================================================= */
/* Try to execute the command in the new environment. */
/* The process will to fork here: */
/* The parent will wait for the child to exist and log this event and */
/* return 1 on success and 0 on error. */
/* The child will try to execute the target executable in the selected rule */
/* and does not return on success or returns 0 on error. */
/* ========================================================================= */
int
exec_command(rule_t *rule,
int argc,
char **argv,
char **new_environ,
int daemon_flag,
user_t *user_data,
ht_t *user_pw_ok_ht,
uid_t new_uid,
gid_t new_gid,
char *new_username,
char *new_groupname,
param_t *pwd_param,
int identity_failure,
char **extra_path_array,
char **error_msg)
{
char *tag;
char *path;
char *base;
char **args;
int i;
int status;
int sup_gid_nb;
char *str;
pid_t pid;
gid_t *groups;
gid_t gid;
struct passwd *pwd;
int exit_status;
int sig_status;
glob_t g;
*error_msg = NULL;
exit_status = -1;
sig_status = -1;
if (rule->tag[0] == '+')
my_asprintf(&tag, "External command %s", rule->tag + 1);
else
tag = xstrdup(rule->tag);
/* Fork to create an asynchronous child process. */
/* """"""""""""""""""""""""""""""""""""""""""""" */
switch (pid = fork())
{
int rc;
case -1: /* Error. */
error("fork has failed.");
break;
case 0: /* Child. */
/* Set the new user's uid, gid and supplementary gids */
/* """""""""""""""""""""""""""""""""""""""""""""""""" */
setgroups(0, NULL); /* clear the supplementary groups. */
/* Get the main group of new_username */
/* '''''''''''''''''''''''''''''''''' */
pwd = getpwnam(new_username);
if (pwd == NULL)
{
my_asprintf(error_msg,
"[%s] unable to get the groups "
"for the user \"%s\".",
tag,
new_username);
goto error;
}
gid = pwd->pw_gid;
if (initgroups(new_username, gid) == -1)
{
my_asprintf(error_msg,
"[%s] unable to initialize the supplementary groups "
"for the user \"%s\".",
tag,
new_username);
goto error;
}
if ((sup_gid_nb = getgroups(0, groups)) == -1)
{
my_asprintf(error_msg,
"[%s] unable to get the number of supplementary groups.",
tag);
goto error;
}
groups = (gid_t *)xmalloc(sup_gid_nb * sizeof(gid_t));
if (getgroups(sup_gid_nb, groups) == -1)
{
my_asprintf(error_msg,
"[%s] unable to get the supplementary groups.",
tag);
goto error;
}
/* If the new user is not root, check if the asked new group is */
/* one of its groups or supplementary groups. */
/* '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' */
if (new_uid == 0)
{
rc = setgid(new_gid);
if (rc != 0)
{
my_asprintf(error_msg,
"[%s] unable to set new group to \"%s\".",
tag,
new_groupname);
goto error;
}
}
else
{
int is_in_sg = 0;
for (int i = 0; i < sup_gid_nb; i++)
if (groups[i] == new_gid)
{
if (setgid(new_gid) == -1)
{
my_asprintf(error_msg,
"[%s] unable to set new group to \"%s\".",
tag,
new_groupname);
free(groups);
goto error;
}
is_in_sg = 1;
break;
}
if (!is_in_sg)
{
my_asprintf(error_msg,
"[%s] the new group \"%s\" in not one of the groups "
"to which \"%s\" belongs.",
tag,
new_groupname,
new_username);
free(groups);
goto error;
}
}
rc = setuid(new_uid);
if (rc != 0)
{
my_asprintf(error_msg,
"[%s] unable to set new users to \"%s\".",
tag,
new_username);
goto error;
}
/* If the umask parameter exists and has a legal octal value, then use */
/* it as the new umask else, use the current inherited umask */
/* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
set_umask(rule);
/* Build the new arguments array */
/* """"""""""""""""""""""""""""" */
split_command_path(rule->executable, &path, &base);
args = xmalloc((argc - optind + 1) * sizeof(char *));
args[0] = rule->executable;
args[argc - optind] = NULL;
for (i = 1; i < argc - optind; i++)
args[i] = argv[i + optind];
/* Try to execute the command */
/* """""""""""""""""""""""""" */
if (path != NULL)
{
/* Check if the executable has a valid owner */
/* """"""""""""""""""""""""""""""""""""""""" */
if (!check_owners(rule, "!owners", rule->executable))
{
my_asprintf(error_msg,
"[%s] owners: the owner of %s is in "
"forbidden ones.",
tag,
rule->executable);
goto error;
}
/* Check if the mode of the executable is in the */
/* list of authorized modes. */
/* """"""""""""""""""""""""""""""""""""""""""""" */
if (!check_owners(rule, "owners", rule->executable))
{
my_asprintf(error_msg,
"[%s] owners: the owner of %s are not "
"in authorized ones.",
tag,
rule->executable);
goto error;
}
if (glob(rule->executable, 0, NULL, &g) == 0)
{
if (daemon_flag)
if (!become_daemon())
exit(1);
if (pwd_param && identity_failure)
{
if (ask_password(rule, pwd_param, user_pw_ok_ht, error_msg) == 0)
goto error;
}
else if (identity_failure)
{
char *msg = "[%s] the user is not in one of the allowed "
"users/groups/netgroups.";
my_asprintf(error_msg, msg, tag, rule->executable);
goto error;
}
execve(rule->executable, args, new_environ);
_exit(1); /* Normally newer executed. */
}
else
{
globfree(&g);
my_asprintf(
error_msg,
"[%s] could not locate the executable in the default paths",
tag,
rule->executable);
goto error;
}
}
else
{
char *expanded_path;
/* When the name of the executable if prefixed by an absolute */
/* path we do not have to prefix it with one of it permitted path */
/* if it is found in one of them. */
/* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
if (*args[0] != '/')
expanded_path = search_in_paths(rule, args[0], extra_path_array);
else
expanded_path = rule->executable;
if (expanded_path)
{
/* Check if the executable has no forbidden owners */
/* """"""""""""""""""""""""""""""""""""""""""""""" */
if (!check_owners(rule, "!owners", expanded_path))
{
my_asprintf(error_msg,
"[%s] owners: the owner of %s is in "
"forbidden ones.",
tag,
expanded_path);
goto error;
}
/* Check if the owner of the executable is in the */
/* list of authorized ones. */
/* """""""""""""""""""""""""""""""""""""""""""""" */
if (!check_owners(rule, "owners", expanded_path))
{
my_asprintf(error_msg,
"[%s] owners: the owner of %s are not "
"in authorized ones.",
tag,
expanded_path);
goto error;
}
if (glob(expanded_path, 0, NULL, &g) == 0)
{
if (daemon_flag)
if (!become_daemon())
exit(1);
if (pwd_param && identity_failure)
{
if (ask_password(rule, pwd_param, user_pw_ok_ht, error_msg) == 0)
goto error;
}
else if (identity_failure)
{
char *msg = "[%s] the user is not in one of the allowed "
"users/groups/netgroups.";
my_asprintf(error_msg, msg, tag, rule->executable);
goto error;
}
execve(expanded_path, args, new_environ);
_exit(1); /* Normally newer executed. */
}
else
{
globfree(&g);
my_asprintf(
error_msg,
"[%s] could not locate the executable in the default paths",
tag,
expanded_path);
goto error;
}
}
else
{
my_asprintf(
error_msg,
"[%s] could not locate the executable in the default paths",
tag,
rule->executable);
goto error;
}
}
/* Normally unreachable code */
/* """"""""""""""""""""""""" */
error("[%s] exec has failed (%s).", tag, strerror(errno));
default: /* Parent. */
/* TODO Free var_tree, rule_tree env_tree and other dynamically */
/* allocated objects. */
/* ************************************************************ */