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();
43 static void set_bound_dn();
49 int rc; /* return code from ldap_bind() */
50 char *passwd = NULL; /* returned by mygetpass() */
51 char **rdns; /* for fiddling with the DN */
53 int name_provided; /* was a name passed in? */
54 struct passwd *pw; /* for getting user id */
57 char **krbnames; /* for kerberos names */
62 LDAPMessage *mp; /* returned from find() */
63 static char prompt[MED_BUF_SIZE]; /* place for us to sprintf the prompt */
64 static char name[MED_BUF_SIZE]; /* place to store the user's name */
65 static char password[MED_BUF_SIZE]; /* password entered by user */
66 extern struct entry Entry; /* look here for a name if needed */
67 extern LDAPMessage *find(); /* for looking up 'name' */
68 extern char *search_base; /* for printing later */
69 extern char *default_bind_object; /* bind as this on failure */
70 extern void printbase(); /* used to pretty-print a base */
71 extern int bind_status;
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 && (pw = getpwuid((uid_t)geteuid()))
85 != (struct passwd *) NULL) {
86 sprintf(uidname, "uid=%s", pw->pw_name);
87 /* who = pw->pw_name; /* */
93 printf( "You must first authenticate yourself to the Directory.\n" );
95 printf(" What is your name or uniqname? ");
97 printf(" What is your name or user id? ");
100 fetch_buffer(name, sizeof(name), stdin);
107 if (debug & D_AUTHENTICAT)
108 printf(" Authenticating as \"%s\"\n", who);
112 * Bail out if the name is bogus. If not, strip off the junk
113 * at the start of the DN, build a prompt, and get a password
114 * from the user. Then perform the ldap_bind().
116 if ((mp = find(who, TRUE)) == NULL) {
117 (void) ldap_msgfree(mp);
118 printf(" I could not find \"%s\" in the Directory.\n", who);
119 printf(" I used a search base of ");
120 printbase("", search_base);
123 if (debug & D_AUTHENTICAT)
124 printf(" Could not find \"%s\"\n", who);
130 * Fill in the Entry structure. May be handy later.
132 (void) parse_answer(mp);
134 rdns = ldap_explode_dn(Entry.DN, TRUE);
135 printf(" Authenticating to the directory as \"%s\"...\n", *rdns );
139 * First, if the user has a choice of auth methods, ask which
140 * one they want to use. if they want kerberos, ask which
141 * krbname they want to bind as.
144 if ( (krbnames = ldap_get_values( ld, mp, "krbName" )) != NULL ) {
145 int choice, hassimple;
147 hassimple = (ldap_compare_s( ld, Entry.DN,
148 "userPassword", "x" ) == LDAP_COMPARE_FALSE);
149 (void) ldap_msgfree(mp);
151 /* if we're running as a server (e.g., out of inetd) */
152 if ( ! isatty( 1 ) ) {
153 strcpy( tktpath, "/tmp/ud_tktXXXXXX" );
155 krb_set_tkt_string( tktpath );
158 kinited = valid_tgt( krbnames );
160 if ( hassimple && !kinited ) {
161 printf(" Which password would you like to use?\n");
162 printf(" 1 -> LDAP password\n");
164 printf(" 2 -> UMICH password (aka Uniqname or Kerberos password)\n");
166 printf(" 2 -> Kerberos password\n");
170 printf(" Enter 1 or 2: ");
173 fetch_buffer(buf, sizeof(buf), stdin);
175 } while (choice != 1 && choice != 2);
177 authmethod = (choice == 1 ? LDAP_AUTH_SIMPLE :
180 authmethod = LDAP_AUTH_KRBV4;
183 authmethod = LDAP_AUTH_SIMPLE;
184 (void) ldap_msgfree(mp);
188 * if they are already kinited, we don't need to ask for a
192 if ( authmethod == LDAP_AUTH_KRBV4 ) {
194 if ( krbnames[1] != NULL ) {
197 /* ask which one to use */
199 printf(" Which UMICH (aka Kerberos or uniqname) name would you like to use?\n");
201 printf(" Which Kerberos name would you like to use?\n");
203 for ( i = 0; krbnames[i] != NULL; i++ ) {
204 printf( " %d -> %s\n", i + 1,
208 printf(" Enter a number between 1 and %d: ", i );
211 fetch_buffer(buf, sizeof(buf), stdin);
212 ikrb = atoi(buf) - 1;
213 } while ( ikrb > i - 1 || ikrb < 0 );
219 if ( kinit( krbnames[ikrb] ) != 0 ) {
220 (void) ldap_value_free(rdns);
221 (void) ldap_value_free(krbnames);
227 authmethod = LDAP_AUTH_SIMPLE;
228 sprintf(prompt, " Enter your LDAP password: ");
230 passwd = mygetpass(prompt);
231 } while (passwd != NULL && *passwd == '\0');
232 if (passwd == NULL) {
233 (void) ldap_value_free(rdns);
238 (void) ldap_value_free(krbnames);
240 ldap_flush_cache( ld );
241 rc = ldap_bind_s(ld, Entry.DN, passwd, authmethod);
242 if (rc != LDAP_SUCCESS) {
243 if (ld->ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
244 fprintf(stderr, " Entry has no password\n");
245 else if (ld->ld_errno == LDAP_INVALID_CREDENTIALS)
247 if ( authmethod == LDAP_AUTH_KRBV4 ) {
248 fprintf(stderr, " The Kerberos credentials are invalid.\n");
251 fprintf(stderr, " The password you provided is incorrect.\n");
256 ldap_perror(ld, "ldap_bind_s" );
257 (void) ldap_bind_s(ld, default_bind_object,
258 (char *) UD_BIND_CRED, LDAP_AUTH_SIMPLE);
259 if (default_bind_object == NULL)
262 set_bound_dn(default_bind_object);
263 bind_status = UD_NOT_BOUND;
265 printf(" Authentication failed.\n\n");
266 (void) ldap_value_free(rdns);
270 printf(" Authentication successful.\n\n");
273 set_bound_dn(Entry.DN);
274 bind_status = UD_BOUND;
276 (void) strcpy(password, passwd);
277 (void) ldap_value_free(rdns);
283 #define FIVEMINS ( 5 * 60 )
286 static void str2upper( s )
291 for ( p = s; *p != '\0'; ++p ) {
292 if ( islower( *p )) {
299 static valid_tgt( names )
303 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
306 for ( i = 0; names[i] != NULL; i++ ) {
307 if ( kname_parse( name, inst, realm, names[i] ) != KSUCCESS ) {
308 fprintf( stderr, "Bad format for krbName %s\n",
310 fprintf( stderr, "Contact x500@umich.edu\n" );
314 #ifdef HAVE_AFS_KERBEROS
316 * realm must be uppercase for krb_ routines
319 #endif /* HAVE_AFS_KERBEROS */
322 * check ticket file for a valid ticket granting ticket
323 * my check is: have ticket granting ticket and it is good for
324 * at least 5 more minutes
326 if ( krb_get_cred( TGT, realm, realm,
327 &cred ) == KSUCCESS && time( 0 ) + FIVEMINS <
328 cred.issue_date + (u_char)cred.lifetime * FIVEMINS ) {
336 static char *kauth_name;
340 krbgetpass( user, inst, realm, pw, key )
341 char *user, *inst, *realm, *pw;
344 char *p, lcrealm[ REALM_SZ ], prompt[256], *passwd;
347 sprintf(prompt, " Enter the UMICH password (same as Uniqname or Kerberos password)\n for %s: ", kauth_name );
349 sprintf(prompt, " Enter Kerberos password for %s: ", kauth_name );
352 passwd = mygetpass(prompt);
353 } while (passwd != NULL && *passwd == '\0');
354 if (passwd == NULL) {
358 #ifdef HAVE_AFS_KERBEROS
359 strcpy( lcrealm, realm );
360 for ( p = lcrealm; *p != '\0'; ++p ) {
361 if ( isupper( *p )) {
366 ka_StringToKey( passwd, lcrealm, key );
367 #else /* HAVE_AFS_KERBEROS */
368 string_to_key( passwd, key );
369 #endif /* HAVE_AFS_KERBEROS */
374 static kinit( kname )
378 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
382 if ( kname_parse( name, inst, realm, kname ) != KSUCCESS ) {
383 fprintf( stderr, "Bad format for krbName %s\n",
385 fprintf( stderr, "Contact x500@umich.edu\n" );
389 #ifdef HAVE_AFS_KERBEROS
391 * realm must be uppercase for krb_ routines
394 #endif /* HAVE_AFS_KERBEROS */
396 rc = krb_get_in_tkt( name, inst, realm, TGT, realm,
397 DEFAULT_TKT_LIFE, krbgetpass, NULL, NULL );
399 if ( rc != KSUCCESS ) {
402 fprintf( stderr, "Can't contact Kerberos server for %s\n", realm );
405 fprintf( stderr, "%s: %s\n", name, krb_err_txt[ rc ] );
414 void destroy_tickets(void)
416 if ( *tktpath != '\0' ) {
422 static void set_bound_dn(char *s)
425 extern char *bound_dn;
427 if (bound_dn != NULL)
429 bound_dn = (s == NULL) ? NULL : strdup(s);