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