3 * Copyright 2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/stdlib.h>
13 #include <ac/string.h>
14 #include <ac/unistd.h>
19 #include "lutil_ldap.h"
22 typedef struct lutil_sasl_defaults_s {
40 lutilSASLdefaults *defaults;
42 defaults = ber_memalloc( sizeof( lutilSASLdefaults ) );
44 if( defaults == NULL ) return NULL;
46 defaults->mech = mech;
47 defaults->realm = realm;
48 defaults->authcid = authcid;
49 defaults->passwd = passwd;
50 defaults->authzid = authzid;
52 if( defaults->mech == NULL ) {
53 ldap_get_option( ld, LDAP_OPT_X_SASL_MECH, &defaults->mech );
55 if( defaults->realm == NULL ) {
56 ldap_get_option( ld, LDAP_OPT_X_SASL_REALM, &defaults->realm );
58 if( defaults->authcid == NULL ) {
59 ldap_get_option( ld, LDAP_OPT_X_SASL_AUTHCID, &defaults->authcid );
61 if( defaults->authzid == NULL ) {
62 ldap_get_option( ld, LDAP_OPT_X_SASL_AUTHZID, &defaults->authzid );
68 static int interaction(
70 sasl_interact_t *interact,
71 lutilSASLdefaults *defaults )
73 const char *dflt = interact->defresult;
79 switch( interact->id ) {
80 case SASL_CB_GETREALM:
81 if( defaults ) dflt = defaults->realm;
83 case SASL_CB_AUTHNAME:
84 if( defaults ) dflt = defaults->authcid;
87 if( defaults ) dflt = defaults->passwd;
91 if( defaults ) dflt = defaults->authzid;
93 case SASL_CB_NOECHOPROMPT:
97 case SASL_CB_ECHOPROMPT:
102 if( dflt && !*dflt ) dflt = NULL;
104 if( flags != LDAP_SASL_INTERACTIVE &&
105 ( dflt || interact->id == SASL_CB_USER ) )
110 if( flags == LDAP_SASL_QUIET ) {
116 if( interact->challenge ) {
117 fprintf( stderr, "Challenge: %s\n", interact->challenge );
122 fprintf( stderr, "Default: %s\n", dflt );
125 sprintf( input, "%s: ",
126 interact->prompt ? interact->prompt : "Interact" );
129 interact->result = (char *) getpassphrase( input );
130 interact->len = interact->result
131 ? strlen( interact->result ) : 0;
135 fputs( input, stderr );
138 interact->result = fgets( input, sizeof(input), stdin );
140 if( interact->result == NULL ) {
142 return LDAP_UNAVAILABLE;
146 interact->len = strlen(input);
148 if( interact->len > 0 && input[interact->len - 1] == '\n' ) {
149 /* input includes '\n', trim it */
151 input[interact->len] = '\0';
156 if( interact->len > 0 ) {
158 char *p = interact->result;
159 interact->result = strdup( p );
162 memset( p, '\0', interact->len );
166 /* input must be empty */
167 interact->result = strdup( (dflt && *dflt) ? dflt : "" );
168 interact->len = interact->result
169 ? strlen( interact->result ) : 0;
172 if( defaults && defaults->passwd && interact->id == SASL_CB_PASS ) {
173 /* zap password after first use */
174 memset( defaults->passwd, '\0', strlen(defaults->passwd) );
175 defaults->passwd = NULL;
181 int lutil_sasl_interact(
187 sasl_interact_t *interact = in;
189 if( flags == LDAP_SASL_INTERACTIVE ) {
190 fputs( "SASL Interaction\n", stderr );
193 while( interact->id != SASL_CB_LIST_END ) {
194 int rc = interaction( flags, interact, defaults );