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