]> andersk Git - openssh.git/blame - openbsd-compat/port-aix.c
- (tim) [configure.ac] Bug #1149. Changes in QNX section only. Patch by
[openssh.git] / openbsd-compat / port-aix.c
CommitLineData
0ba40daa 1/*
34a88012 2 *
3 * Copyright (c) 2001 Gert Doering. All rights reserved.
c8ed2130 4 * Copyright (c) 2003,2004,2005 Darren Tucker. All rights reserved.
34a88012 5 *
0ba40daa 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 */
90e70cfc 27#include "includes.h"
262b1744 28#include "auth.h"
73d9dad3 29#include "ssh.h"
30#include "log.h"
805dcf3a 31#include "xmalloc.h"
f0b467ef 32#include "buffer.h"
90e70cfc 33
34#ifdef _AIX
35
36#include <uinfo.h>
5ccf88cb 37#include <sys/socket.h>
2aa3a16c 38#include "port-aix.h"
90e70cfc 39
d852ad8b 40# ifdef HAVE_SETAUTHDB
41static char old_registry[REGISTRY_SIZE] = "";
42# endif
43
90e70cfc 44/*
5700232d 45 * AIX has a "usrinfo" area where logname and other stuff is stored -
0ba40daa 46 * a few applications actually use this and die if it's not set
47 *
48 * NOTE: TTY= should be set, but since no one uses it and it's hard to
49 * acquire due to privsep code. We will just drop support.
90e70cfc 50 */
51void
0ba40daa 52aix_usrinfo(struct passwd *pw)
90e70cfc 53{
90e70cfc 54 u_int i;
9901cb37 55 size_t len;
0ba40daa 56 char *cp;
90e70cfc 57
9901cb37 58 len = sizeof("LOGNAME= NAME= ") + (2 * strlen(pw->pw_name));
59 cp = xmalloc(len);
60
5700232d 61 i = snprintf(cp, len, "LOGNAME=%s%cNAME=%s%c", pw->pw_name, '\0',
1e72a7e3 62 pw->pw_name, '\0');
90e70cfc 63 if (usrinfo(SETUINFO, cp, i) == -1)
64 fatal("Couldn't set usrinfo: %s", strerror(errno));
65 debug3("AIX/UsrInfo: set len %d", i);
9901cb37 66
90e70cfc 67 xfree(cp);
68}
69
f0b467ef 70# ifdef WITH_AIXAUTHENTICATE
bc7dfc06 71/*
72 * Remove embedded newlines in string (if any).
73 * Used before logging messages returned by AIX authentication functions
74 * so the message is logged on one line.
75 */
76void
77aix_remove_embedded_newlines(char *p)
78{
79 if (p == NULL)
80 return;
81
82 for (; *p; p++) {
83 if (*p == '\n')
84 *p = ' ';
85 }
86 /* Remove trailing whitespace */
87 if (*--p == ' ')
88 *p = '\0';
89}
f0b467ef 90
1383f285 91/*
92 * Test specifically for the case where SYSTEM == NONE and AUTH1 contains
93 * anything other than NONE or SYSTEM, which indicates that the admin has
94 * configured the account for purely AUTH1-type authentication.
95 *
96 * Since authenticate() doesn't check AUTH1, and sshd can't sanely support
97 * AUTH1 itself, in such a case authenticate() will allow access without
98 * authentation, which is almost certainly not what the admin intends.
99 *
100 * (The native tools, eg login, will process the AUTH1 list in addition to
101 * the SYSTEM list by using ckuserID(), however ckuserID() and AUTH1 methods
102 * have been deprecated since AIX 4.2.x and would be very difficult for sshd
103 * to support.
104 *
105 * Returns 0 if an unsupportable combination is found, 1 otherwise.
106 */
107static int
108aix_valid_authentications(const char *user)
109{
110 char *auth1, *sys, *p;
111 int valid = 1;
112
113 if (getuserattr((char *)user, S_AUTHSYSTEM, &sys, SEC_CHAR) != 0) {
114 logit("Can't retrieve attribute SYSTEM for %s: %.100s",
115 user, strerror(errno));
116 return 0;
117 }
118
119 debug3("AIX SYSTEM attribute %s", sys);
120 if (strcmp(sys, "NONE") != 0)
121 return 1; /* not "NONE", so is OK */
122
123 if (getuserattr((char *)user, S_AUTH1, &auth1, SEC_LIST) != 0) {
124 logit("Can't retrieve attribute auth1 for %s: %.100s",
125 user, strerror(errno));
126 return 0;
127 }
128
129 p = auth1;
130 /* A SEC_LIST is concatenated strings, ending with two NULs. */
131 while (p[0] != '\0' && p[1] != '\0') {
132 debug3("AIX auth1 attribute list member %s", p);
133 if (strcmp(p, "NONE") != 0 && strcmp(p, "SYSTEM")) {
134 logit("Account %s has unsupported auth1 value '%s'",
135 user, p);
136 valid = 0;
137 }
138 p += strlen(p) + 1;
139 }
140
141 return (valid);
142}
143
f0b467ef 144/*
145 * Do authentication via AIX's authenticate routine. We loop until the
146 * reenter parameter is 0, but normally authenticate is called only once.
147 *
148 * Note: this function returns 1 on success, whereas AIX's authenticate()
149 * returns 0.
150 */
151int
1b394137 152sys_auth_passwd(Authctxt *ctxt, const char *password)
f0b467ef 153{
c8ed2130 154 char *authmsg = NULL, *msg = NULL, *name = ctxt->pw->pw_name;
cadfc759 155 int authsuccess = 0, expired, reenter, result;
f0b467ef 156
157 do {
158 result = authenticate((char *)name, (char *)password, &reenter,
159 &authmsg);
160 aix_remove_embedded_newlines(authmsg);
ec7f28f2 161 debug3("AIX/authenticate result %d, authmsg %.100s", result,
f0b467ef 162 authmsg);
163 } while (reenter);
164
1383f285 165 if (!aix_valid_authentications(name))
166 result = -1;
167
f0b467ef 168 if (result == 0) {
169 authsuccess = 1;
170
5700232d 171 /*
cadfc759 172 * Record successful login. We don't have a pty yet, so just
173 * label the line as "ssh"
174 */
f0b467ef 175 aix_setauthdb(name);
cadfc759 176
177 /*
178 * Check if the user's password is expired.
179 */
6d05637a 180 expired = passwdexpired(name, &msg);
181 if (msg && *msg) {
1b394137 182 buffer_append(ctxt->loginmsg, msg, strlen(msg));
6d05637a 183 aix_remove_embedded_newlines(msg);
184 }
185 debug3("AIX/passwdexpired returned %d msg %.100s", expired, msg);
cadfc759 186
187 switch (expired) {
188 case 0: /* password not expired */
189 break;
190 case 1: /* expired, password change required */
191 ctxt->force_pwchange = 1;
cadfc759 192 break;
193 default: /* user can't change(2) or other error (-1) */
194 logit("Password can't be changed for user %s: %.100s",
195 name, msg);
196 if (msg)
197 xfree(msg);
198 authsuccess = 0;
199 }
200
d852ad8b 201 aix_restoreauthdb();
f0b467ef 202 }
203
204 if (authmsg != NULL)
205 xfree(authmsg);
206
207 return authsuccess;
208}
bc5c2025 209
210/*
211 * Check if specified account is permitted to log in.
212 * Returns 1 if login is allowed, 0 if not allowed.
213 */
214int
5ccf88cb 215sys_auth_allowed_user(struct passwd *pw, Buffer *loginmsg)
bc5c2025 216{
217 char *msg = NULL;
218 int result, permitted = 0;
219 struct stat st;
220
221 /*
222 * Don't perform checks for root account (PermitRootLogin controls
223 * logins via * ssh) or if running as non-root user (since
224 * loginrestrictions will always fail due to insufficient privilege).
225 */
226 if (pw->pw_uid == 0 || geteuid() != 0) {
13f72b91 227 debug3("%s: not checking", __func__);
bc5c2025 228 return 1;
229 }
230
231 result = loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &msg);
232 if (result == 0)
233 permitted = 1;
234 /*
235 * If restricted because /etc/nologin exists, the login will be denied
236 * in session.c after the nologin message is sent, so allow for now
237 * and do not append the returned message.
238 */
239 if (result == -1 && errno == EPERM && stat(_PATH_NOLOGIN, &st) == 0)
240 permitted = 1;
241 else if (msg != NULL)
5ccf88cb 242 buffer_append(loginmsg, msg, strlen(msg));
bc5c2025 243 if (msg == NULL)
244 msg = xstrdup("(none)");
245 aix_remove_embedded_newlines(msg);
246 debug3("AIX/loginrestrictions returned %d msg %.100s", result, msg);
247
248 if (!permitted)
249 logit("Login restricted for %s: %.100s", pw->pw_name, msg);
250 xfree(msg);
251 return permitted;
252}
253
f5ed3301 254int
5ccf88cb 255sys_auth_record_login(const char *user, const char *host, const char *ttynm,
256 Buffer *loginmsg)
f5ed3301 257{
c8ed2130 258 char *msg = NULL;
f5ed3301 259 int success = 0;
260
261 aix_setauthdb(user);
c2edf154 262 if (loginsuccess((char *)user, (char *)host, (char *)ttynm, &msg) == 0) {
f5ed3301 263 success = 1;
264 if (msg != NULL) {
c2edf154 265 debug("AIX/loginsuccess: msg %s", msg);
5ccf88cb 266 buffer_append(loginmsg, msg, strlen(msg));
f5ed3301 267 xfree(msg);
268 }
269 }
270 aix_restoreauthdb();
271 return (success);
272}
273
f0b467ef 274# ifdef CUSTOM_FAILED_LOGIN
73d9dad3 275/*
276 * record_failed_login: generic "login failed" interface function
277 */
278void
575e336f 279record_failed_login(const char *user, const char *hostname, const char *ttyname)
73d9dad3 280{
2aa3a16c 281 if (geteuid() != 0)
282 return;
283
284 aix_setauthdb(user);
f0b467ef 285# ifdef AIX_LOGINFAILED_4ARG
c2edf154 286 loginfailed((char *)user, (char *)hostname, (char *)ttyname,
287 AUDIT_FAIL_AUTH);
f0b467ef 288# else
c2edf154 289 loginfailed((char *)user, (char *)hostname, (char *)ttyname);
f0b467ef 290# endif
d852ad8b 291 aix_restoreauthdb();
73d9dad3 292}
f0b467ef 293# endif /* CUSTOM_FAILED_LOGIN */
2aa3a16c 294
295/*
296 * If we have setauthdb, retrieve the password registry for the user's
d852ad8b 297 * account then feed it to setauthdb. This will mean that subsequent AIX auth
298 * functions will only use the specified loadable module. If we don't have
299 * setauthdb this is a no-op.
2aa3a16c 300 */
301void
302aix_setauthdb(const char *user)
303{
304# ifdef HAVE_SETAUTHDB
d852ad8b 305 char *registry;
2aa3a16c 306
307 if (setuserdb(S_READ) == -1) {
308 debug3("%s: Could not open userdb to read", __func__);
309 return;
310 }
311
312 if (getuserattr((char *)user, S_REGISTRY, &registry, SEC_CHAR) == 0) {
d852ad8b 313 if (setauthdb(registry, old_registry) == 0)
314 debug3("AIX/setauthdb set registry '%s'", registry);
2aa3a16c 315 else
d852ad8b 316 debug3("AIX/setauthdb set registry '%s' failed: %s",
317 registry, strerror(errno));
2aa3a16c 318 } else
319 debug3("%s: Could not read S_REGISTRY for user: %s", __func__,
320 strerror(errno));
321 enduserdb();
f0b467ef 322# endif /* HAVE_SETAUTHDB */
2aa3a16c 323}
90e70cfc 324
d852ad8b 325/*
326 * Restore the user's registry settings from old_registry.
327 * Note that if the first aix_setauthdb fails, setauthdb("") is still safe
328 * (it restores the system default behaviour). If we don't have setauthdb,
329 * this is a no-op.
330 */
331void
332aix_restoreauthdb(void)
333{
334# ifdef HAVE_SETAUTHDB
335 if (setauthdb(old_registry, NULL) == 0)
336 debug3("%s: restoring old registry '%s'", __func__,
337 old_registry);
338 else
339 debug3("%s: failed to restore old registry %s", __func__,
340 old_registry);
341# endif /* HAVE_SETAUTHDB */
342}
343
f0b467ef 344# endif /* WITH_AIXAUTHENTICATE */
345
5ccf88cb 346# if defined(AIX_GETNAMEINFO_HACK) && !defined(BROKEN_ADDRINFO)
347# undef getnameinfo
348/*
349 * For some reason, AIX's getnameinfo will refuse to resolve the all-zeros
350 * IPv6 address into its textual representation ("::"), so we wrap it
351 * with a function that will.
352 */
353int
354sshaix_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
355 size_t hostlen, char *serv, size_t servlen, int flags)
356{
357 struct sockaddr_in6 *sa6;
358 u_int32_t *a6;
359
360 if (flags & (NI_NUMERICHOST|NI_NUMERICSERV) &&
361 sa->sa_family == AF_INET6) {
362 sa6 = (struct sockaddr_in6 *)sa;
363 a6 = sa6->sin6_addr.u6_addr.u6_addr32;
364
365 if (a6[0] == 0 && a6[1] == 0 && a6[2] == 0 && a6[3] == 0) {
366 strlcpy(host, "::", hostlen);
367 snprintf(serv, servlen, "%d", sa6->sin6_port);
368 return 0;
369 }
370 }
371 return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
372}
373# endif /* AIX_GETNAMEINFO_HACK */
374
f0b467ef 375#endif /* _AIX */
This page took 0.223702 seconds and 5 git commands to generate.