3 * Copyright (c) 2001 Gert Doering. All rights reserved.
4 * Copyright (c) 2003,2004,2005 Darren Tucker. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
40 #if defined(HAVE_NETDB_H)
47 #include <sys/socket.h>
49 #ifdef WITH_AIXAUTHENTICATE
52 # if defined(HAVE_SYS_AUDIT_H) && defined(AIX_LOGINFAILED_4ARG)
54 # include <sys/audit.h>
61 # ifdef HAVE_SETAUTHDB
62 static char old_registry[REGISTRY_SIZE] = "";
66 * AIX has a "usrinfo" area where logname and other stuff is stored -
67 * a few applications actually use this and die if it's not set
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.
73 aix_usrinfo(struct passwd *pw)
79 len = sizeof("LOGNAME= NAME= ") + (2 * strlen(pw->pw_name));
82 i = snprintf(cp, len, "LOGNAME=%s%cNAME=%s%c", pw->pw_name, '\0',
84 if (usrinfo(SETUINFO, cp, i) == -1)
85 fatal("Couldn't set usrinfo: %s", strerror(errno));
86 debug3("AIX/UsrInfo: set len %d", i);
91 # ifdef WITH_AIXAUTHENTICATE
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.
98 aix_remove_embedded_newlines(char *p)
107 /* Remove trailing whitespace */
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.
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.
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
126 * Returns 0 if an unsupportable combination is found, 1 otherwise.
129 aix_valid_authentications(const char *user)
131 char *auth1, *sys, *p;
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));
140 debug3("AIX SYSTEM attribute %s", sys);
141 if (strcmp(sys, "NONE") != 0)
142 return 1; /* not "NONE", so is OK */
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));
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'",
166 * Do authentication via AIX's authenticate routine. We loop until the
167 * reenter parameter is 0, but normally authenticate is called only once.
169 * Note: this function returns 1 on success, whereas AIX's authenticate()
173 sys_auth_passwd(Authctxt *ctxt, const char *password)
175 char *authmsg = NULL, *msg = NULL, *name = ctxt->pw->pw_name;
176 int authsuccess = 0, expired, reenter, result;
179 result = authenticate((char *)name, (char *)password, &reenter,
181 aix_remove_embedded_newlines(authmsg);
182 debug3("AIX/authenticate result %d, authmsg %.100s", result,
186 if (!aix_valid_authentications(name))
193 * Record successful login. We don't have a pty yet, so just
194 * label the line as "ssh"
199 * Check if the user's password is expired.
201 expired = passwdexpired(name, &msg);
203 buffer_append(ctxt->loginmsg, msg, strlen(msg));
204 aix_remove_embedded_newlines(msg);
206 debug3("AIX/passwdexpired returned %d msg %.100s", expired, msg);
209 case 0: /* password not expired */
211 case 1: /* expired, password change required */
212 ctxt->force_pwchange = 1;
214 default: /* user can't change(2) or other error (-1) */
215 logit("Password can't be changed for user %s: %.100s",
232 * Check if specified account is permitted to log in.
233 * Returns 1 if login is allowed, 0 if not allowed.
236 sys_auth_allowed_user(struct passwd *pw, Buffer *loginmsg)
239 int result, permitted = 0;
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).
247 if (pw->pw_uid == 0 || geteuid() != 0) {
248 debug3("%s: not checking", __func__);
252 result = loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &msg);
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.
260 if (result == -1 && errno == EPERM && stat(_PATH_NOLOGIN, &st) == 0)
262 else if (msg != NULL)
263 buffer_append(loginmsg, msg, strlen(msg));
265 msg = xstrdup("(none)");
266 aix_remove_embedded_newlines(msg);
267 debug3("AIX/loginrestrictions returned %d msg %.100s", result, msg);
270 logit("Login restricted for %s: %.100s", pw->pw_name, msg);
276 sys_auth_record_login(const char *user, const char *host, const char *ttynm,
280 static int msg_done = 0;
284 if (loginsuccess((char *)user, (char *)host, (char *)ttynm, &msg) == 0) {
286 if (msg != NULL && loginmsg != NULL && !msg_done) {
287 debug("AIX/loginsuccess: msg %s", msg);
288 buffer_append(loginmsg, msg, strlen(msg));
297 # ifdef CUSTOM_FAILED_LOGIN
299 * record_failed_login: generic "login failed" interface function
302 record_failed_login(const char *user, const char *hostname, const char *ttyname)
308 # ifdef AIX_LOGINFAILED_4ARG
309 loginfailed((char *)user, (char *)hostname, (char *)ttyname,
312 loginfailed((char *)user, (char *)hostname, (char *)ttyname);
316 # endif /* CUSTOM_FAILED_LOGIN */
319 * If we have setauthdb, retrieve the password registry for the user's
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.
325 aix_setauthdb(const char *user)
327 # ifdef HAVE_SETAUTHDB
330 if (setuserdb(S_READ) == -1) {
331 debug3("%s: Could not open userdb to read", __func__);
335 if (getuserattr((char *)user, S_REGISTRY, ®istry, SEC_CHAR) == 0) {
336 if (setauthdb(registry, old_registry) == 0)
337 debug3("AIX/setauthdb set registry '%s'", registry);
339 debug3("AIX/setauthdb set registry '%s' failed: %s",
340 registry, strerror(errno));
342 debug3("%s: Could not read S_REGISTRY for user: %s", __func__,
345 # endif /* HAVE_SETAUTHDB */
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,
355 aix_restoreauthdb(void)
357 # ifdef HAVE_SETAUTHDB
358 if (setauthdb(old_registry, NULL) == 0)
359 debug3("%s: restoring old registry '%s'", __func__,
362 debug3("%s: failed to restore old registry %s", __func__,
364 # endif /* HAVE_SETAUTHDB */
367 # endif /* WITH_AIXAUTHENTICATE */
369 # if defined(AIX_GETNAMEINFO_HACK) && !defined(BROKEN_ADDRINFO)
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.
377 sshaix_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
378 size_t hostlen, char *serv, size_t servlen, int flags)
380 struct sockaddr_in6 *sa6;
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;
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);
394 return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
396 # endif /* AIX_GETNAMEINFO_HACK */