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