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.
19 #include <sys/types.h>
25 #include <ldapconfig.h>
28 extern LDAP *ld; /* our LDAP descriptor */
29 extern int verbose; /* verbosity indicator */
30 extern char *mygetpass(); /* getpass() passwds are too short */
33 extern int debug; /* debug flag */
37 static char tktpath[20]; /* ticket file path */
39 static int valid_tgt();
46 int rc; /* return code from ldap_bind() */
47 char *passwd = NULL; /* returned by mygetpass() */
48 char **rdns; /* for fiddling with the DN */
50 int name_provided; /* was a name passed in? */
51 struct passwd *pw; /* for getting user id */
54 char **krbnames; /* for kerberos names */
59 LDAPMessage *mp; /* returned from find() */
60 static char prompt[MED_BUF_SIZE]; /* place for us to sprintf the prompt */
61 static char name[MED_BUF_SIZE]; /* place to store the user's name */
62 static char password[MED_BUF_SIZE]; /* password entered by user */
63 extern struct entry Entry; /* look here for a name if needed */
64 extern LDAPMessage *find(); /* for looking up 'name' */
65 extern char *search_base; /* for printing later */
66 extern char *default_bind_object; /* bind as this on failure */
67 extern void printbase(); /* used to pretty-print a base */
68 extern int bind_status;
70 static void set_bound_dn();
74 fprintf(stderr, "auth(%s, NULL)\n", who);
76 name_provided = ( who != NULL );
79 * The user needs to bind. If <who> is not specified, we
80 * assume that authenticating as user id is what user wants.
82 if (who == NULL && implicit && (pw = getpwuid((uid_t)geteuid()))
83 != (struct passwd *) NULL) {
84 sprintf(uidname, "uid=%s", pw->pw_name);
85 /* who = pw->pw_name; /* */
91 printf( "You must first authenticate yourself to the Directory.\n" );
93 printf(" What is your name or uniqname? ");
95 printf(" What is your name or user id? ");
98 fetch_buffer(name, sizeof(name), stdin);
105 if (debug & D_AUTHENTICAT)
106 printf(" Authenticating as \"%s\"\n", who);
110 * Bail out if the name is bogus. If not, strip off the junk
111 * at the start of the DN, build a prompt, and get a password
112 * from the user. Then perform the ldap_bind().
114 if ((mp = find(who, TRUE)) == NULL) {
115 (void) ldap_msgfree(mp);
116 printf(" I could not find \"%s\" in the Directory.\n", who);
117 printf(" I used a search base of ");
118 printbase("", search_base);
121 if (debug & D_AUTHENTICAT)
122 printf(" Could not find \"%s\"\n", who);
128 * Fill in the Entry structure. May be handy later.
130 (void) parse_answer(mp);
132 rdns = ldap_explode_dn(Entry.DN, TRUE);
133 printf(" Authenticating to the directory as \"%s\"...\n", *rdns );
137 * First, if the user has a choice of auth methods, ask which
138 * one they want to use. if they want kerberos, ask which
139 * krbname they want to bind as.
142 if ( (krbnames = ldap_get_values( ld, mp, "krbName" )) != NULL ) {
143 int choice, hassimple;
145 hassimple = (ldap_compare_s( ld, Entry.DN,
146 "userPassword", "x" ) == LDAP_COMPARE_FALSE);
147 (void) ldap_msgfree(mp);
149 /* if we're running as a server (e.g., out of inetd) */
150 if ( ! isatty( 1 ) ) {
151 strcpy( tktpath, "/tmp/ud_tktXXXXXX" );
153 krb_set_tkt_string( tktpath );
156 kinited = valid_tgt( krbnames );
158 if ( hassimple && !kinited ) {
159 printf(" Which password would you like to use?\n");
160 printf(" 1 -> X.500 password\n");
162 printf(" 2 -> UMICH password (aka Uniqname or Kerberos password)\n");
164 printf(" 2 -> Kerberos password\n");
168 printf(" Enter 1 or 2: ");
171 fetch_buffer(buf, sizeof(buf), stdin);
173 } while (choice != 1 && choice != 2);
175 authmethod = (choice == 1 ? LDAP_AUTH_SIMPLE :
178 authmethod = LDAP_AUTH_KRBV4;
181 authmethod = LDAP_AUTH_SIMPLE;
182 (void) ldap_msgfree(mp);
186 * if they are already kinited, we don't need to ask for a
190 if ( authmethod == LDAP_AUTH_KRBV4 ) {
192 if ( krbnames[1] != NULL ) {
195 /* ask which one to use */
197 printf(" Which UMICH (aka Kerberos or uniqname) name would you like to use?\n");
199 printf(" Which Kerberos name would you like to use?\n");
201 for ( i = 0; krbnames[i] != NULL; i++ ) {
202 printf( " %d -> %s\n", i + 1,
206 printf(" Enter a number between 1 and %d: ", i );
209 fetch_buffer(buf, sizeof(buf), stdin);
210 ikrb = atoi(buf) - 1;
211 } while ( ikrb > i - 1 || ikrb < 0 );
217 if ( kinit( krbnames[ikrb] ) != 0 ) {
218 (void) ldap_value_free(rdns);
219 (void) ldap_value_free(krbnames);
225 authmethod = LDAP_AUTH_SIMPLE;
226 sprintf(prompt, " Enter your X.500 password: ");
228 passwd = mygetpass(prompt);
229 } while (passwd != NULL && *passwd == '\0');
230 if (passwd == NULL) {
231 (void) ldap_value_free(rdns);
236 (void) ldap_value_free(krbnames);
238 ldap_flush_cache( ld );
239 rc = ldap_bind_s(ld, Entry.DN, passwd, authmethod);
240 if (rc != LDAP_SUCCESS) {
241 if (ld->ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
242 fprintf(stderr, " Entry has no password\n");
243 else if (ld->ld_errno == LDAP_INVALID_CREDENTIALS)
245 if ( authmethod == LDAP_AUTH_KRBV4 ) {
246 fprintf(stderr, " The Kerberos credentials are invalid.\n");
249 fprintf(stderr, " The password you provided is incorrect.\n");
254 ldap_perror(ld, "ldap_bind_s" );
255 (void) ldap_bind_s(ld, default_bind_object,
256 (char *) UD_BIND_CRED, LDAP_AUTH_SIMPLE);
257 if (default_bind_object == NULL)
260 set_bound_dn(default_bind_object);
261 bind_status = UD_NOT_BOUND;
263 printf(" Authentication failed.\n\n");
264 (void) ldap_value_free(rdns);
268 printf(" Authentication successful.\n\n");
271 set_bound_dn(Entry.DN);
272 bind_status = UD_BOUND;
274 (void) strcpy(password, passwd);
275 (void) ldap_value_free(rdns);
281 #define FIVEMINS ( 5 * 60 )
289 for ( p = s; *p != '\0'; ++p ) {
290 if ( islower( *p )) {
297 static valid_tgt( names )
301 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
304 for ( i = 0; names[i] != NULL; i++ ) {
305 if ( kname_parse( name, inst, realm, names[i] ) != KSUCCESS ) {
306 fprintf( stderr, "Bad format for krbName %s\n",
308 fprintf( stderr, "Contact x500@umich.edu\n" );
314 * realm must be uppercase for krb_ routines
317 #endif /* AFSKERBEROS */
320 * check ticket file for a valid ticket granting ticket
321 * my check is: have ticket granting ticket and it is good for
322 * at least 5 more minutes
324 if ( krb_get_cred( TGT, realm, realm,
325 &cred ) == KSUCCESS && time( 0 ) + FIVEMINS <
326 cred.issue_date + (u_char)cred.lifetime * FIVEMINS ) {
334 static char *kauth_name;
338 krbgetpass( user, inst, realm, pw, key )
339 char *user, *inst, *realm, *pw;
342 char *p, lcrealm[ REALM_SZ ], prompt[256], *passwd;
345 sprintf(prompt, " Enter the UMICH password (same as Uniqname or Kerberos password)\n for %s: ", kauth_name );
347 sprintf(prompt, " Enter Kerberos password for %s: ", kauth_name );
350 passwd = mygetpass(prompt);
351 } while (passwd != NULL && *passwd == '\0');
352 if (passwd == NULL) {
357 strcpy( lcrealm, realm );
358 for ( p = lcrealm; *p != '\0'; ++p ) {
359 if ( isupper( *p )) {
364 ka_StringToKey( passwd, lcrealm, key );
365 #else /* AFSKERBEROS */
366 string_to_key( passwd, key );
367 #endif /* AFSKERBEROS */
372 static kinit( kname )
376 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
380 if ( kname_parse( name, inst, realm, kname ) != KSUCCESS ) {
381 fprintf( stderr, "Bad format for krbName %s\n",
383 fprintf( stderr, "Contact x500@umich.edu\n" );
389 * realm must be uppercase for krb_ routines
392 #endif /* AFSKERBEROS */
394 rc = krb_get_in_tkt( name, inst, realm, TGT, realm,
395 DEFAULT_TKT_LIFE, krbgetpass, NULL, NULL );
397 if ( rc != KSUCCESS ) {
400 fprintf( stderr, "Can't contact Kerberos server for %s\n", realm );
403 fprintf( stderr, "%s: %s\n", name, krb_err_txt[ rc ] );
414 if ( *tktpath != '\0' ) {
420 static void set_bound_dn(s)
424 extern char *bound_dn;
426 if (bound_dn != NULL)
428 bound_dn = strdup(s);