1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
* Copyright (c) 2001 Proofpoint, Inc. and its suppliers.
* All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*/
#include <sm/gen.h>
SM_RCSID("@(#)$Id: niprop.c,v 1.9 2013-11-22 20:51:43 ca Exp $")
#if NETINFO
#include <ctype.h>
#include <stdlib.h>
#include <sm/io.h>
#include <sm/assert.h>
#include <sm/debug.h>
#include <sm/string.h>
#include <sm/varargs.h>
#include <sm/heap.h>
/*
** NI_PROPVAL -- NetInfo property value lookup routine
**
** Parameters:
** keydir -- the NetInfo directory name in which to search
** for the key.
** keyprop -- the name of the property in which to find the
** property we are interested. Defaults to "name".
** keyval -- the value for which we are really searching.
** valprop -- the property name for the value in which we
** are interested.
** sepchar -- if non-nil, this can be multiple-valued, and
** we should return a string separated by this
** character.
**
** Returns:
** NULL -- if:
** 1. the directory is not found
** 2. the property name is not found
** 3. the property contains multiple values
** 4. some error occurred
** else -- the value of the lookup.
**
** Example:
** To search for an alias value, use:
** ni_propval("/aliases", "name", aliasname, "members", ',')
**
** Notes:
** Caller should free the return value of ni_proval
*/
# include <netinfo/ni.h>
# define LOCAL_NETINFO_DOMAIN "."
# define PARENT_NETINFO_DOMAIN ".."
# define MAX_NI_LEVELS 256
char *
ni_propval(keydir, keyprop, keyval, valprop, sepchar)
char *keydir;
char *keyprop;
char *keyval;
char *valprop;
int sepchar;
{
char *propval = NULL;
int i;
int j, alen, l;
void *ni = NULL;
void *lastni = NULL;
ni_status nis;
ni_id nid;
ni_namelist ninl;
register char *p;
char keybuf[1024];
/*
** Create the full key from the two parts.
**
** Note that directory can end with, e.g., "name=" to specify
** an alternate search property.
*/
i = strlen(keydir) + strlen(keyval) + 2;
if (keyprop != NULL)
i += strlen(keyprop) + 1;
if (i >= sizeof keybuf)
return NULL;
(void) sm_strlcpyn(keybuf, sizeof keybuf, 2, keydir, "/");
if (keyprop != NULL)
{
(void) sm_strlcat2(keybuf, keyprop, "=", sizeof keybuf);
}
(void) sm_strlcat(keybuf, keyval, sizeof keybuf);
# if 0
if (tTd(38, 21))
sm_dprintf("ni_propval(%s, %s, %s, %s, %d) keybuf='%s'\n",
keydir, keyprop, keyval, valprop, sepchar, keybuf);
# endif
/*
** If the passed directory and property name are found
** in one of netinfo domains we need to search (starting
** from the local domain moving all the way back to the
** root domain) set propval to the property's value
** and return it.
*/
for (i = 0; i < MAX_NI_LEVELS && propval == NULL; i++)
{
if (i == 0)
{
nis = ni_open(NULL, LOCAL_NETINFO_DOMAIN, &ni);
# if 0
if (tTd(38, 20))
sm_dprintf("ni_open(LOCAL) = %d\n", nis);
# endif
}
else
{
if (lastni != NULL)
ni_free(lastni);
lastni = ni;
nis = ni_open(lastni, PARENT_NETINFO_DOMAIN, &ni);
# if 0
if (tTd(38, 20))
sm_dprintf("ni_open(PARENT) = %d\n", nis);
# endif
}
/*
** Don't bother if we didn't get a handle on a
** proper domain. This is not necessarily an error.
** We would get a positive ni_status if, for instance
** we never found the directory or property and tried
** to open the parent of the root domain!
*/
if (nis != 0)
break;
/*
** Find the path to the server information.
*/
if (ni_pathsearch(ni, &nid, keybuf) != 0)
continue;
/*
** Find associated value information.
*/
if (ni_lookupprop(ni, &nid, valprop, &ninl) != 0)
continue;
# if 0
if (tTd(38, 20))
sm_dprintf("ni_lookupprop: len=%d\n",
ninl.ni_namelist_len);
# endif
/*
** See if we have an acceptable number of values.
*/
if (ninl.ni_namelist_len <= 0)
continue;
if (sepchar == '\0' && ninl.ni_namelist_len > 1)
{
ni_namelist_free(&ninl);
continue;
}
/*
** Calculate number of bytes needed and build result
*/
alen = 1;
for (j = 0; j < ninl.ni_namelist_len; j++)
alen += strlen(ninl.ni_namelist_val[j]) + 1;
propval = p = sm_malloc(alen);
if (propval == NULL)
goto cleanup;
for (j = 0; j < ninl.ni_namelist_len; j++)
{
(void) sm_strlcpy(p, ninl.ni_namelist_val[j], alen);
l = strlen(p);
p += l;
*p++ = sepchar;
alen -= l + 1;
}
*--p = '\0';
ni_namelist_free(&ninl);
}
cleanup:
if (ni != NULL)
ni_free(ni);
if (lastni != NULL && ni != lastni)
ni_free(lastni);
# if 0
if (tTd(38, 20))
sm_dprintf("ni_propval returns: '%s'\n", propval);
# endif
return propval;
}
#endif /* NETINFO */
|