2 * Copyright (C) 2002-2004 Novell, Inc.
4 * edir_ldapext.c LDAP extension for reading eDirectory universal password
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of version 2 of the GNU General Public License as published
8 * by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, contact Novell, Inc.
18 * To contact Novell about this file by physical or electronic mail, you may
19 * find current contact information at www.novell.com.
26 /* NMAS error codes */
27 #define NMAS_E_BASE (-1600)
29 #define NMAS_SUCCESS 0
30 #define NMAS_E_SUCCESS NMAS_SUCCESS /* Alias */
31 #define NMAS_OK NMAS_SUCCESS /* Alias */
33 #define NMAS_E_FRAG_FAILURE (NMAS_E_BASE-31) /* -1631 0xFFFFF9A1 */
34 #define NMAS_E_BUFFER_OVERFLOW (NMAS_E_BASE-33) /* -1633 0xFFFFF99F */
35 #define NMAS_E_SYSTEM_RESOURCES (NMAS_E_BASE-34) /* -1634 0xFFFFF99E */
36 #define NMAS_E_INSUFFICIENT_MEMORY (NMAS_E_BASE-35) /* -1635 0xFFFFF99D */
37 #define NMAS_E_NOT_SUPPORTED (NMAS_E_BASE-36) /* -1636 0xFFFFF99C */
38 #define NMAS_E_INVALID_PARAMETER (NMAS_E_BASE-43) /* -1643 0xFFFFF995 */
39 #define NMAS_E_INVALID_VERSION (NMAS_E_BASE-52) /* -1652 0xFFFFF98C */
41 /* OID of LDAP extenstion calls to read Universal Password */
42 #define NMASLDAP_GET_PASSWORD_REQUEST "2.16.840.1.113719.1.39.42.100.13"
43 #define NMASLDAP_GET_PASSWORD_RESPONSE "2.16.840.1.113719.1.39.42.100.14"
45 #define NMAS_LDAP_EXT_VERSION 1
49 /* ------------------------------------------------------------------------
50 * berEncodePasswordData
51 * ==============================
52 * RequestBer contents:
53 * clientVersion INTEGER
54 * targetObjectDN OCTET STRING
55 * password1 OCTET STRING
56 * password2 OCTET STRING
59 * This function takes the request BER value and input data items
60 * and BER encodes the data into the BER value
62 * ------------------------------------------------------------------------ */
63 int berEncodePasswordData(
64 struct berval **requestBV,
70 BerElement *requestBer = NULL;
72 char * utf8ObjPtr = NULL;
74 char * utf8PwdPtr = NULL;
76 char * utf8Pwd2Ptr = NULL;
80 utf8ObjSize = strlen(objectDN)+1;
81 utf8ObjPtr = objectDN;
85 utf8PwdSize = strlen(password)+1;
86 utf8PwdPtr = password;
89 if (password2 != NULL)
91 utf8Pwd2Size = strlen(password2)+1;
92 utf8Pwd2Ptr = password2;
95 /* Allocate a BerElement for the request parameters.*/
96 if((requestBer = ber_alloc()) == NULL)
98 err = NMAS_E_FRAG_FAILURE;
102 if (password != NULL && password2 != NULL)
104 /* BER encode the NMAS Version, the objectDN, and the password */
105 rc = ber_printf(requestBer, "{iooo}", NMAS_LDAP_EXT_VERSION, utf8ObjPtr, utf8ObjSize, utf8PwdPtr, utf8PwdSize, utf8Pwd2Ptr, utf8Pwd2Size);
107 else if (password != NULL)
109 /* BER encode the NMAS Version, the objectDN, and the password */
110 rc = ber_printf(requestBer, "{ioo}", NMAS_LDAP_EXT_VERSION, utf8ObjPtr, utf8ObjSize, utf8PwdPtr, utf8PwdSize);
114 /* BER encode the NMAS Version and the objectDN */
115 rc = ber_printf(requestBer, "{io}", NMAS_LDAP_EXT_VERSION, utf8ObjPtr, utf8ObjSize);
120 err = NMAS_E_FRAG_FAILURE;
129 * Convert the BER we just built to a berval that we'll send with the extended request.
131 if(ber_flatten(requestBer, requestBV) == LBER_ERROR)
133 err = NMAS_E_FRAG_FAILURE;
141 ber_free(requestBer, 1);
145 } /* End of berEncodePasswordData */
147 /* ------------------------------------------------------------------------
148 * berDecodeLoginData()
149 * ==============================
150 * ResponseBer contents:
151 * serverVersion INTEGER
156 * This function takes the reply BER Value and decodes the
157 * NMAS server version and return code and if a non null retData
158 * buffer was supplied, tries to decode the the return data and length
160 * ------------------------------------------------------------------------ */
161 int berDecodeLoginData(
162 struct berval *replyBV,
168 BerElement *replyBer = NULL;
169 char *retOctStr = NULL;
170 size_t retOctStrLen = 0;
172 if((replyBer = ber_init(replyBV)) == NULL)
174 err = NMAS_E_SYSTEM_RESOURCES;
180 retOctStrLen = *retDataLen + 1;
181 retOctStr = (char *)malloc(retOctStrLen);
184 err = NMAS_E_SYSTEM_RESOURCES;
188 if( (rc = ber_scanf(replyBer, "{iis}", serverVersion, &err, retOctStr, &retOctStrLen)) != -1)
190 if (*retDataLen >= retOctStrLen)
192 memcpy(retData, retOctStr, retOctStrLen);
196 err = NMAS_E_BUFFER_OVERFLOW;
199 *retDataLen = retOctStrLen;
203 err = NMAS_E_FRAG_FAILURE;
208 if( (rc = ber_scanf(replyBer, "{ii}", serverVersion, &err)) == -1)
212 err = NMAS_E_FRAG_FAILURE;
221 ber_free(replyBer, 1);
224 if (retOctStr != NULL)
226 memset(retOctStr, 0, retOctStrLen);
231 } /* End of berDecodeLoginData */
233 /* -----------------------------------------------------------------------
234 * nmasldap_get_password()
235 * ==============================
238 * This API attempts to get the universal password
240 * ------------------------------------------------------------------------ */
241 int nmasldap_get_password(
244 size_t *pwdSize, // in bytes
249 struct berval *requestBV = NULL;
250 char *replyOID = NULL;
251 struct berval *replyBV = NULL;
254 size_t pwdBufLen, bufferLen;
256 #ifdef NOT_N_PLAT_NLM
257 int currentThreadGroupID;
260 /* Validate char parameters. */
261 if(objectDN == NULL || (strlen(objectDN) == 0) || pwdSize == NULL || ld == NULL)
263 return NMAS_E_INVALID_PARAMETER;
266 bufferLen = pwdBufLen = *pwdSize;
267 pwdBuf = (char *)malloc(pwdBufLen+2);
270 return NMAS_E_INSUFFICIENT_MEMORY;
273 #ifdef NOT_N_PLAT_NLM
274 currentThreadGroupID = SetThreadGroupID(nmasLDAPThreadGroupID);
277 err = berEncodePasswordData(&requestBV, objectDN, NULL, NULL);
283 /* Call the ldap_extended_operation (synchronously) */
284 if((err = ldap_extended_operation_s(ld, NMASLDAP_GET_PASSWORD_REQUEST, requestBV, NULL, NULL, &replyOID, &replyBV)))
289 /* Make sure there is a return OID */
292 err = NMAS_E_NOT_SUPPORTED;
296 /* Is this what we were expecting to get back. */
297 if(strcmp(replyOID, NMASLDAP_GET_PASSWORD_RESPONSE))
299 err = NMAS_E_NOT_SUPPORTED;
303 /* Do we have a good returned berval? */
307 * No; returned berval means we experienced a rather drastic error.
308 * Return operations error.
310 err = NMAS_E_SYSTEM_RESOURCES;
314 err = berDecodeLoginData(replyBV, &serverVersion, &pwdBufLen, pwdBuf);
316 if(serverVersion != NMAS_LDAP_EXT_VERSION)
318 err = NMAS_E_INVALID_VERSION;
322 if (!err && pwdBufLen != 0)
324 if (*pwdSize >= pwdBufLen+1 && pwd != NULL)
326 memcpy(pwd, pwdBuf, pwdBufLen);
327 pwd[pwdBufLen] = 0; /* add null termination */
329 *pwdSize = pwdBufLen; /* does not include null termination */
339 /* Free the return OID string if one was returned. */
342 ldap_memfree(replyOID);
345 /* Free memory allocated while building the request ber and berval. */
348 ber_bvfree(requestBV);
353 memset(pwdBuf, 0, bufferLen);
357 #ifdef NOT_N_PLAT_NLM
358 SetThreadGroupID(currentThreadGroupID);
361 /* Return the appropriate error/success code. */
363 } /* end of nmasldap_get_password */