2 * Dropbear - a SSH2 server
4 * Copyright (c) 2002,2003 Matt Johnston
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 /* This file (auth.c) handles authentication requests, passing it to the
26 * particular type (auth-passwd, auth-pubkey). */
38 static void authclear();
39 static int checkusername(unsigned char *username
, unsigned int userlen
);
40 static void send_msg_userauth_banner();
42 /* initialise the first time for a session, resetting all parameters */
43 void svr_authinitialise() {
45 ses
.authstate
.failcount
= 0;
46 ses
.authstate
.pw_name
= NULL
;
47 ses
.authstate
.pw_dir
= NULL
;
48 ses
.authstate
.pw_shell
= NULL
;
49 ses
.authstate
.pw_passwd
= NULL
;
54 /* Reset the auth state, but don't reset the failcount. This is for if the
55 * user decides to try with a different username etc, and is also invoked
56 * on initialisation */
57 static void authclear() {
59 memset(&ses
.authstate
, 0, sizeof(ses
.authstate
));
60 #ifdef ENABLE_SVR_PUBKEY_AUTH
61 ses
.authstate
.authtypes
|= AUTH_TYPE_PUBKEY
;
63 #if defined(ENABLE_SVR_PASSWORD_AUTH) || defined(ENABLE_SVR_PAM_AUTH)
64 if (!svr_opts
.noauthpass
) {
65 ses
.authstate
.authtypes
|= AUTH_TYPE_PASSWORD
;
68 if (ses
.authstate
.pw_name
) {
69 m_free(ses
.authstate
.pw_name
);
71 if (ses
.authstate
.pw_shell
) {
72 m_free(ses
.authstate
.pw_shell
);
74 if (ses
.authstate
.pw_dir
) {
75 m_free(ses
.authstate
.pw_dir
);
77 if (ses
.authstate
.pw_passwd
) {
78 m_free(ses
.authstate
.pw_passwd
);
83 /* Send a banner message if specified to the client. The client might
84 * ignore this, but possibly serves as a legal "no trespassing" sign */
85 static void send_msg_userauth_banner() {
87 TRACE(("enter send_msg_userauth_banner"))
88 if (svr_opts
.banner
== NULL
) {
89 TRACE(("leave send_msg_userauth_banner: banner is NULL"))
95 buf_putbyte(ses
.writepayload
, SSH_MSG_USERAUTH_BANNER
);
96 buf_putstring(ses
.writepayload
, buf_getptr(svr_opts
.banner
,
97 svr_opts
.banner
->len
), svr_opts
.banner
->len
);
98 buf_putstring(ses
.writepayload
, "en", 2);
101 buf_free(svr_opts
.banner
);
102 svr_opts
.banner
= NULL
;
104 TRACE(("leave send_msg_userauth_banner"))
107 /* handle a userauth request, check validity, pass to password or pubkey
108 * checking, and handle success or failure */
109 void recv_msg_userauth_request() {
111 unsigned char *username
= NULL
, *servicename
= NULL
, *methodname
= NULL
;
112 unsigned int userlen
, servicelen
, methodlen
;
114 TRACE(("enter recv_msg_userauth_request"))
116 /* ignore packets if auth is already done */
117 if (ses
.authstate
.authdone
== 1) {
118 TRACE(("leave recv_msg_userauth_request: authdone already"))
122 /* send the banner if it exists, it will only exist once */
123 if (svr_opts
.banner
) {
124 send_msg_userauth_banner();
128 username
= buf_getstring(ses
.payload
, &userlen
);
129 servicename
= buf_getstring(ses
.payload
, &servicelen
);
130 methodname
= buf_getstring(ses
.payload
, &methodlen
);
132 /* only handle 'ssh-connection' currently */
133 if (servicelen
!= SSH_SERVICE_CONNECTION_LEN
134 && (strncmp(servicename
, SSH_SERVICE_CONNECTION
,
135 SSH_SERVICE_CONNECTION_LEN
) != 0)) {
137 /* TODO - disconnect here */
141 dropbear_exit("unknown service in auth");
144 /* user wants to know what methods are supported */
145 if (methodlen
== AUTH_METHOD_NONE_LEN
&&
146 strncmp(methodname
, AUTH_METHOD_NONE
,
147 AUTH_METHOD_NONE_LEN
) == 0) {
148 TRACE(("recv_msg_userauth_request: 'none' request"))
149 send_msg_userauth_failure(0, 0);
153 /* check username is good before continuing */
154 if (checkusername(username
, userlen
) == DROPBEAR_FAILURE
) {
155 /* username is invalid/no shell/etc - send failure */
156 TRACE(("sending checkusername failure"))
157 send_msg_userauth_failure(0, 1);
161 #ifdef ENABLE_SVR_PASSWORD_AUTH
162 if (!svr_opts
.noauthpass
&&
163 !(svr_opts
.norootpass
&& ses
.authstate
.pw_uid
== 0) ) {
164 /* user wants to try password auth */
165 if (methodlen
== AUTH_METHOD_PASSWORD_LEN
&&
166 strncmp(methodname
, AUTH_METHOD_PASSWORD
,
167 AUTH_METHOD_PASSWORD_LEN
) == 0) {
174 #ifdef ENABLE_SVR_PAM_AUTH
175 if (!svr_opts
.noauthpass
&&
176 !(svr_opts
.norootpass
&& ses
.authstate
.pw_uid
== 0) ) {
177 /* user wants to try password auth */
178 if (methodlen
== AUTH_METHOD_PASSWORD_LEN
&&
179 strncmp(methodname
, AUTH_METHOD_PASSWORD
,
180 AUTH_METHOD_PASSWORD_LEN
) == 0) {
187 #ifdef ENABLE_SVR_PUBKEY_AUTH
188 /* user wants to try pubkey auth */
189 if (methodlen
== AUTH_METHOD_PUBKEY_LEN
&&
190 strncmp(methodname
, AUTH_METHOD_PUBKEY
,
191 AUTH_METHOD_PUBKEY_LEN
) == 0) {
197 /* nothing matched, we just fail */
198 send_msg_userauth_failure(0, 1);
208 /* Check that the username exists, has a non-empty password, and has a valid
210 * returns DROPBEAR_SUCCESS on valid username, DROPBEAR_FAILURE on failure */
211 static int checkusername(unsigned char *username
, unsigned int userlen
) {
213 char* listshell
= NULL
;
214 char* usershell
= NULL
;
215 TRACE(("enter checkusername"))
216 if (userlen
> MAX_USERNAME_LEN
) {
217 return DROPBEAR_FAILURE
;
220 /* new user or username has changed */
221 if (ses
.authstate
.username
== NULL
||
222 strcmp(username
, ses
.authstate
.username
) != 0) {
223 /* the username needs resetting */
224 if (ses
.authstate
.username
!= NULL
) {
225 dropbear_log(LOG_WARNING
, "Client trying multiple usernames from %s",
227 m_free(ses
.authstate
.username
);
230 fill_passwd(username
);
231 ses
.authstate
.username
= m_strdup(username
);
234 /* check that user exists */
235 if (!ses
.authstate
.pw_name
) {
236 TRACE(("leave checkusername: user '%s' doesn't exist", username
))
237 dropbear_log(LOG_WARNING
,
238 "Login attempt for nonexistent user from %s",
240 send_msg_userauth_failure(0, 1);
241 return DROPBEAR_FAILURE
;
244 /* check for non-root if desired */
245 if (svr_opts
.norootlogin
&& ses
.authstate
.pw_uid
== 0) {
246 TRACE(("leave checkusername: root login disabled"))
247 dropbear_log(LOG_WARNING
, "root login rejected");
248 send_msg_userauth_failure(0, 1);
249 return DROPBEAR_FAILURE
;
252 TRACE(("shell is %s", ses
.authstate
.pw_shell
))
254 /* check that the shell is set */
255 usershell
= ses
.authstate
.pw_shell
;
256 if (usershell
[0] == '\0') {
257 /* empty shell in /etc/passwd means /bin/sh according to passwd(5) */
258 usershell
= "/bin/sh";
261 /* check the shell is valid. If /etc/shells doesn't exist, getusershell()
262 * should return some standard shells like "/bin/sh" and "/bin/csh" (this
263 * is platform-specific) */
265 while ((listshell
= getusershell()) != NULL
) {
266 TRACE(("test shell is '%s'", listshell
))
267 if (strcmp(listshell
, usershell
) == 0) {
272 /* no matching shell */
274 TRACE(("no matching shell"))
275 dropbear_log(LOG_WARNING
, "User '%s' has invalid shell, rejected",
276 ses
.authstate
.pw_name
);
277 send_msg_userauth_failure(0, 1);
278 return DROPBEAR_FAILURE
;
282 TRACE(("matching shell"))
284 TRACE(("uid = %d", ses
.authstate
.pw_uid
))
285 TRACE(("leave checkusername"))
286 return DROPBEAR_SUCCESS
;
290 /* Send a failure message to the client, in responds to a userauth_request.
291 * Partial indicates whether to set the "partial success" flag,
292 * incrfail is whether to count this failure in the failure count (which
293 * is limited. This function also handles disconnection after too many
295 void send_msg_userauth_failure(int partial
, int incrfail
) {
297 buffer
*typebuf
= NULL
;
299 TRACE(("enter send_msg_userauth_failure"))
303 buf_putbyte(ses
.writepayload
, SSH_MSG_USERAUTH_FAILURE
);
305 /* put a list of allowed types */
306 typebuf
= buf_new(30); /* long enough for PUBKEY and PASSWORD */
308 if (ses
.authstate
.authtypes
& AUTH_TYPE_PUBKEY
) {
309 buf_putbytes(typebuf
, AUTH_METHOD_PUBKEY
, AUTH_METHOD_PUBKEY_LEN
);
310 if (ses
.authstate
.authtypes
& AUTH_TYPE_PASSWORD
) {
311 buf_putbyte(typebuf
, ',');
315 if (ses
.authstate
.authtypes
& AUTH_TYPE_PASSWORD
) {
316 buf_putbytes(typebuf
, AUTH_METHOD_PASSWORD
, AUTH_METHOD_PASSWORD_LEN
);
319 buf_setpos(typebuf
, 0);
320 buf_putstring(ses
.writepayload
, buf_getptr(typebuf
, typebuf
->len
),
323 TRACE(("auth fail: methods %d, '%s'", ses
.authstate
.authtypes
,
324 buf_getptr(typebuf
, typebuf
->len
)));
328 buf_putbyte(ses
.writepayload
, partial
? 1 : 0);
333 genrandom((unsigned char*)&delay
, sizeof(delay
));
334 /* We delay for 300ms +- 50ms, 0.1ms granularity */
335 delay
= 250000 + (delay
% 1000)*100;
337 ses
.authstate
.failcount
++;
340 if (ses
.authstate
.failcount
>= MAX_AUTH_TRIES
) {
342 /* XXX - send disconnect ? */
343 TRACE(("Max auth tries reached, exiting"))
345 if (ses
.authstate
.pw_name
== NULL
) {
346 userstr
= "is invalid";
348 userstr
= ses
.authstate
.pw_name
;
350 dropbear_exit("Max auth tries reached - user '%s' from %s",
351 userstr
, svr_ses
.addrstring
);
354 TRACE(("leave send_msg_userauth_failure"))
357 /* Send a success message to the user, and set the "authdone" flag */
358 void send_msg_userauth_success() {
360 TRACE(("enter send_msg_userauth_success"))
364 buf_putbyte(ses
.writepayload
, SSH_MSG_USERAUTH_SUCCESS
);
367 /* authdone must be set after encrypt_packet() for
368 * delayed-zlib mode */
369 ses
.authstate
.authdone
= 1;
370 ses
.connect_time
= 0;
373 if (ses
.authstate
.pw_uid
== 0) {
374 ses
.allowprivport
= 1;
377 /* Remove from the list of pre-auth sockets. Should be m_close(), since if
378 * we fail, we might end up leaking connection slots, and disallow new
379 * logins - a nasty situation. */
380 m_close(svr_ses
.childpipe
);
382 TRACE(("leave send_msg_userauth_success"))