Import OpenSSH-5.1p1.
[dragonfly.git] / crypto / openssh-3.9p1 / openbsd-compat / port-aix.c
blob78f4faea3b3b61780393f5d5a7e16b7610ad0350
1 /*
3 * Copyright (c) 2001 Gert Doering. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "includes.h"
27 #include "auth.h"
28 #include "ssh.h"
29 #include "log.h"
30 #include "servconf.h"
31 #include "canohost.h"
32 #include "xmalloc.h"
33 #include "buffer.h"
35 #ifdef _AIX
37 #include <uinfo.h>
38 #include "port-aix.h"
40 extern ServerOptions options;
41 extern Buffer loginmsg;
43 # ifdef HAVE_SETAUTHDB
44 static char old_registry[REGISTRY_SIZE] = "";
45 # endif
48 * AIX has a "usrinfo" area where logname and other stuff is stored -
49 * a few applications actually use this and die if it's not set
51 * NOTE: TTY= should be set, but since no one uses it and it's hard to
52 * acquire due to privsep code. We will just drop support.
54 void
55 aix_usrinfo(struct passwd *pw)
57 u_int i;
58 size_t len;
59 char *cp;
61 len = sizeof("LOGNAME= NAME= ") + (2 * strlen(pw->pw_name));
62 cp = xmalloc(len);
64 i = snprintf(cp, len, "LOGNAME=%s%cNAME=%s%c", pw->pw_name, '\0',
65 pw->pw_name, '\0');
66 if (usrinfo(SETUINFO, cp, i) == -1)
67 fatal("Couldn't set usrinfo: %s", strerror(errno));
68 debug3("AIX/UsrInfo: set len %d", i);
70 xfree(cp);
73 # ifdef WITH_AIXAUTHENTICATE
75 * Remove embedded newlines in string (if any).
76 * Used before logging messages returned by AIX authentication functions
77 * so the message is logged on one line.
79 void
80 aix_remove_embedded_newlines(char *p)
82 if (p == NULL)
83 return;
85 for (; *p; p++) {
86 if (*p == '\n')
87 *p = ' ';
89 /* Remove trailing whitespace */
90 if (*--p == ' ')
91 *p = '\0';
95 * Do authentication via AIX's authenticate routine. We loop until the
96 * reenter parameter is 0, but normally authenticate is called only once.
98 * Note: this function returns 1 on success, whereas AIX's authenticate()
99 * returns 0.
102 sys_auth_passwd(Authctxt *ctxt, const char *password)
104 char *authmsg = NULL, *msg, *name = ctxt->pw->pw_name;
105 int authsuccess = 0, expired, reenter, result;
107 do {
108 result = authenticate((char *)name, (char *)password, &reenter,
109 &authmsg);
110 aix_remove_embedded_newlines(authmsg);
111 debug3("AIX/authenticate result %d, msg %.100s", result,
112 authmsg);
113 } while (reenter);
115 if (result == 0) {
116 authsuccess = 1;
119 * Record successful login. We don't have a pty yet, so just
120 * label the line as "ssh"
122 aix_setauthdb(name);
125 * Check if the user's password is expired.
127 expired = passwdexpired(name, &msg);
128 if (msg && *msg) {
129 buffer_append(&loginmsg, msg, strlen(msg));
130 aix_remove_embedded_newlines(msg);
132 debug3("AIX/passwdexpired returned %d msg %.100s", expired, msg);
134 switch (expired) {
135 case 0: /* password not expired */
136 break;
137 case 1: /* expired, password change required */
138 ctxt->force_pwchange = 1;
139 disable_forwarding();
140 break;
141 default: /* user can't change(2) or other error (-1) */
142 logit("Password can't be changed for user %s: %.100s",
143 name, msg);
144 if (msg)
145 xfree(msg);
146 authsuccess = 0;
149 aix_restoreauthdb();
152 if (authmsg != NULL)
153 xfree(authmsg);
155 return authsuccess;
159 * Check if specified account is permitted to log in.
160 * Returns 1 if login is allowed, 0 if not allowed.
163 sys_auth_allowed_user(struct passwd *pw)
165 char *msg = NULL;
166 int result, permitted = 0;
167 struct stat st;
170 * Don't perform checks for root account (PermitRootLogin controls
171 * logins via * ssh) or if running as non-root user (since
172 * loginrestrictions will always fail due to insufficient privilege).
174 if (pw->pw_uid == 0 || geteuid() != 0) {
175 debug3("%s: not checking", __func__);
176 return 1;
179 result = loginrestrictions(pw->pw_name, S_RLOGIN, NULL, &msg);
180 if (result == 0)
181 permitted = 1;
183 * If restricted because /etc/nologin exists, the login will be denied
184 * in session.c after the nologin message is sent, so allow for now
185 * and do not append the returned message.
187 if (result == -1 && errno == EPERM && stat(_PATH_NOLOGIN, &st) == 0)
188 permitted = 1;
189 else if (msg != NULL)
190 buffer_append(&loginmsg, msg, strlen(msg));
191 if (msg == NULL)
192 msg = xstrdup("(none)");
193 aix_remove_embedded_newlines(msg);
194 debug3("AIX/loginrestrictions returned %d msg %.100s", result, msg);
196 if (!permitted)
197 logit("Login restricted for %s: %.100s", pw->pw_name, msg);
198 xfree(msg);
199 return permitted;
203 sys_auth_record_login(const char *user, const char *host, const char *ttynm)
205 char *msg;
206 int success = 0;
208 aix_setauthdb(user);
209 if (loginsuccess((char *)user, host, ttynm, &msg) == 0) {
210 success = 1;
211 if (msg != NULL) {
212 debug("AIX/loginsuccess: msg %s", __func__, msg);
213 buffer_append(&loginmsg, msg, strlen(msg));
214 xfree(msg);
217 aix_restoreauthdb();
218 return (success);
221 # ifdef CUSTOM_FAILED_LOGIN
223 * record_failed_login: generic "login failed" interface function
225 void
226 record_failed_login(const char *user, const char *ttyname)
228 char *hostname = (char *)get_canonical_hostname(options.use_dns);
230 if (geteuid() != 0)
231 return;
233 aix_setauthdb(user);
234 # ifdef AIX_LOGINFAILED_4ARG
235 loginfailed((char *)user, hostname, (char *)ttyname, AUDIT_FAIL_AUTH);
236 # else
237 loginfailed((char *)user, hostname, (char *)ttyname);
238 # endif
239 aix_restoreauthdb();
241 # endif /* CUSTOM_FAILED_LOGIN */
244 * If we have setauthdb, retrieve the password registry for the user's
245 * account then feed it to setauthdb. This will mean that subsequent AIX auth
246 * functions will only use the specified loadable module. If we don't have
247 * setauthdb this is a no-op.
249 void
250 aix_setauthdb(const char *user)
252 # ifdef HAVE_SETAUTHDB
253 char *registry;
255 if (setuserdb(S_READ) == -1) {
256 debug3("%s: Could not open userdb to read", __func__);
257 return;
260 if (getuserattr((char *)user, S_REGISTRY, &registry, SEC_CHAR) == 0) {
261 if (setauthdb(registry, old_registry) == 0)
262 debug3("AIX/setauthdb set registry '%s'", registry);
263 else
264 debug3("AIX/setauthdb set registry '%s' failed: %s",
265 registry, strerror(errno));
266 } else
267 debug3("%s: Could not read S_REGISTRY for user: %s", __func__,
268 strerror(errno));
269 enduserdb();
270 # endif /* HAVE_SETAUTHDB */
274 * Restore the user's registry settings from old_registry.
275 * Note that if the first aix_setauthdb fails, setauthdb("") is still safe
276 * (it restores the system default behaviour). If we don't have setauthdb,
277 * this is a no-op.
279 void
280 aix_restoreauthdb(void)
282 # ifdef HAVE_SETAUTHDB
283 if (setauthdb(old_registry, NULL) == 0)
284 debug3("%s: restoring old registry '%s'", __func__,
285 old_registry);
286 else
287 debug3("%s: failed to restore old registry %s", __func__,
288 old_registry);
289 # endif /* HAVE_SETAUTHDB */
292 # endif /* WITH_AIXAUTHENTICATE */
294 #endif /* _AIX */