xref: /openbsd-src/usr.sbin/amd/amd/info_passwd.c (revision bf0193d8cd0ca4e683146c29a671bf62f193ec92)
1 /*
2  * Copyright (c) 1990 Jan-Simon Pendry
3  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Jan-Simon Pendry at Imperial College, London.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)info_passwd.c	8.1 (Berkeley) 6/6/93
35  *	$Id: info_passwd.c,v 1.11 2021/10/21 10:55:56 deraadt Exp $
36  */
37 
38 /*
39  * Get info from password "file"
40  *
41  * This is experimental and probably doesn't
42  * do what you expect.
43  */
44 
45 #include "am.h"
46 
47 #include <pwd.h>
48 
49 #define	PASSWD_MAP	"/etc/passwd"
50 
51 /*
52  * Nothing to probe - check the map name is PASSWD_MAP.
53  */
54 int
passwd_init(char * map,time_t * tp)55 passwd_init(char *map, time_t *tp)
56 {
57 	*tp = 0;
58 	return strcmp(map, PASSWD_MAP) == 0 ? 0 : ENOENT;
59 }
60 
61 
62 /*
63  * Grab the entry via the getpwname routine
64  * Modify time is ignored by passwd - XXX
65  */
66 int
passwd_search(mnt_map * m,char * map,char * key,char ** pval,time_t * tp)67 passwd_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
68 {
69 	struct passwd *pw;
70 	char *dir = 0;
71 
72 	if (strcmp(key, "/defaults") == 0) {
73 		*pval = strdup("type:=nfs");
74 		return 0;
75 	}
76 
77 	pw = getpwnam(key);
78 	if (pw) {
79 		/*
80 		 * We chop the home directory up as follows:
81 		 * /anydir/dom1/dom2/dom3/user
82 		 *
83 		 * and return
84 		 * rfs:=/anydir/dom3;rhost:=dom3.dom2.dom1;sublink:=user
85 		 *
86 		 * This allows cross-domain entries in your passwd file.
87 		 * ... but forget about security!
88 		 */
89 		char val[PATH_MAX], rhost[HOST_NAME_MAX+1];
90 		char *user, *p, *q;
91 
92 		dir = strdup(pw->pw_dir);
93 		/*
94 		 * Find user name.  If no / then Invalid...
95 		 */
96 		user = strrchr(dir, '/');
97 		if (!user)
98 			goto enoent;
99 		*user++ = '\0';
100 		/*
101 		 * Find start of host "path".  If no / then Invalid...
102 		 */
103 		p = strchr(dir+1, '/');
104 		if (!p)
105 			goto enoent;
106 		*p++ = '\0';
107 		/*
108 		 * At this point, p is dom1/dom2/dom3
109 		 * Copy, backwards, into rhost replacing
110 		 * / with .
111 		 */
112 		rhost[0] = '\0';
113 		do {
114 			q = strrchr(p, '/');
115 			if (q) {
116 				strlcat(rhost, q + 1, sizeof(rhost));
117 				strlcat(rhost, ".", sizeof(rhost));
118 				*q = '\0';
119 			} else {
120 				strlcat(rhost, p, sizeof(rhost));
121 			}
122 		} while (q);
123 
124 		/*
125 		 * Sanity check
126 		 */
127 		if (*rhost == '\0' || *user == '\0' || *dir == '\0')
128 			goto enoent;
129 		/*
130 		 * Make up return string
131 		 */
132 		q = strchr(rhost, '.');
133 		if (q)
134 			*q = '\0';
135 		snprintf(val, sizeof(val),
136 		    "rfs:=%s/%s;rhost:=%s;sublink:=%s;fs:=${autodir}%s",
137 		    dir, rhost, rhost, user, pw->pw_dir);
138 		free(dir);
139 		if (q)
140 			*q = '.';
141 		*pval = strdup(val);
142 		return 0;
143 	}
144 
145 enoent:
146 	free(dir);
147 
148 	return ENOENT;
149 }
150