sasl.c 4.83 KB
Newer Older
1
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
2
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 1998-2021 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
6
7
8
9
10
11
12
13
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
14
15
16
17
18
19
20
21
22
23
 */

#include "portable.h"

#ifdef HAVE_CYRUS_SASL

#include <stdio.h>
#include <ac/stdlib.h>
#include <ac/string.h>
#include <ac/unistd.h>
24
#include <ac/socket.h>
25

Howard Chu's avatar
Howard Chu committed
26
27
28
#ifdef HAVE_SASL_SASL_H
#include <sasl/sasl.h>
#else
29
#include <sasl.h>
Howard Chu's avatar
Howard Chu committed
30
#endif
31
32

#include <ldap.h>
33
#include "ldap_pvt.h"
34
35
#include "lutil_ldap.h"

36
37
38
39
40
41
42

typedef struct lutil_sasl_defaults_s {
	char *mech;
	char *realm;
	char *authcid;
	char *passwd;
	char *authzid;
43
44
	char **resps;
	int nresps;
45
46
47
} lutilSASLdefaults;


48
49
50
51
52
void
lutil_sasl_freedefs(
	void *defaults )
{
	lutilSASLdefaults *defs = defaults;
53
54

	assert( defs != NULL );
55
56
57
58
59
60
61
62
63
64
65
	
	if (defs->mech) ber_memfree(defs->mech);
	if (defs->realm) ber_memfree(defs->realm);
	if (defs->authcid) ber_memfree(defs->authcid);
	if (defs->passwd) ber_memfree(defs->passwd);
	if (defs->authzid) ber_memfree(defs->authzid);
	if (defs->resps) ldap_charray_free(defs->resps);

	ber_memfree(defs);
}

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
void *
lutil_sasl_defaults(
	LDAP *ld,
	char *mech,
	char *realm,
	char *authcid,
	char *passwd,
	char *authzid )
{
	lutilSASLdefaults *defaults;
	
	defaults = ber_memalloc( sizeof( lutilSASLdefaults ) );

	if( defaults == NULL ) return NULL;

81
82
83
84
85
	defaults->mech = mech ? ber_strdup(mech) : NULL;
	defaults->realm = realm ? ber_strdup(realm) : NULL;
	defaults->authcid = authcid ? ber_strdup(authcid) : NULL;
	defaults->passwd = passwd ? ber_strdup(passwd) : NULL;
	defaults->authzid = authzid ? ber_strdup(authzid) : NULL;
86
87
88
89
90
91
92
93
94
95
96
97
98

	if( defaults->mech == NULL ) {
		ldap_get_option( ld, LDAP_OPT_X_SASL_MECH, &defaults->mech );
	}
	if( defaults->realm == NULL ) {
		ldap_get_option( ld, LDAP_OPT_X_SASL_REALM, &defaults->realm );
	}
	if( defaults->authcid == NULL ) {
		ldap_get_option( ld, LDAP_OPT_X_SASL_AUTHCID, &defaults->authcid );
	}
	if( defaults->authzid == NULL ) {
		ldap_get_option( ld, LDAP_OPT_X_SASL_AUTHZID, &defaults->authzid );
	}
99
100
	defaults->resps = NULL;
	defaults->nresps = 0;
101
102
103
104

	return defaults;
}

105
static int interaction(
106
107
108
	unsigned flags,
	sasl_interact_t *interact,
	lutilSASLdefaults *defaults )
109
{
110
	const char *dflt = interact->defresult;
111
112
113
114
115
116
	char input[1024];

	int noecho=0;
	int challenge=0;

	switch( interact->id ) {
117
118
119
120
121
122
123
124
125
126
127
128
129
	case SASL_CB_GETREALM:
		if( defaults ) dflt = defaults->realm;
		break;
	case SASL_CB_AUTHNAME:
		if( defaults ) dflt = defaults->authcid;
		break;
	case SASL_CB_PASS:
		if( defaults ) dflt = defaults->passwd;
		noecho = 1;
		break;
	case SASL_CB_USER:
		if( defaults ) dflt = defaults->authzid;
		break;
130
131
132
133
134
135
136
	case SASL_CB_NOECHOPROMPT:
		noecho = 1;
		challenge = 1;
		break;
	case SASL_CB_ECHOPROMPT:
		challenge = 1;
		break;
137
138
139
140
	}

	if( dflt && !*dflt ) dflt = NULL;

141
142
143
	if( flags != LDAP_SASL_INTERACTIVE &&
		( dflt || interact->id == SASL_CB_USER ) )
	{
144
145
146
		goto use_default;
	}

147
	if( flags == LDAP_SASL_QUIET ) {
148
149
		/* don't prompt */
		return LDAP_OTHER;
150
151
152
153
	}

	if( challenge ) {
		if( interact->challenge ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
154
			fprintf( stderr, _("Challenge: %s\n"), interact->challenge );
155
		}
156
157
158
	}

	if( dflt ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
159
		fprintf( stderr, _("Default: %s\n"), dflt );
160
161
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
162
	snprintf( input, sizeof input, "%s: ",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
163
		interact->prompt ? interact->prompt : _("Interact") );
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

	if( noecho ) {
		interact->result = (char *) getpassphrase( input );
		interact->len = interact->result
			? strlen( interact->result ) : 0;

	} else {
		/* prompt user */
		fputs( input, stderr );

		/* get input */
		interact->result = fgets( input, sizeof(input), stdin );

		if( interact->result == NULL ) {
			interact->len = 0;
			return LDAP_UNAVAILABLE;
		}

		/* len of input */
		interact->len = strlen(input); 

		if( interact->len > 0 && input[interact->len - 1] == '\n' ) {
			/* input includes '\n', trim it */
			interact->len--;
			input[interact->len] = '\0';
		}
	}


	if( interact->len > 0 ) {
		/* duplicate */
195
		char *p = (char *)interact->result;
196
197
		ldap_charray_add(&defaults->resps, interact->result);
		interact->result = defaults->resps[defaults->nresps++];
198
199
200
201
202

		/* zap */
		memset( p, '\0', interact->len );

	} else {
203
use_default:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
204
		/* input must be empty */
205
206
		interact->result = (dflt && *dflt) ? dflt : "";
		interact->len = strlen( interact->result );
207
208
209
210
211
212
213
	}

	return LDAP_SUCCESS;
}

int lutil_sasl_interact(
	LDAP *ld,
214
	unsigned flags,
215
	void *defaults,
216
217
218
219
	void *in )
{
	sasl_interact_t *interact = in;

220
	if( flags == LDAP_SASL_INTERACTIVE ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
221
		fputs( _("SASL Interaction\n"), stderr );
222
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
223

224
	while( interact->id != SASL_CB_LIST_END ) {
225
		int rc = interaction( flags, interact, defaults );
226
227
228
229
230
231
232
233

		if( rc )  return rc;
		interact++;
	}
	
	return LDAP_SUCCESS;
}
#endif