138494Sobrien/*
2174294Sobrien * Copyright (c) 1997-2006 Erez Zadok
338494Sobrien * Copyright (c) 1990 Jan-Simon Pendry
438494Sobrien * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
538494Sobrien * Copyright (c) 1990 The Regents of the University of California.
638494Sobrien * All rights reserved.
738494Sobrien *
838494Sobrien * This code is derived from software contributed to Berkeley by
938494Sobrien * Jan-Simon Pendry at Imperial College, London.
1038494Sobrien *
1138494Sobrien * Redistribution and use in source and binary forms, with or without
1238494Sobrien * modification, are permitted provided that the following conditions
1338494Sobrien * are met:
1438494Sobrien * 1. Redistributions of source code must retain the above copyright
1538494Sobrien *    notice, this list of conditions and the following disclaimer.
1638494Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1738494Sobrien *    notice, this list of conditions and the following disclaimer in the
1838494Sobrien *    documentation and/or other materials provided with the distribution.
1938494Sobrien * 3. All advertising materials mentioning features or use of this software
2042629Sobrien *    must display the following acknowledgment:
2138494Sobrien *      This product includes software developed by the University of
2238494Sobrien *      California, Berkeley and its contributors.
2338494Sobrien * 4. Neither the name of the University nor the names of its contributors
2438494Sobrien *    may be used to endorse or promote products derived from this software
2538494Sobrien *    without specific prior written permission.
2638494Sobrien *
2738494Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2838494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2938494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3038494Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3138494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3238494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3338494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3438494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3538494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3638494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3738494Sobrien * SUCH DAMAGE.
3838494Sobrien *
3938494Sobrien *
40174294Sobrien * File: am-utils/amd/info_passwd.c
4138494Sobrien *
4238494Sobrien */
4338494Sobrien
4438494Sobrien/*
4538494Sobrien * Get info from password "file"
4638494Sobrien *
4738494Sobrien * This is experimental and probably doesn't do what you expect.
4838494Sobrien */
4938494Sobrien
5038494Sobrien#ifdef HAVE_CONFIG_H
5138494Sobrien# include <config.h>
5238494Sobrien#endif /* HAVE_CONFIG_H */
5338494Sobrien#include <am_defs.h>
5438494Sobrien#include <amd.h>
5538494Sobrien
5638494Sobrien#define	PASSWD_MAP	"/etc/passwd"
5738494Sobrien
5838494Sobrien/* forward declarations */
5938494Sobrienint passwd_init(mnt_map *m, char *map, time_t *tp);
6038494Sobrienint passwd_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp);
6138494Sobrien
6238494Sobrien
6338494Sobrien/*
6438494Sobrien * Nothing to probe - check the map name is PASSWD_MAP.
6538494Sobrien */
6638494Sobrienint
6738494Sobrienpasswd_init(mnt_map *m, char *map, time_t *tp)
6838494Sobrien{
6938494Sobrien  *tp = 0;
7038494Sobrien
7138494Sobrien  /*
7238494Sobrien   * Recognize the old format "PASSWD_MAP"
7338494Sobrien   * Uses default return string
7438494Sobrien   * "type:=nfs;rfs:=/${var0}/${var1};rhost:=${var1};sublink:=${var2};fs:=${autodir}${var3}"
7538494Sobrien   */
7638494Sobrien  if (STREQ(map, PASSWD_MAP))
7738494Sobrien    return 0;
7838494Sobrien  /*
7938494Sobrien   * Recognize the new format "PASSWD_MAP:pval-format"
8038494Sobrien   */
8138494Sobrien  if (!NSTREQ(map, PASSWD_MAP, sizeof(PASSWD_MAP) - 1))
8238494Sobrien    return ENOENT;
8338494Sobrien  if (map[sizeof(PASSWD_MAP)-1] != ':')
8438494Sobrien    return ENOENT;
8538494Sobrien
8638494Sobrien  return 0;
8738494Sobrien}
8838494Sobrien
8938494Sobrien
9038494Sobrien/*
9138494Sobrien * Grab the entry via the getpwname routine
9238494Sobrien * Modify time is ignored by passwd - XXX
9338494Sobrien */
9438494Sobrienint
9538494Sobrienpasswd_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
9638494Sobrien{
9738494Sobrien  char *dir = 0;
9838494Sobrien  struct passwd *pw;
9938494Sobrien
10038494Sobrien  if (STREQ(key, "/defaults")) {
10138494Sobrien    *pval = strdup("type:=nfs");
10238494Sobrien    return 0;
10338494Sobrien  }
10438494Sobrien  pw = getpwnam(key);
10538494Sobrien
10638494Sobrien  if (pw) {
10738494Sobrien    /*
10838494Sobrien     * We chop the home directory up as follows:
10938494Sobrien     * /anydir/dom1/dom2/dom3/user
11038494Sobrien     *
11138494Sobrien     * and return
11238494Sobrien     * rfs:=/anydir/dom3;rhost:=dom3.dom2.dom1;sublink:=user
11338494Sobrien     * and now have
11438494Sobrien     * var0:=pw-prefix:=anydir
11538494Sobrien     * var1:=pw-rhost:=dom3.dom2.dom1
11638494Sobrien     * var2:=pw-user:=user
11738494Sobrien     * var3:=pw-home:=/anydir/dom1/dom2/dom3/user
11838494Sobrien     *
11938494Sobrien     * This allows cross-domain entries in your passwd file.
12038494Sobrien     * ... but forget about security!
12138494Sobrien     */
12238494Sobrien    char *user;
12338494Sobrien    char *p, *q;
12438494Sobrien    char val[MAXPATHLEN];
12538494Sobrien    char rhost[MAXHOSTNAMELEN];
12638494Sobrien    dir = strdup(pw->pw_dir);
12738494Sobrien
12838494Sobrien    /*
12938494Sobrien     * Find user name.  If no / then Invalid...
13038494Sobrien     */
13138494Sobrien    user = strrchr(dir, '/');
13238494Sobrien    if (!user)
13338494Sobrien      goto enoent;
13438494Sobrien    *user++ = '\0';
13538494Sobrien
13638494Sobrien    /*
13738494Sobrien     * Find start of host "path".  If no / then Invalid...
13838494Sobrien     */
13938494Sobrien    p = strchr(dir + 1, '/');
14038494Sobrien    if (!p)
14138494Sobrien      goto enoent;
14238494Sobrien    *p++ = '\0';
14338494Sobrien
14438494Sobrien    /*
14538494Sobrien     * At this point, p is dom1/dom2/dom3
14638494Sobrien     * Copy, backwards, into rhost replacing
14738494Sobrien     * / with .
14838494Sobrien     */
14938494Sobrien    rhost[0] = '\0';
15038494Sobrien    do {
15138494Sobrien      q = strrchr(p, '/');
15238494Sobrien      if (q) {
153174294Sobrien	xstrlcat(rhost, q + 1, sizeof(rhost));
154174294Sobrien	xstrlcat(rhost, ".", sizeof(rhost));
15538494Sobrien	*q = '\0';
15638494Sobrien      } else {
157174294Sobrien	xstrlcat(rhost, p, sizeof(rhost));
15838494Sobrien      }
15938494Sobrien    } while (q);
16038494Sobrien
16138494Sobrien    /*
16238494Sobrien     * Sanity check
16338494Sobrien     */
16438494Sobrien    if (*rhost == '\0' || *user == '\0' || *dir == '\0')
16538494Sobrien      goto enoent;
16638494Sobrien
16738494Sobrien    /*
16838494Sobrien     * Make up return string
16938494Sobrien     */
17038494Sobrien    q = strchr(rhost, '.');
17138494Sobrien    if (q)
17238494Sobrien      *q = '\0';
17338494Sobrien    p = strchr(map, ':');
17438494Sobrien    if (p)
17538494Sobrien      p++;
17638494Sobrien    else
17738494Sobrien      p = "type:=nfs;rfs:=/${var0}/${var1};rhost:=${var1};sublink:=${var2};fs:=${autodir}${var3}";
178174294Sobrien    xsnprintf(val, sizeof(val), "var0:=%s;var1:=%s;var2:=%s;var3:=%s;%s",
179174294Sobrien	      dir+1, rhost, user, pw->pw_dir, p);
18038494Sobrien    dlog("passwd_search: map=%s key=%s -> %s", map, key, val);
18138494Sobrien    if (q)
18238494Sobrien      *q = '.';
18338494Sobrien    *pval = strdup(val);
18438494Sobrien    return 0;
18538494Sobrien  }
18638494Sobrien
18738494Sobrienenoent:
18838494Sobrien  if (dir)
18938494Sobrien    XFREE(dir);
19038494Sobrien
19138494Sobrien  return ENOENT;
19238494Sobrien}
193