xref: /openbsd/usr.sbin/amd/amd/info_passwd.c (revision 898184e3)
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.8 2010/03/30 17:10:37 zinovik 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 #ifdef HAS_PASSWD_MAPS
48 #include <pwd.h>
49 
50 #define	PASSWD_MAP	"/etc/passwd"
51 
52 /*
53  * Nothing to probe - check the map name is PASSWD_MAP.
54  */
55 int
56 passwd_init(char *map, time_t *tp)
57 {
58 	*tp = 0;
59 	return strcmp(map, PASSWD_MAP) == 0 ? 0 : ENOENT;
60 }
61 
62 
63 /*
64  * Grab the entry via the getpwname routine
65  * Modify time is ignored by passwd - XXX
66  */
67 int
68 passwd_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
69 {
70 	struct passwd *pw;
71 	char *dir = 0;
72 
73 	if (strcmp(key, "/defaults") == 0) {
74 		*pval = strdup("type:=nfs");
75 		return 0;
76 	}
77 
78 	pw = getpwnam(key);
79 	if (pw) {
80 		/*
81 		 * We chop the home directory up as follows:
82 		 * /anydir/dom1/dom2/dom3/user
83 		 *
84 		 * and return
85 		 * rfs:=/anydir/dom3;rhost:=dom3.dom2.dom1;sublink:=user
86 		 *
87 		 * This allows cross-domain entries in your passwd file.
88 		 * ... but forget about security!
89 		 */
90 		char val[MAXPATHLEN], rhost[MAXHOSTNAMELEN];
91 		char *user, *p, *q;
92 
93 		dir = strdup(pw->pw_dir);
94 		/*
95 		 * Find user name.  If no / then Invalid...
96 		 */
97 		user = strrchr(dir, '/');
98 		if (!user)
99 			goto enoent;
100 		*user++ = '\0';
101 		/*
102 		 * Find start of host "path".  If no / then Invalid...
103 		 */
104 		p = strchr(dir+1, '/');
105 		if (!p)
106 			goto enoent;
107 		*p++ = '\0';
108 		/*
109 		 * At this point, p is dom1/dom2/dom3
110 		 * Copy, backwards, into rhost replacing
111 		 * / with .
112 		 */
113 		rhost[0] = '\0';
114 		do {
115 			q = strrchr(p, '/');
116 			if (q) {
117 				strlcat(rhost, q + 1, sizeof(rhost));
118 				strlcat(rhost, ".", sizeof(rhost));
119 				*q = '\0';
120 			} else {
121 				strlcat(rhost, p, sizeof(rhost));
122 			}
123 		} while (q);
124 
125 		/*
126 		 * Sanity check
127 		 */
128 		if (*rhost == '\0' || *user == '\0' || *dir == '\0')
129 			goto enoent;
130 		/*
131 		 * Make up return string
132 		 */
133 		q = strchr(rhost, '.');
134 		if (q)
135 			*q = '\0';
136 		snprintf(val, sizeof(val),
137 		    "rfs:=%s/%s;rhost:=%s;sublink:=%s;fs:=${autodir}%s",
138 		    dir, rhost, rhost, user, pw->pw_dir);
139 		free(dir);
140 		if (q)
141 			*q = '.';
142 		*pval = strdup(val);
143 		return 0;
144 	}
145 
146 enoent:
147 	if (dir)
148 		free(dir);
149 
150 	return ENOENT;
151 }
152 #endif /* HAS_PASSWD_MAPS */
153