]> andersk Git - openssh.git/blob - openbsd-compat/port-aix.c
- (dtucker) [openbsd-compat/port-aix.c openbsd-compat/port-aix.h] Restore
[openssh.git] / openbsd-compat / port-aix.c
1 /*
2  *
3  * Copyright (c) 2001 Gert Doering.  All rights reserved.
4  *
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.
13  *
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.
24  *
25  */
26 #include "includes.h"
27 #include "ssh.h"
28 #include "log.h"
29 #include "servconf.h"
30 #include "canohost.h"
31 #include "xmalloc.h"
32 #include "buffer.h"
33
34 #ifdef _AIX
35
36 #include <uinfo.h>
37 #include "port-aix.h"
38
39 extern ServerOptions options;
40 extern Buffer loginmsg;
41
42 # ifdef HAVE_SETAUTHDB
43 static char old_registry[REGISTRY_SIZE] = "";
44 # endif
45
46 /*
47  * AIX has a "usrinfo" area where logname and other stuff is stored - 
48  * a few applications actually use this and die if it's not set
49  *
50  * NOTE: TTY= should be set, but since no one uses it and it's hard to
51  * acquire due to privsep code.  We will just drop support.
52  */
53 void
54 aix_usrinfo(struct passwd *pw)
55 {
56         u_int i;
57         size_t len;
58         char *cp;
59
60         len = sizeof("LOGNAME= NAME= ") + (2 * strlen(pw->pw_name));
61         cp = xmalloc(len);
62
63         i = snprintf(cp, len, "LOGNAME=%s%cNAME=%s%c", pw->pw_name, '\0', 
64             pw->pw_name, '\0');
65         if (usrinfo(SETUINFO, cp, i) == -1)
66                 fatal("Couldn't set usrinfo: %s", strerror(errno));
67         debug3("AIX/UsrInfo: set len %d", i);
68
69         xfree(cp);
70 }
71
72 # ifdef WITH_AIXAUTHENTICATE
73 /*
74  * Remove embedded newlines in string (if any).
75  * Used before logging messages returned by AIX authentication functions
76  * so the message is logged on one line.
77  */
78 void
79 aix_remove_embedded_newlines(char *p)
80 {
81         if (p == NULL)
82                 return;
83
84         for (; *p; p++) {
85                 if (*p == '\n')
86                         *p = ' ';
87         }
88         /* Remove trailing whitespace */
89         if (*--p == ' ')
90                 *p = '\0';
91 }
92
93 /*
94  * Do authentication via AIX's authenticate routine.  We loop until the
95  * reenter parameter is 0, but normally authenticate is called only once.
96  *
97  * Note: this function returns 1 on success, whereas AIX's authenticate()
98  * returns 0.
99  */
100 int
101 aix_authenticate(const char *name, const char *password, const char *host)
102 {
103         char *authmsg = NULL, *msg;
104         int authsuccess = 0, reenter, result;
105
106         do {
107                 result = authenticate((char *)name, (char *)password, &reenter,
108                     &authmsg);
109                 aix_remove_embedded_newlines(authmsg);  
110                 debug3("AIX/authenticate result %d, msg %.100s", result,
111                     authmsg);
112         } while (reenter);
113
114         if (result == 0) {
115                 authsuccess = 1;
116
117                 /* No pty yet, so just label the line as "ssh" */
118                 aix_setauthdb(name);
119                 if (loginsuccess((char *)name, (char *)host, "ssh", &msg) == 0) {
120                         if (msg != NULL) {
121                                 debug("%s: msg %s", __func__, msg);
122                                 buffer_append(&loginmsg, msg, strlen(msg));
123                                 xfree(msg);
124                         }
125                 }
126                 aix_restoreauthdb();
127         }
128
129         if (authmsg != NULL)
130                 xfree(authmsg);
131
132         return authsuccess;
133 }
134   
135 #  ifdef CUSTOM_FAILED_LOGIN
136 /*
137  * record_failed_login: generic "login failed" interface function
138  */
139 void
140 record_failed_login(const char *user, const char *ttyname)
141 {
142         char *hostname = (char *)get_canonical_hostname(options.use_dns);
143
144         if (geteuid() != 0)
145                 return;
146
147         aix_setauthdb(user);
148 #   ifdef AIX_LOGINFAILED_4ARG
149         loginfailed((char *)user, hostname, (char *)ttyname, AUDIT_FAIL_AUTH);
150 #   else
151         loginfailed((char *)user, hostname, (char *)ttyname);
152 #   endif
153         aix_restoreauthdb();
154 }
155 #  endif /* CUSTOM_FAILED_LOGIN */
156
157 /*
158  * If we have setauthdb, retrieve the password registry for the user's
159  * account then feed it to setauthdb.  This will mean that subsequent AIX auth
160  * functions will only use the specified loadable module.  If we don't have
161  * setauthdb this is a no-op.
162  */
163 void
164 aix_setauthdb(const char *user)
165 {
166 #  ifdef HAVE_SETAUTHDB
167         char *registry;
168
169         if (setuserdb(S_READ) == -1) {
170                 debug3("%s: Could not open userdb to read", __func__);
171                 return;
172         }
173         
174         if (getuserattr((char *)user, S_REGISTRY, &registry, SEC_CHAR) == 0) {
175                 if (setauthdb(registry, old_registry) == 0)
176                         debug3("AIX/setauthdb set registry '%s'", registry);
177                 else 
178                         debug3("AIX/setauthdb set registry '%s' failed: %s",
179                             registry, strerror(errno));
180         } else
181                 debug3("%s: Could not read S_REGISTRY for user: %s", __func__,
182                     strerror(errno));
183         enduserdb();
184 #  endif /* HAVE_SETAUTHDB */
185 }
186
187 /*
188  * Restore the user's registry settings from old_registry.
189  * Note that if the first aix_setauthdb fails, setauthdb("") is still safe
190  * (it restores the system default behaviour).  If we don't have setauthdb,
191  * this is a no-op.
192  */
193 void
194 aix_restoreauthdb(void)
195 {
196 #  ifdef HAVE_SETAUTHDB
197         if (setauthdb(old_registry, NULL) == 0)
198                 debug3("%s: restoring old registry '%s'", __func__,
199                     old_registry);
200         else
201                 debug3("%s: failed to restore old registry %s", __func__,
202                     old_registry);
203 #  endif /* HAVE_SETAUTHDB */
204 }
205
206 # endif /* WITH_AIXAUTHENTICATE */
207
208 #endif /* _AIX */
This page took 0.438673 seconds and 5 git commands to generate.