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