1*b2b64449Skamil /* $NetBSD: passwd.c,v 1.53 2018/06/24 01:53:14 kamil Exp $ */
2f5646a08Schristos
3e42a9484Sjtc /*
4e42a9484Sjtc * Copyright (c) 1987, 1993, 1994, 1995
5e42a9484Sjtc * The Regents of the University of California. All rights reserved.
6e42a9484Sjtc *
7e42a9484Sjtc * Redistribution and use in source and binary forms, with or without
8e42a9484Sjtc * modification, are permitted provided that the following conditions
9e42a9484Sjtc * are met:
10e42a9484Sjtc * 1. Redistributions of source code must retain the above copyright
11e42a9484Sjtc * notice, this list of conditions and the following disclaimer.
12e42a9484Sjtc * 2. Redistributions in binary form must reproduce the above copyright
13e42a9484Sjtc * notice, this list of conditions and the following disclaimer in the
14e42a9484Sjtc * documentation and/or other materials provided with the distribution.
15eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
16e42a9484Sjtc * may be used to endorse or promote products derived from this software
17e42a9484Sjtc * without specific prior written permission.
18e42a9484Sjtc *
19e42a9484Sjtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20e42a9484Sjtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21e42a9484Sjtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22e42a9484Sjtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23e42a9484Sjtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24e42a9484Sjtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25e42a9484Sjtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26e42a9484Sjtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27e42a9484Sjtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28e42a9484Sjtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29e42a9484Sjtc * SUCH DAMAGE.
30e42a9484Sjtc */
31e42a9484Sjtc
32f5646a08Schristos #include <sys/cdefs.h>
33e42a9484Sjtc #if defined(LIBC_SCCS) && !defined(lint)
34*b2b64449Skamil __RCSID("$NetBSD: passwd.c,v 1.53 2018/06/24 01:53:14 kamil Exp $");
35e42a9484Sjtc #endif /* LIBC_SCCS and not lint */
36e42a9484Sjtc
37e42a9484Sjtc #include <sys/types.h>
38a3a468eeStron #include <sys/param.h>
39e42a9484Sjtc #include <sys/stat.h>
40e42a9484Sjtc #include <sys/time.h>
41e42a9484Sjtc #include <sys/resource.h>
42e42a9484Sjtc #include <sys/wait.h>
43e42a9484Sjtc
44b48252f3Slukem #include <assert.h>
453fe99e29Sthorpej #include <ctype.h>
468feb354eSmycroft #include <err.h>
47e42a9484Sjtc #include <errno.h>
4832e5dd1fSlukem #include <fcntl.h>
49e42a9484Sjtc #include <limits.h>
5032e5dd1fSlukem #include <paths.h>
5132e5dd1fSlukem #include <pwd.h>
524c3e77efSchristos #include <grp.h>
5332e5dd1fSlukem #include <signal.h>
5432e5dd1fSlukem #include <stdio.h>
5532e5dd1fSlukem #include <stdlib.h>
5632e5dd1fSlukem #include <string.h>
5732e5dd1fSlukem #include <unistd.h>
58e42a9484Sjtc #include <util.h>
59e42a9484Sjtc
60a3a468eeStron static const char *pw_filename(const char *filename);
6182be161eSad static void pw_cont(int sig);
62fa2088e5Schristos static const char * pw_equal(char *buf, struct passwd *old_pw);
632a9ac348Sad static const char *pw_default(const char *option);
642a9ac348Sad static int read_line(FILE *fp, char *line, int max);
652a9ac348Sad static void trim_whitespace(char *line);
66e42a9484Sjtc
67a3a468eeStron static char pw_prefix[MAXPATHLEN];
68a3a468eeStron
69a3a468eeStron const char *
pw_getprefix(void)70a3a468eeStron pw_getprefix(void)
71a3a468eeStron {
72a3a468eeStron
73a3a468eeStron return(pw_prefix);
74a3a468eeStron }
75a3a468eeStron
76a3a468eeStron int
pw_setprefix(const char * new_prefix)77a3a468eeStron pw_setprefix(const char *new_prefix)
78a3a468eeStron {
79a3a468eeStron size_t length;
80a3a468eeStron
81d06a762aSlukem _DIAGASSERT(new_prefix != NULL);
82d06a762aSlukem
83a3a468eeStron length = strlen(new_prefix);
84a3a468eeStron if (length < sizeof(pw_prefix)) {
85a3a468eeStron (void)strcpy(pw_prefix, new_prefix);
86a3a468eeStron while (length > 0 && pw_prefix[length - 1] == '/')
87a3a468eeStron pw_prefix[--length] = '\0';
88a3a468eeStron return(0);
89a3a468eeStron }
90a3a468eeStron errno = ENAMETOOLONG;
91a3a468eeStron return(-1);
92a3a468eeStron }
93a3a468eeStron
94a3a468eeStron static const char *
pw_filename(const char * filename)95a3a468eeStron pw_filename(const char *filename)
96a3a468eeStron {
97a3a468eeStron static char newfilename[MAXPATHLEN];
98a3a468eeStron
99d06a762aSlukem _DIAGASSERT(filename != NULL);
100d06a762aSlukem
101a3a468eeStron if (pw_prefix[0] == '\0')
102a3a468eeStron return filename;
103a3a468eeStron
104a3a468eeStron if (strlen(pw_prefix) + strlen(filename) < sizeof(newfilename))
105a3a468eeStron return strcat(strcpy(newfilename, pw_prefix), filename);
106a3a468eeStron
107a3a468eeStron errno = ENAMETOOLONG;
108a3a468eeStron return(NULL);
109a3a468eeStron }
110a3a468eeStron
111e42a9484Sjtc int
pw_lock(int retries)11282be161eSad pw_lock(int retries)
113e42a9484Sjtc {
114a3a468eeStron const char *filename;
115e42a9484Sjtc int i, fd;
116e42a9484Sjtc mode_t old_mode;
1176f4f1ab0Schristos int oerrno;
118e42a9484Sjtc
119e42a9484Sjtc /* Acquire the lock file. */
120a3a468eeStron filename = pw_filename(_PATH_MASTERPASSWD_LOCK);
121a3a468eeStron if (filename == NULL)
122a3a468eeStron return(-1);
123e42a9484Sjtc old_mode = umask(0);
124a3a468eeStron fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, 0600);
125e42a9484Sjtc for (i = 0; i < retries && fd < 0 && errno == EEXIST; i++) {
126e42a9484Sjtc sleep(1);
127a3a468eeStron fd = open(filename, O_WRONLY|O_CREAT|O_EXCL,
128e42a9484Sjtc 0600);
129e42a9484Sjtc }
1306f4f1ab0Schristos oerrno = errno;
1316f4f1ab0Schristos (void)umask(old_mode);
1326f4f1ab0Schristos errno = oerrno;
133e42a9484Sjtc return(fd);
134e42a9484Sjtc }
135e42a9484Sjtc
136e42a9484Sjtc int
pw_mkdb(const char * username,int secureonly)1379e66e6d7Sabs pw_mkdb(const char *username, int secureonly)
138e42a9484Sjtc {
1397e44d4f9Sad const char *args[9];
1407e44d4f9Sad int pstat, i;
141e42a9484Sjtc pid_t pid;
142e42a9484Sjtc
143e42a9484Sjtc pid = vfork();
144183e61b9Smjl if (pid == -1)
1456afe6deeSchristos return -1;
146183e61b9Smjl
147e42a9484Sjtc if (pid == 0) {
1487e44d4f9Sad args[0] = "pwd_mkdb";
1497e44d4f9Sad args[1] = "-d";
1507e44d4f9Sad args[2] = pw_prefix;
15190f5be5cSchristos args[3] = "-pl";
1527e44d4f9Sad i = 4;
1537e44d4f9Sad
1547e44d4f9Sad if (secureonly)
1557e44d4f9Sad args[i++] = "-s";
1567e44d4f9Sad if (username != NULL) {
1577e44d4f9Sad args[i++] = "-u";
1587e44d4f9Sad args[i++] = username;
1597e44d4f9Sad }
1607e44d4f9Sad
1617e44d4f9Sad args[i++] = pw_filename(_PATH_MASTERPASSWD_LOCK);
1627e44d4f9Sad args[i] = NULL;
163a5c1a01eSchristos execv(_PATH_PWD_MKDB, (char * const *)__UNCONST(args));
164fbb90399Sthorpej _exit(1);
165e42a9484Sjtc }
166e42a9484Sjtc pid = waitpid(pid, &pstat, 0);
1676afe6deeSchristos if (pid == -1) {
1686afe6deeSchristos warn("error waiting for pid %lu", (unsigned long)pid);
1696afe6deeSchristos return -1;
1706afe6deeSchristos }
1716afe6deeSchristos if (WIFEXITED(pstat)) {
1726afe6deeSchristos if (WEXITSTATUS(pstat) != 0) {
173322a9e79Schristos warnx("pwd_mkdb exited with status %d",
1746afe6deeSchristos WEXITSTATUS(pstat));
1756afe6deeSchristos return -1;
1766afe6deeSchristos }
1776afe6deeSchristos } else if (WIFSIGNALED(pstat)) {
1786afe6deeSchristos warnx("pwd_mkdb exited with signal %d", WTERMSIG(pstat));
1796afe6deeSchristos return -1;
1806afe6deeSchristos }
1816afe6deeSchristos return 0;
182e42a9484Sjtc }
183e42a9484Sjtc
184e42a9484Sjtc int
pw_abort(void)18582be161eSad pw_abort(void)
186e42a9484Sjtc {
187a3a468eeStron const char *filename;
18882be161eSad
189a3a468eeStron filename = pw_filename(_PATH_MASTERPASSWD_LOCK);
190a3a468eeStron return((filename == NULL) ? -1 : unlink(filename));
191e42a9484Sjtc }
192e42a9484Sjtc
193e42a9484Sjtc /* Everything below this point is intended for the convenience of programs
194e42a9484Sjtc * which allow a user to interactively edit the passwd file. Errors in the
195e42a9484Sjtc * routines below will cause the process to abort. */
196e42a9484Sjtc
197e42a9484Sjtc static pid_t editpid = -1;
198e42a9484Sjtc
199e42a9484Sjtc static void
pw_cont(int sig)20082be161eSad pw_cont(int sig)
201e42a9484Sjtc {
202e42a9484Sjtc
203e42a9484Sjtc if (editpid != -1)
204e42a9484Sjtc kill(editpid, sig);
205e42a9484Sjtc }
206e42a9484Sjtc
207e42a9484Sjtc void
pw_init(void)20882be161eSad pw_init(void)
209e42a9484Sjtc {
210e42a9484Sjtc struct rlimit rlim;
211e42a9484Sjtc
212e42a9484Sjtc /* Unlimited resource limits. */
213e42a9484Sjtc rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
214e42a9484Sjtc (void)setrlimit(RLIMIT_CPU, &rlim);
215e42a9484Sjtc (void)setrlimit(RLIMIT_FSIZE, &rlim);
216e42a9484Sjtc (void)setrlimit(RLIMIT_STACK, &rlim);
217e42a9484Sjtc (void)setrlimit(RLIMIT_DATA, &rlim);
218e42a9484Sjtc (void)setrlimit(RLIMIT_RSS, &rlim);
219e42a9484Sjtc
220e42a9484Sjtc /* Don't drop core (not really necessary, but GP's). */
221e42a9484Sjtc rlim.rlim_cur = rlim.rlim_max = 0;
222e42a9484Sjtc (void)setrlimit(RLIMIT_CORE, &rlim);
223e42a9484Sjtc
224e42a9484Sjtc /* Turn off signals. */
225e42a9484Sjtc (void)signal(SIGALRM, SIG_IGN);
226e42a9484Sjtc (void)signal(SIGHUP, SIG_IGN);
227e42a9484Sjtc (void)signal(SIGINT, SIG_IGN);
228e42a9484Sjtc (void)signal(SIGPIPE, SIG_IGN);
229e42a9484Sjtc (void)signal(SIGQUIT, SIG_IGN);
230e42a9484Sjtc (void)signal(SIGTERM, SIG_IGN);
231e42a9484Sjtc (void)signal(SIGCONT, pw_cont);
232e42a9484Sjtc }
233e42a9484Sjtc
234e42a9484Sjtc void
pw_edit(int notsetuid,const char * filename)23582be161eSad pw_edit(int notsetuid, const char *filename)
236e42a9484Sjtc {
237183e61b9Smjl int pstat;
238a5c1a01eSchristos char *p;
239ebed6f0bSchristos const char * volatile editor;
240a5c1a01eSchristos const char *argp[] = { "sh", "-c", NULL, NULL };
241183e61b9Smjl
2423fe99e29Sthorpej if (filename == NULL)
243e42a9484Sjtc filename = _PATH_MASTERPASSWD_LOCK;
244183e61b9Smjl
245a3a468eeStron filename = pw_filename(filename);
246a3a468eeStron if (filename == NULL)
247a3a468eeStron return;
248a3a468eeStron
2493fe99e29Sthorpej if ((editor = getenv("EDITOR")) == NULL)
250183e61b9Smjl editor = _PATH_VI;
251e42a9484Sjtc
252183e61b9Smjl p = malloc(strlen(editor) + 1 + strlen(filename) + 1);
253183e61b9Smjl if (p == NULL)
254183e61b9Smjl return;
2553fe99e29Sthorpej
256183e61b9Smjl sprintf(p, "%s %s", editor, filename);
257183e61b9Smjl argp[2] = p;
2583fe99e29Sthorpej
259183e61b9Smjl switch(editpid = vfork()) {
260183e61b9Smjl case -1:
261183e61b9Smjl free(p);
262183e61b9Smjl return;
263183e61b9Smjl case 0:
264e42a9484Sjtc if (notsetuid) {
265e42a9484Sjtc setgid(getgid());
266e42a9484Sjtc setuid(getuid());
267e42a9484Sjtc }
268a5c1a01eSchristos execvp(_PATH_BSHELL, (char *const *)__UNCONST(argp));
269e42a9484Sjtc _exit(1);
270e42a9484Sjtc }
271183e61b9Smjl
272183e61b9Smjl free(p);
273183e61b9Smjl
274e42a9484Sjtc for (;;) {
275e42a9484Sjtc editpid = waitpid(editpid, (int *)&pstat, WUNTRACED);
276e42a9484Sjtc if (editpid == -1)
277e42a9484Sjtc pw_error(editor, 1, 1);
278e42a9484Sjtc else if (WIFSTOPPED(pstat))
279e42a9484Sjtc raise(WSTOPSIG(pstat));
280e42a9484Sjtc else if (WIFEXITED(pstat) && WEXITSTATUS(pstat) == 0)
281e42a9484Sjtc break;
282e42a9484Sjtc else
283e42a9484Sjtc pw_error(editor, 1, 1);
284e42a9484Sjtc }
285e42a9484Sjtc editpid = -1;
286e42a9484Sjtc }
287e42a9484Sjtc
288e42a9484Sjtc void
pw_prompt(void)28982be161eSad pw_prompt(void)
290e42a9484Sjtc {
291e42a9484Sjtc int c;
292e42a9484Sjtc
293e42a9484Sjtc (void)printf("re-edit the password file? [y]: ");
294e42a9484Sjtc (void)fflush(stdout);
295e42a9484Sjtc c = getchar();
296e42a9484Sjtc if (c != EOF && c != '\n')
297e42a9484Sjtc while (getchar() != '\n');
298e42a9484Sjtc if (c == 'n')
299e42a9484Sjtc pw_error(NULL, 0, 0);
300e42a9484Sjtc }
301e42a9484Sjtc
30201d72f4aSphil /* for use in pw_copy(). Compare a pw entry to a pw struct. */
303fa2088e5Schristos /* returns a character string labelling the miscompared field or 0 */
304fa2088e5Schristos static const char *
pw_equal(char * buf,struct passwd * pw)30582be161eSad pw_equal(char *buf, struct passwd *pw)
306e42a9484Sjtc {
30701d72f4aSphil struct passwd buf_pw;
30875beed7eSelad size_t len;
309b48252f3Slukem
310b48252f3Slukem _DIAGASSERT(buf != NULL);
311b48252f3Slukem _DIAGASSERT(pw != NULL);
312b48252f3Slukem
313b48252f3Slukem len = strlen (buf);
31401d72f4aSphil if (buf[len-1] == '\n')
31501d72f4aSphil buf[len-1] = '\0';
31601d72f4aSphil if (!pw_scan(buf, &buf_pw, NULL))
317fa2088e5Schristos return "corrupt line";
318fa2088e5Schristos if (strcmp(pw->pw_name, buf_pw.pw_name) != 0)
319fa2088e5Schristos return "name";
320fa2088e5Schristos if (pw->pw_uid != buf_pw.pw_uid)
321fa2088e5Schristos return "uid";
322fa2088e5Schristos if (pw->pw_gid != buf_pw.pw_gid)
323fa2088e5Schristos return "gid";
324fa2088e5Schristos if (strcmp( pw->pw_class, buf_pw.pw_class) != 0)
325fa2088e5Schristos return "class";
326fa2088e5Schristos if (pw->pw_change != buf_pw.pw_change)
327fa2088e5Schristos return "change";
328fa2088e5Schristos if (pw->pw_expire != buf_pw.pw_expire)
329fa2088e5Schristos return "expire";
330fa2088e5Schristos if (strcmp( pw->pw_gecos, buf_pw.pw_gecos) != 0)
331fa2088e5Schristos return "gecos";
332fa2088e5Schristos if (strcmp( pw->pw_dir, buf_pw.pw_dir) != 0)
333fa2088e5Schristos return "dir";
334fa2088e5Schristos if (strcmp( pw->pw_shell, buf_pw.pw_shell) != 0)
335fa2088e5Schristos return "shell";
336fa2088e5Schristos return (char *)0;
33701d72f4aSphil }
33801d72f4aSphil
33901d72f4aSphil void
pw_copy(int ffd,int tfd,struct passwd * pw,struct passwd * old_pw)34082be161eSad pw_copy(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw)
34101d72f4aSphil {
3427c161da8Sthorpej char errbuf[200];
3437c161da8Sthorpej int rv;
3447c161da8Sthorpej
3457c161da8Sthorpej rv = pw_copyx(ffd, tfd, pw, old_pw, errbuf, sizeof(errbuf));
3467c161da8Sthorpej if (rv == 0) {
3477c161da8Sthorpej warnx("%s", errbuf);
3487c161da8Sthorpej pw_error(NULL, 0, 1);
3497c161da8Sthorpej }
3507c161da8Sthorpej }
3517c161da8Sthorpej
35250728e78Schristos static void
pw_print(FILE * to,const struct passwd * pw)35350728e78Schristos pw_print(FILE *to, const struct passwd *pw)
35450728e78Schristos {
35550728e78Schristos (void)fprintf(to, "%s:%s:%d:%d:%s:%lld:%lld:%s:%s:%s\n",
35650728e78Schristos pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid,
35750728e78Schristos pw->pw_class, (long long)pw->pw_change,
35850728e78Schristos (long long)pw->pw_expire,
35950728e78Schristos pw->pw_gecos, pw->pw_dir, pw->pw_shell);
36050728e78Schristos }
36150728e78Schristos
3627c161da8Sthorpej int
pw_copyx(int ffd,int tfd,struct passwd * pw,struct passwd * old_pw,char * errbuf,size_t errbufsz)3637c161da8Sthorpej pw_copyx(int ffd, int tfd, struct passwd *pw, struct passwd *old_pw,
3647c161da8Sthorpej char *errbuf, size_t errbufsz)
3657c161da8Sthorpej {
366a3a468eeStron const char *filename;
367a3a468eeStron char mpwd[MAXPATHLEN], mpwdl[MAXPATHLEN], *p, buf[8192];
368e42a9484Sjtc FILE *from, *to;
369e42a9484Sjtc int done;
370e42a9484Sjtc
371b48252f3Slukem _DIAGASSERT(pw != NULL);
3727c161da8Sthorpej _DIAGASSERT(errbuf != NULL);
373b48252f3Slukem /* old_pw may be NULL */
374b48252f3Slukem
3757c161da8Sthorpej if ((filename = pw_filename(_PATH_MASTERPASSWD)) == NULL) {
3767c161da8Sthorpej snprintf(errbuf, errbufsz, "%s: %s", pw_prefix,
3777c161da8Sthorpej strerror(errno));
3787c161da8Sthorpej return (0);
3797c161da8Sthorpej }
380a3a468eeStron (void)strcpy(mpwd, filename);
3817c161da8Sthorpej if ((filename = pw_filename(_PATH_MASTERPASSWD_LOCK)) == NULL) {
3827c161da8Sthorpej snprintf(errbuf, errbufsz, "%s: %s", pw_prefix,
3837c161da8Sthorpej strerror(errno));
3847c161da8Sthorpej return (0);
3857c161da8Sthorpej }
386a3a468eeStron (void)strcpy(mpwdl, filename);
387a3a468eeStron
3887c161da8Sthorpej if (!(from = fdopen(ffd, "r"))) {
3897c161da8Sthorpej snprintf(errbuf, errbufsz, "%s: %s", mpwd, strerror(errno));
3907c161da8Sthorpej return (0);
3917c161da8Sthorpej }
3927c161da8Sthorpej if (!(to = fdopen(tfd, "w"))) {
3937c161da8Sthorpej snprintf(errbuf, errbufsz, "%s: %s", mpwdl, strerror(errno));
394d7e635e0Selad (void)fclose(from);
3957c161da8Sthorpej return (0);
3967c161da8Sthorpej }
397e42a9484Sjtc
39875beed7eSelad for (done = 0; fgets(buf, (int)sizeof(buf), from);) {
399fa2088e5Schristos const char *neq;
400e42a9484Sjtc if (!strchr(buf, '\n')) {
4017c161da8Sthorpej snprintf(errbuf, errbufsz, "%s: line too long", mpwd);
402d7e635e0Selad (void)fclose(from);
403d7e635e0Selad (void)fclose(to);
4047c161da8Sthorpej return (0);
405e42a9484Sjtc }
406e42a9484Sjtc if (done) {
407e42a9484Sjtc (void)fprintf(to, "%s", buf);
4087c161da8Sthorpej if (ferror(to)) {
4097c161da8Sthorpej snprintf(errbuf, errbufsz, "%s",
4107c161da8Sthorpej strerror(errno));
411d7e635e0Selad (void)fclose(from);
412d7e635e0Selad (void)fclose(to);
4137c161da8Sthorpej return (0);
4147c161da8Sthorpej }
415e42a9484Sjtc continue;
416e42a9484Sjtc }
417e42a9484Sjtc if (!(p = strchr(buf, ':'))) {
4187c161da8Sthorpej snprintf(errbuf, errbufsz, "%s: corrupted entry", mpwd);
419d7e635e0Selad (void)fclose(from);
420d7e635e0Selad (void)fclose(to);
4217c161da8Sthorpej return (0);
422e42a9484Sjtc }
423e42a9484Sjtc *p = '\0';
424e42a9484Sjtc if (strcmp(buf, pw->pw_name)) {
425e42a9484Sjtc *p = ':';
426e42a9484Sjtc (void)fprintf(to, "%s", buf);
4277c161da8Sthorpej if (ferror(to)) {
4287c161da8Sthorpej snprintf(errbuf, errbufsz, "%s",
4297c161da8Sthorpej strerror(errno));
430d7e635e0Selad (void)fclose(from);
431d7e635e0Selad (void)fclose(to);
4327c161da8Sthorpej return (0);
4337c161da8Sthorpej }
434e42a9484Sjtc continue;
435e42a9484Sjtc }
43601d72f4aSphil *p = ':';
437fa2088e5Schristos if (old_pw && (neq = pw_equal(buf, old_pw)) != NULL) {
438fa2088e5Schristos if (strcmp(neq, "corrupt line") == 0)
439fa2088e5Schristos (void)snprintf(errbuf, errbufsz,
440fa2088e5Schristos "%s: entry %s corrupted", mpwd,
441fa2088e5Schristos pw->pw_name);
442fa2088e5Schristos else
443fa2088e5Schristos (void)snprintf(errbuf, errbufsz,
444fa2088e5Schristos "%s: entry %s inconsistent %s",
445fa2088e5Schristos mpwd, pw->pw_name, neq);
446d7e635e0Selad (void)fclose(from);
447d7e635e0Selad (void)fclose(to);
4487c161da8Sthorpej return (0);
44901d72f4aSphil }
45050728e78Schristos pw_print(to, pw);
451e42a9484Sjtc done = 1;
4527c161da8Sthorpej if (ferror(to)) {
4537c161da8Sthorpej snprintf(errbuf, errbufsz, "%s", strerror(errno));
454d7e635e0Selad (void)fclose(from);
455d7e635e0Selad (void)fclose(to);
4567c161da8Sthorpej return (0);
4577c161da8Sthorpej }
458e42a9484Sjtc }
45901d72f4aSphil /* Only append a new entry if real uid is root! */
4608d7b6b5dSthorpej if (!done) {
4617c161da8Sthorpej if (getuid() == 0) {
46250728e78Schristos pw_print(to, pw);
4637c161da8Sthorpej done = 1;
4647c161da8Sthorpej } else {
4657c161da8Sthorpej snprintf(errbuf, errbufsz,
4667c161da8Sthorpej "%s: changes not made, no such entry", mpwd);
4677c161da8Sthorpej }
4688d7b6b5dSthorpej }
469e42a9484Sjtc
4707c161da8Sthorpej if (ferror(to)) {
4717c161da8Sthorpej snprintf(errbuf, errbufsz, "%s", strerror(errno));
472d7e635e0Selad (void)fclose(from);
473d7e635e0Selad (void)fclose(to);
4747c161da8Sthorpej return (0);
4757c161da8Sthorpej }
476d7e635e0Selad (void)fclose(from);
477e42a9484Sjtc (void)fclose(to);
4787c161da8Sthorpej
4797c161da8Sthorpej return (done);
480e42a9484Sjtc }
481e42a9484Sjtc
482e42a9484Sjtc void
pw_error(const char * name,int error,int eval)4839ee8b0caSlukem pw_error(const char *name, int error, int eval)
484e42a9484Sjtc {
485b48252f3Slukem
4869ee8b0caSlukem if (error) {
48798290643Sitojun if (name)
488d68f3adeSsommerfeld warn("%s", name);
48998290643Sitojun else
49098290643Sitojun warn(NULL);
49198290643Sitojun }
492e42a9484Sjtc
493a3a468eeStron warnx("%s%s: unchanged", pw_prefix, _PATH_MASTERPASSWD);
494e42a9484Sjtc pw_abort();
495e42a9484Sjtc exit(eval);
496e42a9484Sjtc }
49782be161eSad
49882be161eSad /* Removes head and/or tail spaces. */
49982be161eSad static void
trim_whitespace(char * line)50082be161eSad trim_whitespace(char *line)
50182be161eSad {
50282be161eSad char *p;
50382be161eSad
504d06a762aSlukem _DIAGASSERT(line != NULL);
505d06a762aSlukem
506*b2b64449Skamil /* Handle empty string */
507*b2b64449Skamil if (*line == '\0')
508*b2b64449Skamil return;
509*b2b64449Skamil
51082be161eSad /* Remove leading spaces */
51182be161eSad p = line;
512f9863c47Sitohy while (isspace((unsigned char) *p))
51382be161eSad p++;
51482be161eSad memmove(line, p, strlen(p) + 1);
51582be161eSad
516*b2b64449Skamil /* Handle empty string after removal of whitespace characters */
517*b2b64449Skamil if (*line == '\0')
518*b2b64449Skamil return;
519*b2b64449Skamil
520*b2b64449Skamil /* Remove trailing spaces, line must not be empty string here */
52182be161eSad p = line + strlen(line) - 1;
522f9863c47Sitohy while (isspace((unsigned char) *p))
52382be161eSad p--;
52482be161eSad *(p + 1) = '\0';
52582be161eSad }
52682be161eSad
52782be161eSad
52882be161eSad /* Get one line, remove spaces from front and tail */
52982be161eSad static int
read_line(FILE * fp,char * line,int max)53082be161eSad read_line(FILE *fp, char *line, int max)
53182be161eSad {
53282be161eSad char *p;
53382be161eSad
534d06a762aSlukem _DIAGASSERT(fp != NULL);
535d06a762aSlukem _DIAGASSERT(line != NULL);
536d06a762aSlukem
53782be161eSad /* Read one line of config */
53882be161eSad if (fgets(line, max, fp) == NULL)
53982be161eSad return (0);
54082be161eSad
54182be161eSad if ((p = strchr(line, '\n')) == NULL) {
54282be161eSad warnx("line too long");
54382be161eSad return (0);
54482be161eSad }
54582be161eSad *p = '\0';
54682be161eSad
54782be161eSad /* Remove comments */
54882be161eSad if ((p = strchr(line, '#')) != NULL)
54982be161eSad *p = '\0';
55082be161eSad
55182be161eSad trim_whitespace(line);
55282be161eSad return (1);
55382be161eSad }
55482be161eSad
55582be161eSad static const char *
pw_default(const char * option)55682be161eSad pw_default(const char *option)
55782be161eSad {
55882be161eSad static const char *options[][2] = {
5598cde67ebSad { "localcipher", "old" },
56082be161eSad { "ypcipher", "old" },
56182be161eSad };
562c5eb4ab6Slukem size_t i;
56382be161eSad
564d06a762aSlukem _DIAGASSERT(option != NULL);
56582be161eSad for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
56682be161eSad if (strcmp(options[i][0], option) == 0)
56782be161eSad return (options[i][1]);
56882be161eSad
56982be161eSad return (NULL);
57082be161eSad }
57182be161eSad
57282be161eSad /*
57382be161eSad * Retrieve password information from the /etc/passwd.conf file, at the
57482be161eSad * moment this is only for choosing the cipher to use. It could easily be
57582be161eSad * used for other authentication methods as well.
57682be161eSad */
57782be161eSad void
pw_getconf(char * data,size_t max,const char * key,const char * option)57882be161eSad pw_getconf(char *data, size_t max, const char *key, const char *option)
57982be161eSad {
58082be161eSad FILE *fp;
58182be161eSad char line[LINE_MAX], *p, *p2;
58282be161eSad static char result[LINE_MAX];
58382be161eSad int got, found;
58482be161eSad const char *cp;
58582be161eSad
586d06a762aSlukem _DIAGASSERT(data != NULL);
587d06a762aSlukem _DIAGASSERT(key != NULL);
588d06a762aSlukem _DIAGASSERT(option != NULL);
589d06a762aSlukem
59082be161eSad got = 0;
59182be161eSad found = 0;
59282be161eSad result[0] = '\0';
59382be161eSad
594b2a1c144Slukem if ((fp = fopen(_PATH_PASSWD_CONF, "r")) == NULL) {
59582be161eSad if ((cp = pw_default(option)) != NULL)
596dbd0d289Sad strlcpy(data, cp, max);
59782be161eSad else
59882be161eSad data[0] = '\0';
59982be161eSad return;
60082be161eSad }
60182be161eSad
60282be161eSad while (!found && (got || read_line(fp, line, LINE_MAX))) {
60382be161eSad got = 0;
60482be161eSad
60582be161eSad if (strncmp(key, line, strlen(key)) != 0 ||
60682be161eSad line[strlen(key)] != ':')
60782be161eSad continue;
60882be161eSad
60982be161eSad /* Now we found our specified key */
61082be161eSad while (read_line(fp, line, LINE_MAX)) {
61182be161eSad /* Leaving key field */
612240f3596Sad if (line[0] != '\0' && strchr(line + 1, ':') != NULL) {
61382be161eSad got = 1;
61482be161eSad break;
61582be161eSad }
61682be161eSad p2 = line;
61782be161eSad if ((p = strsep(&p2, "=")) == NULL || p2 == NULL)
61882be161eSad continue;
61982be161eSad trim_whitespace(p);
62082be161eSad
62182be161eSad if (!strncmp(p, option, strlen(option))) {
62282be161eSad trim_whitespace(p2);
62382be161eSad strcpy(result, p2);
62482be161eSad found = 1;
62582be161eSad break;
62682be161eSad }
62782be161eSad }
62882be161eSad }
62982be161eSad fclose(fp);
63082be161eSad
631dd3453b4Selad if (!found)
632dd3453b4Selad errno = ENOENT;
633dd3453b4Selad if (!got)
634dd3453b4Selad errno = ENOTDIR;
635dd3453b4Selad
63682be161eSad /*
63782be161eSad * If we got no result and were looking for a default
63882be161eSad * value, try hard coded defaults.
63982be161eSad */
64082be161eSad
64182be161eSad if (strlen(result) == 0 && strcmp(key, "default") == 0 &&
64282be161eSad (cp = pw_default(option)) != NULL)
643dbd0d289Sad strlcpy(data, cp, max);
64482be161eSad else
64582be161eSad strlcpy(data, result, max);
64682be161eSad }
6474c3e77efSchristos
6484c3e77efSchristos void
pw_getpwconf(char * data,size_t max,const struct passwd * pwd,const char * option)6494c3e77efSchristos pw_getpwconf(char *data, size_t max, const struct passwd *pwd,
6504c3e77efSchristos const char *option)
6514c3e77efSchristos {
6524c3e77efSchristos char grpkey[LINE_MAX];
6533ff3472eSchristos struct group grs, *grp;
6543ff3472eSchristos char grbuf[1024];
6554c3e77efSchristos
6564c3e77efSchristos pw_getconf(data, max, pwd->pw_name, option);
6574c3e77efSchristos
6584c3e77efSchristos /* Try to find an entry for the group */
6594c3e77efSchristos if (*data == '\0') {
6603ff3472eSchristos (void)getgrgid_r(pwd->pw_gid, &grs, grbuf, sizeof(grbuf), &grp);
6613ff3472eSchristos if (grp != NULL) {
6623ff3472eSchristos (void)snprintf(grpkey, sizeof(grpkey), ":%s",
6633ff3472eSchristos grp->gr_name);
6644c3e77efSchristos pw_getconf(data, max, grpkey, option);
6654c3e77efSchristos }
666163d05f5Schristos if (*data == '\0')
6674c3e77efSchristos pw_getconf(data, max, "default", option);
6684c3e77efSchristos }
6694c3e77efSchristos }
670