2 * Copyright (c) 2001 Sendmail, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
11 SM_RCSID("@(#)$Id: niprop.c,v 1.8 2001/09/11 04:04:48 gshapiro Exp $")
17 #include <sm/assert.h>
19 #include <sm/string.h>
20 #include <sm/varargs.h>
24 ** NI_PROPVAL -- NetInfo property value lookup routine
27 ** keydir -- the NetInfo directory name in which to search
29 ** keyprop -- the name of the property in which to find the
30 ** property we are interested. Defaults to "name".
31 ** keyval -- the value for which we are really searching.
32 ** valprop -- the property name for the value in which we
34 ** sepchar -- if non-nil, this can be multiple-valued, and
35 ** we should return a string separated by this
40 ** 1. the directory is not found
41 ** 2. the property name is not found
42 ** 3. the property contains multiple values
43 ** 4. some error occurred
44 ** else -- the value of the lookup.
47 ** To search for an alias value, use:
48 ** ni_propval("/aliases", "name", aliasname, "members", ',')
51 ** Caller should free the return value of ni_proval
54 # include <netinfo/ni.h>
56 # define LOCAL_NETINFO_DOMAIN "."
57 # define PARENT_NETINFO_DOMAIN ".."
58 # define MAX_NI_LEVELS 256
61 ni_propval(keydir, keyprop, keyval, valprop, sepchar)
80 ** Create the full key from the two parts.
82 ** Note that directory can end with, e.g., "name=" to specify
83 ** an alternate search property.
86 i = strlen(keydir) + strlen(keyval) + 2;
88 i += strlen(keyprop) + 1;
89 if (i >= sizeof keybuf)
91 (void) sm_strlcpyn(keybuf, sizeof keybuf, 2, keydir, "/");
94 (void) sm_strlcat2(keybuf, keyprop, "=", sizeof keybuf);
96 (void) sm_strlcat(keybuf, keyval, sizeof keybuf);
100 sm_dprintf("ni_propval(%s, %s, %s, %s, %d) keybuf='%s'\n",
101 keydir, keyprop, keyval, valprop, sepchar, keybuf);
105 ** If the passed directory and property name are found
106 ** in one of netinfo domains we need to search (starting
107 ** from the local domain moving all the way back to the
108 ** root domain) set propval to the property's value
112 for (i = 0; i < MAX_NI_LEVELS && propval == NULL; i++)
116 nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
119 sm_dprintf("ni_open(LOCAL) = %d\n", nis);
127 nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
130 sm_dprintf("ni_open(PARENT) = %d\n", nis);
135 ** Don't bother if we didn't get a handle on a
136 ** proper domain. This is not necessarily an error.
137 ** We would get a positive ni_status if, for instance
138 ** we never found the directory or property and tried
139 ** to open the parent of the root domain!
146 ** Find the path to the server information.
149 if (ni_pathsearch(ni, &nid, keybuf) != 0)
153 ** Find associated value information.
156 if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
161 sm_dprintf("ni_lookupprop: len=%d\n",
162 ninl.ni_namelist_len);
166 ** See if we have an acceptable number of values.
169 if (ninl.ni_namelist_len <= 0)
172 if (sepchar == '\0' && ninl.ni_namelist_len > 1)
174 ni_namelist_free(&ninl);
179 ** Calculate number of bytes needed and build result
183 for (j = 0; j < ninl.ni_namelist_len; j++)
184 alen += strlen(ninl.ni_namelist_val[j]) + 1;
185 propval = p = sm_malloc(alen);
188 for (j = 0; j < ninl.ni_namelist_len; j++)
190 (void) sm_strlcpy(p, ninl.ni_namelist_val[j], alen);
198 ni_namelist_free(&ninl);
204 if (lastni != NULL && ni != lastni)
208 sm_dprintf("ni_propval returns: '%s'\n", propval);