-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadduser.c
More file actions
160 lines (148 loc) · 4.28 KB
/
Copy pathadduser.c
File metadata and controls
160 lines (148 loc) · 4.28 KB
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
#define _GNU_SOURCE
#define _DEFAULT_SOURCE
#include "password.h"
#include <ctype.h>
#include <grp.h>
#include <pwd.h>
#include <shadow.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define HOMEPREFIX "/home/"
int main(int argc, char **argv) {
if (geteuid() != 0) {
fprintf(stderr, "(%s: must be run as root)\n", argv[0]);
return EXIT_FAILURE;
}
if (argc != 2) {
fprintf(stderr, "usage: %s <login>\n", argv[0]);
return EXIT_FAILURE;
}
char *login = argv[1];
if (*login >= '0' && *login <= '9') {
fprintf(stderr, "(%s: login cannot start with number)\n", argv[0]);
return EXIT_FAILURE;
}
for (char *check = login; *check != '\0'; check++) {
if ((*check < '0' || *check > '9') && (*check < 'a' || *check > 'z')) {
fprintf(stderr, "(%s: login must be lowercase alphanumeric)\n", argv[0]);
return EXIT_FAILURE;
}
}
// get password and confirm it
char *password = NULL;
for (int i = 0; i < 2; i++) {
char *p = NULL;
size_t cap = 0;
if (!password)
printf("password: ");
else
printf("confirm password: ");
fflush(NULL);
ssize_t len = getpassword(&p, &cap, stdin);
if (len <= 0) {
fprintf(stderr, "(%s: cannot read: %s)\n", argv[0], strerror(errno));
abort();
}
while (isspace(p[len - 1]))
p[--len] = '\0'; // trim end
if (!password)
password = p;
else if (strcmp(password, p) != 0) {
// confirm password failed
fprintf(stderr, "(%s: mismatched password)\n", argv[0]);
return EXIT_FAILURE;
} else
free(p);
}
// make sure the user does not already exist
size_t loginlen = strlen(login);
errno = 0;
if (getpwnam(login) != NULL) {
fprintf(stderr, "(%s: user already exists)\n", argv[0]);
return EXIT_FAILURE;
} else if (errno != 0) {
fprintf(stderr, "(%s: cannot getpwnam: %s)\n", argv[0], strerror(errno));
abort();
}
// find a new uid for the user
uid_t maxid = 999;
struct passwd *pwd;
setpwent();
while ((pwd = getpwent()) != NULL)
if (pwd->pw_uid > maxid)
maxid = pwd->pw_uid;
endpwent();
setgrent();
struct group *grp;
while ((grp = getgrent()) != NULL)
if (grp->gr_gid > maxid)
maxid = grp->gr_gid;
endgrent();
// create the password hash if password isn't blank
char *hash = NULL;
if (*password && newpassword(password, &hash, 6) != 0) {
fprintf(stderr, "(%s: could not make new password)\n", argv[0]);
return EXIT_FAILURE;
}
// emplace new /etc/passwd entry
char *homedir = malloc(sizeof(HOMEPREFIX) + loginlen);
memcpy(homedir, HOMEPREFIX, sizeof(HOMEPREFIX));
memcpy(&homedir[sizeof(HOMEPREFIX) - 1], login, loginlen + 1);
struct passwd newpwd = {0};
newpwd.pw_name = login;
newpwd.pw_passwd = hash ? "x" : "";
newpwd.pw_uid = maxid + 1;
newpwd.pw_gid = maxid + 1;
newpwd.pw_gecos = "";
newpwd.pw_dir = homedir;
newpwd.pw_shell = "/usr/bin/sh";
FILE *fpasswd = fopen("/etc/passwd", "a");
if (fpasswd == NULL) {
fprintf(stderr, "(%s: cannot open /etc/passwd: %s)\n", argv[0],
strerror(errno));
abort();
}
putpwent(&newpwd, fpasswd);
fclose(fpasswd);
// emplace new /etc/group entry
char *mem[] = {NULL};
struct group newgrp = {0};
newgrp.gr_name = login;
newgrp.gr_passwd = "x";
newgrp.gr_gid = maxid + 1;
newgrp.gr_mem = mem;
FILE *fgroup = fopen("/etc/group", "a");
if (fgroup == NULL) {
fprintf(stderr, "(%s: cannot open /etc/group: %s)\n", argv[0],
strerror(errno));
abort();
}
putgrent(&newgrp, fgroup);
fclose(fgroup);
// emplace new /etc/shadow entry (if required)
if (hash) {
struct spwd newshdw = {0};
newshdw.sp_namp = login;
newshdw.sp_pwdp = hash;
// TODO: set additional flags
FILE *fshadow = fopen("/etc/shadow", "a");
if (fshadow == NULL) {
fprintf(stderr, "(%s: cannot open /etc/shadow: %s)\n", argv[0],
strerror(errno));
abort();
}
putspent(&newshdw, fshadow);
fclose(fshadow);
}
// make the home directory for the new user
mkdir("/home", 0755);
if (mkdir(homedir, 0700) < 0 || chown(homedir, maxid + 1, maxid + 1) < 0) {
fprintf(stderr, "(%s: cannot make home directory: %s)\n", argv[0],
strerror(errno));
abort();
}
return EXIT_SUCCESS;
}