]>
Commit | Line | Data |
---|---|---|
d03f4262 | 1 | /* |
2 | * | |
3 | * Copyright (c) 2001 Gert Doering. All rights reserved. | |
2ce0bfe4 | 4 | * Copyright (c) 2003,2004,2005 Darren Tucker. All rights reserved. |
d03f4262 | 5 | * |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * | |
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
25 | * | |
26 | */ | |
d832c38e | 27 | #include "includes.h" |
30460aeb | 28 | |
29 | #include "xmalloc.h" | |
30 | #include "buffer.h" | |
31 | #include "key.h" | |
32 | #include "hostfile.h" | |
540d72c3 | 33 | #include "auth.h" |
7cac2b65 | 34 | #include "ssh.h" |
35 | #include "log.h" | |
d832c38e | 36 | |
37 | #ifdef _AIX | |
38 | ||
30460aeb | 39 | #include <errno.h> |
40 | #if defined(HAVE_NETDB_H) | |
41 | # include <netdb.h> | |
42 | #endif | |
d832c38e | 43 | #include <uinfo.h> |
30460aeb | 44 | #include <stdarg.h> |
45 | #include <string.h> | |
46 | #include <unistd.h> | |
dfddba3d | 47 | #include <sys/socket.h> |
30460aeb | 48 | |
49 | #ifdef WITH_AIXAUTHENTICATE | |
50 | # include <login.h> | |
51 | # include <userpw.h> | |
52 | # if defined(HAVE_SYS_AUDIT_H) && defined(AIX_LOGINFAILED_4ARG) | |
8c8b185b | 53 | # undef T_NULL |
30460aeb | 54 | # include <sys/audit.h> |
55 | # endif | |
56 | # include <usersec.h> | |
57 | #endif | |
58 | ||
7cac2b65 | 59 | #include "port-aix.h" |
60 | ||
540d72c3 | 61 | # ifdef HAVE_SETAUTHDB |
62 | static char old_registry[REGISTRY_SIZE] = ""; | |
63 | # endif | |
d832c38e | 64 | |
d832c38e | 65 | /* |
2ce0bfe4 | 66 | * AIX has a "usrinfo" area where logname and other stuff is stored - |
d03f4262 | 67 | * a few applications actually use this and die if it's not set |
68 | * | |
69 | * NOTE: TTY= should be set, but since no one uses it and it's hard to | |
70 | * acquire due to privsep code. We will just drop support. | |
d832c38e | 71 | */ |
72 | void | |
d03f4262 | 73 | aix_usrinfo(struct passwd *pw) |
d832c38e | 74 | { |
75 | u_int i; | |
7cac2b65 | 76 | size_t len; |
d03f4262 | 77 | char *cp; |
d832c38e | 78 | |
7cac2b65 | 79 | len = sizeof("LOGNAME= NAME= ") + (2 * strlen(pw->pw_name)); |
80 | cp = xmalloc(len); | |
81 | ||
2ce0bfe4 | 82 | i = snprintf(cp, len, "LOGNAME=%s%cNAME=%s%c", pw->pw_name, '\0', |
7cac2b65 | 83 | pw->pw_name, '\0'); |
d832c38e | 84 | if (usrinfo(SETUINFO, cp, i) == -1) |
85 | fatal("Couldn't set usrinfo: %s", strerror(errno)); | |
86 | debug3("AIX/UsrInfo: set len %d", i); | |
7cac2b65 | 87 | |
d832c38e | 88 | xfree(cp); |
89 | } | |
90 | ||
540d72c3 | 91 | # ifdef WITH_AIXAUTHENTICATE |
7cac2b65 | 92 | /* |
93 | * Remove embedded newlines in string (if any). | |
94 | * Used before logging messages returned by AIX authentication functions | |
95 | * so the message is logged on one line. | |
96 | */ | |
97 | void | |
98 | aix_remove_embedded_newlines(char *p) | |
99 | { | |
100 | if (p == NULL) | |
101 | return; | |
102 | ||
103 | for (; *p; p++) { | |
104 | if (*p == '\n') | |
105 | *p = ' '; | |
106 | } | |
107 | /* Remove trailing whitespace */ | |
108 | if (*--p == ' ') | |
109 | *p = '\0'; | |
110 | } | |
540d72c3 | 111 | |
dfddba3d | 112 | /* |
113 | * Test specifically for the case where SYSTEM == NONE and AUTH1 contains | |
114 | * anything other than NONE or SYSTEM, which indicates that the admin has | |
115 | * configured the account for purely AUTH1-type authentication. | |
116 | * | |
117 | * Since authenticate() doesn't check AUTH1, and sshd can't sanely support | |
118 | * AUTH1 itself, in such a case authenticate() will allow access without | |
119 | * authentation, which is almost certainly not what the admin intends. | |
120 | * | |
121 | * (The native tools, eg login, will process the AUTH1 list in addition to | |
122 | * the SYSTEM list by using ckuserID(), however ckuserID() and AUTH1 methods | |
123 | * have been deprecated since AIX 4.2.x and would be very difficult for sshd | |
124 | * to support. | |
125 | * | |
126 | * Returns 0 if an unsupportable combination is found, 1 otherwise. | |
127 | */ | |
128 | static int | |
129 | aix_valid_authentications(const char *user) | |
130 | { | |
131 | char *auth1, *sys, *p; | |
132 | int valid = 1; | |
133 | ||
134 | if (getuserattr((char *)user, S_AUTHSYSTEM, &sys, SEC_CHAR) != 0) { | |
135 | logit("Can't retrieve attribute SYSTEM for %s: %.100s", | |
136 | user, strerror(errno)); | |
137 | return 0; | |
138 | } | |
139 | ||
140 | debug3("AIX SYSTEM attribute %s", sys); | |
141 | if (strcmp(sys, "NONE") != 0) | |
142 | return 1; /* not "NONE", so is OK */ | |
143 | ||
144 | if (getuserattr((char *)user, S_AUTH1, &auth1, SEC_LIST) != 0) { | |
145 | logit("Can't retrieve attribute auth1 for %s: %.100s", | |
146 | user, strerror(errno)); | |
147 | return 0; | |
148 | } | |
149 | ||
150 | p = auth1; | |
151 | /* A SEC_LIST is concatenated strings, ending with two NULs. */ | |
152 | while (p[0] != '\0' && p[1] != '\0') { | |
153 | debug3("AIX auth1 attribute list member %s", p); | |
154 | if (strcmp(p, "NONE") != 0 && strcmp(p, "SYSTEM")) { | |
155 | logit("Account %s has unsupported auth1 value '%s'", | |
156 | user, p); | |
157 | valid = 0; | |
158 | } | |
159 | p += strlen(p) + 1; | |
160 | } | |
161 | ||
162 | return (valid); | |
163 | } | |
164 | ||
540d72c3 | 165 | /* |
166 | * Do authentication via AIX's authenticate routine. We loop until the | |
167 | * reenter parameter is 0, but normally authenticate is called only once. | |
168 | * | |
169 | * Note: this function returns 1 on success, whereas AIX's authenticate() | |
170 | * returns 0. | |
171 | */ | |
172 | int | |
8b32eddc | 173 | sys_auth_passwd(Authctxt *ctxt, const char *password) |
540d72c3 | 174 | { |
2ce0bfe4 | 175 | char *authmsg = NULL, *msg = NULL, *name = ctxt->pw->pw_name; |
540d72c3 | 176 | int authsuccess = 0, expired, reenter, result; |
177 | ||
178 | do { | |
179 | result = authenticate((char *)name, (char *)password, &reenter, | |
180 | &authmsg); | |
181 | aix_remove_embedded_newlines(authmsg); | |
2ce0bfe4 | 182 | debug3("AIX/authenticate result %d, authmsg %.100s", result, |
540d72c3 | 183 | authmsg); |
184 | } while (reenter); | |
185 | ||
dfddba3d | 186 | if (!aix_valid_authentications(name)) |
187 | result = -1; | |
188 | ||
540d72c3 | 189 | if (result == 0) { |
190 | authsuccess = 1; | |
191 | ||
2ce0bfe4 | 192 | /* |
540d72c3 | 193 | * Record successful login. We don't have a pty yet, so just |
194 | * label the line as "ssh" | |
195 | */ | |
196 | aix_setauthdb(name); | |
540d72c3 | 197 | |
198 | /* | |
199 | * Check if the user's password is expired. | |
200 | */ | |
7e82606e | 201 | expired = passwdexpired(name, &msg); |
202 | if (msg && *msg) { | |
8b32eddc | 203 | buffer_append(ctxt->loginmsg, msg, strlen(msg)); |
7e82606e | 204 | aix_remove_embedded_newlines(msg); |
205 | } | |
206 | debug3("AIX/passwdexpired returned %d msg %.100s", expired, msg); | |
540d72c3 | 207 | |
208 | switch (expired) { | |
209 | case 0: /* password not expired */ | |
210 | break; | |
211 | case 1: /* expired, password change required */ | |
212 | ctxt->force_pwchange = 1; | |
540d72c3 | 213 | break; |
214 | default: /* user can't change(2) or other error (-1) */ | |
215 | logit("Password can't be changed for user %s: %.100s", | |
216 | name, msg); | |
217 | if (msg) | |
218 | xfree(msg); | |
219 | authsuccess = 0; | |
220 | } | |
221 | ||
222 | aix_restoreauthdb(); | |
223 | } | |
224 | ||
225 | if (authmsg != NULL) | |
226 | xfree(authmsg); | |
227 | ||
228 | return authsuccess; | |
229 | } | |
7e82606e | 230 | |
231 | /* | |
232 | * Check if specified account is permitted to log in. | |
233 | * Returns 1 if login is allowed, 0 if not allowed. | |
234 | */ | |
235 | int | |
dfddba3d | 236 | sys_auth_allowed_user(struct passwd *pw, Buffer *loginmsg) |
7e82606e | 237 | { |
238 | char *msg = NULL; | |
239 | int result, permitted = 0; | |
240 | struct stat st; | |
241 | ||
242 | /* | |
243 | * Don't perform checks for root account (PermitRootLogin controls | |
244 | * logins via * ssh) or if running as non-root user (since | |
245 | * loginrestrictions will always fail due to insufficient privilege). | |
246 | */ | |
247 | if (pw->pw_uid == 0 || geteuid() != 0) { | |
248 | debug3("%s: not checking", __func__); | |
249 | return 1; | |
250 | } | |
251 | ||
252 | result = loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &msg); | |
253 | if (result == 0) | |
254 | permitted = 1; | |
255 | /* | |
256 | * If restricted because /etc/nologin exists, the login will be denied | |
257 | * in session.c after the nologin message is sent, so allow for now | |
258 | * and do not append the returned message. | |
259 | */ | |
260 | if (result == -1 && errno == EPERM && stat(_PATH_NOLOGIN, &st) == 0) | |
261 | permitted = 1; | |
262 | else if (msg != NULL) | |
dfddba3d | 263 | buffer_append(loginmsg, msg, strlen(msg)); |
7e82606e | 264 | if (msg == NULL) |
265 | msg = xstrdup("(none)"); | |
266 | aix_remove_embedded_newlines(msg); | |
267 | debug3("AIX/loginrestrictions returned %d msg %.100s", result, msg); | |
268 | ||
269 | if (!permitted) | |
270 | logit("Login restricted for %s: %.100s", pw->pw_name, msg); | |
271 | xfree(msg); | |
272 | return permitted; | |
273 | } | |
274 | ||
275 | int | |
dfddba3d | 276 | sys_auth_record_login(const char *user, const char *host, const char *ttynm, |
277 | Buffer *loginmsg) | |
7e82606e | 278 | { |
2ce0bfe4 | 279 | char *msg = NULL; |
30460aeb | 280 | static int msg_done = 0; |
7e82606e | 281 | int success = 0; |
282 | ||
283 | aix_setauthdb(user); | |
dfddba3d | 284 | if (loginsuccess((char *)user, (char *)host, (char *)ttynm, &msg) == 0) { |
7e82606e | 285 | success = 1; |
30460aeb | 286 | if (msg != NULL && loginmsg != NULL && !msg_done) { |
dfddba3d | 287 | debug("AIX/loginsuccess: msg %s", msg); |
288 | buffer_append(loginmsg, msg, strlen(msg)); | |
7e82606e | 289 | xfree(msg); |
30460aeb | 290 | msg_done = 1; |
7e82606e | 291 | } |
292 | } | |
293 | aix_restoreauthdb(); | |
294 | return (success); | |
295 | } | |
296 | ||
540d72c3 | 297 | # ifdef CUSTOM_FAILED_LOGIN |
7cac2b65 | 298 | /* |
299 | * record_failed_login: generic "login failed" interface function | |
300 | */ | |
301 | void | |
dfddba3d | 302 | record_failed_login(const char *user, const char *hostname, const char *ttyname) |
7cac2b65 | 303 | { |
7cac2b65 | 304 | if (geteuid() != 0) |
305 | return; | |
306 | ||
307 | aix_setauthdb(user); | |
540d72c3 | 308 | # ifdef AIX_LOGINFAILED_4ARG |
dfddba3d | 309 | loginfailed((char *)user, (char *)hostname, (char *)ttyname, |
310 | AUDIT_FAIL_AUTH); | |
540d72c3 | 311 | # else |
dfddba3d | 312 | loginfailed((char *)user, (char *)hostname, (char *)ttyname); |
540d72c3 | 313 | # endif |
314 | aix_restoreauthdb(); | |
7cac2b65 | 315 | } |
540d72c3 | 316 | # endif /* CUSTOM_FAILED_LOGIN */ |
7cac2b65 | 317 | |
318 | /* | |
319 | * If we have setauthdb, retrieve the password registry for the user's | |
540d72c3 | 320 | * account then feed it to setauthdb. This will mean that subsequent AIX auth |
321 | * functions will only use the specified loadable module. If we don't have | |
322 | * setauthdb this is a no-op. | |
7cac2b65 | 323 | */ |
324 | void | |
325 | aix_setauthdb(const char *user) | |
326 | { | |
327 | # ifdef HAVE_SETAUTHDB | |
540d72c3 | 328 | char *registry; |
7cac2b65 | 329 | |
330 | if (setuserdb(S_READ) == -1) { | |
331 | debug3("%s: Could not open userdb to read", __func__); | |
332 | return; | |
333 | } | |
334 | ||
335 | if (getuserattr((char *)user, S_REGISTRY, ®istry, SEC_CHAR) == 0) { | |
540d72c3 | 336 | if (setauthdb(registry, old_registry) == 0) |
337 | debug3("AIX/setauthdb set registry '%s'", registry); | |
7cac2b65 | 338 | else |
540d72c3 | 339 | debug3("AIX/setauthdb set registry '%s' failed: %s", |
340 | registry, strerror(errno)); | |
7cac2b65 | 341 | } else |
342 | debug3("%s: Could not read S_REGISTRY for user: %s", __func__, | |
343 | strerror(errno)); | |
344 | enduserdb(); | |
540d72c3 | 345 | # endif /* HAVE_SETAUTHDB */ |
7cac2b65 | 346 | } |
d832c38e | 347 | |
540d72c3 | 348 | /* |
349 | * Restore the user's registry settings from old_registry. | |
350 | * Note that if the first aix_setauthdb fails, setauthdb("") is still safe | |
351 | * (it restores the system default behaviour). If we don't have setauthdb, | |
352 | * this is a no-op. | |
353 | */ | |
354 | void | |
355 | aix_restoreauthdb(void) | |
356 | { | |
357 | # ifdef HAVE_SETAUTHDB | |
358 | if (setauthdb(old_registry, NULL) == 0) | |
359 | debug3("%s: restoring old registry '%s'", __func__, | |
360 | old_registry); | |
361 | else | |
362 | debug3("%s: failed to restore old registry %s", __func__, | |
363 | old_registry); | |
364 | # endif /* HAVE_SETAUTHDB */ | |
365 | } | |
366 | ||
367 | # endif /* WITH_AIXAUTHENTICATE */ | |
368 | ||
dfddba3d | 369 | # if defined(AIX_GETNAMEINFO_HACK) && !defined(BROKEN_ADDRINFO) |
370 | # undef getnameinfo | |
371 | /* | |
372 | * For some reason, AIX's getnameinfo will refuse to resolve the all-zeros | |
373 | * IPv6 address into its textual representation ("::"), so we wrap it | |
374 | * with a function that will. | |
375 | */ | |
376 | int | |
377 | sshaix_getnameinfo(const struct sockaddr *sa, size_t salen, char *host, | |
378 | size_t hostlen, char *serv, size_t servlen, int flags) | |
379 | { | |
380 | struct sockaddr_in6 *sa6; | |
381 | u_int32_t *a6; | |
382 | ||
383 | if (flags & (NI_NUMERICHOST|NI_NUMERICSERV) && | |
384 | sa->sa_family == AF_INET6) { | |
385 | sa6 = (struct sockaddr_in6 *)sa; | |
386 | a6 = sa6->sin6_addr.u6_addr.u6_addr32; | |
387 | ||
388 | if (a6[0] == 0 && a6[1] == 0 && a6[2] == 0 && a6[3] == 0) { | |
389 | strlcpy(host, "::", hostlen); | |
390 | snprintf(serv, servlen, "%d", sa6->sin6_port); | |
391 | return 0; | |
392 | } | |
393 | } | |
394 | return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags); | |
395 | } | |
396 | # endif /* AIX_GETNAMEINFO_HACK */ | |
397 | ||
540d72c3 | 398 | #endif /* _AIX */ |