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 <ldapconfig.h>
22 #include <sys/types.h>
26 extern LDAP *ld; /* our LDAP descriptor */
27 extern int verbose; /* verbosity indicator */
28 extern char *mygetpass(); /* getpass() passwds are too short */
31 extern int debug; /* debug flag */
35 static char tktpath[20]; /* ticket file path */
37 static int valid_tgt();
44 int rc; /* return code from ldap_bind() */
45 char *passwd = NULL; /* returned by mygetpass() */
46 char **rdns; /* for fiddling with the DN */
48 int name_provided; /* was a name passed in? */
49 struct passwd *pw; /* for getting user id */
52 char **krbnames; /* for kerberos names */
57 LDAPMessage *mp; /* returned from find() */
58 static char prompt[MED_BUF_SIZE]; /* place for us to sprintf the prompt */
59 static char name[MED_BUF_SIZE]; /* place to store the user's name */
60 static char password[MED_BUF_SIZE]; /* password entered by user */
61 extern struct entry Entry; /* look here for a name if needed */
62 extern LDAPMessage *find(); /* for looking up 'name' */
63 extern char *search_base; /* for printing later */
64 extern char *default_bind_object; /* bind as this on failure */
65 extern void printbase(); /* used to pretty-print a base */
66 extern int bind_status;
68 static void set_bound_dn();
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 && (pw = getpwuid((uid_t)geteuid()))
81 != (struct passwd *) NULL) {
82 sprintf(uidname, "uid=%s", pw->pw_name);
83 /* who = pw->pw_name; /* */
89 printf( "You must first authenticate yourself to the Directory.\n" );
91 printf(" What is your name or uniqname? ");
93 printf(" What is your name or user id? ");
96 fetch_buffer(name, sizeof(name), stdin);
103 if (debug & D_AUTHENTICAT)
104 printf(" Authenticating as \"%s\"\n", who);
108 * Bail out if the name is bogus. If not, strip off the junk
109 * at the start of the DN, build a prompt, and get a password
110 * from the user. Then perform the ldap_bind().
112 if ((mp = find(who, TRUE)) == NULL) {
113 (void) ldap_msgfree(mp);
114 printf(" I could not find \"%s\" in the Directory.\n", who);
115 printf(" I used a search base of ");
116 printbase("", search_base);
119 if (debug & D_AUTHENTICAT)
120 printf(" Could not find \"%s\"\n", who);
126 * Fill in the Entry structure. May be handy later.
128 (void) parse_answer(mp);
130 rdns = ldap_explode_dn(Entry.DN, TRUE);
131 printf(" Authenticating to the directory as \"%s\"...\n", *rdns );
135 * First, if the user has a choice of auth methods, ask which
136 * one they want to use. if they want kerberos, ask which
137 * krbname they want to bind as.
140 if ( (krbnames = ldap_get_values( ld, mp, "krbName" )) != NULL ) {
141 int choice, hassimple;
143 hassimple = (ldap_compare_s( ld, Entry.DN,
144 "userPassword", "x" ) == LDAP_COMPARE_FALSE);
145 (void) ldap_msgfree(mp);
147 /* if we're running as a server (e.g., out of inetd) */
148 if ( ! isatty( 1 ) ) {
149 strcpy( tktpath, "/tmp/ud_tktXXXXXX" );
151 krb_set_tkt_string( tktpath );
154 kinited = valid_tgt( krbnames );
156 if ( hassimple && !kinited ) {
157 printf(" Which password would you like to use?\n");
158 printf(" 1 -> X.500 password\n");
160 printf(" 2 -> UMICH password (aka Uniqname or Kerberos password)\n");
162 printf(" 2 -> Kerberos password\n");
166 printf(" Enter 1 or 2: ");
169 fetch_buffer(buf, sizeof(buf), stdin);
171 } while (choice != 1 && choice != 2);
173 authmethod = (choice == 1 ? LDAP_AUTH_SIMPLE :
176 authmethod = LDAP_AUTH_KRBV4;
179 authmethod = LDAP_AUTH_SIMPLE;
180 (void) ldap_msgfree(mp);
184 * if they are already kinited, we don't need to ask for a
188 if ( authmethod == LDAP_AUTH_KRBV4 ) {
190 if ( krbnames[1] != NULL ) {
193 /* ask which one to use */
195 printf(" Which UMICH (aka Kerberos or uniqname) name would you like to use?\n");
197 printf(" Which Kerberos name would you like to use?\n");
199 for ( i = 0; krbnames[i] != NULL; i++ ) {
200 printf( " %d -> %s\n", i + 1,
204 printf(" Enter a number between 1 and %d: ", i );
207 fetch_buffer(buf, sizeof(buf), stdin);
208 ikrb = atoi(buf) - 1;
209 } while ( ikrb > i - 1 || ikrb < 0 );
215 if ( kinit( krbnames[ikrb] ) != 0 ) {
216 (void) ldap_value_free(rdns);
217 (void) ldap_value_free(krbnames);
223 authmethod = LDAP_AUTH_SIMPLE;
224 sprintf(prompt, " Enter your X.500 password: ");
226 passwd = mygetpass(prompt);
227 } while (passwd != NULL && *passwd == '\0');
228 if (passwd == NULL) {
229 (void) ldap_value_free(rdns);
234 (void) ldap_value_free(krbnames);
236 ldap_flush_cache( ld );
237 rc = ldap_bind_s(ld, Entry.DN, passwd, authmethod);
238 if (rc != LDAP_SUCCESS) {
239 if (ld->ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
240 fprintf(stderr, " Entry has no password\n");
241 else if (ld->ld_errno == LDAP_INVALID_CREDENTIALS)
243 if ( authmethod == LDAP_AUTH_KRBV4 ) {
244 fprintf(stderr, " The Kerberos credentials are invalid.\n");
247 fprintf(stderr, " The password you provided is incorrect.\n");
252 ldap_perror(ld, "ldap_bind_s" );
253 (void) ldap_bind_s(ld, default_bind_object,
254 (char *) UD_PASSWD, LDAP_AUTH_SIMPLE);
255 if (default_bind_object == NULL)
258 set_bound_dn(default_bind_object);
259 bind_status = UD_NOT_BOUND;
261 printf(" Authentication failed.\n\n");
262 (void) ldap_value_free(rdns);
266 printf(" Authentication successful.\n\n");
269 set_bound_dn(Entry.DN);
270 bind_status = UD_BOUND;
272 (void) strcpy(password, passwd);
273 (void) ldap_value_free(rdns);
279 #define FIVEMINS ( 5 * 60 )
287 for ( p = s; *p != '\0'; ++p ) {
288 if ( islower( *p )) {
295 static valid_tgt( names )
299 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
302 for ( i = 0; names[i] != NULL; i++ ) {
303 if ( kname_parse( name, inst, realm, names[i] ) != KSUCCESS ) {
304 fprintf( stderr, "Bad format for krbName %s\n",
306 fprintf( stderr, "Contact x500@umich.edu\n" );
312 * realm must be uppercase for krb_ routines
315 #endif /* AFSKERBEROS */
318 * check ticket file for a valid ticket granting ticket
319 * my check is: have ticket granting ticket and it is good for
320 * at least 5 more minutes
322 if ( krb_get_cred( TGT, realm, realm,
323 &cred ) == KSUCCESS && time( 0 ) + FIVEMINS <
324 cred.issue_date + (u_char)cred.lifetime * FIVEMINS ) {
332 static char *kauth_name;
336 krbgetpass( user, inst, realm, pw, key )
337 char *user, *inst, *realm, *pw;
340 char *p, lcrealm[ REALM_SZ ], prompt[256], *passwd;
343 sprintf(prompt, " Enter the UMICH password (same as Uniqname or Kerberos password)\n for %s: ", kauth_name );
345 sprintf(prompt, " Enter Kerberos password for %s: ", kauth_name );
348 passwd = mygetpass(prompt);
349 } while (passwd != NULL && *passwd == '\0');
350 if (passwd == NULL) {
355 strcpy( lcrealm, realm );
356 for ( p = lcrealm; *p != '\0'; ++p ) {
357 if ( isupper( *p )) {
362 ka_StringToKey( passwd, lcrealm, key );
363 #else /* AFSKERBEROS */
364 string_to_key( passwd, key );
365 #endif /* AFSKERBEROS */
370 static kinit( kname )
374 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
378 if ( kname_parse( name, inst, realm, kname ) != KSUCCESS ) {
379 fprintf( stderr, "Bad format for krbName %s\n",
381 fprintf( stderr, "Contact x500@umich.edu\n" );
387 * realm must be uppercase for krb_ routines
390 #endif /* AFSKERBEROS */
392 rc = krb_get_in_tkt( name, inst, realm, TGT, realm,
393 DEFAULT_TKT_LIFE, krbgetpass, NULL, NULL );
395 if ( rc != KSUCCESS ) {
398 fprintf( stderr, "Can't contact Kerberos server for %s\n", realm );
401 fprintf( stderr, "%s: %s\n", name, krb_err_txt[ rc ] );
412 if ( *tktpath != '\0' ) {
418 static void set_bound_dn(s)
422 extern char *bound_dn;
424 if (bound_dn != NULL)
426 bound_dn = strdup(s);