|
Line 0
Link Here
|
|
|
1 |
/*- |
| 2 |
* Copyright (c) 2009 Quest Software, Inc. All rights reserved. |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "includes.h" |
| 18 |
#ifdef WITH_AIXAUTHENTICATE |
| 19 |
#include <sys/audit.h> |
| 20 |
#include <usersec.h> |
| 21 |
#include <pwd.h> |
| 22 |
#include "auth.h" |
| 23 |
#include "buffer.h" |
| 24 |
#include "bufaux.h" |
| 25 |
#include "canohost.h" |
| 26 |
#include "log.h" |
| 27 |
#include "monitor_wrap.h" |
| 28 |
#include "msg.h" |
| 29 |
#include "packet.h" |
| 30 |
#include "misc.h" |
| 31 |
#include "servconf.h" |
| 32 |
#include "ssh2.h" |
| 33 |
#include "uidswap.h" |
| 34 |
#include "xmalloc.h" |
| 35 |
#include "auth-options.h" |
| 36 |
#include "openbsd-compat/port-aix.h" |
| 37 |
|
| 38 |
extern ServerOptions options; |
| 39 |
extern Buffer loginmsg; |
| 40 |
|
| 41 |
struct lam_ctxt { |
| 42 |
char * user; |
| 43 |
char * message; |
| 44 |
int reenter; |
| 45 |
int faking_noent; |
| 46 |
}; |
| 47 |
struct Authctxt *sshlam_authctxt; |
| 48 |
|
| 49 |
static void * sshlam_init_ctx(Authctxt *authctxt); |
| 50 |
static int sshlam_query(void *ctx, char **name, char **info, |
| 51 |
u_int *num, char ***prompts, u_int **echo_on); |
| 52 |
static int sshlam_respond(void *ctx, u_int num, char **resp); |
| 53 |
static void sshlam_free_ctx(void *ctxtp); |
| 54 |
static void sshlam_force_pwchange(int reqd); |
| 55 |
|
| 56 |
KbdintDevice sshlam_device = { |
| 57 |
"lam", |
| 58 |
sshlam_init_ctx, |
| 59 |
sshlam_query, |
| 60 |
sshlam_respond, |
| 61 |
sshlam_free_ctx |
| 62 |
}; |
| 63 |
|
| 64 |
KbdintDevice mm_sshlam_device = { |
| 65 |
"lam", |
| 66 |
mm_sshlam_init_ctx, |
| 67 |
mm_sshlam_query, |
| 68 |
mm_sshlam_respond, |
| 69 |
mm_sshlam_free_ctx |
| 70 |
}; |
| 71 |
|
| 72 |
/* |
| 73 |
* Allocates and initialises storage for LAM authentication state. |
| 74 |
* Returns NULL if LAM authentication is not available. |
| 75 |
*/ |
| 76 |
static void * |
| 77 |
sshlam_init_ctx(Authctxt *authctxt) |
| 78 |
{ |
| 79 |
struct lam_ctxt *ctxt; |
| 80 |
|
| 81 |
debug3("LAM: sshlam_init_ctx entering"); |
| 82 |
|
| 83 |
ctxt = xmalloc(sizeof *ctxt); |
| 84 |
memset(ctxt, 0, sizeof *ctxt); |
| 85 |
|
| 86 |
ctxt->user = xstrdup(authctxt->user); |
| 87 |
ctxt->message = NULL; |
| 88 |
ctxt->faking_noent = 0; |
| 89 |
ctxt->reenter = 0; |
| 90 |
sshlam_authctxt = authctxt; |
| 91 |
|
| 92 |
debug3("LAM: AUTHSTATE=%s", getenv("AUTHSTATE") ? |
| 93 |
getenv("AUTHSTATE") : "NULL"); |
| 94 |
|
| 95 |
/* aix_setauthdb(ctxt->user); */ |
| 96 |
if (authenticate(ctxt->user, NULL, &ctxt->reenter, &ctxt->message)) |
| 97 |
{ |
| 98 |
error("LAM: authenticate(%.100s) failed: %.100s", ctxt->user, |
| 99 |
strerror(errno)); |
| 100 |
if (errno == ENOENT) { |
| 101 |
char buf[200]; |
| 102 |
/* |
| 103 |
* When a user is known not to exist, we pretend |
| 104 |
* that they do, and fake a password prompt |
| 105 |
*/ |
| 106 |
logit("LAM: pretending that user '%.100s' exists", ctxt->user); |
| 107 |
ctxt->faking_noent = 1; |
| 108 |
snprintf(buf, sizeof buf, "%.100s's Password:", ctxt->user); |
| 109 |
ctxt->message = xstrdup(buf); |
| 110 |
ctxt->reenter = 1; |
| 111 |
} else { |
| 112 |
goto fail; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/* Instant authentication should never happen here; |
| 117 |
* 'none' authentication should have permitted it */ |
| 118 |
if (!ctxt->reenter) { |
| 119 |
error("LAM: %.100s authenticated immediately; ignoring", |
| 120 |
ctxt->user); |
| 121 |
goto fail; |
| 122 |
} |
| 123 |
|
| 124 |
return (ctxt); |
| 125 |
|
| 126 |
fail: |
| 127 |
sshlam_free_ctx(ctxt); |
| 128 |
return (NULL); |
| 129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
* Releases storage associated with the context |
| 133 |
* returned from sshlam_init_ctx |
| 134 |
*/ |
| 135 |
static void |
| 136 |
sshlam_free_ctx(void *ctxtp) |
| 137 |
{ |
| 138 |
struct lam_ctxt *ctxt = ctxtp; |
| 139 |
|
| 140 |
debug3("LAM: sshlam_free_ctx entering"); |
| 141 |
|
| 142 |
/* aix_restoreauthdb(); */ |
| 143 |
if (ctxt->message) |
| 144 |
xfree(ctxt->message); |
| 145 |
xfree(ctxt->user); |
| 146 |
xfree(ctxt); |
| 147 |
} |
| 148 |
|
| 149 |
/* |
| 150 |
* Returns the user prompt for the current round of |
| 151 |
* AIX LAM authentication. |
| 152 |
*/ |
| 153 |
static int |
| 154 |
sshlam_query(void *ctx, char **name, char **info, |
| 155 |
u_int *num, char ***prompts, u_int **echo_on) |
| 156 |
{ |
| 157 |
struct lam_ctxt *ctxt = ctx; |
| 158 |
|
| 159 |
debug3("LAM: sshlam_query entering"); |
| 160 |
|
| 161 |
*name = xstrdup(""); |
| 162 |
*info = xstrdup(""); |
| 163 |
*prompts = (char **)xmalloc(sizeof(char *)); |
| 164 |
*echo_on = (u_int *)xmalloc(sizeof(u_int)); |
| 165 |
if (ctxt->message) { |
| 166 |
**prompts = xstrdup(ctxt->message); |
| 167 |
**echo_on = 0; |
| 168 |
*num = 1; |
| 169 |
} else |
| 170 |
*num = 0; |
| 171 |
|
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
/* |
| 176 |
* Processes a user response for an AIX LAM authentication round. |
| 177 |
* Returns |
| 178 |
* 1 if another round must be performed |
| 179 |
* 0 if authentication completed with success |
| 180 |
* -1 if authentication failed |
| 181 |
*/ |
| 182 |
static int |
| 183 |
sshlam_respond(void *ctx, u_int num, char **resp) |
| 184 |
{ |
| 185 |
struct lam_ctxt *ctxt = ctx; |
| 186 |
int e; |
| 187 |
char *message = NULL; |
| 188 |
struct passwd *pwuser = NULL; |
| 189 |
char badpw[] = "\b\n\r\177INCORRECT"; /* XXX: Duplicated from auth-pam.c */ |
| 190 |
char *authpw = NULL; |
| 191 |
|
| 192 |
debug2("LAM: sshlam_respond entering, %u responses", num); |
| 193 |
|
| 194 |
if (num != 1) { |
| 195 |
error("LAM: expected one response, got %u", num); |
| 196 |
return (-1); |
| 197 |
} |
| 198 |
|
| 199 |
if (ctxt->faking_noent) |
| 200 |
return (-1); |
| 201 |
|
| 202 |
pwuser = getpwnam(ctxt->user); |
| 203 |
if (!pwuser) { |
| 204 |
error("LAM: could not resolve user %.100s", ctxt->user); |
| 205 |
return (-1); |
| 206 |
} |
| 207 |
|
| 208 |
/* Have LAM process a junk password to avoid timing-based disclosure. */ |
| 209 |
if (pwuser->pw_uid == 0 && options.permit_root_login != PERMIT_YES) |
| 210 |
authpw = badpw; |
| 211 |
else |
| 212 |
authpw = *resp; |
| 213 |
|
| 214 |
/* Perform an authentication round */ |
| 215 |
e = authenticate(ctxt->user, authpw, &ctxt->reenter, &message); |
| 216 |
if (e) { |
| 217 |
if (message) |
| 218 |
xfree(message); |
| 219 |
error("LAM: authenticate %s: %d %.100s", ctxt->user, e, |
| 220 |
strerror(errno)); |
| 221 |
return (-1); |
| 222 |
} |
| 223 |
|
| 224 |
if (pwuser->pw_uid == 0 && options.permit_root_login != PERMIT_YES) { |
| 225 |
error("LAM: denying root access"); |
| 226 |
return (-1); |
| 227 |
} |
| 228 |
|
| 229 |
if (ctxt->reenter) { |
| 230 |
if (message == NULL) { |
| 231 |
error("LAM: authenticate %s returned NULL message", ctxt->user); |
| 232 |
return (-1); |
| 233 |
} |
| 234 |
ctxt->message = message; |
| 235 |
return 1; |
| 236 |
} |
| 237 |
|
| 238 |
/* Handle extra messages by adding them to the login banner */ |
| 239 |
if (message) { |
| 240 |
debug3("LAM: authenticate: %.100s", message); |
| 241 |
buffer_append(&loginmsg, message, strlen(message)); |
| 242 |
xfree(message); |
| 243 |
message = NULL; |
| 244 |
} |
| 245 |
|
| 246 |
/* Detect expired passwords */ |
| 247 |
e = passwdexpired(ctxt->user, &message); |
| 248 |
if (e == -1) { |
| 249 |
error("LAM: passwdexpired %s: %.100s", ctxt->user, strerror(errno)); |
| 250 |
return (-1); |
| 251 |
} |
| 252 |
if (message) { |
| 253 |
debug3("LAM: passwdexpired: %.100s", message); |
| 254 |
buffer_append(&loginmsg, message, strlen(message)); |
| 255 |
xfree(message); |
| 256 |
message = NULL; |
| 257 |
} |
| 258 |
switch (e) { |
| 259 |
case 0: |
| 260 |
break; |
| 261 |
case 1: |
| 262 |
logit("LAM: password for %s expired", ctxt->user); |
| 263 |
sshlam_force_pwchange(1); |
| 264 |
break; |
| 265 |
case 2: |
| 266 |
error("LAM: password for %s expired and unchangeable", ctxt->user); |
| 267 |
return (-1); |
| 268 |
default: |
| 269 |
error("LAM: passwdexpired returned %d", e); |
| 270 |
return (-1); |
| 271 |
} |
| 272 |
|
| 273 |
return 0; |
| 274 |
} |
| 275 |
|
| 276 |
static void |
| 277 |
sshlam_force_pwchange(int reqd) |
| 278 |
{ |
| 279 |
sshlam_authctxt->force_pwchange = reqd; |
| 280 |
} |
| 281 |
|
| 282 |
#endif /* WITH_AIXAUTHENTICATE */ |
| 283 |
|