blob: 13a73e873cebbf27a2d42c457f83277c1f89d7a9 [file] [log] [blame]
Ben Lindstrom51b24882002-07-04 03:08:40 +00001/*
Ben Lindstrom024f08f2002-07-07 02:17:36 +00002 *
3 * Copyright (c) 2001 Gert Doering. All rights reserved.
Darren Tucker782727a2005-05-29 10:28:48 +10004 * Copyright (c) 2003,2004,2005 Darren Tucker. All rights reserved.
Ben Lindstrom024f08f2002-07-07 02:17:36 +00005 *
Ben Lindstrom51b24882002-07-04 03:08:40 +00006 * 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 */
Ben Lindstroma9c039c2002-02-19 20:27:55 +000027#include "includes.h"
Damien Millerd7834352006-08-05 12:39:39 +100028
29#include "xmalloc.h"
30#include "buffer.h"
31#include "key.h"
32#include "hostfile.h"
Darren Tuckercfea2062004-02-10 15:27:34 +110033#include "auth.h"
Darren Tucker97363a82003-05-02 23:42:25 +100034#include "ssh.h"
35#include "log.h"
Ben Lindstroma9c039c2002-02-19 20:27:55 +000036
37#ifdef _AIX
38
Darren Tucker2eaea992006-07-12 23:41:33 +100039#include <errno.h>
Damien Millerbe43ebf2006-07-24 13:51:51 +100040#if defined(HAVE_NETDB_H)
41# include <netdb.h>
42#endif
Ben Lindstroma9c039c2002-02-19 20:27:55 +000043#include <uinfo.h>
Damien Miller62da44f2006-07-24 15:08:35 +100044#include <string.h>
45#include <unistd.h>
Darren Tucker691d5232005-02-15 21:45:57 +110046#include <sys/socket.h>
Darren Tuckerfc3454e2003-07-14 16:41:55 +100047#include "port-aix.h"
Ben Lindstroma9c039c2002-02-19 20:27:55 +000048
Darren Tuckere45674a2004-02-06 16:17:51 +110049# ifdef HAVE_SETAUTHDB
50static char old_registry[REGISTRY_SIZE] = "";
51# endif
52
Ben Lindstroma9c039c2002-02-19 20:27:55 +000053/*
Darren Tucker6b2fe312005-05-29 10:32:47 +100054 * AIX has a "usrinfo" area where logname and other stuff is stored -
Ben Lindstrom51b24882002-07-04 03:08:40 +000055 * a few applications actually use this and die if it's not set
56 *
57 * NOTE: TTY= should be set, but since no one uses it and it's hard to
58 * acquire due to privsep code. We will just drop support.
Ben Lindstroma9c039c2002-02-19 20:27:55 +000059 */
60void
Ben Lindstrom51b24882002-07-04 03:08:40 +000061aix_usrinfo(struct passwd *pw)
Ben Lindstroma9c039c2002-02-19 20:27:55 +000062{
Ben Lindstroma9c039c2002-02-19 20:27:55 +000063 u_int i;
Damien Miller31741252003-05-19 00:13:38 +100064 size_t len;
Ben Lindstrom51b24882002-07-04 03:08:40 +000065 char *cp;
Ben Lindstroma9c039c2002-02-19 20:27:55 +000066
Damien Miller31741252003-05-19 00:13:38 +100067 len = sizeof("LOGNAME= NAME= ") + (2 * strlen(pw->pw_name));
68 cp = xmalloc(len);
69
Darren Tucker6b2fe312005-05-29 10:32:47 +100070 i = snprintf(cp, len, "LOGNAME=%s%cNAME=%s%c", pw->pw_name, '\0',
Damien Miller3867bf32003-05-19 09:33:15 +100071 pw->pw_name, '\0');
Ben Lindstroma9c039c2002-02-19 20:27:55 +000072 if (usrinfo(SETUINFO, cp, i) == -1)
73 fatal("Couldn't set usrinfo: %s", strerror(errno));
74 debug3("AIX/UsrInfo: set len %d", i);
Damien Miller31741252003-05-19 00:13:38 +100075
Ben Lindstroma9c039c2002-02-19 20:27:55 +000076 xfree(cp);
77}
78
Darren Tuckerd7634162003-11-22 14:16:56 +110079# ifdef WITH_AIXAUTHENTICATE
Darren Tuckerb9aa0a02003-07-08 22:59:59 +100080/*
81 * Remove embedded newlines in string (if any).
82 * Used before logging messages returned by AIX authentication functions
83 * so the message is logged on one line.
84 */
85void
86aix_remove_embedded_newlines(char *p)
87{
88 if (p == NULL)
89 return;
90
91 for (; *p; p++) {
92 if (*p == '\n')
93 *p = ' ';
94 }
95 /* Remove trailing whitespace */
96 if (*--p == ' ')
97 *p = '\0';
98}
Darren Tuckerd7634162003-11-22 14:16:56 +110099
100/*
Darren Tucker5a88d002004-08-29 21:43:33 +1000101 * Test specifically for the case where SYSTEM == NONE and AUTH1 contains
102 * anything other than NONE or SYSTEM, which indicates that the admin has
103 * configured the account for purely AUTH1-type authentication.
104 *
105 * Since authenticate() doesn't check AUTH1, and sshd can't sanely support
106 * AUTH1 itself, in such a case authenticate() will allow access without
107 * authentation, which is almost certainly not what the admin intends.
108 *
109 * (The native tools, eg login, will process the AUTH1 list in addition to
110 * the SYSTEM list by using ckuserID(), however ckuserID() and AUTH1 methods
111 * have been deprecated since AIX 4.2.x and would be very difficult for sshd
112 * to support.
113 *
114 * Returns 0 if an unsupportable combination is found, 1 otherwise.
115 */
116static int
117aix_valid_authentications(const char *user)
118{
119 char *auth1, *sys, *p;
120 int valid = 1;
121
122 if (getuserattr((char *)user, S_AUTHSYSTEM, &sys, SEC_CHAR) != 0) {
123 logit("Can't retrieve attribute SYSTEM for %s: %.100s",
124 user, strerror(errno));
125 return 0;
126 }
127
128 debug3("AIX SYSTEM attribute %s", sys);
129 if (strcmp(sys, "NONE") != 0)
130 return 1; /* not "NONE", so is OK */
131
132 if (getuserattr((char *)user, S_AUTH1, &auth1, SEC_LIST) != 0) {
133 logit("Can't retrieve attribute auth1 for %s: %.100s",
134 user, strerror(errno));
135 return 0;
136 }
137
138 p = auth1;
139 /* A SEC_LIST is concatenated strings, ending with two NULs. */
140 while (p[0] != '\0' && p[1] != '\0') {
141 debug3("AIX auth1 attribute list member %s", p);
142 if (strcmp(p, "NONE") != 0 && strcmp(p, "SYSTEM")) {
143 logit("Account %s has unsupported auth1 value '%s'",
144 user, p);
145 valid = 0;
146 }
147 p += strlen(p) + 1;
148 }
149
150 return (valid);
151}
152
153/*
Darren Tuckerd7634162003-11-22 14:16:56 +1100154 * Do authentication via AIX's authenticate routine. We loop until the
155 * reenter parameter is 0, but normally authenticate is called only once.
156 *
157 * Note: this function returns 1 on success, whereas AIX's authenticate()
158 * returns 0.
159 */
160int
Darren Tuckerf3bb4342005-03-31 21:39:25 +1000161sys_auth_passwd(Authctxt *ctxt, const char *password)
Darren Tuckerd7634162003-11-22 14:16:56 +1100162{
Darren Tucker782727a2005-05-29 10:28:48 +1000163 char *authmsg = NULL, *msg = NULL, *name = ctxt->pw->pw_name;
Darren Tuckere3dba822004-02-10 12:50:19 +1100164 int authsuccess = 0, expired, reenter, result;
Darren Tuckerd7634162003-11-22 14:16:56 +1100165
166 do {
167 result = authenticate((char *)name, (char *)password, &reenter,
168 &authmsg);
169 aix_remove_embedded_newlines(authmsg);
Darren Tuckerf9fea652005-05-29 10:54:27 +1000170 debug3("AIX/authenticate result %d, authmsg %.100s", result,
Darren Tuckerd7634162003-11-22 14:16:56 +1100171 authmsg);
172 } while (reenter);
173
Darren Tucker5a88d002004-08-29 21:43:33 +1000174 if (!aix_valid_authentications(name))
175 result = -1;
176
Darren Tuckerd7634162003-11-22 14:16:56 +1100177 if (result == 0) {
178 authsuccess = 1;
179
Darren Tucker6b2fe312005-05-29 10:32:47 +1000180 /*
Darren Tuckere3dba822004-02-10 12:50:19 +1100181 * Record successful login. We don't have a pty yet, so just
182 * label the line as "ssh"
183 */
Darren Tuckerd7634162003-11-22 14:16:56 +1100184 aix_setauthdb(name);
Darren Tuckere3dba822004-02-10 12:50:19 +1100185
186 /*
187 * Check if the user's password is expired.
188 */
Darren Tuckera7ea5462004-06-16 12:01:15 +1000189 expired = passwdexpired(name, &msg);
190 if (msg && *msg) {
Darren Tuckerf3bb4342005-03-31 21:39:25 +1000191 buffer_append(ctxt->loginmsg, msg, strlen(msg));
Darren Tuckera7ea5462004-06-16 12:01:15 +1000192 aix_remove_embedded_newlines(msg);
193 }
194 debug3("AIX/passwdexpired returned %d msg %.100s", expired, msg);
Darren Tuckere3dba822004-02-10 12:50:19 +1100195
196 switch (expired) {
197 case 0: /* password not expired */
198 break;
199 case 1: /* expired, password change required */
200 ctxt->force_pwchange = 1;
Darren Tuckere3dba822004-02-10 12:50:19 +1100201 break;
202 default: /* user can't change(2) or other error (-1) */
203 logit("Password can't be changed for user %s: %.100s",
204 name, msg);
205 if (msg)
206 xfree(msg);
207 authsuccess = 0;
208 }
209
Darren Tuckere45674a2004-02-06 16:17:51 +1100210 aix_restoreauthdb();
Darren Tuckerd7634162003-11-22 14:16:56 +1100211 }
212
213 if (authmsg != NULL)
214 xfree(authmsg);
215
216 return authsuccess;
217}
Darren Tucker0a9d43d2004-06-23 13:45:24 +1000218
219/*
220 * Check if specified account is permitted to log in.
221 * Returns 1 if login is allowed, 0 if not allowed.
222 */
223int
Darren Tucker691d5232005-02-15 21:45:57 +1100224sys_auth_allowed_user(struct passwd *pw, Buffer *loginmsg)
Darren Tucker0a9d43d2004-06-23 13:45:24 +1000225{
226 char *msg = NULL;
227 int result, permitted = 0;
228 struct stat st;
229
230 /*
231 * Don't perform checks for root account (PermitRootLogin controls
232 * logins via * ssh) or if running as non-root user (since
233 * loginrestrictions will always fail due to insufficient privilege).
234 */
235 if (pw->pw_uid == 0 || geteuid() != 0) {
Darren Tucker5288cb22004-06-28 18:11:19 +1000236 debug3("%s: not checking", __func__);
Darren Tucker0a9d43d2004-06-23 13:45:24 +1000237 return 1;
238 }
239
240 result = loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &msg);
241 if (result == 0)
242 permitted = 1;
243 /*
244 * If restricted because /etc/nologin exists, the login will be denied
245 * in session.c after the nologin message is sent, so allow for now
246 * and do not append the returned message.
247 */
248 if (result == -1 && errno == EPERM && stat(_PATH_NOLOGIN, &st) == 0)
249 permitted = 1;
250 else if (msg != NULL)
Darren Tucker691d5232005-02-15 21:45:57 +1100251 buffer_append(loginmsg, msg, strlen(msg));
Darren Tucker0a9d43d2004-06-23 13:45:24 +1000252 if (msg == NULL)
253 msg = xstrdup("(none)");
254 aix_remove_embedded_newlines(msg);
255 debug3("AIX/loginrestrictions returned %d msg %.100s", result, msg);
256
257 if (!permitted)
258 logit("Login restricted for %s: %.100s", pw->pw_name, msg);
259 xfree(msg);
260 return permitted;
261}
262
Darren Tucker397a2f22004-08-15 00:09:11 +1000263int
Darren Tucker691d5232005-02-15 21:45:57 +1100264sys_auth_record_login(const char *user, const char *host, const char *ttynm,
265 Buffer *loginmsg)
Darren Tucker397a2f22004-08-15 00:09:11 +1000266{
Darren Tucker782727a2005-05-29 10:28:48 +1000267 char *msg = NULL;
Darren Tucker26d4e192006-08-30 22:33:09 +1000268 static int msg_done = 0;
Darren Tucker397a2f22004-08-15 00:09:11 +1000269 int success = 0;
270
271 aix_setauthdb(user);
Darren Tuckerb4d30122005-02-08 21:06:55 +1100272 if (loginsuccess((char *)user, (char *)host, (char *)ttynm, &msg) == 0) {
Darren Tucker397a2f22004-08-15 00:09:11 +1000273 success = 1;
Darren Tucker26d4e192006-08-30 22:33:09 +1000274 if (msg != NULL && loginmsg != NULL && !msg_done) {
Darren Tuckerb4d30122005-02-08 21:06:55 +1100275 debug("AIX/loginsuccess: msg %s", msg);
Darren Tucker691d5232005-02-15 21:45:57 +1100276 buffer_append(loginmsg, msg, strlen(msg));
Darren Tucker397a2f22004-08-15 00:09:11 +1000277 xfree(msg);
Darren Tucker26d4e192006-08-30 22:33:09 +1000278 msg_done = 1;
Darren Tucker397a2f22004-08-15 00:09:11 +1000279 }
280 }
281 aix_restoreauthdb();
282 return (success);
283}
284
Darren Tuckerd7634162003-11-22 14:16:56 +1100285# ifdef CUSTOM_FAILED_LOGIN
Darren Tucker97363a82003-05-02 23:42:25 +1000286/*
287 * record_failed_login: generic "login failed" interface function
288 */
289void
Darren Tucker42d9dc72005-02-02 17:10:11 +1100290record_failed_login(const char *user, const char *hostname, const char *ttyname)
Darren Tucker97363a82003-05-02 23:42:25 +1000291{
Darren Tuckerfc3454e2003-07-14 16:41:55 +1000292 if (geteuid() != 0)
293 return;
294
295 aix_setauthdb(user);
Darren Tuckerd7634162003-11-22 14:16:56 +1100296# ifdef AIX_LOGINFAILED_4ARG
Darren Tuckerb4d30122005-02-08 21:06:55 +1100297 loginfailed((char *)user, (char *)hostname, (char *)ttyname,
298 AUDIT_FAIL_AUTH);
Darren Tuckerd7634162003-11-22 14:16:56 +1100299# else
Darren Tuckerb4d30122005-02-08 21:06:55 +1100300 loginfailed((char *)user, (char *)hostname, (char *)ttyname);
Darren Tuckerd7634162003-11-22 14:16:56 +1100301# endif
Darren Tuckere45674a2004-02-06 16:17:51 +1100302 aix_restoreauthdb();
Darren Tucker97363a82003-05-02 23:42:25 +1000303}
Darren Tuckerd7634162003-11-22 14:16:56 +1100304# endif /* CUSTOM_FAILED_LOGIN */
Darren Tuckerfc3454e2003-07-14 16:41:55 +1000305
306/*
307 * If we have setauthdb, retrieve the password registry for the user's
Darren Tuckere45674a2004-02-06 16:17:51 +1100308 * account then feed it to setauthdb. This will mean that subsequent AIX auth
309 * functions will only use the specified loadable module. If we don't have
310 * setauthdb this is a no-op.
Darren Tuckerfc3454e2003-07-14 16:41:55 +1000311 */
312void
313aix_setauthdb(const char *user)
314{
315# ifdef HAVE_SETAUTHDB
Darren Tuckere45674a2004-02-06 16:17:51 +1100316 char *registry;
Darren Tuckerfc3454e2003-07-14 16:41:55 +1000317
318 if (setuserdb(S_READ) == -1) {
319 debug3("%s: Could not open userdb to read", __func__);
320 return;
321 }
322
323 if (getuserattr((char *)user, S_REGISTRY, &registry, SEC_CHAR) == 0) {
Darren Tuckere45674a2004-02-06 16:17:51 +1100324 if (setauthdb(registry, old_registry) == 0)
325 debug3("AIX/setauthdb set registry '%s'", registry);
Darren Tuckerfc3454e2003-07-14 16:41:55 +1000326 else
Darren Tuckere45674a2004-02-06 16:17:51 +1100327 debug3("AIX/setauthdb set registry '%s' failed: %s",
328 registry, strerror(errno));
Darren Tuckerfc3454e2003-07-14 16:41:55 +1000329 } else
330 debug3("%s: Could not read S_REGISTRY for user: %s", __func__,
331 strerror(errno));
332 enduserdb();
Darren Tuckerd7634162003-11-22 14:16:56 +1100333# endif /* HAVE_SETAUTHDB */
Darren Tuckerfc3454e2003-07-14 16:41:55 +1000334}
Ben Lindstroma9c039c2002-02-19 20:27:55 +0000335
Darren Tuckere45674a2004-02-06 16:17:51 +1100336/*
337 * Restore the user's registry settings from old_registry.
338 * Note that if the first aix_setauthdb fails, setauthdb("") is still safe
339 * (it restores the system default behaviour). If we don't have setauthdb,
340 * this is a no-op.
341 */
342void
343aix_restoreauthdb(void)
344{
345# ifdef HAVE_SETAUTHDB
346 if (setauthdb(old_registry, NULL) == 0)
347 debug3("%s: restoring old registry '%s'", __func__,
348 old_registry);
349 else
350 debug3("%s: failed to restore old registry %s", __func__,
351 old_registry);
352# endif /* HAVE_SETAUTHDB */
353}
354
Darren Tuckerd7634162003-11-22 14:16:56 +1100355# endif /* WITH_AIXAUTHENTICATE */
356
Darren Tucker691d5232005-02-15 21:45:57 +1100357# if defined(AIX_GETNAMEINFO_HACK) && !defined(BROKEN_ADDRINFO)
358# undef getnameinfo
359/*
360 * For some reason, AIX's getnameinfo will refuse to resolve the all-zeros
361 * IPv6 address into its textual representation ("::"), so we wrap it
362 * with a function that will.
363 */
364int
365sshaix_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
366 size_t hostlen, char *serv, size_t servlen, int flags)
367{
368 struct sockaddr_in6 *sa6;
369 u_int32_t *a6;
370
371 if (flags & (NI_NUMERICHOST|NI_NUMERICSERV) &&
372 sa->sa_family == AF_INET6) {
373 sa6 = (struct sockaddr_in6 *)sa;
374 a6 = sa6->sin6_addr.u6_addr.u6_addr32;
375
376 if (a6[0] == 0 && a6[1] == 0 && a6[2] == 0 && a6[3] == 0) {
377 strlcpy(host, "::", hostlen);
378 snprintf(serv, servlen, "%d", sa6->sin6_port);
379 return 0;
380 }
381 }
382 return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
383}
384# endif /* AIX_GETNAMEINFO_HACK */
385
Darren Tuckerd7634162003-11-22 14:16:56 +1100386#endif /* _AIX */