3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1991, 1992 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
22 #include <ac/stdlib.h>
26 #include <ac/string.h>
28 #include <ac/unistd.h>
37 #include "ldap_defaults.h"
40 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
41 static char tktpath[20]; /* ticket file path */
43 static int valid_tgt();
46 static void set_bound_dn(char *s);
50 auth( char *who, int implicit )
52 int rc; /* return code from ldap_bind() */
53 char *passwd = NULL; /* returned by getpassphrase() */
54 char **rdns; /* for fiddling with the DN */
56 int name_provided; /* was a name passed in? */
58 struct passwd *pw; /* for getting user id */
63 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
64 char **krbnames; /* for kerberos names */
69 LDAPMessage *mp; /* returned from find() */
70 static char prompt[MED_BUF_SIZE]; /* place for us to sprintf the prompt */
71 static char name[MED_BUF_SIZE]; /* place to store the user's name */
72 static char password[MED_BUF_SIZE]; /* password entered by user */
76 fprintf(stderr, "auth(%s, NULL)\n", who);
78 name_provided = ( who != NULL );
81 * The user needs to bind. If <who> is not specified, we
82 * assume that authenticating as user id is what user wants.
84 if (who == NULL && implicit) {
88 if ((pw = getpwuid((uid_t)geteuid())) != (struct passwd *) NULL) {
89 sprintf(uidname, "uid=%s", pw->pw_name);
92 user = getenv("USER");
93 if(user == NULL) user = getenv("USERNAME");
94 if(user == NULL) user = getenv("LOGNAME");
97 sprintf(uidname, "uid=%s", user);
101 if(uidname[0] != '\0') {
108 printf( "You must first authenticate yourself to the Directory.\n" );
110 printf(" What is your name or uniqname? ");
112 printf(" What is your name or user id? ");
115 fetch_buffer(name, sizeof(name), stdin);
122 if (debug & D_AUTHENTICAT)
123 printf(" Authenticating as \"%s\"\n", who);
127 * Bail out if the name is bogus. If not, strip off the junk
128 * at the start of the DN, build a prompt, and get a password
129 * from the user. Then perform the ldap_bind().
131 if ((mp = find(who, TRUE)) == NULL) {
132 printf(" I could not find \"%s\" in the Directory.\n", who);
133 printf(" I used a search base of ");
134 printbase("", search_base);
137 if (debug & D_AUTHENTICAT)
138 printf(" Could not find \"%s\"\n", who);
144 * Fill in the Entry structure. May be handy later.
146 (void) parse_answer(mp);
148 rdns = ldap_explode_dn(Entry.DN, TRUE);
149 printf(" Authenticating to the directory as \"%s\"...\n", *rdns );
151 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
153 * First, if the user has a choice of auth methods, ask which
154 * one they want to use. if they want kerberos, ask which
155 * krbname they want to bind as.
158 if ( (krbnames = ldap_get_values( ld, mp, "krbName" )) != NULL ) {
159 int choice, hassimple;
161 hassimple = (ldap_compare_s( ld, Entry.DN,
162 "userPassword", "x" ) == LDAP_COMPARE_FALSE);
163 (void) ldap_msgfree(mp);
165 /* if we're running as a server (e.g., out of inetd) */
166 if ( ! isatty( 1 ) ) {
167 strcpy( tktpath, "/tmp/ud_tktXXXXXX" );
169 krb_set_tkt_string( tktpath );
172 kinited = valid_tgt( krbnames );
174 if ( hassimple && !kinited ) {
175 printf(" Which password would you like to use?\n");
176 printf(" 1 -> LDAP password\n");
178 printf(" 2 -> UMICH password (aka Uniqname or Kerberos password)\n");
180 printf(" 2 -> Kerberos password\n");
184 printf(" Enter 1 or 2: ");
187 fetch_buffer(buf, sizeof(buf), stdin);
189 } while (choice != 1 && choice != 2);
191 authmethod = (choice == 1 ? LDAP_AUTH_SIMPLE :
194 authmethod = LDAP_AUTH_KRBV4;
197 authmethod = LDAP_AUTH_SIMPLE;
198 (void) ldap_msgfree(mp);
202 * if they are already kinited, we don't need to ask for a
206 if ( authmethod == LDAP_AUTH_KRBV4 ) {
208 if ( krbnames[1] != NULL ) {
211 /* ask which one to use */
213 printf(" Which UMICH (aka Kerberos or uniqname) name would you like to use?\n");
215 printf(" Which Kerberos name would you like to use?\n");
217 for ( i = 0; krbnames[i] != NULL; i++ ) {
218 printf( " %d -> %s\n", i + 1,
222 printf(" Enter a number between 1 and %d: ", i );
225 fetch_buffer(buf, sizeof(buf), stdin);
226 ikrb = atoi(buf) - 1;
227 } while ( ikrb > i - 1 || ikrb < 0 );
233 if ( kinit( krbnames[ikrb] ) != 0 ) {
234 (void) ldap_value_free(rdns);
235 (void) ldap_value_free(krbnames);
241 authmethod = LDAP_AUTH_SIMPLE;
242 sprintf(prompt, " Enter your LDAP password: ");
244 passwd = getpassphrase(prompt);
245 } while (passwd != NULL && *passwd == '\0');
246 if (passwd == NULL) {
247 (void) ldap_value_free(rdns);
250 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
252 (void) ldap_value_free(krbnames);
254 ldap_flush_cache( ld );
255 rc = ldap_bind_s(ld, Entry.DN, passwd, authmethod);
256 if (rc != LDAP_SUCCESS) {
258 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
259 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
260 fprintf(stderr, " Entry has no password\n");
261 else if (ld_errno == LDAP_INVALID_CREDENTIALS)
262 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
263 if ( authmethod == LDAP_AUTH_KRBV4 ) {
264 fprintf(stderr, " The Kerberos credentials are invalid.\n");
267 fprintf(stderr, " The password you provided is incorrect.\n");
268 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
272 ldap_perror(ld, "ldap_bind_s" );
273 (void) ldap_bind_s(ld, default_bind_object,
274 (char *) NULL, LDAP_AUTH_SIMPLE);
275 if (default_bind_object == NULL)
278 set_bound_dn(default_bind_object);
279 bind_status = UD_NOT_BOUND;
281 printf(" Authentication failed.\n\n");
282 (void) ldap_value_free(rdns);
286 printf(" Authentication successful.\n\n");
289 set_bound_dn(Entry.DN);
290 bind_status = UD_BOUND;
292 (void) strcpy(password, passwd);
293 (void) ldap_value_free(rdns);
297 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
299 #define FIVEMINS ( 5 * 60 )
303 valid_tgt( char **names )
306 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
309 for ( i = 0; names[i] != NULL; i++ ) {
310 if ( kname_parse( name, inst, realm, names[i] ) != KSUCCESS ) {
311 fprintf( stderr, "Bad format for krbName %s\n",
313 fprintf( stderr, "Contact x500@umich.edu\n" );
317 #ifdef HAVE_AFS_KERBEROS
319 * realm must be uppercase for krb_ routines
321 ldap_pvt_str2upper( realm );
322 #endif /* HAVE_AFS_KERBEROS */
325 * check ticket file for a valid ticket granting ticket
326 * my check is: have ticket granting ticket and it is good for
327 * at least 5 more minutes
329 if ( krb_get_cred( TGT, realm, realm,
330 &cred ) == KSUCCESS && time( 0 ) + FIVEMINS <
331 cred.issue_date + (u_char)cred.lifetime * FIVEMINS ) {
339 static char *kauth_name;
341 #ifndef HAVE_KTH_KERBEROS
345 krbgetpass( char *user, char *inst, char *realm, char *pw, C_Block key )
347 char *p, lcrealm[ REALM_SZ ], prompt[256], *passwd;
350 sprintf(prompt, " Enter the UMICH password (same as Uniqname or Kerberos password)\n for %s: ", kauth_name );
352 sprintf(prompt, " Enter Kerberos password for %s: ", kauth_name );
355 passwd = getpassphrase(prompt);
356 } while (passwd != NULL && *passwd == '\0');
357 if (passwd == NULL) {
361 #ifdef HAVE_AFS_KERBEROS
362 strcpy( lcrealm, realm );
363 for ( p = lcrealm; *p != '\0'; ++p ) {
364 *p = TOLOWER( (unsigned char) *p );
367 ka_StringToKey( passwd, lcrealm, key );
368 #else /* HAVE_AFS_KERBEROS */
369 string_to_key( passwd, key );
370 #endif /* HAVE_AFS_KERBEROS */
374 #endif /* HAVE_KTH_KERBEROS */
380 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
384 if ( kname_parse( name, inst, realm, kname ) != KSUCCESS ) {
385 fprintf( stderr, "Bad format for krbName %s\n",
387 fprintf( stderr, "Contact x500@umich.edu\n" );
391 #ifdef HAVE_AFS_KERBEROS
392 /* realm must be uppercase for AFS krb_ routines */
393 ldap_pvt_str2upper( realm );
394 #endif /* HAVE_AFS_KERBEROS */
396 #ifdef HAVE_KTH_KERBEROS
397 /* Kth kerberos knows how to do both string to keys */
398 rc = krb_get_pw_in_tkt( name, inst, realm, TGT, realm,
399 DEFAULT_TKT_LIFE, 0 );
401 rc = krb_get_in_tkt( name, inst, realm, TGT, realm,
402 DEFAULT_TKT_LIFE, krbgetpass, NULL, NULL );
405 if ( rc != KSUCCESS ) {
408 fprintf( stderr, "Can't contact Kerberos server for %s\n", realm );
411 fprintf( stderr, "%s: %s\n", name, krb_err_txt[ rc ] );
421 destroy_tickets( void )
423 if ( *tktpath != '\0' ) {
430 set_bound_dn( char *s )
432 if (bound_dn != NULL)
434 bound_dn = (s == NULL) ? NULL : strdup(s);