1/*
2 * $OpenBSD: search.c,v 1.1 2020/09/12 15:06:12 martijn Exp $
3 * Copyright (c) 2002 Institute for Open Systems Technology Australia (IFOST)
4 * Copyright (c) 2007 Michael Erdely <merdely@openbsd.org>
5 * Copyright (c) 2019 Martijn van Duren <martijn@openbsd.org>
6 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
23 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31#include <netinet/in.h>
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <login_cap.h>
37#include <unistd.h>
38
39#include "aldap.h"
40#include "login_ldap.h"
41
42int q = 0;
43
44char *
45search(struct auth_ctx *ctx, char *base, char *flt, enum scope scp)
46{
47	struct aldap_message *m;
48	int mcode;
49	const char *errstr;
50	char *userdn = NULL;
51
52	dlog(1, "%d: search (%s, %s)", q, base ? base : "no base", flt);
53
54	if (aldap_search(ctx->ld, base, scp, flt, NULL, 0, 0, ctx->timeout,
55	    NULL) == -1) {
56		aldap_get_errno(ctx->ld, &errstr);
57		dlog(1, "search result: %s", errstr);
58		return NULL;
59	}
60
61	q++;
62
63	while ((m = aldap_parse(ctx->ld)) != NULL) {
64		dlog(1, "%d: msgid %d, type %02x",q, m->msgid, m->message_type);
65
66		if (m->message_type == LDAP_RES_SEARCH_RESULT) {
67			mcode = aldap_get_resultcode(m);
68			/*
69			 * if its not a referral we're done with this message
70			 */
71			if (mcode != LDAP_SUCCESS) {
72				dlog(0, "%d: unhandled search result %x %s",
73				    q, mcode, ldap_resultcode(mcode));
74				aldap_freemsg(m);
75				return NULL;
76			}
77			aldap_freemsg(m);
78			break;
79		} else if (m->message_type == LDAP_RES_SEARCH_ENTRY) {
80			userdn = aldap_get_dn(m);
81			dlog(1, "%d: SEARCH_ENTRY userdn %s", q, userdn ? userdn : "none");
82		}
83		aldap_freemsg(m);
84	}
85
86	dlog(1, "%d: returning userdn = %s", q, userdn ? userdn : "no user dn");
87	q--;
88	return userdn;
89}
90