1dd39ce01d973e5f8268cf6775fab909ce12fb49
[musl] / src / passwd / getspnam_r.c
1 #include <fcntl.h>
2 #include <unistd.h>
3 #include "pwf.h"
4
5 /* This implementation support Openwall-style TCB passwords in place of
6  * traditional shadow, if the appropriate directories and files exist.
7  * Thus, it is careful to avoid following symlinks or blocking on fifos
8  * which a malicious user might create in place of his or her TCB shadow
9  * file. It also avoids any allocation to prevent memory-exhaustion
10  * attacks via huge TCB shadow files. */
11
12 int getspnam_r(const char *name, struct spwd *sp, char *buf, size_t size, struct spwd **res)
13 {
14         char path[20+NAME_MAX];
15         FILE *f = 0;
16         int rv = 0;
17         int fd;
18         size_t k, l = strlen(name);
19         char *s;
20         int skip = 0;
21
22         *res = 0;
23
24         /* Disallow potentially-malicious user names */
25         if (*name=='.' || strchr(name, '/') || !l)
26                 return EINVAL;
27
28         /* Buffer size must at least be able to hold name, plus some.. */
29         if (size < l+100) return ERANGE;
30
31         /* Protect against truncation */
32         if (snprintf(path, sizeof path, "/etc/tcb/%s/shadow", name) >= sizeof path)
33                 return EINVAL;
34
35         fd = open(path, O_RDONLY|O_NOFOLLOW|O_NONBLOCK);
36         if (fd >= 0) {
37                 f = fdopen(fd, "rb");
38                 if (!f) {
39                         close(fd);
40                         return errno;
41                 }
42         } else {
43                 f = fopen("/etc/shadow", "rb");
44                 if (!f) return errno;
45         }
46
47         while (fgets(buf, size, f) && (k=strlen(buf))>0) {
48                 if (skip || strncmp(name, buf, l)) {
49                         skip = buf[k-1] != '\n';
50                         continue;
51                 }
52                 if (buf[k-1] != '\n') {
53                         rv = ERANGE;
54                         break;
55                 }
56                 buf[k-1] = 0;
57
58                 s = buf;
59                 sp->sp_namp = s;
60                 if (!(s = strchr(s, ':'))) continue;
61
62                 *s++ = 0; sp->sp_pwdp = s;
63                 if (!(s = strchr(s, ':'))) continue;
64
65                 *s++ = 0; sp->sp_lstchg = atol(s);
66                 if (!(s = strchr(s, ':'))) continue;
67
68                 *s++ = 0; sp->sp_min = atol(s);
69                 if (!(s = strchr(s, ':'))) continue;
70
71                 *s++ = 0; sp->sp_max = atol(s);
72                 if (!(s = strchr(s, ':'))) continue;
73
74                 *s++ = 0; sp->sp_warn = atol(s);
75                 if (!(s = strchr(s, ':'))) continue;
76
77                 *s++ = 0; sp->sp_inact = atol(s);
78                 if (!(s = strchr(s, ':'))) continue;
79
80                 *s++ = 0; sp->sp_expire = atol(s);
81                 if (!(s = strchr(s, ':'))) continue;
82
83                 *s++ = 0; sp->sp_flag = atol(s);
84                 *res = sp;
85                 break;
86         }
87         fclose(f);
88         return rv;
89 }