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.
17 #include <ac/stdlib.h>
21 #include <ac/string.h>
23 #include <ac/unistd.h>
32 #include "ldap_defaults.h"
36 static char tktpath[20]; /* ticket file path */
38 static int valid_tgt();
41 static void set_bound_dn(char *s);
45 auth( char *who, int implicit )
47 int rc; /* return code from ldap_bind() */
48 char *passwd = NULL; /* returned by getpass() */
49 char **rdns; /* for fiddling with the DN */
51 int name_provided; /* was a name passed in? */
53 struct passwd *pw; /* for getting user id */
59 char **krbnames; /* for kerberos names */
64 LDAPMessage *mp; /* returned from find() */
65 static char prompt[MED_BUF_SIZE]; /* place for us to sprintf the prompt */
66 static char name[MED_BUF_SIZE]; /* place to store the user's name */
67 static char password[MED_BUF_SIZE]; /* password entered by user */
71 fprintf(stderr, "auth(%s, NULL)\n", who);
73 name_provided = ( who != NULL );
76 * The user needs to bind. If <who> is not specified, we
77 * assume that authenticating as user id is what user wants.
79 if (who == NULL && implicit) {
83 if ((pw = getpwuid((uid_t)geteuid())) != (struct passwd *) NULL) {
84 sprintf(uidname, "uid=%s", pw->pw_name);
87 user = getenv("USER");
88 if(user == NULL) user = getenv("USERNAME");
89 if(user == NULL) user = getenv("LOGNAME");
92 sprintf(uidname, "uid=%s", user);
96 if(uidname[0] != '\0') {
103 printf( "You must first authenticate yourself to the Directory.\n" );
105 printf(" What is your name or uniqname? ");
107 printf(" What is your name or user id? ");
110 fetch_buffer(name, sizeof(name), stdin);
117 if (debug & D_AUTHENTICAT)
118 printf(" Authenticating as \"%s\"\n", who);
122 * Bail out if the name is bogus. If not, strip off the junk
123 * at the start of the DN, build a prompt, and get a password
124 * from the user. Then perform the ldap_bind().
126 if ((mp = find(who, TRUE)) == NULL) {
127 printf(" I could not find \"%s\" in the Directory.\n", who);
128 printf(" I used a search base of ");
129 printbase("", search_base);
132 if (debug & D_AUTHENTICAT)
133 printf(" Could not find \"%s\"\n", who);
139 * Fill in the Entry structure. May be handy later.
141 (void) parse_answer(mp);
143 rdns = ldap_explode_dn(Entry.DN, TRUE);
144 printf(" Authenticating to the directory as \"%s\"...\n", *rdns );
148 * First, if the user has a choice of auth methods, ask which
149 * one they want to use. if they want kerberos, ask which
150 * krbname they want to bind as.
153 if ( (krbnames = ldap_get_values( ld, mp, "krbName" )) != NULL ) {
154 int choice, hassimple;
156 hassimple = (ldap_compare_s( ld, Entry.DN,
157 "userPassword", "x" ) == LDAP_COMPARE_FALSE);
158 (void) ldap_msgfree(mp);
160 /* if we're running as a server (e.g., out of inetd) */
161 if ( ! isatty( 1 ) ) {
162 strcpy( tktpath, "/tmp/ud_tktXXXXXX" );
164 krb_set_tkt_string( tktpath );
167 kinited = valid_tgt( krbnames );
169 if ( hassimple && !kinited ) {
170 printf(" Which password would you like to use?\n");
171 printf(" 1 -> LDAP password\n");
173 printf(" 2 -> UMICH password (aka Uniqname or Kerberos password)\n");
175 printf(" 2 -> Kerberos password\n");
179 printf(" Enter 1 or 2: ");
182 fetch_buffer(buf, sizeof(buf), stdin);
184 } while (choice != 1 && choice != 2);
186 authmethod = (choice == 1 ? LDAP_AUTH_SIMPLE :
189 authmethod = LDAP_AUTH_KRBV4;
192 authmethod = LDAP_AUTH_SIMPLE;
193 (void) ldap_msgfree(mp);
197 * if they are already kinited, we don't need to ask for a
201 if ( authmethod == LDAP_AUTH_KRBV4 ) {
203 if ( krbnames[1] != NULL ) {
206 /* ask which one to use */
208 printf(" Which UMICH (aka Kerberos or uniqname) name would you like to use?\n");
210 printf(" Which Kerberos name would you like to use?\n");
212 for ( i = 0; krbnames[i] != NULL; i++ ) {
213 printf( " %d -> %s\n", i + 1,
217 printf(" Enter a number between 1 and %d: ", i );
220 fetch_buffer(buf, sizeof(buf), stdin);
221 ikrb = atoi(buf) - 1;
222 } while ( ikrb > i - 1 || ikrb < 0 );
228 if ( kinit( krbnames[ikrb] ) != 0 ) {
229 (void) ldap_value_free(rdns);
230 (void) ldap_value_free(krbnames);
236 authmethod = LDAP_AUTH_SIMPLE;
237 sprintf(prompt, " Enter your LDAP password: ");
239 passwd = getpass(prompt);
240 } while (passwd != NULL && *passwd == '\0');
241 if (passwd == NULL) {
242 (void) ldap_value_free(rdns);
247 (void) ldap_value_free(krbnames);
249 ldap_flush_cache( ld );
250 rc = ldap_bind_s(ld, Entry.DN, passwd, authmethod);
251 if (rc != LDAP_SUCCESS) {
253 ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &ld_errno);
254 if (ld_errno == LDAP_NO_SUCH_ATTRIBUTE)
255 fprintf(stderr, " Entry has no password\n");
256 else if (ld_errno == LDAP_INVALID_CREDENTIALS)
258 if ( authmethod == LDAP_AUTH_KRBV4 ) {
259 fprintf(stderr, " The Kerberos credentials are invalid.\n");
262 fprintf(stderr, " The password you provided is incorrect.\n");
267 ldap_perror(ld, "ldap_bind_s" );
268 (void) ldap_bind_s(ld, default_bind_object,
269 (char *) NULL, LDAP_AUTH_SIMPLE);
270 if (default_bind_object == NULL)
273 set_bound_dn(default_bind_object);
274 bind_status = UD_NOT_BOUND;
276 printf(" Authentication failed.\n\n");
277 (void) ldap_value_free(rdns);
281 printf(" Authentication successful.\n\n");
284 set_bound_dn(Entry.DN);
285 bind_status = UD_BOUND;
287 (void) strcpy(password, passwd);
288 (void) ldap_value_free(rdns);
294 #define FIVEMINS ( 5 * 60 )
298 valid_tgt( char **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" );
312 #ifdef HAVE_AFS_KERBEROS
314 * realm must be uppercase for krb_ routines
316 ldap_pvt_str2upper( realm );
317 #endif /* HAVE_AFS_KERBEROS */
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( char *user, char *inst, char *realm, char *pw, C_Block key )
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 = getpass(prompt);
349 } while (passwd != NULL && *passwd == '\0');
350 if (passwd == NULL) {
354 #ifdef HAVE_AFS_KERBEROS
355 strcpy( lcrealm, realm );
356 for ( p = lcrealm; *p != '\0'; ++p ) {
357 *p = TOLOWER( (unsigned char) *p );
360 ka_StringToKey( passwd, lcrealm, key );
361 #else /* HAVE_AFS_KERBEROS */
362 string_to_key( passwd, key );
363 #endif /* HAVE_AFS_KERBEROS */
372 char name[ ANAME_SZ ], inst[ INST_SZ ], realm[ REALM_SZ ];
376 if ( kname_parse( name, inst, realm, kname ) != KSUCCESS ) {
377 fprintf( stderr, "Bad format for krbName %s\n",
379 fprintf( stderr, "Contact x500@umich.edu\n" );
383 #ifdef HAVE_AFS_KERBEROS
385 * realm must be uppercase for krb_ routines
387 ldap_pvt_str2upper( realm );
388 #endif /* HAVE_AFS_KERBEROS */
390 rc = krb_get_in_tkt( name, inst, realm, TGT, realm,
391 DEFAULT_TKT_LIFE, krbgetpass, NULL, NULL );
393 if ( rc != KSUCCESS ) {
396 fprintf( stderr, "Can't contact Kerberos server for %s\n", realm );
399 fprintf( stderr, "%s: %s\n", name, krb_err_txt[ rc ] );
409 destroy_tickets( void )
411 if ( *tktpath != '\0' ) {
418 set_bound_dn( char *s )
420 if (bound_dn != NULL)
422 bound_dn = (s == NULL) ? NULL : strdup(s);