xref: /original-bsd/usr.sbin/amd/amd/info_passwd.c (revision b001ed0c)
1 /*
2  * $Id: info_passwd.c,v 5.2 90/06/23 22:19:34 jsp Rel $
3  *
4  * Copyright (c) 1990 Jan-Simon Pendry
5  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
6  * Copyright (c) 1990 The Regents of the University of California.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Jan-Simon Pendry at Imperial College, London.
11  *
12  * %sccs.include.redist.c%
13  *
14  *	@(#)info_passwd.c	5.1 (Berkeley) 06/29/90
15  */
16 
17 /*
18  * Get info from password "file"
19  *
20  * This is experimental and probably doesn't
21  * do what you expect.
22  */
23 
24 #include "am.h"
25 
26 #ifdef HAS_PASSWD_MAPS
27 #include <pwd.h>
28 
29 #define	PASSWD_MAP	"/etc/passwd"
30 
31 /*
32  * Nothing to probe - check the map name is PASSWD_MAP.
33  */
34 passwd_init(map)
35 char *map;
36 {
37 	return strcmp(map, PASSWD_MAP) == 0 ? 0 : ENOENT;
38 }
39 
40 
41 /*
42  * Grab the entry via the getpwname routine
43  * Modify time is ignored by passwd - XXX
44  */
45 int passwd_search(m, map, key, pval, tp)
46 mnt_map *m;
47 char *map;
48 char *key;
49 char **pval;
50 time_t *tp;
51 {
52 	char *dir = 0;
53 	struct passwd *pw;
54 	if (strcmp(key, "/defaults") == 0) {
55 		*pval = strdup("type:=nfs");
56 		return 0;
57 	}
58 
59 	pw = getpwnam(key);
60 	if (pw) {
61 		/*
62 		 * We chop the home directory up as follows:
63 		 * /anydir/dom1/dom2/dom3/user
64 		 *
65 		 * and return
66 		 * rfs:=/anydir/dom3;rhost:=dom3.dom2.dom1;sublink:=user
67 		 *
68 		 * This allows cross-domain entries in your passwd file.
69 		 * ... but forget about security!
70 		 */
71 		char *user;
72 		char *p, *q;
73 		char val[MAXPATHLEN];
74 		char rhost[MAXHOSTNAMELEN];
75 		dir = strdup(pw->pw_dir);
76 		/*
77 		 * Find user name.  If no / then Invalid...
78 		 */
79 		user = strrchr(dir, '/');
80 		if (!user)
81 			goto enoent;
82 		*user++ = '\0';
83 		/*
84 		 * Find start of host "path".  If no / then Invalid...
85 		 */
86 		p = strchr(dir+1, '/');
87 		if (!p)
88 			goto enoent;
89 		*p++ = '\0';
90 		/*
91 		 * At this point, p is dom1/dom2/dom3
92 		 * Copy, backwards, into rhost replacing
93 		 * / with .
94 		 */
95 		rhost[0] = '\0';
96 		do {
97 			q = strrchr(p, '/');
98 			if (q) {
99 				strcat(rhost, q + 1);
100 				strcat(rhost, ".");
101 				*q = '\0';
102 			} else {
103 				strcat(rhost, p);
104 			}
105 		} while (q);
106 		/*
107 		 * Sanity check
108 		 */
109 		if (*rhost == '\0' || *user == '\0' || *dir == '\0')
110 			goto enoent;
111 		/*
112 		 * Make up return string
113 		 */
114 		q = strchr(rhost, '.');
115 		if (q)
116 			*q = '\0';
117 		sprintf(val, "rfs:=%s/%s;rhost:=%s;sublink:=%s;fs:=${autodir}%s",
118 			dir, rhost, rhost, user, pw->pw_dir);
119 		if (q)
120 			*q = '.';
121 		*pval = strdup(val);
122 		return 0;
123 	}
124 
125 enoent:
126 	if (dir)
127 		free(dir);
128 
129 	return ENOENT;
130 }
131 #endif /* HAS_PASSWD_MAPS */
132