3 * Copyright (c) 1991, 1992 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
18 #include <ac/stdlib.h>
22 #include <ac/string.h>
24 #include <ac/unistd.h>
33 #include "ldap_defaults.h"
37 static char tktpath[20]; /* ticket file path */
39 static int valid_tgt();
42 static void set_bound_dn(char *s);
46 auth( char *who, int implicit )
48 int rc; /* return code from ldap_bind() */
49 char *passwd = NULL; /* returned by getpass() */
50 char **rdns; /* for fiddling with the DN */
52 int name_provided; /* was a name passed in? */
54 struct passwd *pw; /* for getting user id */
60 char **krbnames; /* for kerberos names */
65 LDAPMessage *mp; /* returned from find() */
66 static char prompt[MED_BUF_SIZE]; /* place for us to sprintf the prompt */
67 static char name[MED_BUF_SIZE]; /* place to store the user's name */
68 static char password[MED_BUF_SIZE]; /* password entered by user */
72 fprintf(stderr, "auth(%s, NULL)\n", who);
74 name_provided = ( who != NULL );
77 * The user needs to bind. If <who> is not specified, we
78 * assume that authenticating as user id is what user wants.
80 if (who == NULL && implicit) {
84 if ((pw = getpwuid((uid_t)geteuid())) != (struct passwd *) NULL) {
85 sprintf(uidname, "uid=%s", pw->pw_name);
88 user = getenv("USER");
89 if(user == NULL) user = getenv("USERNAME");
90 if(user == NULL) user = getenv("LOGNAME");
93 sprintf(uidname, "uid=%s", user);
97 if(uidname[0] != '\0') {
104 printf( "You must first authenticate yourself to the Directory.\n" );
106 printf(" What is your name or uniqname? ");
108 printf(" What is your name or user id? ");
111 fetch_buffer(name, sizeof(name), stdin);
118 if (debug & D_AUTHENTICAT)
119 printf(" Authenticating as \"%s\"\n", who);
123 * Bail out if the name is bogus. If not, strip off the junk
124 * at the start of the DN, build a prompt, and get a password
125 * from the user. Then perform the ldap_bind().
127 if ((mp = find(who, TRUE)) == NULL) {
128 printf(" I could not find \"%s\" in the Directory.\n", who);
129 printf(" I used a search base of ");
130 printbase("", search_base);
133 if (debug & D_AUTHENTICAT)
134 printf(" Could not find \"%s\"\n", who);
140 * Fill in the Entry structure. May be handy later.
142 (void) parse_answer(mp);
144 rdns = ldap_explode_dn(Entry.DN, TRUE);
145 printf(" Authenticating to the directory as \"%s\"...\n", *rdns );
149 * First, if the user has a choice of auth methods, ask which
150 * one they want to use. if they want kerberos, ask which
151 * krbname they want to bind as.
154 if ( (krbnames = ldap_get_values( ld, mp, "krbName" )) != NULL ) {
155 int choice, hassimple;
157 hassimple = (ldap_compare_s( ld, Entry.DN,
158 "userPassword", "x" ) == LDAP_COMPARE_FALSE);
159 (void) ldap_msgfree(mp);
161 /* if we're running as a server (e.g., out of inetd) */
162 if ( ! isatty( 1 ) ) {
163 strcpy( tktpath, "/tmp/ud_tktXXXXXX" );
165 krb_set_tkt_string( tktpath );
168 kinited = valid_tgt( krbnames );
170 if ( hassimple && !kinited ) {
171 printf(" Which password would you like to use?\n");
172 printf(" 1 -> LDAP password\n");
174 printf(" 2 -> UMICH password (aka Uniqname or Kerberos password)\n");
176 printf(" 2 -> Kerberos password\n");
180 printf(" Enter 1 or 2: ");
183 fetch_buffer(buf, sizeof(buf), stdin);
185 } while (choice != 1 && choice != 2);
187 authmethod = (choice == 1 ? LDAP_AUTH_SIMPLE :
190 authmethod = LDAP_AUTH_KRBV4;
193 authmethod = LDAP_AUTH_SIMPLE;
194 (void) ldap_msgfree(mp);
198 * if they are already kinited, we don't need to ask for a
202 if ( authmethod == LDAP_AUTH_KRBV4 ) {
204 if ( krbnames[1] != NULL ) {
207 /* ask which one to use */
209 printf(" Which UMICH (aka Kerberos or uniqname) name would you like to use?\n");
211 printf(" Which Kerberos name would you like to use?\n");
213 for ( i = 0; krbnames[i] != NULL; i++ ) {
214 printf( " %d -> %s\n", i + 1,
218 printf(" Enter a number between 1 and %d: ", i );
221 fetch_buffer(buf, sizeof(buf), stdin);
222 ikrb = atoi(buf) - 1;
223 } while ( ikrb > i - 1 || ikrb < 0 );
229 if ( kinit( krbnames[ikrb] ) != 0 ) {
230 (void) ldap_value_free(rdns);
231 (void) ldap_value_free(krbnames);
237 authmethod = LDAP_AUTH_SIMPLE;
238 sprintf(prompt, " Enter your LDAP password: ");
240 passwd = getpass(prompt);
241 } while (passwd != NULL && *passwd == '\0');
242 if (passwd == NULL) {
243 (void) ldap_value_free(rdns);
248 (void) ldap_value_free(krbnames);
250 ldap_flush_cache( ld );
251 rc = ldap_bind_s(ld, Entry.DN, passwd, authmethod);
252 if (rc != LDAP_SUCCESS) {
254 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
255 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
256 fprintf(stderr, " Entry has no password\n");
257 else if (ld_errno == LDAP_INVALID_CREDENTIALS)
259 if ( authmethod == LDAP_AUTH_KRBV4 ) {
260 fprintf(stderr, " The Kerberos credentials are invalid.\n");
263 fprintf(stderr, " The password you provided is incorrect.\n");
268 ldap_perror(ld, "ldap_bind_s" );
269 (void) ldap_bind_s(ld, default_bind_object,
270 (char *) NULL, LDAP_AUTH_SIMPLE);
271 if (default_bind_object == NULL)
274 set_bound_dn(default_bind_object);
275 bind_status = UD_NOT_BOUND;
277 printf(" Authentication failed.\n\n");
278 (void) ldap_value_free(rdns);
282 printf(" Authentication successful.\n\n");
285 set_bound_dn(Entry.DN);
286 bind_status = UD_BOUND;
288 (void) strcpy(password, passwd);
289 (void) ldap_value_free(rdns);
295 #define FIVEMINS ( 5 * 60 )
299 valid_tgt( char **names )
302 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
305 for ( i = 0; names[i] != NULL; i++ ) {
306 if ( kname_parse( name, inst, realm, names[i] ) != KSUCCESS ) {
307 fprintf( stderr, "Bad format for krbName %s\n",
309 fprintf( stderr, "Contact x500@umich.edu\n" );
313 #ifdef HAVE_AFS_KERBEROS
315 * realm must be uppercase for krb_ routines
317 ldap_pvt_str2upper( realm );
318 #endif /* HAVE_AFS_KERBEROS */
321 * check ticket file for a valid ticket granting ticket
322 * my check is: have ticket granting ticket and it is good for
323 * at least 5 more minutes
325 if ( krb_get_cred( TGT, realm, realm,
326 &cred ) == KSUCCESS && time( 0 ) + FIVEMINS <
327 cred.issue_date + (u_char)cred.lifetime * FIVEMINS ) {
335 static char *kauth_name;
339 krbgetpass( char *user, char *inst, char *realm, char *pw, C_Block key )
341 char *p, lcrealm[ REALM_SZ ], prompt[256], *passwd;
344 sprintf(prompt, " Enter the UMICH password (same as Uniqname or Kerberos password)\n for %s: ", kauth_name );
346 sprintf(prompt, " Enter Kerberos password for %s: ", kauth_name );
349 passwd = getpass(prompt);
350 } while (passwd != NULL && *passwd == '\0');
351 if (passwd == NULL) {
355 #ifdef HAVE_AFS_KERBEROS
356 strcpy( lcrealm, realm );
357 for ( p = lcrealm; *p != '\0'; ++p ) {
358 *p = TOLOWER( (unsigned char) *p );
361 ka_StringToKey( passwd, lcrealm, key );
362 #else /* HAVE_AFS_KERBEROS */
363 string_to_key( passwd, key );
364 #endif /* HAVE_AFS_KERBEROS */
373 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
377 if ( kname_parse( name, inst, realm, kname ) != KSUCCESS ) {
378 fprintf( stderr, "Bad format for krbName %s\n",
380 fprintf( stderr, "Contact x500@umich.edu\n" );
384 #ifdef HAVE_AFS_KERBEROS
386 * realm must be uppercase for krb_ routines
388 ldap_pvt_str2upper( realm );
389 #endif /* HAVE_AFS_KERBEROS */
391 rc = krb_get_in_tkt( name, inst, realm, TGT, realm,
392 DEFAULT_TKT_LIFE, krbgetpass, NULL, NULL );
394 if ( rc != KSUCCESS ) {
397 fprintf( stderr, "Can't contact Kerberos server for %s\n", realm );
400 fprintf( stderr, "%s: %s\n", name, krb_err_txt[ rc ] );
410 destroy_tickets( void )
412 if ( *tktpath != '\0' ) {
419 set_bound_dn( char *s )
421 if (bound_dn != NULL)
423 bound_dn = (s == NULL) ? NULL : strdup(s);