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
|
/* password.c - password read/update helper functions.
*
* Copyright 2012 Ashwini Kumar <ak.ashwini@gmail.com>
*/
#include "toys.h"
#include <time.h>
// generate appropriate random salt string for given encryption algorithm.
int get_salt(char *salt, char *algo)
{
struct {
char *type, id, len;
} al[] = {{"des", 0, 2}, {"md5", 1, 8}, {"sha256", 5, 16}, {"sha512", 6, 16}};
int i;
for (i = 0; i < ARRAY_LEN(al); i++) {
if (!strcmp(algo, al[i].type)) {
int len = al[i].len;
char *s = salt;
if (al[i].id) {
*s++ = '$';
*s++ = '0'+al[i].id;
*s++ = '$';
}
// Read appropriate number of random bytes for salt
i = xopen("/dev/urandom", O_RDONLY);
xreadall(i, libbuf, ((len*6)+7)/8);
close(i);
// Grab 6 bit chunks and convert to characters in ./0-9a-zA-Z
for (i=0; i<len; i++) {
int bitpos = i*6, bits = bitpos/8;
bits = ((libbuf[i]+(libbuf[i+1]<<8)) >> (bitpos&7)) & 0x3f;
bits += 46;
if (bits > 57) bits += 7;
if (bits > 90) bits += 6;
s[i] = bits;
}
salt[len] = 0;
return s-salt;
}
}
return -1;
}
// Reset terminal to known state, returning old state if old != NULL.
int set_terminal(int fd, int raw, struct termios *old)
{
struct termios termio;
if (!tcgetattr(fd, &termio) && old) *old = termio;
// the following are the bits set for an xterm. Linux text mode TTYs by
// default add two additional bits that only matter for serial processing
// (turn serial line break into an interrupt, and XON/XOFF flow control)
// Any key unblocks output, swap CR and NL on input
termio.c_iflag = IXANY|ICRNL|INLCR;
if (toys.which->flags & TOYFLAG_LOCALE) termio.c_iflag |= IUTF8;
// Output appends CR to NL, does magic undocumented postprocessing
termio.c_oflag = ONLCR|OPOST;
// Leave serial port speed alone
// termio.c_cflag = C_READ|CS8|EXTB;
// Generate signals, input entire line at once, echo output
// erase, line kill, escape control characters with ^
// erase line char at a time
// "extended" behavior: ctrl-V quotes next char, ctrl-R reprints unread chars,
// ctrl-W erases word
termio.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHOCTL|ECHOKE|IEXTEN;
if (raw) cfmakeraw(&termio);
return tcsetattr(fd, TCSANOW, &termio);
}
// Prompt with mesg, read password into buf, return 0 for success 1 for fail
int read_password(char *buf, int buflen, char *mesg)
{
struct termios oldtermio;
struct sigaction sa, oldsa;
int i, ret = 1;
// NOP signal handler to return from the read
memset(&sa, 0, sizeof(sa));
sa.sa_handler = generic_signal;
sigaction(SIGINT, &sa, &oldsa);
tcflush(0, TCIFLUSH);
set_terminal(0, 1, &oldtermio);
xprintf("%s", mesg);
for (i=0; i < buflen-1; i++) {
if ((ret = read(0, buf+i, 1)) < 0 || (!ret && !i)) {
i = 0;
ret = 1;
break;
} else if (!ret || buf[i] == '\n' || buf[i] == '\r') {
ret = 0;
break;
} else if (buf[i] == 8 || buf[i] == 127) i -= i ? 2 : 1;
}
// Restore terminal/signal state, terminate string
sigaction(SIGINT, &oldsa, NULL);
tcsetattr(0, TCSANOW, &oldtermio);
buf[i] = 0;
xputc('\n');
return ret;
}
static char *get_nextcolon(char *line, int cnt)
{
while (cnt--) {
if (!(line = strchr(line, ':'))) error_exit("Invalid Entry\n");
line++; //jump past the colon
}
return line;
}
/*update_password is used by multiple utilities to update /etc/passwd,
* /etc/shadow, /etc/group and /etc/gshadow files,
* which are used as user, group databeses
* entry can be
* 1. encrypted password, when updating user password.
* 2. complete entry for user details, when creating new user
* 3. group members comma',' separated list, when adding user to group
* 4. complete entry for group details, when creating new group
* 5. entry = NULL, delete the named entry user/group
*/
int update_password(char *filename, char* username, char* entry)
{
char *filenamesfx = NULL, *namesfx = NULL, *shadow = NULL,
*sfx = NULL, *line = NULL;
FILE *exfp, *newfp;
int ret = -1, found = 0;
struct flock lock;
shadow = strstr(filename, "shadow");
filenamesfx = xmprintf("%s+", filename);
sfx = strchr(filenamesfx, '+');
exfp = fopen(filename, "r+");
if (!exfp) {
perror_msg("Couldn't open file %s",filename);
goto free_storage;
}
*sfx = '-';
ret = unlink(filenamesfx);
ret = link(filename, filenamesfx);
if (ret < 0) error_msg("can't create backup file");
*sfx = '+';
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
ret = fcntl(fileno(exfp), F_SETLK, &lock);
if (ret < 0) perror_msg("Couldn't lock file %s",filename);
lock.l_type = F_UNLCK; //unlocking at a later stage
newfp = fopen(filenamesfx, "w+");
if (!newfp) {
error_msg("couldn't open file for writing");
ret = -1;
fclose(exfp);
goto free_storage;
}
ret = 0;
namesfx = xmprintf("%s:",username);
while ((line = get_line(fileno(exfp))) != NULL)
{
if (strncmp(line, namesfx, strlen(namesfx)))
fprintf(newfp, "%s\n", line);
else if (entry) {
char *current_ptr = NULL;
found = 1;
if (!strcmp(toys.which->name, "passwd")) {
fprintf(newfp, "%s%s:",namesfx, entry);
current_ptr = get_nextcolon(line, 2); //past passwd
if (shadow) {
fprintf(newfp, "%u:",(unsigned)(time(NULL))/(24*60*60));
current_ptr = get_nextcolon(current_ptr, 1);
fprintf(newfp, "%s\n",current_ptr);
} else fprintf(newfp, "%s\n",current_ptr);
} else if (!strcmp(toys.which->name, "groupadd") ||
!strcmp(toys.which->name, "addgroup") ||
!strcmp(toys.which->name, "delgroup") ||
!strcmp(toys.which->name, "groupdel")){
current_ptr = get_nextcolon(line, 3); //past gid/admin list
*current_ptr = '\0';
fprintf(newfp, "%s", line);
fprintf(newfp, "%s\n", entry);
}
}
free(line);
}
free(namesfx);
if (!found && entry) fprintf(newfp, "%s\n", entry);
fcntl(fileno(exfp), F_SETLK, &lock);
fclose(exfp);
errno = 0;
fflush(newfp);
fsync(fileno(newfp));
fclose(newfp);
rename(filenamesfx, filename);
if (errno) {
perror_msg("File Writing/Saving failed: ");
unlink(filenamesfx);
ret = -1;
}
free_storage:
free(filenamesfx);
return ret;
}
void is_valid_username(const char *name)
{
regex_t rp;
regmatch_t rm[1];
int eval;
char *regex = "^[_.A-Za-z0-9][-_.A-Za-z0-9]*"; //User name REGEX
xregcomp(&rp, regex, REG_NEWLINE);
/* compare string against pattern -- remember that patterns
are anchored to the beginning of the line */
eval = regexec(&rp, name, 1, rm, 0);
regfree(&rp);
if (!eval && !rm[0].rm_so) {
int len = strlen(name);
if ((rm[0].rm_eo == len) ||
(rm[0].rm_eo == len - 1 && name[len - 1] == '$')) {
if (len >= LOGIN_NAME_MAX) error_exit("name is too long");
else return;
}
}
error_exit("'%s', not valid %sname",name,
(((toys.which->name[3] == 'g') ||
(toys.which->name[0] == 'g'))? "group" : "user"));
}
|