1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Sun LDAP C SDK.
*
* The Initial Developer of the Original Code is Sun Microsystems, Inc.
*
* Portions created by Sun Microsystems, Inc are Copyright (C) 2005
* Sun Microsystems, Inc. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* File for ldaptool routines for SASL
*/
#include <ldap.h>
#include "ldaptool.h"
#include "ldaptool-sasl.h"
#include <sasl/sasl.h>
#include <stdio.h>
#if defined(HPUX)
#include <sys/termios.h> /* for tcgetattr and tcsetattr */
#endif /* HPUX */
#define SASL_PROMPT "Interact"
typedef struct
{
char *mech;
char *authid;
char *username;
char *passwd;
char *realm;
} ldaptoolSASLdefaults;
static int get_default(ldaptoolSASLdefaults *defaults, sasl_interact_t *interact, unsigned flags);
static int get_new_value(sasl_interact_t *interact, unsigned flags);
/*
Note that it is important to use "" (the empty string, length 0) as the default
username value for non-interactive cases. This allows the sasl library to find the best
possible default. For example, if using GSSAPI, you want the default value for
the username to be extracted from the Kerberos tgt. The sasl library will do
that for you if you set the default username to "".
*/
void *
ldaptool_set_sasl_defaults(LDAP *ld, unsigned flags, char *mech, char *authid, char *username, char *passwd, char *realm)
{
ldaptoolSASLdefaults *defaults;
char *login = NULL;
if ((defaults = calloc(sizeof(ldaptoolSASLdefaults), 1)) == NULL) {
return NULL;
}
/* Try to get the login name */
if ((login = getlogin()) == NULL) {
login = "";
}
if (mech) {
defaults->mech = strdup(mech);
} else {
ldap_get_option(ld, LDAP_OPT_X_SASL_MECH, &defaults->mech);
}
if (authid) { /* use explicit passed in value */
defaults->authid = strdup(authid);
} else { /* use option value if any */
ldap_get_option(ld, LDAP_OPT_X_SASL_AUTHCID, &defaults->authid);
if (!defaults->authid) {
/* Default to the login name that is running the command */
defaults->authid = strdup(login);
}
}
if (username) { /* use explicit passed in value */
defaults->username = strdup(username);
} else { /* use option value if any */
ldap_get_option(ld, LDAP_OPT_X_SASL_AUTHZID, &defaults->username);
if (!defaults->username && (flags == LDAP_SASL_INTERACTIVE)) {
/* Default to the login name that is running the command */
defaults->username = strdup(login);
} else if (!defaults->username) { /* not interactive - use default sasl value */
defaults->username = strdup("");
}
}
if (passwd)
defaults->passwd = strdup(passwd);
else
defaults->passwd = strdup("");
if (realm) {
defaults->realm = realm;
} else {
ldap_get_option(ld, LDAP_OPT_X_SASL_REALM, &defaults->realm);
}
return defaults;
}
void
ldaptool_free_defaults(void *defaults)
{
ldaptoolSASLdefaults *sasl_defaults = defaults;
if (sasl_defaults) {
if (sasl_defaults->mech)
free(sasl_defaults->mech);
if (sasl_defaults->authid)
free(sasl_defaults->authid);
if (sasl_defaults->username)
free(sasl_defaults->username);
if (sasl_defaults->passwd)
free(sasl_defaults->passwd);
free(sasl_defaults);
sasl_defaults = NULL;
}
}
int
ldaptool_sasl_interact(LDAP *ld __attribute__((unused)), unsigned flags, void *defaults, void *prompts)
{
sasl_interact_t *interact = NULL;
ldaptoolSASLdefaults *sasldefaults = defaults;
int rc;
if (prompts == NULL) {
return (LDAP_PARAM_ERROR);
}
for (interact = prompts; interact->id != SASL_CB_LIST_END; interact++) {
/* Obtain the default value */
if ((rc = get_default(sasldefaults, interact, flags)) != LDAP_SUCCESS) {
return (rc);
}
/* always prompt in interactive mode - only prompt in automatic mode
if there is no default - never prompt in quiet mode */
if ((flags == LDAP_SASL_INTERACTIVE) ||
((interact->result == NULL) && (flags == LDAP_SASL_AUTOMATIC))) {
if ((rc = get_new_value(interact, flags)) != LDAP_SUCCESS)
return (rc);
}
}
return (LDAP_SUCCESS);
}
static int
get_default(ldaptoolSASLdefaults *defaults, sasl_interact_t *interact, unsigned flags __attribute__((unused)))
{
const char *defvalue = interact->defresult;
if (defaults != NULL) {
switch (interact->id) {
case SASL_CB_AUTHNAME:
defvalue = defaults->authid;
break;
case SASL_CB_USER:
defvalue = defaults->username;
break;
case SASL_CB_PASS:
defvalue = defaults->passwd;
break;
case SASL_CB_GETREALM:
defvalue = defaults->realm;
break;
}
}
if (defvalue != NULL) {
interact->result = defvalue;
if ((char *)interact->result == NULL)
return (LDAP_NO_MEMORY);
interact->len = strlen((char *)(interact->result));
}
return (LDAP_SUCCESS);
}
/*
* This function should always be called in LDAP_SASL_INTERACTIVE mode, or
* in LDAP_SASL_AUTOMATIC mode when there is no default value. This function
* will print out the challenge, default value, and prompt to get the value.
* If there is a default value, the user can just press Return/Enter at the
* prompt to use the default value. If there is no default, and the user
* didn't enter anything, this will return "" (the empty string) as the
* value.
*/
static int
get_new_value(sasl_interact_t *interact, unsigned flags __attribute__((unused)))
{
char *newvalue = NULL, str[1024];
int len = 0;
if ((interact->id == SASL_CB_ECHOPROMPT) || (interact->id == SASL_CB_NOECHOPROMPT)) {
if (interact->challenge) {
fprintf(stderr, "Challenge: %s\n", interact->challenge);
}
}
if (interact->result) {
fprintf(stderr, "Default: %s\n", (char *)interact->result);
}
snprintf(str, sizeof(str), "%s:", interact->prompt ? interact->prompt : SASL_PROMPT);
str[sizeof(str) - 1] = '\0';
/* Get the new value */
if ((interact->id == SASL_CB_PASS) || (interact->id == SASL_CB_NOECHOPROMPT)) {
if ((newvalue = ldaptool_getpass(str)) == NULL) {
return (LDAP_UNAVAILABLE);
}
len = strlen(newvalue);
} else {
fputs(str, stderr);
if ((newvalue = fgets(str, sizeof(str), stdin)) == NULL) {
return (LDAP_UNAVAILABLE);
}
len = strlen(str);
if ((len > 0) && (str[len - 1] == '\n')) {
str[len - 1] = '\0';
len--;
}
}
if (len > 0) { /* user typed in something - use it */
if (interact->result) {
free((void *)interact->result);
}
interact->result = strdup(newvalue);
memset(newvalue, '\0', len);
if (interact->result == NULL) {
return (LDAP_NO_MEMORY);
}
interact->len = len;
} else { /* use default or "" */
if (!interact->result) {
interact->result = "";
}
interact->len = strlen(interact->result);
}
return (LDAP_SUCCESS);
}
/*
* Implements getpass like functionality for supported platforms.
*
* It is the callers responsibility to zero out the memory used
* to store the password and to free it when it's finished with
* it.
*/
char *
ldaptool_getpass(const char *prompt)
{
char *pass;
#if defined(SOLARIS)
/* 256 characters on Solaris */
pass = (char *)getpassphrase(prompt);
#else
#if defined(HPUX)
/* HP-UX has deprecated their password asking function, so we have
* to resort to doing it the hard way . . . */
char pbuf[257];
struct termios termstat;
tcflag_t savestat;
/* Only perform terminal manipulation if stdin is a terminal */
int havetty = isatty(fileno(stdin));
fputs(prompt, stdout);
fflush(stdout);
if (havetty) {
if (tcgetattr(fileno(stdin), &termstat) < 0) {
perror("tcgetattr");
exit(LDAP_LOCAL_ERROR);
}
savestat = termstat.c_lflag;
termstat.c_lflag &= ~(ECHO | ECHOE | ECHOK);
termstat.c_lflag |= (ICANON | ECHONL);
if (tcsetattr(fileno(stdin), TCSANOW, &termstat) < 0) {
perror("tcsetattr");
exit(LDAP_LOCAL_ERROR);
}
}
if (fgets(pbuf, 256, stdin) == NULL) {
pass = NULL;
} else {
char *tmp;
pass = NULL;
tmp = strchr(pbuf, '\n');
if (tmp)
*tmp = '\0';
pass = strdup(pbuf);
}
if (havetty) {
termstat.c_lflag = savestat;
if (tcsetattr(fileno(stdin), TCSANOW, &termstat) < 0) {
perror("tcgetattr");
exit(LDAP_LOCAL_ERROR);
}
}
#else
/* limited to 16 chars on Tru64, 32 on AIX */
pass = (char *)getpass(prompt);
#endif
#endif
return pass;
}
|