2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
7 * File for ldaptool routines for SASL
11 #include "ldaptool-sasl.h"
12 #ifdef SOLARIS_LDAP_CMD
13 #include <sasl/sasl.h>
18 #endif /* SOLARIS_LDAP_CMD */
21 #ifndef SOLARIS_LDAP_CMD
25 #ifdef HAVE_SASL_OPTIONS
27 #define SASL_PROMPT "SASL"
35 } ldaptoolSASLdefaults
;
37 static int get_default(ldaptoolSASLdefaults
*defaults
, sasl_interact_t
*interact
);
38 static int get_new_value(sasl_interact_t
*interact
, unsigned flags
);
41 ldaptool_set_sasl_defaults ( LDAP
*ld
, char *mech
, char *authid
, char *username
,
42 char *passwd
, char *realm
)
44 ldaptoolSASLdefaults
*defaults
;
46 if ((defaults
= calloc(sizeof(defaults
[0]), 1)) == NULL
)
50 defaults
->mech
= mech
;
52 ldap_get_option(ld
, LDAP_OPT_X_SASL_MECH
, &defaults
->mech
);
55 defaults
->authid
= authid
;
57 ldap_get_option(ld
, LDAP_OPT_X_SASL_AUTHCID
, &defaults
->authid
);
60 defaults
->username
= username
;
62 ldap_get_option(ld
, LDAP_OPT_X_SASL_AUTHZID
, &defaults
->username
);
64 defaults
->passwd
= passwd
;
67 defaults
->realm
= realm
;
69 ldap_get_option(ld
, LDAP_OPT_X_SASL_REALM
, &defaults
->realm
);
75 ldaptool_sasl_interact( LDAP
*ld
, unsigned flags
, void *defaults
, void *prompts
) {
76 sasl_interact_t
*interact
;
77 ldaptoolSASLdefaults
*sasldefaults
= defaults
;
80 if (prompts
== NULL
|| flags
!= LDAP_SASL_INTERACTIVE
)
81 return (LDAP_PARAM_ERROR
);
83 for (interact
= prompts
; interact
->id
!= SASL_CB_LIST_END
; interact
++) {
84 /* Obtain the default value */
85 if ((rc
= get_default(sasldefaults
, interact
)) != LDAP_SUCCESS
)
88 /* If no default, get the new value from stdin */
89 if (interact
->result
== NULL
) {
90 if ((rc
= get_new_value(interact
, flags
)) != LDAP_SUCCESS
)
95 return (LDAP_SUCCESS
);
99 get_default(ldaptoolSASLdefaults
*defaults
, sasl_interact_t
*interact
) {
100 const char *defvalue
= interact
->defresult
;
102 if (defaults
!= NULL
) {
103 switch( interact
->id
) {
104 case SASL_CB_AUTHNAME
:
105 defvalue
= defaults
->authid
;
108 defvalue
= defaults
->username
;
111 defvalue
= defaults
->passwd
;
113 case SASL_CB_GETREALM
:
114 defvalue
= defaults
->realm
;
119 if (defvalue
!= NULL
) {
120 interact
->result
= (char *)malloc(strlen(defvalue
)+1);
121 if ((char *)interact
->result
!= NULL
) {
122 strcpy((char *)interact
->result
,defvalue
);
123 interact
->len
= strlen((char *)(interact
->result
));
127 if (interact
->id
== SASL_CB_PASS
&& defaults
!= NULL
) {
128 /* At this point defaults->passwd is not NULL */
129 memset( defaults
->passwd
, '\0', strlen(defaults
->passwd
));
132 if ((char *)interact
->result
== NULL
) {
133 return (LDAP_NO_MEMORY
);
136 return (LDAP_SUCCESS
);
140 get_new_value(sasl_interact_t
*interact
, unsigned flags
) {
141 char *newvalue
, str
[1024];
144 #ifdef SOLARIS_LDAP_CMD
148 if (interact
->id
== SASL_CB_ECHOPROMPT
|| interact
->id
== SASL_CB_NOECHOPROMPT
) {
149 if (interact
->challenge
)
150 fprintf(stderr
, gettext("Challenge:%s\n"), interact
->challenge
);
153 #ifdef SOLARIS_LDAP_CMD
154 tmpstr
= ldaptool_UTF82local(interact
->prompt
);
155 snprintf(str
, sizeof(str
), "%s:", tmpstr
?tmpstr
:SASL_PROMPT
);
160 snprintf(str
, sizeof(str
), "%s:", interact
->prompt
?interact
->prompt
:SASL_PROMPT
);
162 sprintf(str
, "%s:", interact
->prompt
?interact
->prompt
:SASL_PROMPT
);
164 #endif /* SOLARIS_LDAP_CMD */
166 /* Get the new value */
167 if (interact
->id
== SASL_CB_PASS
|| interact
->id
== SASL_CB_NOECHOPROMPT
) {
172 if (fgets(pbuf
,256,stdin
) == NULL
) {
177 tmp
= strchr(pbuf
,'\n');
178 if (tmp
) *tmp
= '\0';
179 tmp
= strchr(pbuf
,'\r');
180 if (tmp
) *tmp
= '\0';
181 newvalue
= strdup(pbuf
);
183 if ( newvalue
== NULL
) {
186 if ((newvalue
= (char *)getpassphrase(str
)) == NULL
) {
188 if ((newvalue
= (char *)getpass(str
)) == NULL
) {
191 return (LDAP_UNAVAILABLE
);
193 len
= strlen(newvalue
);
196 if ((newvalue
= fgets(str
, sizeof(str
), stdin
)) == NULL
)
197 return (LDAP_UNAVAILABLE
);
199 if (len
> 0 && str
[len
- 1] == '\n')
203 interact
->result
= (char *) strdup(newvalue
);
204 memset(newvalue
, '\0', len
);
205 if (interact
->result
== NULL
)
206 return (LDAP_NO_MEMORY
);
208 return (LDAP_SUCCESS
);
210 #endif /* HAVE_SASL_OPTIONS */