info_passwd.c revision 1.1
175115Sfenner/*	$NetBSD: info_passwd.c,v 1.1 2008/09/19 20:07:16 christos Exp $	*/
275115Sfenner
375115Sfenner/*
475115Sfenner * Copyright (c) 1997-2007 Erez Zadok
575115Sfenner * Copyright (c) 1990 Jan-Simon Pendry
675115Sfenner * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
775115Sfenner * Copyright (c) 1990 The Regents of the University of California.
875115Sfenner * All rights reserved.
975115Sfenner *
1075115Sfenner * This code is derived from software contributed to Berkeley by
1175115Sfenner * Jan-Simon Pendry at Imperial College, London.
1275115Sfenner *
13127668Sbms * Redistribution and use in source and binary forms, with or without
14168371Sthompsa * modification, are permitted provided that the following conditions
1575115Sfenner * are met:
1675115Sfenner * 1. Redistributions of source code must retain the above copyright
1775115Sfenner *    notice, this list of conditions and the following disclaimer.
1875115Sfenner * 2. Redistributions in binary form must reproduce the above copyright
1975115Sfenner *    notice, this list of conditions and the following disclaimer in the
2075115Sfenner *    documentation and/or other materials provided with the distribution.
21127668Sbms * 3. All advertising materials mentioning features or use of this software
2275115Sfenner *    must display the following acknowledgment:
2375115Sfenner *      This product includes software developed by the University of
2475115Sfenner *      California, Berkeley and its contributors.
2575115Sfenner * 4. Neither the name of the University nor the names of its contributors
2675115Sfenner *    may be used to endorse or promote products derived from this software
2775115Sfenner *    without specific prior written permission.
2875115Sfenner *
2975115Sfenner * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3075115Sfenner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31168371Sthompsa * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32168371Sthompsa * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33168371Sthompsa * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34168371Sthompsa * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35168371Sthompsa * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36168371Sthompsa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37168371Sthompsa * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38168371Sthompsa * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39168371Sthompsa * SUCH DAMAGE.
40168371Sthompsa *
41168371Sthompsa *
42168371Sthompsa * File: am-utils/amd/info_passwd.c
43168371Sthompsa *
44168371Sthompsa */
45168371Sthompsa
46168371Sthompsa/*
47168371Sthompsa * Get info from password "file"
48168371Sthompsa *
49168371Sthompsa * This is experimental and probably doesn't do what you expect.
50168371Sthompsa */
51168371Sthompsa
52168371Sthompsa#ifdef HAVE_CONFIG_H
53168371Sthompsa# include <config.h>
54168371Sthompsa#endif /* HAVE_CONFIG_H */
55168371Sthompsa#include <am_defs.h>
56168371Sthompsa#include <amd.h>
57168371Sthompsa
58168371Sthompsa#define	PASSWD_MAP	"/etc/passwd"
59168371Sthompsa
60168371Sthompsa/* forward declarations */
61168371Sthompsaint passwd_init(mnt_map *m, char *map, time_t *tp);
62168371Sthompsaint passwd_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp);
63168371Sthompsa
64168371Sthompsa
65168371Sthompsa/*
66168371Sthompsa * Nothing to probe - check the map name is PASSWD_MAP.
67168371Sthompsa */
68168371Sthompsaint
69168371Sthompsapasswd_init(mnt_map *m, char *map, time_t *tp)
70168371Sthompsa{
71168371Sthompsa  *tp = 0;
72168371Sthompsa
73168371Sthompsa  /*
74168371Sthompsa   * Recognize the old format "PASSWD_MAP"
75168371Sthompsa   * Uses default return string
76168371Sthompsa   * "type:=nfs;rfs:=/${var0}/${var1};rhost:=${var1};sublink:=${var2};fs:=${autodir}${var3}"
77168371Sthompsa   */
78168371Sthompsa  if (STREQ(map, PASSWD_MAP))
79168371Sthompsa    return 0;
80168371Sthompsa  /*
81168371Sthompsa   * Recognize the new format "PASSWD_MAP:pval-format"
82168371Sthompsa   */
83168371Sthompsa  if (!NSTREQ(map, PASSWD_MAP, sizeof(PASSWD_MAP) - 1))
84168371Sthompsa    return ENOENT;
85168371Sthompsa  if (map[sizeof(PASSWD_MAP)-1] != ':')
86168371Sthompsa    return ENOENT;
87168371Sthompsa
88168371Sthompsa  return 0;
89168371Sthompsa}
90168371Sthompsa
91168371Sthompsa
92168371Sthompsa/*
9375115Sfenner * Grab the entry via the getpwname routine
9475115Sfenner * Modify time is ignored by passwd - XXX
95168371Sthompsa */
96168371Sthompsaint
97168371Sthompsapasswd_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
98168371Sthompsa{
99168371Sthompsa  char *dir = NULL;
100168371Sthompsa  struct passwd *pw;
101168371Sthompsa
10275115Sfenner  if (STREQ(key, "/defaults")) {
10375115Sfenner    *pval = strdup("type:=nfs");
10475115Sfenner    return 0;
105168371Sthompsa  }
10675115Sfenner  pw = getpwnam(key);
107168371Sthompsa
108168371Sthompsa  if (pw) {
10975115Sfenner    /*
110168371Sthompsa     * We chop the home directory up as follows:
111168371Sthompsa     * /anydir/dom1/dom2/dom3/user
112168371Sthompsa     *
11375115Sfenner     * and return
114168371Sthompsa     * rfs:=/anydir/dom3;rhost:=dom3.dom2.dom1;sublink:=user
115168371Sthompsa     * and now have
116168371Sthompsa     * var0:=pw-prefix:=anydir
117168371Sthompsa     * var1:=pw-rhost:=dom3.dom2.dom1
11875115Sfenner     * var2:=pw-user:=user
119168371Sthompsa     * var3:=pw-home:=/anydir/dom1/dom2/dom3/user
120168371Sthompsa     *
121168371Sthompsa     * This allows cross-domain entries in your passwd file.
122168371Sthompsa     * ... but forget about security!
123168371Sthompsa     */
124168371Sthompsa    char *user;
12575115Sfenner    char *p, *q;
126168371Sthompsa    char val[MAXPATHLEN];
127168371Sthompsa    char rhost[MAXHOSTNAMELEN];
128168371Sthompsa    dir = strdup(pw->pw_dir);
129168371Sthompsa
130168371Sthompsa    /*
131168371Sthompsa     * Find user name.  If no / then Invalid...
132168371Sthompsa     */
133168371Sthompsa    user = strrchr(dir, '/');
134168371Sthompsa    if (!user)
135168371Sthompsa      goto enoent;
13675115Sfenner    *user++ = '\0';
13775115Sfenner
138168371Sthompsa    /*
139168371Sthompsa     * Find start of host "path".  If no / then Invalid...
140168371Sthompsa     */
141168371Sthompsa    p = strchr(dir + 1, '/');
142168371Sthompsa    if (!p)
143168371Sthompsa      goto enoent;
144168371Sthompsa    *p++ = '\0';
145168371Sthompsa
146168371Sthompsa    /*
147168371Sthompsa     * At this point, p is dom1/dom2/dom3
148168371Sthompsa     * Copy, backwards, into rhost replacing
149168371Sthompsa     * / with .
150168371Sthompsa     */
151168371Sthompsa    rhost[0] = '\0';
152168371Sthompsa    do {
153168371Sthompsa      q = strrchr(p, '/');
154168371Sthompsa      if (q) {
155168371Sthompsa	xstrlcat(rhost, q + 1, sizeof(rhost));
156168371Sthompsa	xstrlcat(rhost, ".", sizeof(rhost));
157168371Sthompsa	*q = '\0';
158168371Sthompsa      } else {
159168371Sthompsa	xstrlcat(rhost, p, sizeof(rhost));
160168371Sthompsa      }
161168371Sthompsa    } while (q);
162168371Sthompsa
163168371Sthompsa    /*
164168371Sthompsa     * Sanity check
165168371Sthompsa     */
166168371Sthompsa    if (*rhost == '\0' || *user == '\0' || *dir == '\0')
167168371Sthompsa      goto enoent;
168168371Sthompsa
169168371Sthompsa    /*
170168371Sthompsa     * Make up return string
171168371Sthompsa     */
172168371Sthompsa    q = strchr(rhost, '.');
173168371Sthompsa    if (q)
174168371Sthompsa      *q = '\0';
175168371Sthompsa    p = strchr(map, ':');
176168371Sthompsa    if (p)
177168371Sthompsa      p++;
178168371Sthompsa    else
179168371Sthompsa      p = "type:=nfs;rfs:=/${var0}/${var1};rhost:=${var1};sublink:=${var2};fs:=${autodir}${var3}";
180168371Sthompsa    xsnprintf(val, sizeof(val), "var0:=%s;var1:=%s;var2:=%s;var3:=%s;%s",
181168371Sthompsa	      dir+1, rhost, user, pw->pw_dir, p);
182168371Sthompsa    dlog("passwd_search: map=%s key=%s -> %s", map, key, val);
183168371Sthompsa    if (q)
184168371Sthompsa      *q = '.';
185168371Sthompsa    *pval = strdup(val);
186168371Sthompsa    return 0;
187168371Sthompsa  }
188168371Sthompsa
189168371Sthompsaenoent:
190168371Sthompsa  if (dir)
191168371Sthompsa    XFREE(dir);
192168371Sthompsa
193168371Sthompsa  return ENOENT;
194168371Sthompsa}
19575115Sfenner