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 {
42 lutilSASLdefaults *defaults;
44 defaults = ber_memalloc( sizeof( lutilSASLdefaults ) );
46 if( defaults == NULL ) return NULL;
48 defaults->flags = flags;
49 defaults->mech = mech;
50 defaults->realm = realm;
51 defaults->authcid = authcid;
52 defaults->passwd = passwd;
53 defaults->authzid = authzid;
55 if( defaults->mech == NULL ) {
56 ldap_get_option( ld, LDAP_OPT_X_SASL_MECH, &defaults->mech );
58 if( defaults->realm == NULL ) {
59 ldap_get_option( ld, LDAP_OPT_X_SASL_REALM, &defaults->realm );
61 if( defaults->authcid == NULL ) {
62 ldap_get_option( ld, LDAP_OPT_X_SASL_AUTHCID, &defaults->authcid );
64 if( defaults->authzid == NULL ) {
65 ldap_get_option( ld, LDAP_OPT_X_SASL_AUTHZID, &defaults->authzid );
71 static int interaction(
72 sasl_interact_t *interact, lutilSASLdefaults *defaults )
74 unsigned flags = defaults ? defaults->flags : 0;
75 const char *dflt = interact->defresult;
81 switch( interact->id ) {
82 case SASL_CB_GETREALM:
83 if( defaults ) dflt = defaults->realm;
85 case SASL_CB_AUTHNAME:
86 if( defaults ) dflt = defaults->authcid;
89 if( defaults ) dflt = defaults->passwd;
93 if( defaults ) dflt = defaults->authzid;
95 case SASL_CB_NOECHOPROMPT:
99 case SASL_CB_ECHOPROMPT:
104 if( dflt && !*dflt ) dflt = NULL;
106 if( flags != LUTIL_SASL_INTERACTIVE && dflt ) {
110 if( flags == LUTIL_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 );
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(
186 sasl_interact_t *interact = in;
188 fputs( "SASL Interaction\n", stderr );
190 while( interact->id != SASL_CB_LIST_END ) {
191 int rc = interaction( interact, defaults );