xref: /openbsd-src/libexec/login_ldap/bind.c (revision f95714a45af87f6d8b907be4b389c9683b45d161)
1 /*
2  * $OpenBSD: bind.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 <login_cap.h>
35 
36 #include "aldap.h"
37 #include "login_ldap.h"
38 
39 int
bind_password(struct auth_ctx * ctx,char * binddn,char * bindpw)40 bind_password(struct auth_ctx *ctx, char *binddn, char *bindpw)
41 {
42 	struct aldap_message *m;
43 
44 	if (aldap_bind(ctx->ld, binddn, bindpw) == -1) {
45 		dlog(0, "Failed to send bind request");
46 		return 0;
47 	}
48 
49 	if ((m = aldap_parse(ctx->ld)) == NULL) {
50 		dlog(0, "Failed to receive bind response");
51 		return 0;
52 	}
53 
54 	if (ctx->ld->msgid != m->msgid) {
55 		dlog(0, "Failed to bind: Received unexpected message id");
56 		aldap_freemsg(m);
57 		return 0;
58 	}
59 	if (aldap_get_resultcode(m) != LDAP_SUCCESS) {
60 		dlog(0, "Failed to bind: %s",
61 		    ldap_resultcode(aldap_get_resultcode(m)));
62 		aldap_freemsg(m);
63 		return 0;
64 	}
65 	aldap_freemsg(m);
66 
67 	return 1;
68 }
69 
70 int
unbind(struct auth_ctx * ctx)71 unbind(struct auth_ctx *ctx)
72 {
73 	return aldap_unbind(ctx->ld);
74 }
75 
76