1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
|
#include "myproxy_common.h"
#ifdef HAVE_LIBLDAP
#include <ldap.h>
#endif
#define DN_BUFFER_SIZE 512
#define USERNAME_BUFFER_SIZE 256
int resolve_via_mapfile ( char * username, char ** dn ) {
int return_value = 0;
char * userdn = NULL;
myproxy_debug("resolve_via_mapfile()");
if ( globus_gss_assist_map_local_user( username,
&userdn ) ) {
return_value = 1;
goto end;
}
*dn = userdn;
end:
if (return_value) {
if (userdn) {
free(userdn);
userdn = NULL;
}
}
return return_value;
}
int resolve_via_mapapp ( char * app_string, char * username, char ** dn ) {
pid_t childpid;
int fds[3];
int return_value = 0;
char * userdn = NULL;
FILE * app_stream = NULL;
int exit_status;
myproxy_debug("resolve_via_mapapp(%s, %s)", app_string, username);
userdn = malloc(DN_BUFFER_SIZE);
if (userdn == NULL) {
verror_put_string("malloc() failed.");
goto end;
}
memset(userdn, '\0', DN_BUFFER_SIZE);
if ((childpid = myproxy_popen(fds, app_string, username, NULL)) < 0) {
return -1; /* myproxy_popen will set verror */
}
close(fds[0]);
/* wait for child */
if (waitpid(childpid, &exit_status, 0) == -1) {
verror_put_string("wait() failed for mapapp child");
verror_put_errno(errno);
return -1;
}
if (exit_status != 0) {
FILE *fp = NULL;
char buf[100];
verror_put_string("Mapping call-out returned error");
fp = fdopen(fds[1], "r");
if (fp) {
while (fgets(buf, 100, fp) != NULL) {
verror_put_string("%s", buf);
}
fclose(fp);
} else {
close(fds[1]);
}
fp = fdopen(fds[2], "r");
if (fp) {
while (fgets(buf, 100, fp) != NULL) {
verror_put_string("%s", buf);
}
fclose(fp);
} else {
close(fds[2]);
}
return_value = 1;
goto end;
}
close(fds[2]);
app_stream = fdopen(fds[1], "r");
if (fgets(userdn, DN_BUFFER_SIZE, app_stream) == NULL) {
fclose(app_stream);
verror_put_string("Error reading from mapping application.");
return_value = 1;
goto end;
}
fclose(app_stream);
app_stream = NULL;
/* Chop trailing newline if present */
if (userdn[strlen(userdn) - 1] == '\n') {
userdn[strlen(userdn) - 1] = '\0';
}
if (strlen(userdn) == 0) {
verror_put_string("Got zero-length DN from mapping application.");
return_value = 1;
goto end;
}
*dn = userdn;
end:
if (return_value) {
if (userdn) {
free(userdn);
userdn = NULL;
}
*dn = NULL;
}
return return_value;
}
#ifdef HAVE_LIBLDAP
int resolve_via_ldap ( char * username, char ** dn,
myproxy_server_context_t *server_context ) {
int return_value = 0;
char * userdn = NULL;
LDAP *ld = NULL;
int rc;
int ldap_version = LDAP_VERSION3;
char * binduser = NULL;
struct berval cred;
struct berval *servcred;
LDAPMessage *results = NULL;
LDAPMessage *entry = NULL;
char * dnbuffer = NULL;
char * searchfilter = NULL;
char * attr;
BerElement *ber = NULL;
struct berval **vals = NULL;
int found_attribute;
LDAPDN tmpDN;
int dn_set = 0;
size_t filterlen;
myproxy_debug("resolve_via_ldap()");
/* check directives to make sure all is in order.... */
if ( server_context->ca_ldap_uid_attribute == NULL ) {
verror_put_string("Required directive ca_ldap_uid_attribute not set.");
return_value = 1;
goto end;
}
if ( server_context->ca_ldap_searchbase == NULL ) {
verror_put_string("Required directive ca_ldap_searchbase not set.");
return_value = 1;
goto end;
}
if (server_context->ca_ldap_server)
myproxy_debug("ca_ldap_server: %s",
server_context->ca_ldap_server);
if (server_context->ca_ldap_uid_attribute)
myproxy_debug("ca_ldap_uid_attribute: %s",
server_context->ca_ldap_uid_attribute);
if (server_context->ca_ldap_searchbase)
myproxy_debug("ca_ldap_searchbase: %s",
server_context->ca_ldap_searchbase);
if (server_context->ca_ldap_connect_dn)
myproxy_debug("ca_ldap_connect_dn: %s",
server_context->ca_ldap_connect_dn);
if (server_context->ca_ldap_connect_passphrase)
myproxy_debug("ca_ldap_connect_passphase: %s",
server_context->ca_ldap_connect_passphrase);
if (server_context->ca_ldap_dn_attribute)
myproxy_debug("ca_ldap_dn_attribute: %s",
server_context->ca_ldap_dn_attribute);
/* prodeed with the connection */
rc = ldap_initialize( &ld, server_context->ca_ldap_server );
if ( rc != LDAP_SUCCESS ) {
verror_put_string("ldap_initialize() failed");
verror_put_string("ldap_initialize(): %s", ldap_err2string( rc ) );
return_value = 1;
goto end;
} else {
myproxy_debug("LDAP initialized");
}
rc = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &ldap_version);
if ( rc != LDAP_SUCCESS ) {
verror_put_string("ldap_set_option() failed");
verror_put_string("ldap_set_option(): %s", ldap_err2string( rc ) );
return_value = 1;
goto end;
} else {
myproxy_debug("LDAP version set to V.3");
}
if ( server_context->ca_ldap_start_tls ) {
rc = ldap_start_tls_s(ld, NULL, NULL);
if ( rc != LDAP_SUCCESS ) {
verror_put_string("ldap_start_tls_s() failed");
verror_put_string("ldap_start_tls_s(): %s", ldap_err2string( rc ) );
return_value = 1;
goto end;
} else {
myproxy_debug("LDAP StartTLS completed");
}
}
if ( server_context->ca_ldap_connect_passphrase != NULL ) {
cred.bv_val = server_context->ca_ldap_connect_passphrase;
cred.bv_len = strlen(server_context->ca_ldap_connect_passphrase);
} else {
cred.bv_val = "";
cred.bv_len = 0;
}
if ( server_context->ca_ldap_connect_dn != NULL ) {
binduser = strdup( server_context->ca_ldap_connect_dn );
} else {
binduser = strdup("");
}
/* NOTE: the other bind functions have been deprecated out of the current
openldap api. Even though this has the rather misleading name of
_sasl_bind_, this is the function currently in favor and it is
only performing a vanilla ldap simple authentication - mmg */
rc = ldap_sasl_bind_s(ld, binduser, LDAP_SASL_SIMPLE,
&cred, NULL, NULL, &servcred);
if ( rc != LDAP_SUCCESS ) {
verror_put_string("ldap_sasl_bind() failed");
verror_put_string("ldap_sasl_bind(): %s", ldap_err2string( rc ) );
return_value = 1;
goto end;
} else {
myproxy_debug("Bind to %s successful", server_context->ca_ldap_server );
}
/* set up query filter strings and run the search */
filterlen = strlen( server_context->ca_ldap_uid_attribute ) \
+ strlen( username ) + 4;
searchfilter = malloc( filterlen );
memset( searchfilter, '\0', filterlen );
sprintf(searchfilter, "(%s=%s)", server_context->ca_ldap_uid_attribute,
username);
myproxy_debug("Using search filter: %s", searchfilter);
rc = ldap_search_ext_s(ld, server_context->ca_ldap_searchbase,
LDAP_SCOPE_SUBTREE, searchfilter, NULL, 0,
NULL, NULL, NULL, 0, &results);
if ( rc != LDAP_SUCCESS ) {
verror_put_string("ldap_search_ext_s() failed");
verror_put_string("ldap_search_ext_s(): %s", ldap_err2string( rc ) );
return_value = 1;
goto end;
} else {
myproxy_debug("Search on base %s successful",
server_context->ca_ldap_searchbase );
}
/* look at what we got back.... */
if ( ldap_count_entries(ld, results) != 1 ) {
verror_put_string("LDAP search returned %d results - resolution failed",
ldap_count_entries(ld, results));
return_value = 1;
goto end;
} else {
myproxy_debug("LDAP query returned one result - processing");
}
entry = ldap_first_entry( ld, results );
if ( entry == NULL ) {
verror_put_string("Error getting ldap entry from search results");
return_value = 1;
goto end;
} else {
myproxy_debug("Obtained LDAP entry from search results");
}
/* extract and process the DN - the default is to use the dn of the
retrieved record. If ca_ldap_dn_attribute is specified, attempt
to retrieve a value from the specified attribute */
if ( server_context->ca_ldap_dn_attribute != NULL ) {
myproxy_debug("Pulling DN from attribute");
found_attribute = 0;
for ( attr = ldap_first_attribute( ld, entry, &ber ) ;
attr != NULL ; attr = ldap_next_attribute( ld, entry, ber ) ) {
if ( strcmp( attr, server_context->ca_ldap_dn_attribute ) == 0 ) {
myproxy_debug("Found attribute: %s", attr );
if ( ( vals = ldap_get_values_len( ld, entry, attr ) ) == NULL ) {
myproxy_debug("No value found for attribute %s", attr);
break;
} else {
myproxy_debug("Attribute value: %s", vals[0]->bv_val );
dnbuffer = strdup( vals[0]->bv_val );
found_attribute = 1;
break;
}
}
}
if ( !found_attribute ) {
verror_put_string("DN Attribute Error");
verror_put_string("Could not find attribute/value pair");
return_value = 1;
goto end;
}
} else {
myproxy_debug("Using record DN");
dnbuffer = ldap_get_dn(ld, entry);
}
if ( dnbuffer == NULL ) {
verror_put_string("Could not obtain DN from search entry");
return_value = 1;
goto end;
} else {
myproxy_debug("Obtained DN: %s", dnbuffer);
}
/* attempt to parse and load the dn input */
if ( ldap_str2dn( dnbuffer, &tmpDN, LDAP_DN_FORMAT_LDAPV3 )
== LDAP_SUCCESS ) {
myproxy_debug("LDAP V3 Style DN");
} else if ( ldap_str2dn( dnbuffer, &tmpDN, LDAP_DN_FORMAT_LDAPV2 )
== LDAP_SUCCESS ) {
myproxy_debug("LDAP V2 Style DN");
} else if ( ldap_str2dn( dnbuffer, &tmpDN, LDAP_DN_FORMAT_DCE )
== LDAP_SUCCESS ) {
myproxy_debug("DCE Style DN");
} else {
/* give up then */
verror_put_string("Could not parse DN: %s", dnbuffer);
return_value = 1;
goto end;
}
dn_set = 1;
/* recover the DN in DCE format */
if ( ldap_dn2str(tmpDN, &userdn, LDAP_DN_FORMAT_DCE) != LDAP_SUCCESS ) {
verror_put_string("Error formatting DN to DCE format");
return_value = 1;
goto end;
} else {
myproxy_debug("Fomatted DN: %s", userdn);
}
*dn = userdn;
end:
if (return_value) {
if (userdn) {
free(userdn);
userdn = NULL;
}
}
/* also free()s the ld pointer */
ldap_unbind_ext_s( ld, NULL, NULL );
if (binduser != NULL) {
free(binduser);
binduser = NULL;
}
if (searchfilter != NULL) {
free(searchfilter);
searchfilter = NULL;
}
if (results != NULL) {
ldap_msgfree(results);
results = NULL;
}
if (dnbuffer != NULL) {
free(dnbuffer);
dnbuffer = NULL;
}
if ( servcred != NULL ) {
ldap_memfree( servcred );
}
if ( dn_set ) {
ldap_dnfree( tmpDN );
}
if ( ber != NULL ) {
ber_free( ber, 0 );
}
if ( server_context->ca_ldap_dn_attribute != NULL ) {
if ( vals != NULL ) {
ber_bvecfree( vals );
}
}
return return_value;
}
#else /* ldap resolution configured but server not built with ldap support */
int resolve_via_ldap ( char * username, char ** dn,
myproxy_server_context_t *server_context ) {
verror_put_string("CA NOT build with LDAP support");
verror_put_string("Can not do user -> DN resolution via ldap");
return(1);
}
#endif /* HAVE_LIBLDAP */
/* not thread safe. uses static buffers. */
int user_dn_lookup( char * username, char ** dn,
myproxy_server_context_t *server_context ) {
int return_value = 0;
char * userdn = NULL;
static char cached_username[USERNAME_BUFFER_SIZE] = "";
static char cached_dn[DN_BUFFER_SIZE] = "";
myproxy_debug("user_dn_lookup()");
if (username && strcmp(username, cached_username) == 0) {
myproxy_debug("using cached value");
*dn = strdup(cached_dn);
goto end;
} else if ( server_context->ca_ldap_server != NULL ) {
if ( resolve_via_ldap( username, &userdn, server_context ) ) {
myproxy_log("Failed to map username %s to DN via LDAP", username);
return_value = 1;
goto end;
}
} else if (server_context->certificate_mapapp != NULL) {
if (resolve_via_mapapp( server_context->certificate_mapapp,
username, &userdn ) ) {
myproxy_log("Failed to map username %s to DN via call-out", username);
return_value = 1;
goto end;
}
} else {
if ( resolve_via_mapfile( username, &userdn ) ) {
myproxy_log("Failed to map username %s to DN via grid-mapfile", username);
return_value = 1;
goto end;
}
}
myproxy_debug("username \"%s\" mapped to DN \"%s\"", username, userdn);
*dn = userdn;
/* keep cache of last result so we don't need to call-out multiple times */
if (username && strlen(username) < USERNAME_BUFFER_SIZE &&
userdn && strlen(userdn) < DN_BUFFER_SIZE) {
strcpy(cached_username, username);
strcpy(cached_dn, userdn);
}
end:
if (return_value) {
if (userdn) {
free(userdn);
userdn = NULL;
}
}
return return_value;
}
|