This repository has been archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
security_plugin.c
1849 lines (1570 loc) · 46.9 KB
/
security_plugin.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 2015 Horváth Henrich
*
* Sudo security plugin is free software
* released under GNU Lesser General Public License.
*
*/
#define _GNU_SOURCE
#define PLUGIN_NAME "Sudo Dual Authorization Security Plugin"
#define MIN_AUTH_USERS 2
#define PACKAGE_VERSION 1.0
#include "utils.h"
#include "command.h"
#include "io.h"
#include "strings.h"
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <sudo_plugin.h>
#include <security/pam_appl.h>
#include <security/pam_modules.h>
#include <security/_pam_macros.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
typedef enum
{
LIST,
NORMAL,
FULL
} print_mode;
typedef enum
{
AUTHORIZED,
EXECUTED,
SKIPPED,
ERROR
} task_result;
static sudo_conv_t sudo_conv;
static sudo_printf_t sudo_log;
static char * runas_user = NULL;
static char * runas_group = NULL;
static char ** users = NULL;
static char * user = NULL;
static char ** envp = NULL;
static unsigned int min_auth_users = MIN_AUTH_USERS;
static bool use_sudoedit = false; // sudoedit mode
static bool always_auth = false; // always authorise before new command is added
static int pam_auth_type = PAM_DISALLOW_NULL_AUTHTOK;
static int commands_fd = -1;
static int execute(command_data * command, bool as_root);
static int run_editor(char * arg);
static int run_diff(command_data * commmand);
static int load_config();
static void print_table_all_commands(command_data ** commands);
static void print_command(command_data * command, print_mode mode);
static task_result auth_remove(command_data ** cmds, int i);
static task_result auth_exec(command_data ** cmds, int i);
static int append_command(char * file, char ** argv, bool flag_sudoedit, char * authorised_by);
static int sudoedit(char * file);
static int try_lock(char * file);
static int check_pam();
static int check_pam_result(int result);
static void SIG_handler(int signal);
static int PAM_conv (int, const struct pam_message**, struct pam_response**, void*);
struct pam_conv PAM_converse =
{
PAM_conv,
NULL
};
/*
Get max length of selected table column
*/
static size_t field_size(command_data ** commands, int index, int min)
{
command_data ** command = commands;
size_t max = min;
size_t len;
char * exec_by_users;
char * rem_by_users;
while (*command)
{
switch (index)
{
case 0:
if ((*command)->runas_user)
{
len = strlen((*command)->runas_user);
if (len > max)
max = len;
}
break;
case 1:
if ((*command)->runas_group)
{
len = strlen((*command)->runas_group);
if (len > max)
max = len;
}
break;
case 2:
exec_by_users = concat((*command)->exec_by_users, ",");
if (exec_by_users)
{
len = strlen (exec_by_users);
if (len > max)
max = len;
free(exec_by_users);
}
break;
case 3:
rem_by_users = concat((*command)->rem_by_users, ",");
if (rem_by_users)
{
len = strlen (rem_by_users);
if (len > max)
max = len;
free(rem_by_users);
}
break;
}
command++;
}
return max;
}
/*
Prints commands in table
*/
static void print_table_all_commands(command_data ** commands)
{
if (!commands)
{
return;
}
char runas_user_label[] = "USER";
char runas_group_label[] = "GROUP";
char exec_by_users_label[] = "TO EXECUTE";
char rem_by_users_label[] = "TO REMOVE";
char command_label[] = "COMMAND";
size_t columns_width[4];
columns_width[0] = field_size(commands, 0, strlen(runas_user_label)+1);
columns_width[1] = field_size(commands, 1, strlen(runas_group_label)+1);
columns_width[2] = field_size(commands, 2, strlen(exec_by_users_label)+1);
columns_width[3] = field_size(commands, 3, strlen(rem_by_users_label)+1);
size_t total_width = 0;
for (int i = 0; i < 4; i++)
total_width += columns_width[i];
sudo_log(SUDO_CONV_INFO_MSG, "%-*s%-*s%-*s%-*s %s\n",
columns_width[0], runas_user_label,
columns_width[1], runas_group_label,
columns_width[2], exec_by_users_label,
columns_width[3], rem_by_users_label,
command_label);
for (size_t i = 0; i < total_width; i++)
sudo_log(SUDO_CONV_INFO_MSG, "=");
sudo_log(SUDO_CONV_INFO_MSG, "\n");
command_data ** command = commands;
while (*command)
{
char * exec_by_users = concat((*command)->exec_by_users, ",");
char * rem_by_users = concat((*command)->rem_by_users, ",");
char * cmd_line = get_command_line(*command);
sudo_log(SUDO_CONV_INFO_MSG, "%-*s%-*s%-*s%-*s %s\n",
columns_width[0], (*command)->runas_user,
columns_width[1], (*command)->runas_group,
columns_width[2], exec_by_users,
columns_width[3], rem_by_users,
cmd_line);
free(exec_by_users);
free(rem_by_users);
free(cmd_line);
command++;
}
}
/*
Prints command with arguments
*/
static void print_command(command_data * command, print_mode mode)
{
if (!command || !command->argv)
{
return;
}
/* Sudoedit */
if (command->sudoedit)
{
if (mode == LIST)
{
sudo_log(SUDO_CONV_INFO_MSG, "sudoedit %s\n", command->argv[4]);
}
else
{
run_diff(command);
char * exec_by_users = concat(command->exec_by_users, ",");
char * rem_by_users = concat(command->rem_by_users, ",");
sudo_log(SUDO_CONV_INFO_MSG,
"Authorised to execute:%s Authorised to remove:%s\n",
(exec_by_users ? exec_by_users : "N/A"),
(rem_by_users ? rem_by_users : "N/A"));
free(exec_by_users);
free(rem_by_users);
}
return;
}
/* Other command */
char * cmd_line = get_command_line(command);
sudo_log(SUDO_CONV_INFO_MSG, "%s\n", cmd_line);
free(cmd_line);
if (mode == NORMAL || mode == FULL)
{
char * exec_by_users = concat(command->exec_by_users, ",");
char * rem_by_users = concat(command->rem_by_users, ",");
if (command->runas_user)
sudo_log(SUDO_CONV_INFO_MSG,"Run as user:%s ",command->runas_user);
if (command->runas_group)
sudo_log(SUDO_CONV_INFO_MSG,"Run as group:%s ",command->runas_group);
sudo_log(SUDO_CONV_INFO_MSG,
"Authorised to execute:%s Authorised to remove:%s\n",
(exec_by_users ? exec_by_users : "N/A"),
(rem_by_users ? rem_by_users : "N/A"));
free(exec_by_users);
free(rem_by_users);
}
if (mode == FULL)
{
char ** local_envp = command->envp;
while (*local_envp)
{
sudo_log(SUDO_CONV_INFO_MSG, "%s\n", *local_envp);
local_envp++;
}
}
}
/*
Reads data (users, always_auth) from conf file
*/
static int load_config()
{
FILE * fp;
int error = 0;
unsigned int user_count = 0;
size_t user_size = MIN_AUTH_USERS + 1;
ssize_t len = 0;
size_t buflen = 0;
struct passwd *pw;
char* buffer = NULL;
if ((users = calloc(user_size, sizeof(char*))) == NULL)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Cannot allocate data.\n");
return false;
}
if ( (fp = fopen(PLUGIN_CONF_FILE, "r")) == NULL )
{
sudo_log(SUDO_CONV_ERROR_MSG, "File %s not found.\n", STR(PLUGIN_CONF_FILE));
free(users);
return false;
}
// read new line
while ( (len = getline(&buffer, &buflen, fp)) != -1 )
{
// ignore empty lines and comments
if (len == 0 || buffer[0] == '#' || buffer[0] == '\n')
{
continue;
}
// set null instead of new line character
buffer[len - 1] = '\0';
if (str_case_starts(buffer, "always_auth ") && (size_t)len > strlen("always_auth ")) // parsing "user xxx"
{
char * value = rem_whitespace(buffer + strlen("always_auth "));
if (strcasecmp(value,"true") == 0 || strcmp(value,"1") == 0)
{
always_auth = true;
}
else if (strcasecmp(value,"false") == 0 || strcmp(value,"0") == 0)
{
always_auth = false;
}
}
else if (str_case_starts(buffer, "user ") && (size_t)len > strlen("user ")) // parsing "user xxx"
{
char * user_name = strdup(rem_whitespace(buffer + strlen("user ")));
if (!user_name)
{
error = -1;
break;
}
// check if user exists
if (!getpwnam(user_name))
{
sudo_log(SUDO_CONV_ERROR_MSG, "User %s not found.\n", user_name);
free(user_name);
continue;
}
// check if user is already loaded
if (array_null_contains(users, user_name))
{
sudo_log(SUDO_CONV_ERROR_MSG, "Found duplicate of user %s, skipping.\n", user_name);
free(user_name);
continue;
}
// resize array
if (user_count+1 == user_size)
{
user_size += 2;
char ** new_users;
if ((new_users = realloc(users, user_size * sizeof(char*))) == NULL)
{
error = -1;
break;
}
users = new_users;
}
// save user name
users[user_count++] = user_name;
users[user_count] = NULL;
}
else if (str_case_starts(buffer, "uid ") && (size_t)len > strlen("uid ")) // parsing "uid 123"
{
char * user_id = strdup(rem_whitespace(buffer + strlen("uid ")));
if (!user_id)
{
error = -1;
break;
}
// get user struct
char *p;
errno = 0;
uid_t id = strtol(user_id, &p, 10);
if (errno != 0 || *p != 0 || p == user_id)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Invalid UID %s\n", user_id);
free(user_id);
error = -2;
continue;
}
free(user_id);
pw = getpwuid(id);
// check if user with UID exists
if (!pw)
{
sudo_log(SUDO_CONV_ERROR_MSG, "User with uid %s not found.\n", id);
error = -2;
continue;
}
// checks if user is already loaded
if (array_null_contains(users, pw->pw_name))
{
sudo_log(SUDO_CONV_ERROR_MSG, "Found duplicate of user %s, skipping.\n", pw->pw_name);
continue;
}
// resize array
if (user_count+1 == user_size)
{
user_size += 2;
char ** new_users;
if ((new_users = realloc(users, user_size * sizeof(char*))) == NULL)
{
error = -1;
break;
}
users = new_users;
}
char * user_name = strdup(pw->pw_name);
if (!user_name)
{
error = -1;
break;
}
// save user name
users[user_count++] = user_name;
users[user_count] = NULL;
}
else if (str_case_starts(buffer, "authorizations_count "))
{
char * min_users_str = strdup(rem_whitespace(buffer + strlen("authorizations_count ")));
if (!min_users_str)
{
error = -1;
break;
}
// convert to number
char *p;
errno = 0;
size_t _min_auth_users = strtol(min_users_str, &p, 10);
if (errno != 0 || *p != 0 || p == min_users_str || _min_auth_users < 1 || _min_auth_users > SIZE_MAX)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Invalid authorization count: %s\n", min_users_str);
free(min_users_str);
error = -2;
continue;
}
// save
min_auth_users = _min_auth_users;
}
}
fclose(fp);
free(buffer);
switch (error)
{
case -1: // allocation error
free_2d_null(users);
sudo_log(SUDO_CONV_ERROR_MSG, "Cannot allocate data.\n");
return false;
case -2: // invalid configuration error
free_2d_null(users);
return false;
}
if (user_count < min_auth_users)
{
free_2d_null(users);
sudo_log(SUDO_CONV_ERROR_MSG, "Not enough users set in %s (minimum is %d).\n", STR(PLUGIN_CONF_FILE), min_auth_users);
return false;
}
return true;
}
/*-
Returns 1 on success, 0 on failure, -1 if a general error occurred, or -2 if there was a usage error.
In the latter case, sudo will print a usage message before it exits.
*/
static int sudo_open(unsigned int version, sudo_conv_t conversation, sudo_printf_t sudo_printf,
char * const settings[], char * const user_info[], char * const user_env[], char * const options[])
{
char* const *ui;
struct passwd *pw;
struct group *gr;
if (!sudo_conv)
sudo_conv = conversation;
if (!sudo_log)
sudo_log = sudo_printf;
if (SUDO_API_VERSION_GET_MAJOR(version) != SUDO_API_VERSION_MAJOR)
{
sudo_log(SUDO_CONV_ERROR_MSG, "This plugin requires API version %d.x\n", SUDO_API_VERSION_MAJOR);
return -1;
}
/* Load settings */
for (ui = settings; *ui != NULL; ui++)
{
if (str_case_starts(*ui, "runas_user="))
{
runas_user = *ui + sizeof("runas_user=") - 1;
}
else if (str_case_starts(*ui, "runas_group="))
{
runas_group = *ui + sizeof("runas_group=") - 1;
}
else if (strcasecmp(*ui, "sudoedit=true") == 0)
{
use_sudoedit = true;
}
/* Plugin doesn't support running sudo with no arguments. */
else if (strcasecmp(*ui, "implied_shell=true") == 0)
{
return -2;
}
}
for (ui = user_info; *ui != NULL; ui++)
{
if (str_case_starts(*ui, "user="))
{
user = *ui + sizeof("user=") - 1;
break;
}
}
if (runas_user)
{
if ((pw = getpwnam(runas_user)) == NULL)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Unknown user %s.\n", runas_user);
return -1;
}
}
if (runas_group)
{
if ((gr = getgrnam(runas_group)) == NULL)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Unknown group %s.\n", runas_group);
return -1;
}
}
envp = (char **)user_env;
openlog("sudo", LOG_PID|LOG_CONS, LOG_USER);
if (!load_config())
{
return -1;
}
if (!try_lock(PLUGIN_COMMANDS_FILE))
{
return -1;
}
return 1;
}
/*
Sudo_close() is called when the command being run by sudo finishes.
*/
static void sudo_close (int exit_status, int error)
{
free_2d_null(users);
/* Unlock file with commands */
if (commands_fd != -1)
{
flock(commands_fd, LOCK_UN);
close(commands_fd);
}
closelog();
if (error)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Command error: %s.\n", strerror(error));
}
else
{
if (WIFEXITED(exit_status))
{
sudo_log(SUDO_CONV_INFO_MSG, "Command exited with status %d.\n",
WEXITSTATUS(exit_status));
}
else if (WIFSIGNALED(exit_status))
{
sudo_log(SUDO_CONV_INFO_MSG, "Command killed by signal %d.\n",
WTERMSIG(exit_status));
}
}
}
/*
The show_version() function is called by sudo when the user specifies the -V option.
*/
static int sudo_show_version (int verbose)
{
sudo_log(SUDO_CONV_INFO_MSG, "%s\nPackage version %s\n", PLUGIN_NAME, STR(PACKAGE_VERSION));
if (verbose)
{
sudo_log(SUDO_CONV_INFO_MSG, "Sudo API version %d.%d\n", SUDO_API_VERSION_MAJOR, SUDO_API_VERSION_MINOR);
}
return true;
}
/*
Tests if file lock does not exist and locks file
*/
static int try_lock(char * file)
{
if ( (commands_fd = open(file, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR)) == -1 )
{
sudo_log(SUDO_CONV_ERROR_MSG, "Cannot open data file.\n");
return false;
}
if (flock(commands_fd, LOCK_EX | LOCK_NB) == -1)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Instance of sudo is already running.\n");
return false;
}
return true;
}
/*
Error checking for PAM
*/
static int check_pam_result(int result)
{
switch (result)
{
case PAM_SUCCESS:
return true;
case PAM_ABORT:
sudo_log(SUDO_CONV_ERROR_MSG, "critical error\n");
return false;
case PAM_ACCT_EXPIRED:
sudo_log(SUDO_CONV_ERROR_MSG, "user account has expired\n");
return false;
case PAM_AUTH_ERR:
sudo_log(SUDO_CONV_ERROR_MSG, "the user was not authenticated\n");
return false;
case PAM_AUTHINFO_UNAVAIL:
sudo_log(SUDO_CONV_ERROR_MSG, "the modules were not able to access the authentication information\n");
return false;
case PAM_BAD_ITEM:
sudo_log(SUDO_CONV_ERROR_MSG, "attempted to set an undefined or inaccessible item\n");
return false;
case PAM_BUF_ERR:
sudo_log(SUDO_CONV_ERROR_MSG, "memory buffer error\n");
return false;
case PAM_CONV_ERR:
sudo_log(SUDO_CONV_ERROR_MSG, "conversation failure\n");
return false;
case PAM_CRED_INSUFFICIENT:
sudo_log(SUDO_CONV_ERROR_MSG, "insufficient credentials to access authentication data\n");
return false;
case PAM_MAXTRIES:
sudo_log(SUDO_CONV_ERROR_MSG, "one or more of the authentication modules has reached its limit of tries authenticating the user. Do not try again\n");
return false;
case PAM_NEW_AUTHTOK_REQD:
sudo_log(SUDO_CONV_ERROR_MSG, "authentication token expired\n");
return false;
case PAM_PERM_DENIED :
sudo_log(SUDO_CONV_ERROR_MSG, "permission denied\n");
return false;
case PAM_SYSTEM_ERR:
sudo_log(SUDO_CONV_ERROR_MSG, "system error\n");
return false;
case PAM_USER_UNKNOWN:
sudo_log(SUDO_CONV_ERROR_MSG, "user unknown to authentication service\n");
return false;
default:
sudo_log(SUDO_CONV_ERROR_MSG, "unknown PAM error\n");
return false;
}
}
/*
PAM conversation function
*/
static int PAM_conv(int num_msg, const struct pam_message **msg, struct pam_response **resp, void *appdata_ptr)
{
struct pam_response * reply = NULL;
reply = (struct pam_response *) malloc(sizeof(struct pam_response) * num_msg);
if (!reply)
{
return PAM_CONV_ERR;
}
struct sudo_conv_message msgs[1];
struct sudo_conv_reply replies[1];
for (int i = 0; i < num_msg; i++)
{
switch (msg[i]->msg_style)
{
case PAM_PROMPT_ECHO_ON:
case PAM_PROMPT_ECHO_OFF:
if (msg[i]->msg_style == PAM_PROMPT_ECHO_ON)
{
msgs[0].msg_type = SUDO_CONV_PROMPT_ECHO_ON;
}
else
{
msgs[0].msg_type = SUDO_CONV_PROMPT_ECHO_OFF;
}
/* Message */
char * message = strdup(msg[i]->msg);
if (!message)
{
return PAM_BUF_ERR;
}
msgs[0].msg = message;
/* Conversation */
if (sudo_conv(1, msgs, replies, NULL) != 0)
{
free(message);
return PAM_CONV_ERR;
}
/* Response */
reply[i].resp_retcode = 0;
reply[i].resp = strdup(replies[0].reply);
if (!reply[i].resp)
{
free(message);
return PAM_BUF_ERR;
}
free(message);
free(replies[0].reply);
break;
case PAM_TEXT_INFO:
sudo_log(SUDO_CONV_INFO_MSG, msg[i]->msg);
break;
case PAM_ERROR_MSG:
sudo_log(SUDO_CONV_ERROR_MSG, msg[i]->msg);
break;
default:
return PAM_CONV_ERR;
}
}
*resp = reply;
return PAM_SUCCESS;
}
/*
Authenticate user via PAM
*/
static int check_pam()
{
pam_handle_t * pamh = NULL;
/* Initialize PAM */
int result = pam_start("sudo", user, &PAM_converse, &pamh);
if (!check_pam_result(result))
{
pam_end(pamh, result);
return false;
}
/* Authenticate user */
result = pam_authenticate(pamh, pam_auth_type);
if (!check_pam_result(result))
{
syslog(LOG_ERR, "User %s has not authenticated", user);
pam_end(pamh, result);
return false;
}
/* Check for account validation */
result = pam_acct_mgmt(pamh, 0);
if (!check_pam_result(result))
{
syslog(LOG_ERR, "User %s not valid", user);
pam_end(pamh, result);
return false;
}
pam_end(pamh, result);
return true;
}
/*
Execute command
*/
static int execute(command_data * command, bool as_root)
{
if (!command)
{
return false;
}
pid_t pid;
if ((pid = fork()) < 0) // Fork a child process
{
return false;
}
else if (pid == 0) // For the child process
{
char * pwd = getenv_from_envp("PWD", command->envp);
if (chdir(pwd) == -1)
{
_exit(1);
}
// Run as group
if (command->runas_group && !as_root)
{
struct group * gid;
if ((gid = getgrnam(command->runas_group)) != NULL)
{
if (setgid(gid->gr_gid) == -1)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Cannot set gid to %d.\n", gid->gr_gid);
_exit(1);
}
}
else
{
sudo_log(SUDO_CONV_ERROR_MSG, "Group %s not found.\n", command->runas_group);
_exit(1);
}
}
else
{
// Run as root
if (setgid(0) == -1)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Cannot set gid to %d.\n", 0);
_exit(1);
}
}
// Run as user
if (command->runas_user && !as_root)
{
struct passwd * uid;
if ((uid = getpwnam(command->runas_user)) != NULL)
{
if (setuid(uid->pw_uid) == -1)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Cannot set uid to %d.\n", uid->pw_uid);
_exit(1);
}
}
else
{
sudo_log(SUDO_CONV_ERROR_MSG, "User %s not found.\n", command->runas_user);
_exit(1);
}
}
else
{
// Run as root
if (setuid(0) == -1)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Cannot set uid to %d.\n", 0);
_exit(1);
}
}
// Execute the command
if (execve(command->file, &command->argv[0], &command->envp[0]) < 0)
{
// Error
_exit(1);
}
// OK
_exit(0);
}
else // For the parent
{
int status;
// Wait for process to exit
waitpid(pid, &status, 0);
return (status == 0);
}
}
/*
Run editor for sudoedit
*/
static int run_editor(char * arg)
{
if (!arg)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Invalid command.\n");
return false;
}
/* Find editor in ENVP */
char * editor = find_editor(envp);
if (!editor)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Cannot find editor.\n");
return false;
}
char * file = strdup(arg);
if (!file)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Cannot allocate data.\n");
free(editor);
return false;
}
/* Create command */
command_data * cmd = make_command();
if (!cmd)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Cannot allocate data.\n");
free(editor);
free(file);
return false;
}
cmd->argv = malloc(3 * sizeof(char*));
if (!cmd->argv)
{
sudo_log(SUDO_CONV_ERROR_MSG, "Cannot allocate data.\n");
free(editor);
free(file);
free(cmd);
return false;
}
cmd->file = editor;
cmd->argv[0] = x_strdup(basename(editor));
cmd->argv[1] = file;
cmd->argv[2] = NULL;