1666aa9cdes/*
2666aa9cdes *
3666aa9cdes * Copyright (c) 2001 Gert Doering.  All rights reserved.
458ec145des * Copyright (c) 2003,2004,2005,2006 Darren Tucker.  All rights reserved.
5666aa9cdes *
6666aa9cdes * Redistribution and use in source and binary forms, with or without
7666aa9cdes * modification, are permitted provided that the following conditions
8666aa9cdes * are met:
9666aa9cdes * 1. Redistributions of source code must retain the above copyright
10666aa9cdes *    notice, this list of conditions and the following disclaimer.
11666aa9cdes * 2. Redistributions in binary form must reproduce the above copyright
12666aa9cdes *    notice, this list of conditions and the following disclaimer in the
13666aa9cdes *    documentation and/or other materials provided with the distribution.
14666aa9cdes *
15666aa9cdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16666aa9cdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17666aa9cdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18666aa9cdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19666aa9cdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20666aa9cdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21666aa9cdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22666aa9cdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23666aa9cdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24666aa9cdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25666aa9cdes *
26666aa9cdes */
27666aa9cdes#include "includes.h"
28666aa9cdes
29666aa9cdes#include "xmalloc.h"
30705433fdes#include "sshbuf.h"
31705433fdes#include "ssherr.h"
32705433fdes#include "sshkey.h"
33666aa9cdes#include "hostfile.h"
34666aa9cdes#include "auth.h"
35666aa9cdes#include "ssh.h"
36441eac4des#include "ssh_api.h"
37666aa9cdes#include "log.h"
38666aa9cdes
39666aa9cdes#ifdef _AIX
40666aa9cdes
41666aa9cdes#include <errno.h>
42666aa9cdes#if defined(HAVE_NETDB_H)
43666aa9cdes# include <netdb.h>
44666aa9cdes#endif
45666aa9cdes#include <uinfo.h>
46666aa9cdes#include <stdarg.h>
47666aa9cdes#include <string.h>
48666aa9cdes#include <unistd.h>
49666aa9cdes#include <sys/socket.h>
50666aa9cdes
51666aa9cdes#ifdef WITH_AIXAUTHENTICATE
52666aa9cdes# include <login.h>
53666aa9cdes# include <userpw.h>
54666aa9cdes# if defined(HAVE_SYS_AUDIT_H) && defined(AIX_LOGINFAILED_4ARG)
55666aa9cdes#  include <sys/audit.h>
56666aa9cdes# endif
57666aa9cdes# include <usersec.h>
58666aa9cdes#endif
59666aa9cdes
60666aa9cdes#include "port-aix.h"
61666aa9cdes
6298b8242desstatic char *lastlogin_msg = NULL;
6398b8242des
64666aa9cdes# ifdef HAVE_SETAUTHDB
65666aa9cdesstatic char old_registry[REGISTRY_SIZE] = "";
66666aa9cdes# endif
67666aa9cdes
68666aa9cdes/*
69666aa9cdes * AIX has a "usrinfo" area where logname and other stuff is stored -
70666aa9cdes * a few applications actually use this and die if it's not set
71666aa9cdes *
72666aa9cdes * NOTE: TTY= should be set, but since no one uses it and it's hard to
73666aa9cdes * acquire due to privsep code.  We will just drop support.
74666aa9cdes */
75666aa9cdesvoid
76666aa9cdesaix_usrinfo(struct passwd *pw)
77666aa9cdes{
78666aa9cdes	u_int i;
79666aa9cdes	size_t len;
80666aa9cdes	char *cp;
81666aa9cdes
82666aa9cdes	len = sizeof("LOGNAME= NAME= ") + (2 * strlen(pw->pw_name));
83666aa9cdes	cp = xmalloc(len);
84666aa9cdes
85666aa9cdes	i = snprintf(cp, len, "LOGNAME=%s%cNAME=%s%c", pw->pw_name, '\0',
86666aa9cdes	    pw->pw_name, '\0');
87666aa9cdes	if (usrinfo(SETUINFO, cp, i) == -1)
88666aa9cdes		fatal("Couldn't set usrinfo: %s", strerror(errno));
89666aa9cdes	debug3("AIX/UsrInfo: set len %d", i);
90666aa9cdes
91ff2597ddes	free(cp);
92666aa9cdes}
93666aa9cdes
94666aa9cdes# ifdef WITH_AIXAUTHENTICATE
95666aa9cdes/*
96666aa9cdes * Remove embedded newlines in string (if any).
97666aa9cdes * Used before logging messages returned by AIX authentication functions
98666aa9cdes * so the message is logged on one line.
99666aa9cdes */
100666aa9cdesvoid
101666aa9cdesaix_remove_embedded_newlines(char *p)
102666aa9cdes{
103666aa9cdes	if (p == NULL)
104666aa9cdes		return;
105666aa9cdes
106666aa9cdes	for (; *p; p++) {
107666aa9cdes		if (*p == '\n')
108666aa9cdes			*p = ' ';
109666aa9cdes	}
110666aa9cdes	/* Remove trailing whitespace */
111666aa9cdes	if (*--p == ' ')
112666aa9cdes		*p = '\0';
113666aa9cdes}
114666aa9cdes
115666aa9cdes/*
116666aa9cdes * Test specifically for the case where SYSTEM == NONE and AUTH1 contains
117666aa9cdes * anything other than NONE or SYSTEM, which indicates that the admin has
118666aa9cdes * configured the account for purely AUTH1-type authentication.
119666aa9cdes *
120666aa9cdes * Since authenticate() doesn't check AUTH1, and sshd can't sanely support
121666aa9cdes * AUTH1 itself, in such a case authenticate() will allow access without
122666aa9cdes * authentation, which is almost certainly not what the admin intends.
123666aa9cdes *
124666aa9cdes * (The native tools, eg login, will process the AUTH1 list in addition to
125666aa9cdes * the SYSTEM list by using ckuserID(), however ckuserID() and AUTH1 methods
126666aa9cdes * have been deprecated since AIX 4.2.x and would be very difficult for sshd
127666aa9cdes * to support.
128666aa9cdes *
129666aa9cdes * Returns 0 if an unsupportable combination is found, 1 otherwise.
130666aa9cdes */
131666aa9cdesstatic int
132666aa9cdesaix_valid_authentications(const char *user)
133666aa9cdes{
134666aa9cdes	char *auth1, *sys, *p;
135666aa9cdes	int valid = 1;
136666aa9cdes
137666aa9cdes	if (getuserattr((char *)user, S_AUTHSYSTEM, &sys, SEC_CHAR) != 0) {
138666aa9cdes		logit("Can't retrieve attribute SYSTEM for %s: %.100s",
139666aa9cdes		    user, strerror(errno));
140666aa9cdes		return 0;
141666aa9cdes	}
142666aa9cdes
143666aa9cdes	debug3("AIX SYSTEM attribute %s", sys);
144666aa9cdes	if (strcmp(sys, "NONE") != 0)
145666aa9cdes		return 1;	/* not "NONE", so is OK */
146666aa9cdes
147666aa9cdes	if (getuserattr((char *)user, S_AUTH1, &auth1, SEC_LIST) != 0) {
148666aa9cdes		logit("Can't retrieve attribute auth1 for %s: %.100s",
149666aa9cdes		    user, strerror(errno));
150666aa9cdes		return 0;
151666aa9cdes	}
152666aa9cdes
153666aa9cdes	p = auth1;
154666aa9cdes	/* A SEC_LIST is concatenated strings, ending with two NULs. */
155666aa9cdes	while (p[0] != '\0' && p[1] != '\0') {
156666aa9cdes		debug3("AIX auth1 attribute list member %s", p);
157666aa9cdes		if (strcmp(p, "NONE") != 0 && strcmp(p, "SYSTEM")) {
158666aa9cdes			logit("Account %s has unsupported auth1 value '%s'",
159666aa9cdes			    user, p);
160666aa9cdes			valid = 0;
161666aa9cdes		}
162666aa9cdes		p += strlen(p) + 1;
163666aa9cdes	}
164666aa9cdes
165666aa9cdes	return (valid);
166666aa9cdes}
167666aa9cdes
168666aa9cdes/*
169666aa9cdes * Do authentication via AIX's authenticate routine.  We loop until the
170666aa9cdes * reenter parameter is 0, but normally authenticate is called only once.
171666aa9cdes *
172666aa9cdes * Note: this function returns 1 on success, whereas AIX's authenticate()
173666aa9cdes * returns 0.
174666aa9cdes */
175666aa9cdesint
176441eac4dessys_auth_passwd(struct ssh *ssh, const char *password)
177666aa9cdes{
178441eac4des	Authctxt *ctxt = ssh->authctxt;
179666aa9cdes	char *authmsg = NULL, *msg = NULL, *name = ctxt->pw->pw_name;
180705433fdes	int r, authsuccess = 0, expired, reenter, result;
181666aa9cdes
182666aa9cdes	do {
183666aa9cdes		result = authenticate((char *)name, (char *)password, &reenter,
184666aa9cdes		    &authmsg);
185914a57cdes		aix_remove_embedded_newlines(authmsg);
186666aa9cdes		debug3("AIX/authenticate result %d, authmsg %.100s", result,
187666aa9cdes		    authmsg);
188666aa9cdes	} while (reenter);
189666aa9cdes
190666aa9cdes	if (!aix_valid_authentications(name))
191666aa9cdes		result = -1;
192666aa9cdes
193666aa9cdes	if (result == 0) {
194666aa9cdes		authsuccess = 1;
195666aa9cdes
196666aa9cdes		/*
197666aa9cdes		 * Record successful login.  We don't have a pty yet, so just
198666aa9cdes		 * label the line as "ssh"
199666aa9cdes		 */
200666aa9cdes		aix_setauthdb(name);
201666aa9cdes
202666aa9cdes		/*
203666aa9cdes		 * Check if the user's password is expired.
204666aa9cdes		 */
205666aa9cdes		expired = passwdexpired(name, &msg);
206666aa9cdes		if (msg && *msg) {
207705433fdes			if ((r = sshbuf_put(ctxt->loginmsg,
208705433fdes			    msg, strlen(msg))) != 0)
209705433fdes				fatal("%s: buffer error: %s",
210705433fdes				    __func__, ssh_err(r));
211666aa9cdes			aix_remove_embedded_newlines(msg);
212666aa9cdes		}
213666aa9cdes		debug3("AIX/passwdexpired returned %d msg %.100s", expired, msg);
214666aa9cdes
215666aa9cdes		switch (expired) {
216666aa9cdes		case 0: /* password not expired */
217666aa9cdes			break;
218666aa9cdes		case 1: /* expired, password change required */
219666aa9cdes			ctxt->force_pwchange = 1;
220666aa9cdes			break;
221666aa9cdes		default: /* user can't change(2) or other error (-1) */
222666aa9cdes			logit("Password can't be changed for user %s: %.100s",
223666aa9cdes			    name, msg);
224ff2597ddes			free(msg);
225666aa9cdes			authsuccess = 0;
226666aa9cdes		}
227666aa9cdes
228666aa9cdes		aix_restoreauthdb();
229666aa9cdes	}
230666aa9cdes
231ff2597ddes	free(authmsg);
232666aa9cdes
233666aa9cdes	return authsuccess;
234666aa9cdes}
235666aa9cdes
236666aa9cdes/*
237666aa9cdes * Check if specified account is permitted to log in.
238666aa9cdes * Returns 1 if login is allowed, 0 if not allowed.
239666aa9cdes */
240666aa9cdesint
241705433fdessys_auth_allowed_user(struct passwd *pw, struct sshbuf *loginmsg)
242666aa9cdes{
243666aa9cdes	char *msg = NULL;
244705433fdes	int r, result, permitted = 0;
245666aa9cdes	struct stat st;
246666aa9cdes
247666aa9cdes	/*
248666aa9cdes	 * Don't perform checks for root account (PermitRootLogin controls
2493d52116des	 * logins via ssh) or if running as non-root user (since
250666aa9cdes	 * loginrestrictions will always fail due to insufficient privilege).
251666aa9cdes	 */
252666aa9cdes	if (pw->pw_uid == 0 || geteuid() != 0) {
253666aa9cdes		debug3("%s: not checking", __func__);
254666aa9cdes		return 1;
255666aa9cdes	}
256666aa9cdes
257666aa9cdes	result = loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &msg);
258666aa9cdes	if (result == 0)
259666aa9cdes		permitted = 1;
260666aa9cdes	/*
261666aa9cdes	 * If restricted because /etc/nologin exists, the login will be denied
262666aa9cdes	 * in session.c after the nologin message is sent, so allow for now
263666aa9cdes	 * and do not append the returned message.
264666aa9cdes	 */
265666aa9cdes	if (result == -1 && errno == EPERM && stat(_PATH_NOLOGIN, &st) == 0)
266666aa9cdes		permitted = 1;
267705433fdes	else if (msg != NULL) {
268705433fdes		if ((r = sshbuf_put(loginmsg, msg, strlen(msg))) != 0)
269705433fdes			fatal("%s: buffer error: %s", __func__, ssh_err(r));
270705433fdes	}
271666aa9cdes	if (msg == NULL)
272666aa9cdes		msg = xstrdup("(none)");
273666aa9cdes	aix_remove_embedded_newlines(msg);
274666aa9cdes	debug3("AIX/loginrestrictions returned %d msg %.100s", result, msg);
275666aa9cdes
276666aa9cdes	if (!permitted)
277666aa9cdes		logit("Login restricted for %s: %.100s", pw->pw_name, msg);
278ff2597ddes	free(msg);
279666aa9cdes	return permitted;
280666aa9cdes}
281666aa9cdes
282666aa9cdesint
283666aa9cdessys_auth_record_login(const char *user, const char *host, const char *ttynm,
284705433fdes    struct sshbuf *loginmsg)
285666aa9cdes{
286666aa9cdes	char *msg = NULL;
287666aa9cdes	int success = 0;
288666aa9cdes
289666aa9cdes	aix_setauthdb(user);
290666aa9cdes	if (loginsuccess((char *)user, (char *)host, (char *)ttynm, &msg) == 0) {
291666aa9cdes		success = 1;
29298b8242des		if (msg != NULL) {
293666aa9cdes			debug("AIX/loginsuccess: msg %s", msg);
29498b8242des			if (lastlogin_msg == NULL)
29598b8242des				lastlogin_msg = msg;
296666aa9cdes		}
297666aa9cdes	}
298666aa9cdes	aix_restoreauthdb();
299666aa9cdes	return (success);
300666aa9cdes}
301666aa9cdes
30298b8242deschar *
30398b8242dessys_auth_get_lastlogin_msg(const char *user, uid_t uid)
30498b8242des{
30598b8242des	char *msg = lastlogin_msg;
30698b8242des
30798b8242des	lastlogin_msg = NULL;
30898b8242des	return msg;
30998b8242des}
31098b8242des
311666aa9cdes#  ifdef CUSTOM_FAILED_LOGIN
312666aa9cdes/*
313666aa9cdes * record_failed_login: generic "login failed" interface function
314666aa9cdes */
315666aa9cdesvoid
316666aa9cdesrecord_failed_login(const char *user, const char *hostname, const char *ttyname)
317666aa9cdes{
318666aa9cdes	if (geteuid() != 0)
319666aa9cdes		return;
320666aa9cdes
321666aa9cdes	aix_setauthdb(user);
322666aa9cdes#   ifdef AIX_LOGINFAILED_4ARG
323666aa9cdes	loginfailed((char *)user, (char *)hostname, (char *)ttyname,
324666aa9cdes	    AUDIT_FAIL_AUTH);
325666aa9cdes#   else
326666aa9cdes	loginfailed((char *)user, (char *)hostname, (char *)ttyname);
327666aa9cdes#   endif
328666aa9cdes	aix_restoreauthdb();
329666aa9cdes}
330666aa9cdes#  endif /* CUSTOM_FAILED_LOGIN */
331666aa9cdes
332666aa9cdes/*
333666aa9cdes * If we have setauthdb, retrieve the password registry for the user's
334666aa9cdes * account then feed it to setauthdb.  This will mean that subsequent AIX auth
335666aa9cdes * functions will only use the specified loadable module.  If we don't have
336666aa9cdes * setauthdb this is a no-op.
337666aa9cdes */
338666aa9cdesvoid
339666aa9cdesaix_setauthdb(const char *user)
340666aa9cdes{
341666aa9cdes#  ifdef HAVE_SETAUTHDB
342666aa9cdes	char *registry;
343666aa9cdes
344666aa9cdes	if (setuserdb(S_READ) == -1) {
345666aa9cdes		debug3("%s: Could not open userdb to read", __func__);
346666aa9cdes		return;
347666aa9cdes	}
348914a57cdes
349666aa9cdes	if (getuserattr((char *)user, S_REGISTRY, &registry, SEC_CHAR) == 0) {
350666aa9cdes		if (setauthdb(registry, old_registry) == 0)
351666aa9cdes			debug3("AIX/setauthdb set registry '%s'", registry);
352914a57cdes		else
353666aa9cdes			debug3("AIX/setauthdb set registry '%s' failed: %s",
354666aa9cdes			    registry, strerror(errno));
355666aa9cdes	} else
356666aa9cdes		debug3("%s: Could not read S_REGISTRY for user: %s", __func__,
357666aa9cdes		    strerror(errno));
358666aa9cdes	enduserdb();
359666aa9cdes#  endif /* HAVE_SETAUTHDB */
360666aa9cdes}
361666aa9cdes
362666aa9cdes/*
363666aa9cdes * Restore the user's registry settings from old_registry.
364666aa9cdes * Note that if the first aix_setauthdb fails, setauthdb("") is still safe
365666aa9cdes * (it restores the system default behaviour).  If we don't have setauthdb,
366666aa9cdes * this is a no-op.
367666aa9cdes */
368666aa9cdesvoid
369666aa9cdesaix_restoreauthdb(void)
370666aa9cdes{
371666aa9cdes#  ifdef HAVE_SETAUTHDB
372666aa9cdes	if (setauthdb(old_registry, NULL) == 0)
373666aa9cdes		debug3("%s: restoring old registry '%s'", __func__,
374666aa9cdes		    old_registry);
375666aa9cdes	else
376666aa9cdes		debug3("%s: failed to restore old registry %s", __func__,
377666aa9cdes		    old_registry);
378666aa9cdes#  endif /* HAVE_SETAUTHDB */
379666aa9cdes}
380666aa9cdes
381666aa9cdes# endif /* WITH_AIXAUTHENTICATE */
382666aa9cdes
3836c400d2des# ifdef USE_AIX_KRB_NAME
3846c400d2des/*
3856c400d2des * aix_krb5_get_principal_name: returns the user's kerberos client principal name if
3866c400d2des * configured, otherwise NULL.  Caller must free returned string.
3876c400d2des */
3886c400d2deschar *
3896c400d2desaix_krb5_get_principal_name(char *pw_name)
3906c400d2des{
3916c400d2des	char *authname = NULL, *authdomain = NULL, *principal = NULL;
3926c400d2des
3936c400d2des	setuserdb(S_READ);
3946c400d2des	if (getuserattr(pw_name, S_AUTHDOMAIN, &authdomain, SEC_CHAR) != 0)
3956c400d2des		debug("AIX getuserattr S_AUTHDOMAIN: %s", strerror(errno));
3966c400d2des	if (getuserattr(pw_name, S_AUTHNAME, &authname, SEC_CHAR) != 0)
3976c400d2des		debug("AIX getuserattr S_AUTHNAME: %s", strerror(errno));
3986c400d2des
3996c400d2des	if (authdomain != NULL)
4006c400d2des		xasprintf(&principal, "%s@%s", authname ? authname : pw_name, authdomain);
4016c400d2des	else if (authname != NULL)
4026c400d2des		principal = xstrdup(authname);
4036c400d2des	enduserdb();
4046c400d2des	return principal;
4056c400d2des}
4066c400d2des# endif /* USE_AIX_KRB_NAME */
4076c400d2des
408666aa9cdes# if defined(AIX_GETNAMEINFO_HACK) && !defined(BROKEN_ADDRINFO)
409666aa9cdes# undef getnameinfo
410666aa9cdes/*
411666aa9cdes * For some reason, AIX's getnameinfo will refuse to resolve the all-zeros
412666aa9cdes * IPv6 address into its textual representation ("::"), so we wrap it
413666aa9cdes * with a function that will.
414666aa9cdes */
415666aa9cdesint
416666aa9cdessshaix_getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
417666aa9cdes    size_t hostlen, char *serv, size_t servlen, int flags)
418666aa9cdes{
419666aa9cdes	struct sockaddr_in6 *sa6;
420666aa9cdes	u_int32_t *a6;
421666aa9cdes
422666aa9cdes	if (flags & (NI_NUMERICHOST|NI_NUMERICSERV) &&
423666aa9cdes	    sa->sa_family == AF_INET6) {
424666aa9cdes		sa6 = (struct sockaddr_in6 *)sa;
425666aa9cdes		a6 = sa6->sin6_addr.u6_addr.u6_addr32;
426666aa9cdes
427666aa9cdes		if (a6[0] == 0 && a6[1] == 0 && a6[2] == 0 && a6[3] == 0) {
428666aa9cdes			strlcpy(host, "::", hostlen);
429666aa9cdes			snprintf(serv, servlen, "%d", sa6->sin6_port);
430666aa9cdes			return 0;
431666aa9cdes		}
432666aa9cdes	}
433666aa9cdes	return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
434666aa9cdes}
435666aa9cdes# endif /* AIX_GETNAMEINFO_HACK */
436666aa9cdes
43758ec145des# if defined(USE_GETGRSET)
43858ec145des#  include <stdlib.h>
43958ec145desint
44058ec145desgetgrouplist(const char *user, gid_t pgid, gid_t *groups, int *grpcnt)
44158ec145des{
44258ec145des	char *cp, *grplist, *grp;
44358ec145des	gid_t gid;
44458ec145des	int ret = 0, ngroups = 0, maxgroups;
44558ec145des	long l;
44658ec145des
44758ec145des	maxgroups = *grpcnt;
44858ec145des
44958ec145des	if ((cp = grplist = getgrset(user)) == NULL)
45058ec145des		return -1;
45158ec145des
45258ec145des	/* handle zero-length case */
45358ec145des	if (maxgroups <= 0) {
45458ec145des		*grpcnt = 0;
45558ec145des		return -1;
45658ec145des	}
45758ec145des
45858ec145des	/* copy primary group */
45958ec145des	groups[ngroups++] = pgid;
46058ec145des
46158ec145des	/* copy each entry from getgrset into group list */
46258ec145des	while ((grp = strsep(&grplist, ",")) != NULL) {
46358ec145des		l = strtol(grp, NULL, 10);
46458ec145des		if (ngroups >= maxgroups || l == LONG_MIN || l == LONG_MAX) {
46558ec145des			ret = -1;
46658ec145des			goto out;
46758ec145des		}
46858ec145des		gid = (gid_t)l;
46958ec145des		if (gid == pgid)
47058ec145des			continue;	/* we have already added primary gid */
47158ec145des		groups[ngroups++] = gid;
47258ec145des	}
47358ec145desout:
47458ec145des	free(cp);
47558ec145des	*grpcnt = ngroups;
47658ec145des	return ret;
47758ec145des}
47858ec145des# endif	/* USE_GETGRSET */
47958ec145des
480666aa9cdes#endif /* _AIX */
481