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