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