Debian Codegraph @

ftpd/pam.c

raw
1 /*
2 Copyright (C) 2000-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <syslog.h>
26 #include <attribute.h>
27 #include "extern.h"
28
29 #ifdef HAVE_SECURITY_PAM_APPL_H
30 # include <security/pam_appl.h>
31 #endif
32
33 /*
34 * Mechanisms and prerequisites.
35 *
36 * The original code was tailored to rely on the side-effects
37 * of the Linux-PAM module `pam_ftp.so', with its peculiarities:
38 *
39 * 1. If PAM_USER is `ftp' or `anonymous', fetch PAM_AUTHTOK
40 * and split it at `@' into PAM_RUSER and PAM_RHOST.
41 * Return with success and set PAM_USER to `ftp'.
42 *
43 * 2. For other values of PAM_USER, keep the gotten PAM_AUTHTOK
44 * unchanged, and fail.
45 *
46 * This module `pam_ftp.so' does not exist in OpenPAM, nor in
47 * Solaris-PAM. Thus portability requires some care.
48 */
49
50 /* June 3rd, 2012:
51 * The draft of A.G Morgan on behalf of the the Open-PAM
52 * working group has clearly not been able to get the
53 * additions PAM_INCOMPLETE and PAM_CONV_AGAIN accepted
54 * sufficiently well in order that the present code should
55 * force them upon BSD and Solaris. They are thus protected
56 * by preprocessor conditionals for the time being.
57 */
58
59 #ifdef WITH_PAM
60
61 /* PAM authentication, now using the PAM's async feature. */
62 static pam_handle_t *pamh;
63
64 static int PAM_conv (int num_msg, const struct pam_message **msg,
65 struct pam_response **resp, void *appdata_ptr);
66
67 static struct pam_conv PAM_conversation = { &PAM_conv, NULL };
68
69 static int
70 PAM_conv (int num_msg, const struct pam_message **msg,
71 struct pam_response **resp, void *appdata_ptr)
72 {
73 struct pam_response *repl = NULL;
74 int retval, count = 0, replies = 0;
75 int size = sizeof (struct pam_response);
76 struct credentials *pcred = (struct credentials *) appdata_ptr;
77
78 # define GET_MEM \
79 if (!(repl = realloc (repl, size))) \
80 return PAM_BUF_ERR; \
81 size += sizeof (struct pam_response)
82
83 retval = PAM_SUCCESS;
84
85 for (count = 0; count < num_msg; count++)
86 {
87 int savemsg = 0;
88
89 switch (msg[count]->msg_style)
90 {
91 case PAM_PROMPT_ECHO_ON:
92 GET_MEM;
93 repl[replies].resp_retcode = PAM_SUCCESS;
94 repl[replies].resp = sgetsave (pcred->name);
95 replies++;
96 break;
97 case PAM_PROMPT_ECHO_OFF:
98 GET_MEM;
99 if (pcred->pass == NULL)
100 {
101 savemsg = 1;
102 # ifdef PAM_CONV_AGAIN
103 retval = PAM_CONV_AGAIN; /* Linux-PAM */
104 # elif !defined WITH_LINUX_PAM
105 /*
106 * Simulate PAM_CONV_AGAIN.
107 * The alternate value PAM_TRY_AGAIN is not
108 * an expected return value here, so it will
109 * leave an audit trail. */
110 retval = PAM_CRED_INSUFFICIENT;
111 # else/* !PAM_CONV_AGAIN && !WITH_LINUX_PAM */
112 retval = PAM_CONV_ERR;
113 # endif
114 }
115 else
116 {
117 repl[replies].resp_retcode = PAM_SUCCESS;
118 repl[replies].resp = sgetsave (pcred->pass);
119 replies++;
120 }
121 break;
122 case PAM_TEXT_INFO:
123 savemsg = 1;
124 break;
125 case PAM_ERROR_MSG:
126 default:
127 /* Must be an error of some sort... */
128 savemsg = 1;
129 retval = PAM_CONV_ERR;
130 }
131
132 if (savemsg)
133 {
134 /* FIXME: This is a serious problem. If the PAM message
135 is multilines, the reply _must_ be formatted correctly.
136 The way to do this would be to consider \n as a boundary then
137 in the ftpd.c:user() or ftpd.c:pass() check for it and send
138 a lreply(). But I'm not sure the RFCs allow mutilines replies
139 for a passwd challenge. Many clients will simply break. */
140 /* XXX: Attempted solution; collect all messages, appended
141 * one after the other, separated by "\n". Then print all
142 * of them in one single run. This will circumvent the hard
143 * coded protocol convention of not allowing continuation
144 * massage to carry a deviating reply code relative to the
145 * final message.
146 */
147 if (pcred->message) /* XXX: make sure we split newlines correctly */
148 {
149 size_t len = strlen (pcred->message);
150 char *s = realloc (pcred->message, len
151 + strlen (msg[count]->msg) + 2);
152 if (s == NULL)
153 {
154 free (pcred->message);
155 pcred->message = NULL;
156 }
157 else
158 {
159 pcred->message = s;
160 strcat (pcred->message, "\n");
161 strcat (pcred->message, msg[count]->msg);
162 }
163 }
164 else
165 pcred->message = sgetsave (msg[count]->msg);
166
167 if (pcred->message == NULL)
168 retval = PAM_CONV_ERR;
169 else
170 {
171 char *sp;
172 /* Remove trailing space only. */
173 sp = pcred->message + strlen (pcred->message);
174 while (sp > pcred->message && strchr (" \t\n", *--sp))
175 *sp = '\0';
176 }
177 }
178
179 /* In case of error, drop responses and return */
180 if (retval)
181 {
182 /* FIXME: drop_reply is not standard, need to clean this. */
183 //_pam_drop_reply (repl, replies);
184 free (repl);
185 return retval;
186 }
187 }
188 if (repl)
189 *resp = repl;
190 return PAM_SUCCESS;
191 }
192
193 /* Non-zero return status means failure. */
194 static int
195 pam_doit (struct credentials *pcred)
196 {
197 char *username;
198 int error;
199
200 error = pam_authenticate (pamh, 0);
201
202 /* Probably being called with empty passwd. */
203 if (0
204 # ifdef PAM_CONV_AGAIN
205 || error == PAM_CONV_AGAIN
206 # endif
207 # ifdef PAM_INCOMPLETE
208 || error == PAM_INCOMPLETE
209 # endif
210 # ifndef WITH_LINUX_PAM
211 /*
212 * In absence of `pam_ftp.so', catch the simulated,
213 * incomplete state reported by our PAM_conv().
214 */
215 || (error == PAM_CRED_INSUFFICIENT && pcred->pass == NULL)
216 # endif/* !WITH_LINUX_PAM */
217 )
218 {
219 /* Avoid overly terse passwd messages and let the people
220 upstairs do something sane. */
221 if (pcred->message && !strcasecmp (pcred->message, "password:"))
222 {
223 free (pcred->message);
224 pcred->message = NULL;
225 }
226 return PAM_SUCCESS;
227 }
228
229 if (error == PAM_SUCCESS) /* Alright, we got it */
230 {
231 error = pam_acct_mgmt (pamh, 0);
232 if (error == PAM_SUCCESS)
233 error = pam_setcred (pamh, PAM_ESTABLISH_CRED);
234 if (error == PAM_SUCCESS)
235 error = pam_open_session (pamh, 0);
236 if (error == PAM_SUCCESS)
237 error = pam_get_item (pamh, PAM_USER, (const void **) &username);
238 if (error == PAM_SUCCESS)
239 {
240 if (sgetcred (username, pcred) != 0)
241 error = PAM_AUTH_ERR;
242 else
243 {
244 if (strcasecmp (username, "ftp") == 0)
245 pcred->guest = 1;
246 }
247 }
248 }
249 if (error != PAM_SUCCESS)
250 {
251 pam_end (pamh, error);
252 pamh = NULL;
253 }
254
255 return (error != PAM_SUCCESS);
256 }
257
258 # define TTY_FORMAT "/dev/ftp%d"
259
260 /* Non-zero return means failure. */
261 int
262 pam_user (const char *username, struct credentials *pcred)
263 {
264 int error;
265 char tty_name[strlen (TTY_FORMAT) + strlen ("9999999")];
266
267 if (pamh != NULL)
268 {
269 pam_end (pamh, PAM_ABORT);
270 pamh = NULL;
271 }
272
273 free (pcred->name);
274 pcred->name = strdup (username);
275 free (pcred->message);
276 pcred->message = NULL;
277
278 (void) snprintf (tty_name, sizeof (tty_name), TTY_FORMAT, (int) getpid ());
279
280 /* Arrange our creditive. */
281 PAM_conversation.appdata_ptr = (void *) pcred;
282
283 error = pam_start ("ftp", pcred->name, &PAM_conversation, &pamh);
284 if (error == PAM_SUCCESS)
285 error = pam_set_item (pamh, PAM_RHOST, pcred->remotehost);
286 if (error == PAM_SUCCESS)
287 error = pam_set_item (pamh, PAM_TTY, tty_name);
288 if (error != PAM_SUCCESS)
289 {
290 pam_end (pamh, error);
291 pamh = 0;
292 }
293
294 if (pamh)
295 error = pam_doit (pcred);
296
297 return (error != PAM_SUCCESS);
298 }
299
300 /* Nonzero value return for error. */
301 int
302 pam_pass (const char *passwd, struct credentials *pcred)
303 {
304 int error = PAM_AUTH_ERR;
305 if (pamh)
306 {
307 pcred->pass = (char *) passwd;
308 error = pam_doit (pcred);
309 pcred->pass = NULL;
310 }
311 return error != PAM_SUCCESS;
312 }
313
314 void
315 pam_end_login (struct credentials *pcred MAYBE_UNUSED)
316 {
317 int error;
318
319 if (pamh)
320 {
321 error = pam_close_session (pamh, PAM_SILENT);
322 if (logging && error != PAM_SUCCESS)
323 syslog (LOG_ERR, "pam_session: %s", pam_strerror (pamh, error));
324
325 error = pam_setcred (pamh, PAM_SILENT | PAM_DELETE_CRED);
326 if (logging && error != PAM_SUCCESS)
327 syslog (LOG_ERR, "pam_setcred: %s", pam_strerror (pamh, error));
328
329 error = pam_end (pamh, error);
330 if (logging && error != PAM_SUCCESS)
331 syslog (LOG_ERR, "pam_end: %s", pam_strerror (pamh, error));
332
333 pamh = NULL;
334 }
335 }
336 #endif /* WITH_PAM */