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 && dflt ) {
108 if( flags == LDAP_SASL_QUIET ) {
114 if( interact->challenge ) {
115 fprintf( stderr, "Challenge: %s\n", interact->challenge );
120 fprintf( stderr, "Default: %s\n", dflt );
123 sprintf( input, "%s: ",
124 interact->prompt ? interact->prompt : "Interact" );
127 interact->result = (char *) getpassphrase( input );
128 interact->len = interact->result
129 ? strlen( interact->result ) : 0;
133 fputs( input, stderr );
136 interact->result = fgets( input, sizeof(input), stdin );
138 if( interact->result == NULL ) {
140 return LDAP_UNAVAILABLE;
144 interact->len = strlen(input);
146 if( interact->len > 0 && input[interact->len - 1] == '\n' ) {
147 /* input includes '\n', trim it */
149 input[interact->len] = '\0';
154 if( interact->len > 0 ) {
156 char *p = interact->result;
157 interact->result = strdup( p );
160 memset( p, '\0', interact->len );
165 interact->result = strdup( (dflt && *dflt) ? dflt : "" );
166 interact->len = interact->result
167 ? strlen( interact->result ) : 0;
170 if( defaults && defaults->passwd && interact->id == SASL_CB_PASS ) {
171 /* zap password after first use */
172 memset( defaults->passwd, '\0', strlen(defaults->passwd) );
173 defaults->passwd = NULL;
179 int lutil_sasl_interact(
185 sasl_interact_t *interact = in;
187 if( flags != LDAP_SASL_QUIET ) {
188 fputs( "SASL Interaction\n", stderr );
191 while( interact->id != SASL_CB_LIST_END ) {
192 int rc = interaction( flags, interact, defaults );