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>
29 #include <ldapconfig.h>
33 extern LDAP *ld; /* our LDAP descriptor */
34 extern int verbose; /* verbosity indicator */
35 extern char *mygetpass(); /* getpass() passwds are too short */
38 extern int debug; /* debug flag */
42 static char tktpath[20]; /* ticket file path */
44 static int valid_tgt();
47 static void set_bound_dn();
53 int rc; /* return code from ldap_bind() */
54 char *passwd = NULL; /* returned by mygetpass() */
55 char **rdns; /* for fiddling with the DN */
57 int name_provided; /* was a name passed in? */
59 struct passwd *pw; /* for getting user id */
65 char **krbnames; /* for kerberos names */
70 LDAPMessage *mp; /* returned from find() */
71 static char prompt[MED_BUF_SIZE]; /* place for us to sprintf the prompt */
72 static char name[MED_BUF_SIZE]; /* place to store the user's name */
73 static char password[MED_BUF_SIZE]; /* password entered by user */
74 extern struct entry Entry; /* look here for a name if needed */
75 extern LDAPMessage *find(); /* for looking up 'name' */
76 extern char *search_base; /* for printing later */
77 extern char *default_bind_object; /* bind as this on failure */
78 extern void printbase(); /* used to pretty-print a base */
79 extern int bind_status;
84 fprintf(stderr, "auth(%s, NULL)\n", who);
86 name_provided = ( who != NULL );
89 * The user needs to bind. If <who> is not specified, we
90 * assume that authenticating as user id is what user wants.
92 if (who == NULL && implicit) {
96 if ((pw = getpwuid((uid_t)geteuid())) != (struct passwd *) NULL) {
97 sprintf(uidname, "uid=%s", pw->pw_name);
100 user = getenv("USER");
101 if(user == NULL) user = getenv("USERNAME");
102 if(user == NULL) user = getenv("LOGNAME");
105 sprintf(uidname, "uid=%s", user);
109 if(uidname[0] != '\0') {
116 printf( "You must first authenticate yourself to the Directory.\n" );
118 printf(" What is your name or uniqname? ");
120 printf(" What is your name or user id? ");
123 fetch_buffer(name, sizeof(name), stdin);
130 if (debug & D_AUTHENTICAT)
131 printf(" Authenticating as \"%s\"\n", who);
135 * Bail out if the name is bogus. If not, strip off the junk
136 * at the start of the DN, build a prompt, and get a password
137 * from the user. Then perform the ldap_bind().
139 if ((mp = find(who, TRUE)) == NULL) {
140 (void) ldap_msgfree(mp);
141 printf(" I could not find \"%s\" in the Directory.\n", who);
142 printf(" I used a search base of ");
143 printbase("", search_base);
146 if (debug & D_AUTHENTICAT)
147 printf(" Could not find \"%s\"\n", who);
153 * Fill in the Entry structure. May be handy later.
155 (void) parse_answer(mp);
157 rdns = ldap_explode_dn(Entry.DN, TRUE);
158 printf(" Authenticating to the directory as \"%s\"...\n", *rdns );
162 * First, if the user has a choice of auth methods, ask which
163 * one they want to use. if they want kerberos, ask which
164 * krbname they want to bind as.
167 if ( (krbnames = ldap_get_values( ld, mp, "krbName" )) != NULL ) {
168 int choice, hassimple;
170 hassimple = (ldap_compare_s( ld, Entry.DN,
171 "userPassword", "x" ) == LDAP_COMPARE_FALSE);
172 (void) ldap_msgfree(mp);
174 /* if we're running as a server (e.g., out of inetd) */
175 if ( ! isatty( 1 ) ) {
176 strcpy( tktpath, "/tmp/ud_tktXXXXXX" );
178 krb_set_tkt_string( tktpath );
181 kinited = valid_tgt( krbnames );
183 if ( hassimple && !kinited ) {
184 printf(" Which password would you like to use?\n");
185 printf(" 1 -> LDAP password\n");
187 printf(" 2 -> UMICH password (aka Uniqname or Kerberos password)\n");
189 printf(" 2 -> Kerberos password\n");
193 printf(" Enter 1 or 2: ");
196 fetch_buffer(buf, sizeof(buf), stdin);
198 } while (choice != 1 && choice != 2);
200 authmethod = (choice == 1 ? LDAP_AUTH_SIMPLE :
203 authmethod = LDAP_AUTH_KRBV4;
206 authmethod = LDAP_AUTH_SIMPLE;
207 (void) ldap_msgfree(mp);
211 * if they are already kinited, we don't need to ask for a
215 if ( authmethod == LDAP_AUTH_KRBV4 ) {
217 if ( krbnames[1] != NULL ) {
220 /* ask which one to use */
222 printf(" Which UMICH (aka Kerberos or uniqname) name would you like to use?\n");
224 printf(" Which Kerberos name would you like to use?\n");
226 for ( i = 0; krbnames[i] != NULL; i++ ) {
227 printf( " %d -> %s\n", i + 1,
231 printf(" Enter a number between 1 and %d: ", i );
234 fetch_buffer(buf, sizeof(buf), stdin);
235 ikrb = atoi(buf) - 1;
236 } while ( ikrb > i - 1 || ikrb < 0 );
242 if ( kinit( krbnames[ikrb] ) != 0 ) {
243 (void) ldap_value_free(rdns);
244 (void) ldap_value_free(krbnames);
250 authmethod = LDAP_AUTH_SIMPLE;
251 sprintf(prompt, " Enter your LDAP password: ");
253 passwd = mygetpass(prompt);
254 } while (passwd != NULL && *passwd == '\0');
255 if (passwd == NULL) {
256 (void) ldap_value_free(rdns);
261 (void) ldap_value_free(krbnames);
263 ldap_flush_cache( ld );
264 rc = ldap_bind_s(ld, Entry.DN, passwd, authmethod);
265 if (rc != LDAP_SUCCESS) {
267 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
268 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
269 fprintf(stderr, " Entry has no password\n");
270 else if (ld_errno == LDAP_INVALID_CREDENTIALS)
272 if ( authmethod == LDAP_AUTH_KRBV4 ) {
273 fprintf(stderr, " The Kerberos credentials are invalid.\n");
276 fprintf(stderr, " The password you provided is incorrect.\n");
281 ldap_perror(ld, "ldap_bind_s" );
282 (void) ldap_bind_s(ld, default_bind_object,
283 (char *) NULL, LDAP_AUTH_SIMPLE);
284 if (default_bind_object == NULL)
287 set_bound_dn(default_bind_object);
288 bind_status = UD_NOT_BOUND;
290 printf(" Authentication failed.\n\n");
291 (void) ldap_value_free(rdns);
295 printf(" Authentication successful.\n\n");
298 set_bound_dn(Entry.DN);
299 bind_status = UD_BOUND;
301 (void) strcpy(password, passwd);
302 (void) ldap_value_free(rdns);
308 #define FIVEMINS ( 5 * 60 )
311 static void str2upper( s )
316 for ( p = s; *p != '\0'; ++p ) {
322 static valid_tgt( names )
326 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
329 for ( i = 0; names[i] != NULL; i++ ) {
330 if ( kname_parse( name, inst, realm, names[i] ) != KSUCCESS ) {
331 fprintf( stderr, "Bad format for krbName %s\n",
333 fprintf( stderr, "Contact x500@umich.edu\n" );
337 #ifdef HAVE_AFS_KERBEROS
339 * realm must be uppercase for krb_ routines
342 #endif /* HAVE_AFS_KERBEROS */
345 * check ticket file for a valid ticket granting ticket
346 * my check is: have ticket granting ticket and it is good for
347 * at least 5 more minutes
349 if ( krb_get_cred( TGT, realm, realm,
350 &cred ) == KSUCCESS && time( 0 ) + FIVEMINS <
351 cred.issue_date + (u_char)cred.lifetime * FIVEMINS ) {
359 static char *kauth_name;
363 krbgetpass( user, inst, realm, pw, key )
364 char *user, *inst, *realm, *pw;
367 char *p, lcrealm[ REALM_SZ ], prompt[256], *passwd;
370 sprintf(prompt, " Enter the UMICH password (same as Uniqname or Kerberos password)\n for %s: ", kauth_name );
372 sprintf(prompt, " Enter Kerberos password for %s: ", kauth_name );
375 passwd = mygetpass(prompt);
376 } while (passwd != NULL && *passwd == '\0');
377 if (passwd == NULL) {
381 #ifdef HAVE_AFS_KERBEROS
382 strcpy( lcrealm, realm );
383 for ( p = lcrealm; *p != '\0'; ++p ) {
387 ka_StringToKey( passwd, lcrealm, key );
388 #else /* HAVE_AFS_KERBEROS */
389 string_to_key( passwd, key );
390 #endif /* HAVE_AFS_KERBEROS */
395 static kinit( kname )
399 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
403 if ( kname_parse( name, inst, realm, kname ) != KSUCCESS ) {
404 fprintf( stderr, "Bad format for krbName %s\n",
406 fprintf( stderr, "Contact x500@umich.edu\n" );
410 #ifdef HAVE_AFS_KERBEROS
412 * realm must be uppercase for krb_ routines
415 #endif /* HAVE_AFS_KERBEROS */
417 rc = krb_get_in_tkt( name, inst, realm, TGT, realm,
418 DEFAULT_TKT_LIFE, krbgetpass, NULL, NULL );
420 if ( rc != KSUCCESS ) {
423 fprintf( stderr, "Can't contact Kerberos server for %s\n", realm );
426 fprintf( stderr, "%s: %s\n", name, krb_err_txt[ rc ] );
435 void destroy_tickets(void)
437 if ( *tktpath != '\0' ) {
443 static void set_bound_dn(char *s)
446 extern char *bound_dn;
448 if (bound_dn != NULL)
450 bound_dn = (s == NULL) ? NULL : strdup(s);