Debian Codegraph @

ftpd/auth.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 <sys/types.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <pwd.h>
27 #ifdef HAVE_CRYPT_H
28 # include <crypt.h>
29 #endif
30 #ifdef HAVE_SHADOW_H
31 # include <shadow.h>
32 #endif
33 #include <sys/time.h>
34 #include <time.h>
35
36 #include "extern.h"
37
38
39 /* If name is "ftp" or "anonymous", the name is not in
40 PATH_FTPUSERS, and ftp account exists, set cred, then just return.
41 If account doesn't exist, ask for passwd anyway. Otherwise, check user
42 requesting login privileges. Disallow anyone who does not have a standard
43 shell as returned by getusershell(). Disallow anyone mentioned in the file
44 PATH_FTPUSERS to allow people such as root and uucp to be avoided. */
45
46 int
47 auth_user (const char *name, struct credentials *pcred)
48 {
49 int err = 0; /* Never remove initialisation! */
50
51 pcred->guest = 0;
52 pcred->expired = AUTH_EXPIRED_NOT;
53
54 switch (pcred->auth_type)
55 {
56 #ifdef WITH_LINUX_PAM
57 case AUTH_TYPE_PAM:
58 err = pam_user (name, pcred);
59 break;
60 #endif
61 #ifdef WITH_KERBEROS
62 case AUTH_TYPE_KERBEROS:
63 err = -1;
64 break;
65 #endif
66 #ifdef WITH_KERBEROS5
67 case AUTH_TYPE_KERBEROS5:
68 err = -1;
69 break;
70 #endif
71 #ifdef WITH_OPIE
72 case AUTH_TYPE_OPIE:
73 err = -1;
74 break;
75 #endif
76 case AUTH_TYPE_PASSWD:
77 default:
78 {
79 size_t len;
80 free (pcred->message);
81 len = (size_t) (64 + strlen (name));
82 pcred->message = malloc (len);
83 if (pcred->message == NULL)
84 return -1;
85
86 /* Check for anonymous log in.
87 *
88 * This code simulates part of `pam_ftp.so'
89 * for PAM variants that are not Linux-PAM,
90 * in addition to perform the original
91 * default authentication checks.
92 */
93 if (strcmp (name, "ftp") == 0 || strcmp (name, "anonymous") == 0)
94 {
95 if (checkuser (PATH_FTPUSERS, "ftp")
96 || checkuser (PATH_FTPUSERS, "anonymous"))
97 {
98 snprintf (pcred->message, len, "User %s access denied.",
99 name);
100 err = 1;
101 }
102 else if (sgetcred ("ftp", pcred) == 0)
103 {
104 pcred->guest = 1;
105 strcpy (pcred->message,
106 "Guest login ok, type your name as password.");
107 }
108 else
109 {
110 snprintf (pcred->message, len, "User %s unknown.", name);
111 err = 1;
112 }
113 return err;
114 }
115
116 if (sgetcred (name, pcred) == 0)
117 {
118 const char *cp;
119 const char *shell;
120
121 /* Check if the shell is allowed */
122 shell = pcred->shell;
123 if (shell == NULL || *shell == 0)
124 shell = PATH_BSHELL;
125 setusershell ();
126 while ((cp = getusershell ()) != NULL)
127 if (strcmp (cp, shell) == 0)
128 break;
129 endusershell ();
130
131 if (cp == NULL || checkuser (PATH_FTPUSERS, name))
132 {
133 sprintf (pcred->message, "User %s access denied.", name);
134 return 1;
135 }
136 }
137 else
138 {
139 free (pcred->message);
140 pcred->message = NULL;
141 return 1;
142 }
143 snprintf (pcred->message, len,
144 "Password required for %s.", pcred->name);
145 err = 0;
146 }
147 }
148
149 if (err == 0)
150 {
151 pcred->dochroot = checkuser (PATH_FTPCHROOT, pcred->name);
152
153 #if defined WITH_PAM && !defined WITH_LINUX_PAM
154 if (pcred->auth_type == AUTH_TYPE_PAM)
155 err = pam_user (name, pcred);
156 #endif /* WITH_PAM && !WITH_LINUX_PAM */
157 }
158
159 return err;
160 }
161
162 int
163 auth_pass (const char *passwd, struct credentials *pcred)
164 {
165 switch (pcred->auth_type)
166 {
167 #ifdef WITH_PAM
168 case AUTH_TYPE_PAM:
169 return pam_pass (passwd, pcred);
170 #endif
171 #ifdef WITH_KERBEROS
172 case AUTH_TYPE_KERBEROS:
173 return -1;
174 #endif
175 #ifdef WITH_KERBEROS5
176 case AUTH_TYPE_KERBEROS5:
177 return -1;
178 #endif
179 #ifdef WITH_OPIE
180 case AUTH_TYPE_OPIE:
181 return -1;
182 #endif
183 case AUTH_TYPE_PASSWD:
184 default:
185 {
186 char *xpasswd;
187 char *salt = pcred->passwd;
188 /* Try to authenticate the user. */
189 if (pcred->passwd == NULL || *pcred->passwd == '\0')
190 return 1; /* Failed. */
191 xpasswd = crypt (passwd, salt);
192 return (!xpasswd || strcmp (xpasswd, pcred->passwd) != 0);
193 }
194 } /* switch (auth_type) */
195 return -1;
196 }
197
198 int
199 sgetcred (const char *name, struct credentials *pcred)
200 {
201 struct passwd *p;
202
203 p = getpwnam (name);
204 if (p == NULL)
205 return 1;
206
207 free (pcred->name);
208 free (pcred->passwd);
209 free (pcred->homedir);
210 free (pcred->rootdir);
211 free (pcred->shell);
212
213 #if defined HAVE_GETSPNAM && defined HAVE_SHADOW_H
214 if (p->pw_passwd == NULL || strlen (p->pw_passwd) == 1)
215 {
216 struct spwd *spw;
217
218 setspent ();
219 spw = getspnam (p->pw_name);
220 if (spw != NULL)
221 {
222 time_t now;
223 long today;
224 now = time ((time_t *) 0);
225 today = now / (60 * 60 * 24);
226
227 if (spw->sp_expire > 0 && spw->sp_expire < today)
228 {
229 p->pw_passwd = NULL;
230 pcred->expired |= AUTH_EXPIRED_ACCT;
231 }
232 if (spw->sp_max > 0 && spw->sp_lstchg > 0
233 && (spw->sp_lstchg + spw->sp_max < today))
234 {
235 p->pw_passwd = NULL;
236 pcred->expired |= AUTH_EXPIRED_PASS;
237 }
238
239 if (pcred->expired == AUTH_EXPIRED_NOT)
240 p->pw_passwd = spw->sp_pwdp;
241 }
242 endspent ();
243 }
244 #elif defined HAVE_STRUCT_PASSWD_PW_EXPIRE /* !HAVE_SHADOW_H */
245 /* BSD systems provide pw_expire as epoch time,
246 * and the password is exposed in pw_passwd for
247 * a caller with euid 0.
248 *
249 * NetBSD allows -1 for 'pw_change', meaning that immediate
250 * change is required. Let us deny access in that case..
251 */
252 if (p->pw_expire > 0
253 # ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
254 || p->pw_change
255 # endif
256 )
257 {
258 time_t now = time ((time_t *) 0);
259
260 if (p->pw_expire > 0 && difftime (p->pw_expire, now) < 0)
261 {
262 p->pw_passwd = NULL;
263 pcred->expired |= AUTH_EXPIRED_ACCT;
264 }
265 # ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
266 if (p->pw_change && difftime (p->pw_change, now) < 0)
267 {
268 p->pw_passwd = NULL;
269 pcred->expired |= AUTH_EXPIRED_PASS;
270 }
271 # endif
272 }
273 #endif /* !HAVE_STRUCT_PASSWD_PW_EXPIRE */
274
275 pcred->uid = p->pw_uid;
276 pcred->gid = p->pw_gid;
277 pcred->name = sgetsave (p->pw_name);
278 pcred->passwd = sgetsave (p->pw_passwd);
279 pcred->rootdir = sgetsave (p->pw_dir);
280 pcred->homedir = sgetsave ("/");
281 pcred->shell = sgetsave (p->pw_shell);
282
283 return 0;
284 }