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>
22 #include <ac/unistd.h>
30 #include <ldapconfig.h>
35 static char tktpath[20]; /* ticket file path */
37 static int valid_tgt();
40 static void set_bound_dn(char *s);
44 auth( char *who, int implicit )
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? */
52 struct passwd *pw; /* for getting user id */
58 char **krbnames; /* for kerberos names */
63 LDAPMessage *mp; /* returned from find() */
64 static char prompt[MED_BUF_SIZE]; /* place for us to sprintf the prompt */
65 static char name[MED_BUF_SIZE]; /* place to store the user's name */
66 static char password[MED_BUF_SIZE]; /* password entered by user */
70 fprintf(stderr, "auth(%s, NULL)\n", who);
72 name_provided = ( who != NULL );
75 * The user needs to bind. If <who> is not specified, we
76 * assume that authenticating as user id is what user wants.
78 if (who == NULL && implicit) {
82 if ((pw = getpwuid((uid_t)geteuid())) != (struct passwd *) NULL) {
83 sprintf(uidname, "uid=%s", pw->pw_name);
86 user = getenv("USER");
87 if(user == NULL) user = getenv("USERNAME");
88 if(user == NULL) user = getenv("LOGNAME");
91 sprintf(uidname, "uid=%s", user);
95 if(uidname[0] != '\0') {
102 printf( "You must first authenticate yourself to the Directory.\n" );
104 printf(" What is your name or uniqname? ");
106 printf(" What is your name or user id? ");
109 fetch_buffer(name, sizeof(name), stdin);
116 if (debug & D_AUTHENTICAT)
117 printf(" Authenticating as \"%s\"\n", who);
121 * Bail out if the name is bogus. If not, strip off the junk
122 * at the start of the DN, build a prompt, and get a password
123 * from the user. Then perform the ldap_bind().
125 if ((mp = find(who, TRUE)) == NULL) {
126 printf(" I could not find \"%s\" in the Directory.\n", who);
127 printf(" I used a search base of ");
128 printbase("", search_base);
131 if (debug & D_AUTHENTICAT)
132 printf(" Could not find \"%s\"\n", who);
138 * Fill in the Entry structure. May be handy later.
140 (void) parse_answer(mp);
142 rdns = ldap_explode_dn(Entry.DN, TRUE);
143 printf(" Authenticating to the directory as \"%s\"...\n", *rdns );
147 * First, if the user has a choice of auth methods, ask which
148 * one they want to use. if they want kerberos, ask which
149 * krbname they want to bind as.
152 if ( (krbnames = ldap_get_values( ld, mp, "krbName" )) != NULL ) {
153 int choice, hassimple;
155 hassimple = (ldap_compare_s( ld, Entry.DN,
156 "userPassword", "x" ) == LDAP_COMPARE_FALSE);
157 (void) ldap_msgfree(mp);
159 /* if we're running as a server (e.g., out of inetd) */
160 if ( ! isatty( 1 ) ) {
161 strcpy( tktpath, "/tmp/ud_tktXXXXXX" );
163 krb_set_tkt_string( tktpath );
166 kinited = valid_tgt( krbnames );
168 if ( hassimple && !kinited ) {
169 printf(" Which password would you like to use?\n");
170 printf(" 1 -> LDAP password\n");
172 printf(" 2 -> UMICH password (aka Uniqname or Kerberos password)\n");
174 printf(" 2 -> Kerberos password\n");
178 printf(" Enter 1 or 2: ");
181 fetch_buffer(buf, sizeof(buf), stdin);
183 } while (choice != 1 && choice != 2);
185 authmethod = (choice == 1 ? LDAP_AUTH_SIMPLE :
188 authmethod = LDAP_AUTH_KRBV4;
191 authmethod = LDAP_AUTH_SIMPLE;
192 (void) ldap_msgfree(mp);
196 * if they are already kinited, we don't need to ask for a
200 if ( authmethod == LDAP_AUTH_KRBV4 ) {
202 if ( krbnames[1] != NULL ) {
205 /* ask which one to use */
207 printf(" Which UMICH (aka Kerberos or uniqname) name would you like to use?\n");
209 printf(" Which Kerberos name would you like to use?\n");
211 for ( i = 0; krbnames[i] != NULL; i++ ) {
212 printf( " %d -> %s\n", i + 1,
216 printf(" Enter a number between 1 and %d: ", i );
219 fetch_buffer(buf, sizeof(buf), stdin);
220 ikrb = atoi(buf) - 1;
221 } while ( ikrb > i - 1 || ikrb < 0 );
227 if ( kinit( krbnames[ikrb] ) != 0 ) {
228 (void) ldap_value_free(rdns);
229 (void) ldap_value_free(krbnames);
235 authmethod = LDAP_AUTH_SIMPLE;
236 sprintf(prompt, " Enter your LDAP password: ");
238 passwd = mygetpass(prompt);
239 } while (passwd != NULL && *passwd == '\0');
240 if (passwd == NULL) {
241 (void) ldap_value_free(rdns);
246 (void) ldap_value_free(krbnames);
248 ldap_flush_cache( ld );
249 rc = ldap_bind_s(ld, Entry.DN, passwd, authmethod);
250 if (rc != LDAP_SUCCESS) {
252 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
253 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
254 fprintf(stderr, " Entry has no password\n");
255 else if (ld_errno == LDAP_INVALID_CREDENTIALS)
257 if ( authmethod == LDAP_AUTH_KRBV4 ) {
258 fprintf(stderr, " The Kerberos credentials are invalid.\n");
261 fprintf(stderr, " The password you provided is incorrect.\n");
266 ldap_perror(ld, "ldap_bind_s" );
267 (void) ldap_bind_s(ld, default_bind_object,
268 (char *) NULL, LDAP_AUTH_SIMPLE);
269 if (default_bind_object == NULL)
272 set_bound_dn(default_bind_object);
273 bind_status = UD_NOT_BOUND;
275 printf(" Authentication failed.\n\n");
276 (void) ldap_value_free(rdns);
280 printf(" Authentication successful.\n\n");
283 set_bound_dn(Entry.DN);
284 bind_status = UD_BOUND;
286 (void) strcpy(password, passwd);
287 (void) ldap_value_free(rdns);
293 #define FIVEMINS ( 5 * 60 )
301 for ( p = s; *p != '\0'; ++p ) {
302 *p = TOUPPER( (unsigned char) *p );
308 valid_tgt( char **names )
311 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
314 for ( i = 0; names[i] != NULL; i++ ) {
315 if ( kname_parse( name, inst, realm, names[i] ) != KSUCCESS ) {
316 fprintf( stderr, "Bad format for krbName %s\n",
318 fprintf( stderr, "Contact x500@umich.edu\n" );
322 #ifdef HAVE_AFS_KERBEROS
324 * realm must be uppercase for krb_ routines
327 #endif /* HAVE_AFS_KERBEROS */
330 * check ticket file for a valid ticket granting ticket
331 * my check is: have ticket granting ticket and it is good for
332 * at least 5 more minutes
334 if ( krb_get_cred( TGT, realm, realm,
335 &cred ) == KSUCCESS && time( 0 ) + FIVEMINS <
336 cred.issue_date + (u_char)cred.lifetime * FIVEMINS ) {
344 static char *kauth_name;
348 krbgetpass( char *user, char *inst, char *realm, char *pw, C_Block key )
350 char *p, lcrealm[ REALM_SZ ], prompt[256], *passwd;
353 sprintf(prompt, " Enter the UMICH password (same as Uniqname or Kerberos password)\n for %s: ", kauth_name );
355 sprintf(prompt, " Enter Kerberos password for %s: ", kauth_name );
358 passwd = mygetpass(prompt);
359 } while (passwd != NULL && *passwd == '\0');
360 if (passwd == NULL) {
364 #ifdef HAVE_AFS_KERBEROS
365 strcpy( lcrealm, realm );
366 for ( p = lcrealm; *p != '\0'; ++p ) {
367 *p = TOLOWER( (unsigned char) *p );
370 ka_StringToKey( passwd, lcrealm, key );
371 #else /* HAVE_AFS_KERBEROS */
372 string_to_key( passwd, key );
373 #endif /* HAVE_AFS_KERBEROS */
382 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
386 if ( kname_parse( name, inst, realm, kname ) != KSUCCESS ) {
387 fprintf( stderr, "Bad format for krbName %s\n",
389 fprintf( stderr, "Contact x500@umich.edu\n" );
393 #ifdef HAVE_AFS_KERBEROS
395 * realm must be uppercase for krb_ routines
398 #endif /* HAVE_AFS_KERBEROS */
400 rc = krb_get_in_tkt( name, inst, realm, TGT, realm,
401 DEFAULT_TKT_LIFE, krbgetpass, NULL, NULL );
403 if ( rc != KSUCCESS ) {
406 fprintf( stderr, "Can't contact Kerberos server for %s\n", realm );
409 fprintf( stderr, "%s: %s\n", name, krb_err_txt[ rc ] );
419 destroy_tickets( void )
421 if ( *tktpath != '\0' ) {
428 set_bound_dn( char *s )
430 if (bound_dn != NULL)
432 bound_dn = (s == NULL) ? NULL : strdup(s);