2 * Copyright (c) 1991, 1992 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
20 #include <ac/string.h>
25 #include <ldapconfig.h>
29 extern LDAP *ld; /* our LDAP descriptor */
30 extern int verbose; /* verbosity indicator */
31 extern char *mygetpass(); /* getpass() passwds are too short */
34 extern int debug; /* debug flag */
38 static char tktpath[20]; /* ticket file path */
40 static int valid_tgt();
47 int rc; /* return code from ldap_bind() */
48 char *passwd = NULL; /* returned by mygetpass() */
49 char **rdns; /* for fiddling with the DN */
51 int name_provided; /* was a name passed in? */
52 struct passwd *pw; /* for getting user id */
55 char **krbnames; /* for kerberos names */
60 LDAPMessage *mp; /* returned from find() */
61 static char prompt[MED_BUF_SIZE]; /* place for us to sprintf the prompt */
62 static char name[MED_BUF_SIZE]; /* place to store the user's name */
63 static char password[MED_BUF_SIZE]; /* password entered by user */
64 extern struct entry Entry; /* look here for a name if needed */
65 extern LDAPMessage *find(); /* for looking up 'name' */
66 extern char *search_base; /* for printing later */
67 extern char *default_bind_object; /* bind as this on failure */
68 extern void printbase(); /* used to pretty-print a base */
69 extern int bind_status;
71 static void set_bound_dn();
75 fprintf(stderr, "auth(%s, NULL)\n", who);
77 name_provided = ( who != NULL );
80 * The user needs to bind. If <who> is not specified, we
81 * assume that authenticating as user id is what user wants.
83 if (who == NULL && implicit && (pw = getpwuid((uid_t)geteuid()))
84 != (struct passwd *) NULL) {
85 sprintf(uidname, "uid=%s", pw->pw_name);
86 /* who = pw->pw_name; /* */
92 printf( "You must first authenticate yourself to the Directory.\n" );
94 printf(" What is your name or uniqname? ");
96 printf(" What is your name or user id? ");
99 fetch_buffer(name, sizeof(name), stdin);
106 if (debug & D_AUTHENTICAT)
107 printf(" Authenticating as \"%s\"\n", who);
111 * Bail out if the name is bogus. If not, strip off the junk
112 * at the start of the DN, build a prompt, and get a password
113 * from the user. Then perform the ldap_bind().
115 if ((mp = find(who, TRUE)) == NULL) {
116 (void) ldap_msgfree(mp);
117 printf(" I could not find \"%s\" in the Directory.\n", who);
118 printf(" I used a search base of ");
119 printbase("", search_base);
122 if (debug & D_AUTHENTICAT)
123 printf(" Could not find \"%s\"\n", who);
129 * Fill in the Entry structure. May be handy later.
131 (void) parse_answer(mp);
133 rdns = ldap_explode_dn(Entry.DN, TRUE);
134 printf(" Authenticating to the directory as \"%s\"...\n", *rdns );
138 * First, if the user has a choice of auth methods, ask which
139 * one they want to use. if they want kerberos, ask which
140 * krbname they want to bind as.
143 if ( (krbnames = ldap_get_values( ld, mp, "krbName" )) != NULL ) {
144 int choice, hassimple;
146 hassimple = (ldap_compare_s( ld, Entry.DN,
147 "userPassword", "x" ) == LDAP_COMPARE_FALSE);
148 (void) ldap_msgfree(mp);
150 /* if we're running as a server (e.g., out of inetd) */
151 if ( ! isatty( 1 ) ) {
152 strcpy( tktpath, "/tmp/ud_tktXXXXXX" );
154 krb_set_tkt_string( tktpath );
157 kinited = valid_tgt( krbnames );
159 if ( hassimple && !kinited ) {
160 printf(" Which password would you like to use?\n");
161 printf(" 1 -> LDAP password\n");
163 printf(" 2 -> UMICH password (aka Uniqname or Kerberos password)\n");
165 printf(" 2 -> Kerberos password\n");
169 printf(" Enter 1 or 2: ");
172 fetch_buffer(buf, sizeof(buf), stdin);
174 } while (choice != 1 && choice != 2);
176 authmethod = (choice == 1 ? LDAP_AUTH_SIMPLE :
179 authmethod = LDAP_AUTH_KRBV4;
182 authmethod = LDAP_AUTH_SIMPLE;
183 (void) ldap_msgfree(mp);
187 * if they are already kinited, we don't need to ask for a
191 if ( authmethod == LDAP_AUTH_KRBV4 ) {
193 if ( krbnames[1] != NULL ) {
196 /* ask which one to use */
198 printf(" Which UMICH (aka Kerberos or uniqname) name would you like to use?\n");
200 printf(" Which Kerberos name would you like to use?\n");
202 for ( i = 0; krbnames[i] != NULL; i++ ) {
203 printf( " %d -> %s\n", i + 1,
207 printf(" Enter a number between 1 and %d: ", i );
210 fetch_buffer(buf, sizeof(buf), stdin);
211 ikrb = atoi(buf) - 1;
212 } while ( ikrb > i - 1 || ikrb < 0 );
218 if ( kinit( krbnames[ikrb] ) != 0 ) {
219 (void) ldap_value_free(rdns);
220 (void) ldap_value_free(krbnames);
226 authmethod = LDAP_AUTH_SIMPLE;
227 sprintf(prompt, " Enter your LDAP password: ");
229 passwd = mygetpass(prompt);
230 } while (passwd != NULL && *passwd == '\0');
231 if (passwd == NULL) {
232 (void) ldap_value_free(rdns);
237 (void) ldap_value_free(krbnames);
239 ldap_flush_cache( ld );
240 rc = ldap_bind_s(ld, Entry.DN, passwd, authmethod);
241 if (rc != LDAP_SUCCESS) {
242 if (ld->ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
243 fprintf(stderr, " Entry has no password\n");
244 else if (ld->ld_errno == LDAP_INVALID_CREDENTIALS)
246 if ( authmethod == LDAP_AUTH_KRBV4 ) {
247 fprintf(stderr, " The Kerberos credentials are invalid.\n");
250 fprintf(stderr, " The password you provided is incorrect.\n");
255 ldap_perror(ld, "ldap_bind_s" );
256 (void) ldap_bind_s(ld, default_bind_object,
257 (char *) UD_BIND_CRED, LDAP_AUTH_SIMPLE);
258 if (default_bind_object == NULL)
261 set_bound_dn(default_bind_object);
262 bind_status = UD_NOT_BOUND;
264 printf(" Authentication failed.\n\n");
265 (void) ldap_value_free(rdns);
269 printf(" Authentication successful.\n\n");
272 set_bound_dn(Entry.DN);
273 bind_status = UD_BOUND;
275 (void) strcpy(password, passwd);
276 (void) ldap_value_free(rdns);
282 #define FIVEMINS ( 5 * 60 )
285 static void str2upper( s )
290 for ( p = s; *p != '\0'; ++p ) {
291 if ( islower( *p )) {
298 static valid_tgt( 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
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( user, inst, realm, pw, key )
340 char *user, *inst, *realm, *pw;
343 char *p, lcrealm[ REALM_SZ ], prompt[256], *passwd;
346 sprintf(prompt, " Enter the UMICH password (same as Uniqname or Kerberos password)\n for %s: ", kauth_name );
348 sprintf(prompt, " Enter Kerberos password for %s: ", kauth_name );
351 passwd = mygetpass(prompt);
352 } while (passwd != NULL && *passwd == '\0');
353 if (passwd == NULL) {
357 #ifdef HAVE_AFS_KERBEROS
358 strcpy( lcrealm, realm );
359 for ( p = lcrealm; *p != '\0'; ++p ) {
360 if ( isupper( *p )) {
365 ka_StringToKey( passwd, lcrealm, key );
366 #else /* HAVE_AFS_KERBEROS */
367 string_to_key( passwd, key );
368 #endif /* HAVE_AFS_KERBEROS */
373 static kinit( kname )
377 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
381 if ( kname_parse( name, inst, realm, kname ) != KSUCCESS ) {
382 fprintf( stderr, "Bad format for krbName %s\n",
384 fprintf( stderr, "Contact x500@umich.edu\n" );
388 #ifdef HAVE_AFS_KERBEROS
390 * realm must be uppercase for krb_ routines
393 #endif /* HAVE_AFS_KERBEROS */
395 rc = krb_get_in_tkt( name, inst, realm, TGT, realm,
396 DEFAULT_TKT_LIFE, krbgetpass, NULL, NULL );
398 if ( rc != KSUCCESS ) {
401 fprintf( stderr, "Can't contact Kerberos server for %s\n", realm );
404 fprintf( stderr, "%s: %s\n", name, krb_err_txt[ rc ] );
413 void destroy_tickets(void)
415 if ( *tktpath != '\0' ) {
421 static void set_bound_dn(char *s)
424 extern char *bound_dn;
426 if (bound_dn != NULL)
428 bound_dn = (s == NULL) ? NULL : strdup(s);