Blame library/adldap.c

Packit Service 6d40f9
/*
Packit Service 6d40f9
 * adcli
Packit Service 6d40f9
 *
Packit Service 6d40f9
 * Copyright (C) 2012 Red Hat Inc.
Packit Service 6d40f9
 *
Packit Service 6d40f9
 * This program is free software; you can redistribute it and/or modify
Packit Service 6d40f9
 * it under the terms of the GNU Lesser General Public License as
Packit Service 6d40f9
 * published by the Free Software Foundation; either version 2.1 of
Packit Service 6d40f9
 * the License, or (at your option) any later version.
Packit Service 6d40f9
 *
Packit Service 6d40f9
 * This program is distributed in the hope that it will be useful, but
Packit Service 6d40f9
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 6d40f9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 6d40f9
 * Lesser General Public License for more details.
Packit Service 6d40f9
 *
Packit Service 6d40f9
 * You should have received a copy of the GNU Lesser General Public
Packit Service 6d40f9
 * License along with this program; if not, write to the Free Software
Packit Service 6d40f9
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
Packit Service 6d40f9
 * MA 02110-1301 USA
Packit Service 6d40f9
 *
Packit Service 6d40f9
 * Author: Stef Walter <stefw@gnome.org>
Packit Service 6d40f9
 */
Packit Service 6d40f9
Packit Service 6d40f9
#include "config.h"
Packit Service 6d40f9
Packit Service 6d40f9
#include "adcli.h"
Packit Service 6d40f9
#include "adprivate.h"
Packit Service 6d40f9
Packit Service 6d40f9
#include <gssapi/gssapi_krb5.h>
Packit Service 6d40f9
#include <krb5/krb5.h>
Packit Service 6d40f9
#include <ldap.h>
Packit Service 6d40f9
#include <sasl/sasl.h>
Packit Service 6d40f9
Packit Service 6d40f9
#include <assert.h>
Packit Service 6d40f9
#include <ctype.h>
Packit Service 6d40f9
Packit Service 6d40f9
adcli_result
Packit Service 6d40f9
_adcli_ldap_handle_failure (LDAP *ldap,
Packit Service 6d40f9
                            adcli_result defres,
Packit Service 6d40f9
                            const char *desc,
Packit Service 6d40f9
                            ...)
Packit Service 6d40f9
{
Packit Service 6d40f9
	va_list va;
Packit Service 6d40f9
	char *message;
Packit Service 6d40f9
	char *info;
Packit Service 6d40f9
	int code;
Packit Service 6d40f9
Packit Service 6d40f9
	if (ldap_get_option (ldap, LDAP_OPT_RESULT_CODE, &code) != 0)
Packit Service 6d40f9
		return_unexpected_if_reached ();
Packit Service 6d40f9
Packit Service 6d40f9
	if (code == LDAP_NO_MEMORY)
Packit Service 6d40f9
		return_unexpected_if_reached ();
Packit Service 6d40f9
Packit Service 6d40f9
	if (ldap_get_option (ldap, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&info) != 0)
Packit Service 6d40f9
		info = NULL;
Packit Service 6d40f9
Packit Service 6d40f9
	va_start (va, desc);
Packit Service 6d40f9
	if (vasprintf (&message, desc, va) < 0)
Packit Service 6d40f9
		return_unexpected_if_reached ();
Packit Service 6d40f9
	va_end (va);
Packit Service 6d40f9
Packit Service 6d40f9
	_adcli_err ("%s: %s",
Packit Service 6d40f9
	            message,
Packit Service 6d40f9
	            info ? info : ldap_err2string (code));
Packit Service 6d40f9
Packit Service 6d40f9
	free (message);
Packit Service 6d40f9
	return defres;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 4765ac
char *
Packit Service 4765ac
_adcli_ldap_parse_sid (LDAP *ldap,
Packit Service 4765ac
                         LDAPMessage *results,
Packit Service 4765ac
                         const char *attr_name)
Packit Service 4765ac
{
Packit Service 4765ac
	LDAPMessage *entry;
Packit Service 4765ac
	struct berval **bvs;
Packit Service 4765ac
	char *val = NULL;
Packit Service 4765ac
Packit Service 4765ac
	entry = ldap_first_entry (ldap, results);
Packit Service 4765ac
	if (entry != NULL) {
Packit Service 4765ac
		bvs = ldap_get_values_len (ldap, entry, attr_name);
Packit Service 4765ac
		if (bvs != NULL) {
Packit Service 4765ac
			if (bvs[0]) {
Packit Service 4765ac
				val = _adcli_bin_sid_to_str ( (uint8_t *) bvs[0]->bv_val,
Packit Service 4765ac
				                              bvs[0]->bv_len);
Packit Service 4765ac
				return_val_if_fail (val != NULL, NULL);
Packit Service 4765ac
			}
Packit Service 4765ac
			ldap_value_free_len (bvs);
Packit Service 4765ac
		}
Packit Service 4765ac
	}
Packit Service 4765ac
Packit Service 4765ac
	return val;
Packit Service 4765ac
}
Packit Service 6d40f9
Packit Service 6d40f9
char *
Packit Service 6d40f9
_adcli_ldap_parse_value (LDAP *ldap,
Packit Service 6d40f9
                         LDAPMessage *results,
Packit Service 6d40f9
                         const char *attr_name)
Packit Service 6d40f9
{
Packit Service 6d40f9
	LDAPMessage *entry;
Packit Service 6d40f9
	struct berval **bvs;
Packit Service 6d40f9
	char *val = NULL;
Packit Service 6d40f9
Packit Service 6d40f9
	entry = ldap_first_entry (ldap, results);
Packit Service 6d40f9
	if (entry != NULL) {
Packit Service 6d40f9
		bvs = ldap_get_values_len (ldap, entry, attr_name);
Packit Service 6d40f9
		if (bvs != NULL) {
Packit Service 6d40f9
			if (bvs[0]) {
Packit Service 6d40f9
				val = _adcli_str_dupn (bvs[0]->bv_val, bvs[0]->bv_len);
Packit Service 6d40f9
				return_val_if_fail (val != NULL, NULL);
Packit Service 6d40f9
			}
Packit Service 6d40f9
			ldap_value_free_len (bvs);
Packit Service 6d40f9
		}
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	return val;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
char **
Packit Service 6d40f9
_adcli_ldap_parse_values (LDAP *ldap,
Packit Service 6d40f9
                          LDAPMessage *results,
Packit Service 6d40f9
                          const char *attr_name)
Packit Service 6d40f9
{
Packit Service 6d40f9
	LDAPMessage *entry;
Packit Service 6d40f9
	struct berval **bvs;
Packit Service 6d40f9
	char **vals = NULL;
Packit Service 6d40f9
	int length = 0;
Packit Service 6d40f9
	char *val;
Packit Service 6d40f9
	int i;
Packit Service 6d40f9
Packit Service 6d40f9
	entry = ldap_first_entry (ldap, results);
Packit Service 6d40f9
	if (entry != NULL) {
Packit Service 6d40f9
		bvs = ldap_get_values_len (ldap, entry, attr_name);
Packit Service 6d40f9
		if (bvs != NULL) {
Packit Service 6d40f9
			for (i = 0; bvs[i] != NULL; i++) {
Packit Service 6d40f9
				val = _adcli_str_dupn (bvs[i]->bv_val,
Packit Service 6d40f9
				                       bvs[i]->bv_len);
Packit Service 6d40f9
				if (val != NULL)
Packit Service 6d40f9
					vals = _adcli_strv_add (vals, val, &length);
Packit Service 6d40f9
			}
Packit Service 6d40f9
			ldap_value_free_len (bvs);
Packit Service 6d40f9
		}
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	return vals;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
char *
Packit Service 6d40f9
_adcli_ldap_parse_dn (LDAP *ldap,
Packit Service 6d40f9
                      LDAPMessage *results)
Packit Service 6d40f9
{
Packit Service 6d40f9
	LDAPMessage *entry;
Packit Service 6d40f9
	const char *dn;
Packit Service 6d40f9
	char *ret = NULL;
Packit Service 6d40f9
Packit Service 6d40f9
	entry = ldap_first_entry (ldap, results);
Packit Service 6d40f9
	if (entry != NULL) {
Packit Service 6d40f9
		dn = ldap_get_dn (ldap, entry);
Packit Service 6d40f9
		if (dn != NULL) {
Packit Service 6d40f9
			ret = strdup (dn);
Packit Service 6d40f9
			return_val_if_fail (ret != NULL, NULL);
Packit Service 6d40f9
		}
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	return ret;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
int
Packit Service 6d40f9
_adcli_ldap_ber_case_equal (struct berval *one,
Packit Service 6d40f9
                            struct berval *two)
Packit Service 6d40f9
{
Packit Service 6d40f9
	int i;
Packit Service 6d40f9
Packit Service 6d40f9
	if (one->bv_len != two->bv_len)
Packit Service 6d40f9
		return 0;
Packit Service 6d40f9
Packit Service 6d40f9
	for (i = 0; i < one->bv_len; i++) {
Packit Service 6d40f9
		if (toupper (one->bv_val[i]) != toupper (two->bv_val[i]))
Packit Service 6d40f9
			return 0;
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	return 1;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
int
Packit Service 6d40f9
_adcli_ldap_have_vals (struct berval **want,
Packit Service 6d40f9
                       struct berval **have)
Packit Service 6d40f9
{
Packit Service 6d40f9
	int i, j;
Packit Service 6d40f9
Packit Service 6d40f9
	for (i = 0; want[i] != NULL; i++) {
Packit Service 6d40f9
		int found = 0;
Packit Service 6d40f9
		for (j = 0; have[j] != NULL; j++) {
Packit Service 6d40f9
			if (_adcli_ldap_ber_case_equal (want[i], have[j])) {
Packit Service 6d40f9
				found = 1;
Packit Service 6d40f9
				break;
Packit Service 6d40f9
			}
Packit Service 6d40f9
		}
Packit Service 6d40f9
		if (!found)
Packit Service 6d40f9
			return 0;
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	return 1;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
int
Packit Service 6d40f9
_adcli_ldap_have_in_mod (LDAPMod *mod,
Packit Service 6d40f9
                         struct berval **have)
Packit Service 6d40f9
{
Packit Service 6d40f9
	struct berval *vals;
Packit Service 6d40f9
	struct berval **pvals;
Packit Service 6d40f9
	int count = 0;
Packit Service 69847a
	int count_have = 0;
Packit Service 6d40f9
	int i;
Packit Service 6d40f9
	int ret;
Packit Service 6d40f9
Packit Service bc2650
	/* Count number of values */
Packit Service bc2650
	for (i = 0; mod->mod_vals.modv_strvals[i] != 0; i++)
Packit Service bc2650
		count++;
Packit Service 69847a
	for (i = 0; have[i] != 0; i++)
Packit Service 69847a
		count_have++;
Packit Service 69847a
Packit Service 69847a
	/* If numbers different something has to be added or removed */
Packit Service 69847a
	if (count != count_have) {
Packit Service 69847a
		return 0;
Packit Service 69847a
	}
Packit Service 69847a
Packit Service 69847a
	/* Already in berval format, just compare */
Packit Service 69847a
	if (mod->mod_op & LDAP_MOD_BVALUES)
Packit Service 69847a
		return _adcli_ldap_have_vals (mod->mod_vals.modv_bvals, have);
Packit Service bc2650
Packit Service 6d40f9
	vals = malloc (sizeof (struct berval) * (count + 1));
Packit Service 6d40f9
	pvals = malloc (sizeof (struct berval *) * (count + 1));
Packit Service 6d40f9
	for (i = 0; i < count; i++) {
Packit Service 6d40f9
		vals[i].bv_val = mod->mod_vals.modv_strvals[i];
Packit Service 6d40f9
		vals[i].bv_len = strlen (vals[i].bv_val);
Packit Service 6d40f9
		pvals[i] = vals + i;
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	pvals[count] = NULL;
Packit Service 6d40f9
	ret = _adcli_ldap_have_vals (pvals, have);
Packit Service 6d40f9
	free (vals);
Packit Service 6d40f9
	free (pvals);
Packit Service 6d40f9
Packit Service 6d40f9
	return ret;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
int
Packit Service 6d40f9
_adcli_ldap_filter_for_add (void *unused,
Packit Service 6d40f9
                            void *value)
Packit Service 6d40f9
{
Packit Service 6d40f9
	LDAPMod *mod = value;
Packit Service 6d40f9
Packit Service 6d40f9
	/* No need to care about this */
Packit Service 6d40f9
	if ((mod->mod_op & LDAP_MOD_OP) == LDAP_MOD_DELETE)
Packit Service 6d40f9
		return -1;
Packit Service 6d40f9
Packit Service 6d40f9
	if (mod->mod_op & LDAP_MOD_BVALUES) {
Packit Service 6d40f9
		if (mod->mod_vals.modv_bvals == NULL ||
Packit Service 6d40f9
		    mod->mod_vals.modv_bvals[0] == NULL)
Packit Service 6d40f9
			return -1;
Packit Service 6d40f9
		mod->mod_op = LDAP_MOD_ADD | LDAP_MOD_BVALUES;
Packit Service 6d40f9
	} else {
Packit Service 6d40f9
		if (mod->mod_vals.modv_strvals == NULL ||
Packit Service 6d40f9
		    mod->mod_vals.modv_strvals[0] == NULL)
Packit Service 6d40f9
			return -1;
Packit Service 6d40f9
		mod->mod_op = LDAP_MOD_ADD;
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	return 0;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
#define LDAP_NO_ESCAPE "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_0123456789"
Packit Service 6d40f9
#define LDAP_HEX "0123456789abcdef"
Packit Service 6d40f9
Packit Service 6d40f9
char *
Packit Service 6d40f9
_adcli_ldap_escape_filter (const char *value)
Packit Service 6d40f9
{
Packit Service 6d40f9
	const char *in;
Packit Service 6d40f9
	char *out, *result;
Packit Service 6d40f9
	size_t pos;
Packit Service 6d40f9
	size_t len;
Packit Service 6d40f9
Packit Service 6d40f9
	assert (value != NULL);
Packit Service 6d40f9
Packit Service 6d40f9
	len = strlen (value);
Packit Service 6d40f9
	result = malloc ((len * 3) + 1);
Packit Service 6d40f9
	return_val_if_fail (result != NULL, NULL);
Packit Service 6d40f9
Packit Service 6d40f9
	in = value;
Packit Service 6d40f9
	out = result;
Packit Service 6d40f9
Packit Service 6d40f9
	while (*in != '\0') {
Packit Service 6d40f9
		pos = strspn (in, LDAP_NO_ESCAPE);
Packit Service 6d40f9
		if (pos > 0) {
Packit Service 6d40f9
			memcpy (out, in, pos);
Packit Service 6d40f9
			in += pos;
Packit Service 6d40f9
			out += pos;
Packit Service 6d40f9
		}
Packit Service 6d40f9
Packit Service 6d40f9
		while (*in != '\0' && !strchr (LDAP_NO_ESCAPE, *in)) {
Packit Service 6d40f9
			*(out++) = '\\';
Packit Service 6d40f9
			*(out++) = LDAP_HEX[*in >> 4 & 0xf];
Packit Service 6d40f9
			*(out++) = LDAP_HEX[*in & 0xf];
Packit Service 6d40f9
			in++;
Packit Service 6d40f9
		}
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	*out = 0;
Packit Service 6d40f9
	return result;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
static int
Packit Service 6d40f9
berval_case_equals (const struct berval *v1,
Packit Service 6d40f9
                    const struct berval *v2)
Packit Service 6d40f9
{
Packit Service 6d40f9
	return (v1->bv_len == v2->bv_len &&
Packit Service 6d40f9
	        strncasecmp (v1->bv_val, v2->bv_val, v1->bv_len) == 0);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
int
Packit Service 6d40f9
_adcli_ldap_dn_has_ancestor (const char *dn,
Packit Service 6d40f9
                             const char *ancestor)
Packit Service 6d40f9
{
Packit Service 6d40f9
	LDAPDN ld_dn;
Packit Service 6d40f9
	LDAPDN ld_suffix;
Packit Service 6d40f9
	LDAPRDN r_dn;
Packit Service 6d40f9
	LDAPRDN r_suffix;
Packit Service 6d40f9
	int ln_dn;
Packit Service 6d40f9
	int ln_suffix;
Packit Service 6d40f9
	int match = 0;
Packit Service 6d40f9
	int rc;
Packit Service 6d40f9
	int i, j;
Packit Service 6d40f9
Packit Service 6d40f9
	rc = ldap_str2dn (dn, &ld_dn, LDAP_DN_FORMAT_LDAPV3);
Packit Service 6d40f9
	return_val_if_fail (rc == LDAP_SUCCESS, 0);
Packit Service 6d40f9
Packit Service 6d40f9
	/* This is usually provided by user, be less whiny about formatting issues */
Packit Service 6d40f9
	rc = ldap_str2dn (ancestor, &ld_suffix, LDAP_DN_FORMAT_LDAPV3);
Packit Service 6d40f9
	if (rc != LDAP_SUCCESS)
Packit Service 6d40f9
		return 0;
Packit Service 6d40f9
Packit Service 6d40f9
	/* Calculate length of both */
Packit Service 6d40f9
	for (i = 0, ln_dn = 0; ld_dn[i] != NULL; i++)
Packit Service 6d40f9
		ln_dn++;
Packit Service 6d40f9
	for (i = 0, ln_suffix = 0; ld_suffix[i] != NULL; i++)
Packit Service 6d40f9
		ln_suffix++;
Packit Service 6d40f9
Packit Service 6d40f9
	match = (ln_suffix < ln_dn);
Packit Service 6d40f9
	for (i = 1; match && i <= ln_suffix; i++) {
Packit Service 6d40f9
		r_dn = ld_dn[ln_dn - i];
Packit Service 6d40f9
		r_suffix = ld_suffix[ln_suffix - i];
Packit Service 6d40f9
		for (j = 0; match && r_dn[j] != NULL && r_suffix[j] != NULL; j++) {
Packit Service 6d40f9
			if (!berval_case_equals (&(r_dn[j]->la_attr), &(r_suffix[j]->la_attr)) ||
Packit Service 6d40f9
			    !berval_case_equals (&(r_dn[j]->la_value), &(r_suffix[j]->la_value)))
Packit Service 6d40f9
				match = 0;
Packit Service 6d40f9
		}
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	ldap_dnfree (ld_dn);
Packit Service 6d40f9
	ldap_dnfree (ld_suffix);
Packit Service 6d40f9
Packit Service 6d40f9
	return match;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
int
Packit Service 6d40f9
_adcli_ldap_mod_compar (void *match,
Packit Service 6d40f9
                        void *mod)
Packit Service 6d40f9
{
Packit Service 6d40f9
	return strcmp (((LDAPMod *)match)->mod_type,
Packit Service 6d40f9
	               ((LDAPMod *)mod)->mod_type);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
LDAPMod *
Packit Service 6d40f9
_adcli_ldap_mod_new (int mod_op,
Packit Service 6d40f9
                     const char *type,
Packit Service 6d40f9
                     const char **values)
Packit Service 6d40f9
{
Packit Service 6d40f9
	LDAPMod *mod;
Packit Service 6d40f9
Packit Service 6d40f9
	mod = calloc (1, sizeof (LDAPMod));
Packit Service 6d40f9
	return_val_if_fail (mod != NULL, NULL);
Packit Service 6d40f9
Packit Service 6d40f9
	mod->mod_op = mod_op;
Packit Service 6d40f9
	mod->mod_type = strdup (type);
Packit Service 6d40f9
	return_val_if_fail (mod->mod_type != NULL, NULL);
Packit Service 6d40f9
Packit Service 6d40f9
	if (values) {
Packit Service 6d40f9
		mod->mod_vals.modv_strvals = _adcli_strv_dup ((char **)values);
Packit Service 6d40f9
		return_val_if_fail (mod->mod_vals.modv_strvals != NULL, NULL);
Packit Service 6d40f9
	}
Packit Service 6d40f9
Packit Service 6d40f9
	return mod;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
LDAPMod *
Packit Service 6d40f9
_adcli_ldap_mod_new1 (int mod_op,
Packit Service 6d40f9
                      const char *type,
Packit Service 6d40f9
                      const char *value)
Packit Service 6d40f9
{
Packit Service 6d40f9
	const char *values[] = { value, NULL };
Packit Service 6d40f9
	return _adcli_ldap_mod_new (mod_op, type, values);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
void
Packit Service 6d40f9
_adcli_ldap_mod_free (void *value)
Packit Service 6d40f9
{
Packit Service 6d40f9
	LDAPMod *mod = value;
Packit Service 6d40f9
Packit Service 6d40f9
	if (mod == NULL)
Packit Service 6d40f9
		return;
Packit Service 6d40f9
Packit Service 6d40f9
	free (mod->mod_type);
Packit Service 6d40f9
Packit Service 6d40f9
	if (mod->mod_op & LDAP_MOD_BVALUES)
Packit Service 6d40f9
		ldap_value_free_len (mod->mod_vals.modv_bvals);
Packit Service 6d40f9
	else
Packit Service 6d40f9
		_adcli_strv_free (mod->mod_vals.modv_strvals);
Packit Service 6d40f9
	free (mod);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
char *
Packit Service 6d40f9
_adcli_ldap_mods_to_string (LDAPMod **mods)
Packit Service 6d40f9
{
Packit Service 6d40f9
	char **names;
Packit Service 6d40f9
	int names_len;
Packit Service 6d40f9
	char *string;
Packit Service 6d40f9
	int i;
Packit Service 6d40f9
Packit Service 6d40f9
	names = NULL;
Packit Service 6d40f9
	names_len = 0;
Packit Service 6d40f9
Packit Service 6d40f9
	for (i = 0; mods[i] != NULL; i++)
Packit Service 6d40f9
		names = _adcli_strv_add (names, strdup (mods[i]->mod_type), &names_len);
Packit Service 6d40f9
	string = _adcli_strv_join (names, ", ");
Packit Service 6d40f9
Packit Service 6d40f9
	_adcli_strv_free (names);
Packit Service 6d40f9
	return string;
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
#ifdef LDAP_TESTS
Packit Service 6d40f9
Packit Service 6d40f9
#include "seq.h"
Packit Service 6d40f9
#include "test.h"
Packit Service 6d40f9
Packit Service 6d40f9
static void
Packit Service 6d40f9
test_compar (void)
Packit Service 6d40f9
{
Packit Service 6d40f9
	LDAPMod one = { LDAP_MOD_ADD, "one" };
Packit Service 6d40f9
	LDAPMod two = { LDAP_MOD_ADD, "two" };
Packit Service 6d40f9
	LDAPMod same = { LDAP_MOD_REPLACE, "one" };
Packit Service 6d40f9
Packit Service 6d40f9
	assert (_adcli_ldap_mod_compar (&one, &two) < 0);
Packit Service 6d40f9
	assert (_adcli_ldap_mod_compar (&two, &one) > 0);
Packit Service 6d40f9
	assert (_adcli_ldap_mod_compar (&one, &one) == 0);
Packit Service 6d40f9
	assert (_adcli_ldap_mod_compar (&one, &same) == 0);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
static void
Packit Service 6d40f9
test_new_free (void)
Packit Service 6d40f9
{
Packit Service 6d40f9
	const char *values[] = { "value", "two", "three", NULL };
Packit Service 6d40f9
	LDAPMod *mod;
Packit Service 6d40f9
Packit Service 6d40f9
	mod = _adcli_ldap_mod_new (LDAP_MOD_ADD, "test", values);
Packit Service 6d40f9
	assert (mod != NULL);
Packit Service 6d40f9
Packit Service 6d40f9
	assert (mod->mod_op == LDAP_MOD_ADD);
Packit Service 6d40f9
	assert_str_eq (mod->mod_type, "test");
Packit Service 6d40f9
	assert_num_eq (seq_count (mod->mod_vals.modv_strvals), 3);
Packit Service 6d40f9
	assert_str_eq (mod->mod_vals.modv_strvals[0], "value");
Packit Service 6d40f9
	assert_str_eq (mod->mod_vals.modv_strvals[1], "two");
Packit Service 6d40f9
	assert_str_eq (mod->mod_vals.modv_strvals[2], "three");
Packit Service 6d40f9
	assert (mod->mod_vals.modv_strvals[3] == NULL);
Packit Service 6d40f9
Packit Service 6d40f9
	_adcli_ldap_mod_free (mod);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
static void
Packit Service 6d40f9
test_new1 (void)
Packit Service 6d40f9
{
Packit Service 6d40f9
	LDAPMod *mod;
Packit Service 6d40f9
Packit Service 6d40f9
	mod = _adcli_ldap_mod_new1 (LDAP_MOD_ADD, "test", "one");
Packit Service 6d40f9
	assert (mod != NULL);
Packit Service 6d40f9
Packit Service 6d40f9
	assert (mod->mod_op == LDAP_MOD_ADD);
Packit Service 6d40f9
	assert_str_eq (mod->mod_type, "test");
Packit Service 6d40f9
	assert_num_eq (seq_count (mod->mod_vals.modv_strvals), 1);
Packit Service 6d40f9
	assert_str_eq (mod->mod_vals.modv_strvals[0], "one");
Packit Service 6d40f9
	assert (mod->mod_vals.modv_strvals[1] == NULL);
Packit Service 6d40f9
Packit Service 6d40f9
	_adcli_ldap_mod_free (mod);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
static void
Packit Service 6d40f9
test_new_null (void)
Packit Service 6d40f9
{
Packit Service 6d40f9
	LDAPMod *mod;
Packit Service 6d40f9
Packit Service 6d40f9
	mod = _adcli_ldap_mod_new (LDAP_MOD_ADD, "test", NULL);
Packit Service 6d40f9
	assert (mod != NULL);
Packit Service 6d40f9
Packit Service 6d40f9
	assert (mod->mod_op == LDAP_MOD_ADD);
Packit Service 6d40f9
	assert_str_eq (mod->mod_type, "test");
Packit Service 6d40f9
	assert_num_eq (seq_count (mod->mod_vals.modv_strvals), 0);
Packit Service 6d40f9
	assert (mod->mod_vals.modv_strvals == NULL);
Packit Service 6d40f9
Packit Service 6d40f9
	_adcli_ldap_mod_free (mod);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
static void
Packit Service 6d40f9
test_free_null (void)
Packit Service 6d40f9
{
Packit Service 6d40f9
	_adcli_ldap_mod_free (NULL);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
static void
Packit Service 6d40f9
test_to_string (void)
Packit Service 6d40f9
{
Packit Service 6d40f9
	char *vals_objectClass[] = { "computer", NULL };
Packit Service 6d40f9
	LDAPMod objectClass = { 0, "objectClass", { vals_objectClass, } };
Packit Service 6d40f9
	char *vals_sAMAccountName[] = { "value", NULL };
Packit Service 6d40f9
	LDAPMod sAMAccountName = { 0, "sAMAccountName", { vals_sAMAccountName, } };
Packit Service 6d40f9
	char *vals_userAccountControl[] = { "69632", NULL };
Packit Service 6d40f9
	LDAPMod userAccountControl = { 0, "userAccountControl", { vals_userAccountControl, } };
Packit Service 6d40f9
Packit Service 6d40f9
	LDAPMod *mods[] = {
Packit Service 6d40f9
		&objectClass,
Packit Service 6d40f9
		&sAMAccountName,
Packit Service 6d40f9
		&userAccountControl,
Packit Service 6d40f9
		NULL,
Packit Service 6d40f9
	};
Packit Service 6d40f9
Packit Service 6d40f9
	char *string;
Packit Service 6d40f9
Packit Service 6d40f9
	string = _adcli_ldap_mods_to_string (mods);
Packit Service 6d40f9
	assert_str_eq (string, "objectClass, sAMAccountName, userAccountControl");
Packit Service 6d40f9
	free (string);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
int
Packit Service 6d40f9
main (int argc,
Packit Service 6d40f9
      char *argv[])
Packit Service 6d40f9
{
Packit Service 6d40f9
	test_func (test_compar, "/ldap/compar");
Packit Service 6d40f9
	test_func (test_new_free, "/ldap/new_free");
Packit Service 6d40f9
	test_func (test_new1, "/ldap/new1");
Packit Service 6d40f9
	test_func (test_new_null, "/ldap/new_null");
Packit Service 6d40f9
	test_func (test_free_null, "/ldap/free_null");
Packit Service 6d40f9
	test_func (test_to_string, "/ldap/to_string");
Packit Service 6d40f9
	return test_run (argc, argv);
Packit Service 6d40f9
}
Packit Service 6d40f9
Packit Service 6d40f9
#endif /* LDAP_TESTS */