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