-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdouser.c
102 lines (89 loc) · 2.71 KB
/
douser.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
/*
Write a set-user-ID-root program similar to the sudo(8) program.
This program should take command-line options and arguments as follows:
$ ./douser [-u user ] program-file arg1 arg2 ...
The douser program executes program-file, with the given arguments, as though it was run by user.
(If the –u option is omitted, then user should default to root.)
Before executing program-file, douser should request the password for user,
authenticate it against the standard password file (see Listing 8-2, on page 164),
and then set all of the process user and group IDs to the correct values for that user
*/
#define _BSD_SOURCE /* Get getpass() declaration from <unistd.h> */
#include <unistd.h>
#include <crypt.h>
#include <stdio.h>
#include <pwd.h>
#include <limits.h>
#include <shadow.h>
#include <errno.h>
#include "tlpi_hdr.h"
#include "error_functions.c"
/*
authenticate prompts the user for the password
and validates it, returning 0 on success.
*/
int
authenticate(char *username)
{
struct spwd *spwd;
struct passwd *pwd;
char *encrypted;
Boolean authOk;
pwd = getpwnam(username);
if (pwd == NULL)
fatal("could not get password file");
spwd = getspnam(username);
if (spwd == NULL && errno == EACCES)
fatal("no permission to read shadow password file");
if (spwd != NULL)
pwd->pw_passwd = spwd->sp_pwdp;
encrypted = crypt(getpass("Password:"), pwd->pw_passwd);
return strcmp(encrypted, pwd->pw_passwd);
}
int
main(int argc, char **argv)
{
char *envp[] = {"PATH=/bin:/usr/bin", NULL};
int c, status;
char *username = NULL;
struct passwd *pwd;
pid_t pid;
if (argc < 2) {
printf("Usage: %s [-u user] cmd args...\n", argv[0]);
exit(1);
}
while ((c = getopt (argc, argv, "u:")) != -1)
switch (c) {
case 'u':
username = optarg;
break;
case '?':
if (optopt == 'u')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
}
if (username == NULL) {
username = "root\0";
}
if (authenticate(username) != 0) {
printf("Incorrect password\n");
exit(EXIT_FAILURE);
}
pwd = getpwnam(username);
if (pwd == NULL)
fatal("unable to retrieve user info");
if (setuid(pwd->pw_uid) != 0)
errExit("setuid");
pid = fork();
if (pid == -1) {
errExit("fork");
} else if (pid != 0) {
execvpe(argv[optind], &argv[optind], envp);
errExit("execvpe");
}
exit(EXIT_SUCCESS);
}