1 /* $NetBSD: ldap_casa.c,v 1.3 2022/04/03 01:10:59 christos Exp $ */
2
3 /* ldap_casa.c
4
5 CASA routines for DHCPD... */
6
7 /* Copyright (c) 2006 Novell, Inc.
8
9 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * 1.Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * 2.Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * 3.Neither the name of ISC, ISC DHCP, nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20
21 * THIS SOFTWARE IS PROVIDED BY INTERNET SYSTEMS CONSORTIUM AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ISC OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32
33 * This file was written by S Kalyanasundaram <skalyanasundaram@novell.com>
34 */
35
36 /*
37 * Copyright (C) 2004-2022 Internet Systems Consortium, Inc. ("ISC")
38 * Copyright (c) 1995-2003 by Internet Software Consortium
39 *
40 * Permission to use, copy, modify, and distribute this software for any
41 * purpose with or without fee is hereby granted, provided that the above
42 * copyright notice and this permission notice appear in all copies.
43 *
44 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
45 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
46 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
47 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
48 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
49 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
50 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
51 *
52 * Internet Systems Consortium, Inc.
53 * PO Box 360
54 * Newmarket, NH 03857 USA
55 * <info@isc.org>
56 * https://www.isc.org/
57 */
58
59 #include <sys/cdefs.h>
60 __RCSID("$NetBSD: ldap_casa.c,v 1.3 2022/04/03 01:10:59 christos Exp $");
61
62 #if defined(LDAP_CASA_AUTH)
63 #include "dhcpd.h"
64 #include "ldap_casa.h"
65 #include <dlfcn.h>
66 #include <string.h>
67
68 int
load_casa(void)69 load_casa (void)
70 {
71 if( !(casaIDK = dlopen(MICASA_LIB,RTLD_LAZY)))
72 return 0;
73 p_miCASAGetCredential = (CASA_GetCredential_T) dlsym(casaIDK, "miCASAGetCredential");
74 p_miCASASetCredential = (CASA_SetCredential_T) dlsym(casaIDK, "miCASASetCredential");
75 p_miCASARemoveCredential = (CASA_RemoveCredential_T) dlsym(casaIDK, "miCASARemoveCredential");
76
77 if((p_miCASAGetCredential == NULL) ||
78 (p_miCASASetCredential == NULL) ||
79 (p_miCASARemoveCredential == NULL))
80 {
81 if(casaIDK)
82 dlclose(casaIDK);
83 casaIDK = NULL;
84 p_miCASAGetCredential = NULL;
85 p_miCASASetCredential = NULL;
86 p_miCASARemoveCredential = NULL;
87 return 0;
88 }
89 else
90 return 1;
91 }
92
93 static void
release_casa(void)94 release_casa(void)
95 {
96 if(casaIDK)
97 {
98 dlclose(casaIDK);
99 casaIDK = NULL;
100 }
101
102 p_miCASAGetCredential = NULL;
103 p_miCASASetCredential = NULL;
104 p_miCASARemoveCredential = NULL;
105
106 }
107
108 int
load_uname_pwd_from_miCASA(char ** ldap_username,char ** ldap_password)109 load_uname_pwd_from_miCASA (char **ldap_username, char **ldap_password)
110 {
111 int result = 0;
112 uint32_t credentialtype = SSCS_CRED_TYPE_SERVER_F;
113 SSCS_BASIC_CREDENTIAL credential;
114 SSCS_SECRET_ID_T applicationSecretId;
115 char *tempVar = NULL;
116
117 const char applicationName[10] = "dhcp-ldap";
118
119 if ( load_casa() )
120 {
121 memset(&credential, 0, sizeof(SSCS_BASIC_CREDENTIAL));
122 memset(&applicationSecretId, 0, sizeof(SSCS_SECRET_ID_T));
123
124 applicationSecretId.len = strlen(applicationName) + 1;
125 memcpy (applicationSecretId.id, applicationName, applicationSecretId.len);
126
127 credential.unFlags = USERNAME_TYPE_CN_F;
128
129 result = p_miCASAGetCredential (0,
130 &applicationSecretId,NULL,&credentialtype,
131 &credential,NULL);
132
133 if(credential.unLen)
134 {
135 tempVar = dmalloc (credential.unLen + 1, MDL);
136 if (!tempVar)
137 log_fatal ("no memory for ldap_username");
138 memcpy(tempVar , credential.username, credential.unLen);
139 *ldap_username = tempVar;
140
141 tempVar = dmalloc (credential.pwordLen + 1, MDL);
142 if (!tempVar)
143 log_fatal ("no memory for ldap_password");
144 memcpy(tempVar, credential.password, credential.pwordLen);
145 *ldap_password = tempVar;
146
147 #if defined (DEBUG_LDAP)
148 log_info ("Authentication credential taken from CASA");
149 #endif
150
151 release_casa();
152 return 1;
153
154 }
155 else
156 {
157 release_casa();
158 return 0;
159 }
160 }
161 else
162 return 0; //casa libraries not loaded
163 }
164
165 #endif /* LDAP_CASA_AUTH */
166