summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Lindstrom <mouring@eviladmin.org>2001-04-08 18:26:59 +0000
committerBen Lindstrom <mouring@eviladmin.org>2001-04-08 18:26:59 +0000
commit3fcf1a22b52ed8e66d2cf548d5d4528f67377af9 (patch)
treeb635cae14328f373bb682395ab066ee819dd77c4
parentd344763643339b5ddc075cf7b8c8fb4404dc0aa0 (diff)
- markus@cvs.openbsd.org 2001/04/06 21:00:17
[auth-rh-rsa.c auth-rhosts.c auth-rsa.c auth2.c channels.c session.c ssh.c sshconnect.c sshconnect.h uidswap.c uidswap.h] do gid/groups-swap in addition to uid-swap, should help if /home/group is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks to olar@openwall.com is comments. we had many requests for this.
-rw-r--r--ChangeLog8
-rw-r--r--auth-rh-rsa.c4
-rw-r--r--auth-rhosts.c6
-rw-r--r--auth-rsa.c4
-rw-r--r--auth2.c4
-rw-r--r--channels.c4
-rw-r--r--session.c8
-rw-r--r--ssh.c33
-rw-r--r--sshconnect.c25
-rw-r--r--sshconnect.h6
-rw-r--r--uidswap.c101
-rw-r--r--uidswap.h6
12 files changed, 118 insertions, 91 deletions
diff --git a/ChangeLog b/ChangeLog
index 64b4678cc..943f146b6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -9,6 +9,12 @@
9 - markus@cvs.openbsd.org 2001/04/08 11:27:33 9 - markus@cvs.openbsd.org 2001/04/08 11:27:33
10 [clientloop.c] 10 [clientloop.c]
11 leave_raw_mode if ssh2 "session" is closed 11 leave_raw_mode if ssh2 "session" is closed
12 - markus@cvs.openbsd.org 2001/04/06 21:00:17
13 [auth-rh-rsa.c auth-rhosts.c auth-rsa.c auth2.c channels.c session.c
14 ssh.c sshconnect.c sshconnect.h uidswap.c uidswap.h]
15 do gid/groups-swap in addition to uid-swap, should help if /home/group
16 is chmod 750 + chgrp grp /home/group/, work be deraadt and me, thanks
17 to olar@openwall.com is comments. we had many requests for this.
12 18
1320010408 1920010408
14 - OpenBSD CVS Sync 20 - OpenBSD CVS Sync
@@ -4949,4 +4955,4 @@
4949 - Wrote replacements for strlcpy and mkdtemp 4955 - Wrote replacements for strlcpy and mkdtemp
4950 - Released 1.0pre1 4956 - Released 1.0pre1
4951 4957
4952$Id: ChangeLog,v 1.1082 2001/04/08 18:07:22 mouring Exp $ 4958$Id: ChangeLog,v 1.1083 2001/04/08 18:26:59 mouring Exp $
diff --git a/auth-rh-rsa.c b/auth-rh-rsa.c
index 0edbdb5f3..506a5a239 100644
--- a/auth-rh-rsa.c
+++ b/auth-rh-rsa.c
@@ -13,7 +13,7 @@
13 */ 13 */
14 14
15#include "includes.h" 15#include "includes.h"
16RCSID("$OpenBSD: auth-rh-rsa.c,v 1.22 2001/02/03 10:08:36 markus Exp $"); 16RCSID("$OpenBSD: auth-rh-rsa.c,v 1.23 2001/04/06 21:00:04 markus Exp $");
17 17
18#include "packet.h" 18#include "packet.h"
19#include "xmalloc.h" 19#include "xmalloc.h"
@@ -80,7 +80,7 @@ auth_rhosts_rsa(struct passwd *pw, const char *client_user, RSA *client_host_key
80 pw->pw_name, user_hostfile); 80 pw->pw_name, user_hostfile);
81 } else { 81 } else {
82 /* XXX race between stat and the following open() */ 82 /* XXX race between stat and the following open() */
83 temporarily_use_uid(pw->pw_uid); 83 temporarily_use_uid(pw);
84 host_status = check_host_in_hostfile(user_hostfile, canonical_hostname, 84 host_status = check_host_in_hostfile(user_hostfile, canonical_hostname,
85 client_key, found, NULL); 85 client_key, found, NULL);
86 restore_uid(); 86 restore_uid();
diff --git a/auth-rhosts.c b/auth-rhosts.c
index 9b018c9d9..c71e9b55d 100644
--- a/auth-rhosts.c
+++ b/auth-rhosts.c
@@ -14,7 +14,7 @@
14 */ 14 */
15 15
16#include "includes.h" 16#include "includes.h"
17RCSID("$OpenBSD: auth-rhosts.c,v 1.21 2001/02/08 19:30:51 itojun Exp $"); 17RCSID("$OpenBSD: auth-rhosts.c,v 1.22 2001/04/06 21:00:06 markus Exp $");
18 18
19#include "packet.h" 19#include "packet.h"
20#include "xmalloc.h" 20#include "xmalloc.h"
@@ -161,7 +161,7 @@ auth_rhosts(struct passwd *pw, const char *client_user)
161 if (pw == NULL) 161 if (pw == NULL)
162 return 0; 162 return 0;
163 /* Switch to the user's uid. */ 163 /* Switch to the user's uid. */
164 temporarily_use_uid(pw->pw_uid); 164 temporarily_use_uid(pw);
165 /* 165 /*
166 * Quick check: if the user has no .shosts or .rhosts files, return 166 * Quick check: if the user has no .shosts or .rhosts files, return
167 * failure immediately without doing costly lookups from name 167 * failure immediately without doing costly lookups from name
@@ -223,7 +223,7 @@ auth_rhosts(struct passwd *pw, const char *client_user)
223 return 0; 223 return 0;
224 } 224 }
225 /* Temporarily use the user's uid. */ 225 /* Temporarily use the user's uid. */
226 temporarily_use_uid(pw->pw_uid); 226 temporarily_use_uid(pw);
227 227
228 /* Check all .rhosts files (currently .shosts and .rhosts). */ 228 /* Check all .rhosts files (currently .shosts and .rhosts). */
229 for (rhosts_file_index = 0; rhosts_files[rhosts_file_index]; 229 for (rhosts_file_index = 0; rhosts_files[rhosts_file_index];
diff --git a/auth-rsa.c b/auth-rsa.c
index 30e5a54c4..59bee18bd 100644
--- a/auth-rsa.c
+++ b/auth-rsa.c
@@ -14,7 +14,7 @@
14 */ 14 */
15 15
16#include "includes.h" 16#include "includes.h"
17RCSID("$OpenBSD: auth-rsa.c,v 1.39 2001/03/01 02:45:10 deraadt Exp $"); 17RCSID("$OpenBSD: auth-rsa.c,v 1.40 2001/04/06 21:00:07 markus Exp $");
18 18
19#include <openssl/rsa.h> 19#include <openssl/rsa.h>
20#include <openssl/md5.h> 20#include <openssl/md5.h>
@@ -135,7 +135,7 @@ auth_rsa(struct passwd *pw, BIGNUM *client_n)
135 return 0; 135 return 0;
136 136
137 /* Temporarily use the user's uid. */ 137 /* Temporarily use the user's uid. */
138 temporarily_use_uid(pw->pw_uid); 138 temporarily_use_uid(pw);
139 139
140 /* The authorized keys. */ 140 /* The authorized keys. */
141 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir, 141 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
diff --git a/auth2.c b/auth2.c
index 8c9eb17a9..d676270b7 100644
--- a/auth2.c
+++ b/auth2.c
@@ -23,7 +23,7 @@
23 */ 23 */
24 24
25#include "includes.h" 25#include "includes.h"
26RCSID("$OpenBSD: auth2.c,v 1.50 2001/04/04 20:32:56 markus Exp $"); 26RCSID("$OpenBSD: auth2.c,v 1.51 2001/04/06 21:00:08 markus Exp $");
27 27
28#include <openssl/evp.h> 28#include <openssl/evp.h>
29 29
@@ -590,7 +590,7 @@ user_key_allowed(struct passwd *pw, Key *key)
590 return 0; 590 return 0;
591 591
592 /* Temporarily use the user's uid. */ 592 /* Temporarily use the user's uid. */
593 temporarily_use_uid(pw->pw_uid); 593 temporarily_use_uid(pw);
594 594
595 /* The authorized keys. */ 595 /* The authorized keys. */
596 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir, 596 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
diff --git a/channels.c b/channels.c
index 6ca31b840..d5526fb83 100644
--- a/channels.c
+++ b/channels.c
@@ -40,7 +40,7 @@
40 */ 40 */
41 41
42#include "includes.h" 42#include "includes.h"
43RCSID("$OpenBSD: channels.c,v 1.101 2001/04/05 10:42:48 markus Exp $"); 43RCSID("$OpenBSD: channels.c,v 1.102 2001/04/06 21:00:10 markus Exp $");
44 44
45#include <openssl/rsa.h> 45#include <openssl/rsa.h>
46#include <openssl/dsa.h> 46#include <openssl/dsa.h>
@@ -2410,7 +2410,7 @@ auth_input_request_forwarding(struct passwd * pw)
2410 fatal("Protocol error: authentication forwarding requested twice."); 2410 fatal("Protocol error: authentication forwarding requested twice.");
2411 2411
2412 /* Temporarily drop privileged uid for mkdir/bind. */ 2412 /* Temporarily drop privileged uid for mkdir/bind. */
2413 temporarily_use_uid(pw->pw_uid); 2413 temporarily_use_uid(pw);
2414 2414
2415 /* Allocate a buffer for the socket name, and format the name. */ 2415 /* Allocate a buffer for the socket name, and format the name. */
2416 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME); 2416 channel_forwarded_auth_socket_name = xmalloc(MAX_SOCKET_NAME);
diff --git a/session.c b/session.c
index 5264ac182..8da799335 100644
--- a/session.c
+++ b/session.c
@@ -33,7 +33,7 @@
33 */ 33 */
34 34
35#include "includes.h" 35#include "includes.h"
36RCSID("$OpenBSD: session.c,v 1.70 2001/04/05 15:48:18 stevesk Exp $"); 36RCSID("$OpenBSD: session.c,v 1.71 2001/04/06 21:00:12 markus Exp $");
37 37
38#include "ssh.h" 38#include "ssh.h"
39#include "ssh1.h" 39#include "ssh1.h"
@@ -365,7 +365,7 @@ do_authenticated1(Authctxt *authctxt)
365 /* Setup to always have a local .Xauthority. */ 365 /* Setup to always have a local .Xauthority. */
366 xauthfile = xmalloc(MAXPATHLEN); 366 xauthfile = xmalloc(MAXPATHLEN);
367 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN); 367 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
368 temporarily_use_uid(s->pw->pw_uid); 368 temporarily_use_uid(s->pw);
369 if (mkdtemp(xauthfile) == NULL) { 369 if (mkdtemp(xauthfile) == NULL) {
370 restore_uid(); 370 restore_uid();
371 error("private X11 dir: mkdtemp %s failed: %s", 371 error("private X11 dir: mkdtemp %s failed: %s",
@@ -1152,7 +1152,7 @@ do_child(Session *s, const char *command)
1152#endif 1152#endif
1153 1153
1154 /* Permanently switch to the desired uid. */ 1154 /* Permanently switch to the desired uid. */
1155 permanently_set_uid(pw->pw_uid); 1155 permanently_set_uid(pw);
1156# endif /* HAVE_LOGIN_CAP */ 1156# endif /* HAVE_LOGIN_CAP */
1157 } 1157 }
1158#endif /* HAVE_OSF_SIA */ 1158#endif /* HAVE_OSF_SIA */
@@ -1710,7 +1710,7 @@ session_x11_req(Session *s)
1710 } 1710 }
1711 xauthfile = xmalloc(MAXPATHLEN); 1711 xauthfile = xmalloc(MAXPATHLEN);
1712 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN); 1712 strlcpy(xauthfile, "/tmp/ssh-XXXXXXXX", MAXPATHLEN);
1713 temporarily_use_uid(s->pw->pw_uid); 1713 temporarily_use_uid(s->pw);
1714 if (mkdtemp(xauthfile) == NULL) { 1714 if (mkdtemp(xauthfile) == NULL) {
1715 restore_uid(); 1715 restore_uid();
1716 error("private X11 dir: mkdtemp %s failed: %s", 1716 error("private X11 dir: mkdtemp %s failed: %s",
diff --git a/ssh.c b/ssh.c
index 278e7eda4..294bcf39d 100644
--- a/ssh.c
+++ b/ssh.c
@@ -39,7 +39,7 @@
39 */ 39 */
40 40
41#include "includes.h" 41#include "includes.h"
42RCSID("$OpenBSD: ssh.c,v 1.106 2001/04/05 21:05:24 markus Exp $"); 42RCSID("$OpenBSD: ssh.c,v 1.107 2001/04/06 21:00:13 markus Exp $");
43 43
44#include <openssl/evp.h> 44#include <openssl/evp.h>
45#include <openssl/err.h> 45#include <openssl/err.h>
@@ -265,6 +265,15 @@ main(int ac, char **av)
265 fatal("setrlimit failed: %.100s", strerror(errno)); 265 fatal("setrlimit failed: %.100s", strerror(errno));
266 } 266 }
267#endif 267#endif
268 /* Get user data. */
269 pw = getpwuid(original_real_uid);
270 if (!pw) {
271 log("You don't exist, go away!");
272 exit(1);
273 }
274 /* Take a copy of the returned structure. */
275 pw = pwcopy(pw);
276
268 /* 277 /*
269 * Use uid-swapping to give up root privileges for the duration of 278 * Use uid-swapping to give up root privileges for the duration of
270 * option processing. We will re-instantiate the rights when we are 279 * option processing. We will re-instantiate the rights when we are
@@ -272,7 +281,7 @@ main(int ac, char **av)
272 * them when the port has been created (actually, when the connection 281 * them when the port has been created (actually, when the connection
273 * has been made, as we may need to create the port several times). 282 * has been made, as we may need to create the port several times).
274 */ 283 */
275 temporarily_use_uid(original_real_uid); 284 temporarily_use_uid(pw);
276 285
277 /* 286 /*
278 * Set our umask to something reasonable, as some files are created 287 * Set our umask to something reasonable, as some files are created
@@ -545,15 +554,6 @@ main(int ac, char **av)
545 tty_flag = 0; 554 tty_flag = 0;
546 } 555 }
547 556
548 /* Get user data. */
549 pw = getpwuid(original_real_uid);
550 if (!pw) {
551 log("You don't exist, go away!");
552 exit(1);
553 }
554 /* Take a copy of the returned structure. */
555 pw = pwcopy(pw);
556
557 /* 557 /*
558 * Initialize "log" output. Since we are the client all output 558 * Initialize "log" output. Since we are the client all output
559 * actually goes to stderr. 559 * actually goes to stderr.
@@ -592,6 +592,7 @@ main(int ac, char **av)
592 "originating port will not be trusted."); 592 "originating port will not be trusted.");
593 options.rhosts_authentication = 0; 593 options.rhosts_authentication = 0;
594 } 594 }
595
595 /* 596 /*
596 * If using rsh has been selected, exec it now (without trying 597 * If using rsh has been selected, exec it now (without trying
597 * anything else). Note that we must release privileges first. 598 * anything else). Note that we must release privileges first.
@@ -604,7 +605,7 @@ main(int ac, char **av)
604 restore_uid(); 605 restore_uid();
605 606
606 /* Switch to the original uid permanently. */ 607 /* Switch to the original uid permanently. */
607 permanently_set_uid(original_real_uid); 608 permanently_set_uid(pw);
608 609
609 /* Execute rsh. */ 610 /* Execute rsh. */
610 rsh_connect(host, options.user, &command); 611 rsh_connect(host, options.user, &command);
@@ -618,8 +619,7 @@ main(int ac, char **av)
618 ok = ssh_connect(host, &hostaddr, options.port, 619 ok = ssh_connect(host, &hostaddr, options.port,
619 options.connection_attempts, 620 options.connection_attempts,
620 original_effective_uid != 0 || !options.use_privileged_port, 621 original_effective_uid != 0 || !options.use_privileged_port,
621 original_real_uid, 622 pw, options.proxy_command);
622 options.proxy_command);
623 623
624 /* 624 /*
625 * If we successfully made the connection, load the host private key 625 * If we successfully made the connection, load the host private key
@@ -646,7 +646,7 @@ main(int ac, char **av)
646 * process, read the private hostkey and impersonate the host. 646 * process, read the private hostkey and impersonate the host.
647 * OpenBSD does not allow ptracing of setuid processes. 647 * OpenBSD does not allow ptracing of setuid processes.
648 */ 648 */
649 permanently_set_uid(original_real_uid); 649 permanently_set_uid(pw);
650 650
651 /* 651 /*
652 * Now that we are back to our own permissions, create ~/.ssh 652 * Now that we are back to our own permissions, create ~/.ssh
@@ -688,8 +688,7 @@ main(int ac, char **av)
688 tilde_expand_filename(options.user_hostfile2, original_real_uid); 688 tilde_expand_filename(options.user_hostfile2, original_real_uid);
689 689
690 /* Log into the remote system. This never returns if the login fails. */ 690 /* Log into the remote system. This never returns if the login fails. */
691 ssh_login(host_private_key, host, (struct sockaddr *)&hostaddr, 691 ssh_login(host_private_key, host, (struct sockaddr *)&hostaddr, pw);
692 original_real_uid);
693 692
694 /* We no longer need the host private key. Clear it now. */ 693 /* We no longer need the host private key. Clear it now. */
695 if (host_private_key != NULL) 694 if (host_private_key != NULL)
diff --git a/sshconnect.c b/sshconnect.c
index 754ac1243..85427b44d 100644
--- a/sshconnect.c
+++ b/sshconnect.c
@@ -13,7 +13,7 @@
13 */ 13 */
14 14
15#include "includes.h" 15#include "includes.h"
16RCSID("$OpenBSD: sshconnect.c,v 1.102 2001/04/05 10:42:55 markus Exp $"); 16RCSID("$OpenBSD: sshconnect.c,v 1.103 2001/04/06 21:00:14 markus Exp $");
17 17
18#include <openssl/bn.h> 18#include <openssl/bn.h>
19 19
@@ -45,7 +45,7 @@ extern int IPv4or6;
45 * Connect to the given ssh server using a proxy command. 45 * Connect to the given ssh server using a proxy command.
46 */ 46 */
47int 47int
48ssh_proxy_connect(const char *host, u_short port, uid_t original_real_uid, 48ssh_proxy_connect(const char *host, u_short port, struct passwd *pw,
49 const char *proxy_command) 49 const char *proxy_command)
50{ 50{
51 Buffer command; 51 Buffer command;
@@ -96,7 +96,7 @@ ssh_proxy_connect(const char *host, u_short port, uid_t original_real_uid,
96 char *argv[10]; 96 char *argv[10];
97 97
98 /* Child. Permanently give up superuser privileges. */ 98 /* Child. Permanently give up superuser privileges. */
99 permanently_set_uid(original_real_uid); 99 permanently_set_uid(pw);
100 100
101 /* Redirect stdin and stdout. */ 101 /* Redirect stdin and stdout. */
102 close(pin[1]); 102 close(pin[1]);
@@ -145,7 +145,7 @@ ssh_proxy_connect(const char *host, u_short port, uid_t original_real_uid,
145 * Creates a (possibly privileged) socket for use as the ssh connection. 145 * Creates a (possibly privileged) socket for use as the ssh connection.
146 */ 146 */
147int 147int
148ssh_create_socket(uid_t original_real_uid, int privileged, int family) 148ssh_create_socket(struct passwd *pw, int privileged, int family)
149{ 149{
150 int sock; 150 int sock;
151 151
@@ -165,7 +165,7 @@ ssh_create_socket(uid_t original_real_uid, int privileged, int family)
165 * Just create an ordinary socket on arbitrary port. We use 165 * Just create an ordinary socket on arbitrary port. We use
166 * the user's uid to create the socket. 166 * the user's uid to create the socket.
167 */ 167 */
168 temporarily_use_uid(original_real_uid); 168 temporarily_use_uid(pw);
169 sock = socket(family, SOCK_STREAM, 0); 169 sock = socket(family, SOCK_STREAM, 0);
170 if (sock < 0) 170 if (sock < 0)
171 error("socket: %.100s", strerror(errno)); 171 error("socket: %.100s", strerror(errno));
@@ -188,7 +188,7 @@ ssh_create_socket(uid_t original_real_uid, int privileged, int family)
188int 188int
189ssh_connect(const char *host, struct sockaddr_storage * hostaddr, 189ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
190 u_short port, int connection_attempts, 190 u_short port, int connection_attempts,
191 int anonymous, uid_t original_real_uid, 191 int anonymous, struct passwd *pw,
192 const char *proxy_command) 192 const char *proxy_command)
193{ 193{
194 int gaierr; 194 int gaierr;
@@ -212,7 +212,7 @@ ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
212 } 212 }
213 /* If a proxy command is given, connect using it. */ 213 /* If a proxy command is given, connect using it. */
214 if (proxy_command != NULL) 214 if (proxy_command != NULL)
215 return ssh_proxy_connect(host, port, original_real_uid, proxy_command); 215 return ssh_proxy_connect(host, port, pw, proxy_command);
216 216
217 /* No proxy command. */ 217 /* No proxy command. */
218 218
@@ -248,7 +248,7 @@ ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
248 host, ntop, strport); 248 host, ntop, strport);
249 249
250 /* Create a socket for connecting. */ 250 /* Create a socket for connecting. */
251 sock = ssh_create_socket(original_real_uid, 251 sock = ssh_create_socket(pw,
252#ifdef HAVE_CYGWIN 252#ifdef HAVE_CYGWIN
253 !anonymous, 253 !anonymous,
254#else 254#else
@@ -262,7 +262,7 @@ ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
262 * hope that it will help with tcp_wrappers showing 262 * hope that it will help with tcp_wrappers showing
263 * the remote uid as root. 263 * the remote uid as root.
264 */ 264 */
265 temporarily_use_uid(original_real_uid); 265 temporarily_use_uid(pw);
266 if (connect(sock, ai->ai_addr, ai->ai_addrlen) >= 0) { 266 if (connect(sock, ai->ai_addr, ai->ai_addrlen) >= 0) {
267 /* Successful connection. */ 267 /* Successful connection. */
268 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen); 268 memcpy(hostaddr, ai->ai_addr, ai->ai_addrlen);
@@ -739,16 +739,11 @@ check_host_key(char *host, struct sockaddr *hostaddr, Key *host_key,
739 */ 739 */
740void 740void
741ssh_login(Key *own_host_key, const char *orighost, 741ssh_login(Key *own_host_key, const char *orighost,
742 struct sockaddr *hostaddr, uid_t original_real_uid) 742 struct sockaddr *hostaddr, struct passwd *pw)
743{ 743{
744 struct passwd *pw;
745 char *host, *cp; 744 char *host, *cp;
746 char *server_user, *local_user; 745 char *server_user, *local_user;
747 746
748 /* Get local user name. Use it as server user if no user name was given. */
749 pw = getpwuid(original_real_uid);
750 if (!pw)
751 fatal("User id %u not found from user database.", original_real_uid);
752 local_user = xstrdup(pw->pw_name); 747 local_user = xstrdup(pw->pw_name);
753 server_user = options.user ? options.user : local_user; 748 server_user = options.user ? options.user : local_user;
754 749
diff --git a/sshconnect.h b/sshconnect.h
index 3a71d9c2b..45caf7395 100644
--- a/sshconnect.h
+++ b/sshconnect.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: sshconnect.h,v 1.7 2001/03/26 08:07:09 markus Exp $ */ 1/* $OpenBSD: sshconnect.h,v 1.8 2001/04/06 21:00:15 markus Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2000 Markus Friedl. All rights reserved. 4 * Copyright (c) 2000 Markus Friedl. All rights reserved.
@@ -37,7 +37,7 @@
37int 37int
38ssh_connect(const char *host, struct sockaddr_storage * hostaddr, 38ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
39 u_short port, int connection_attempts, 39 u_short port, int connection_attempts,
40 int anonymous, uid_t original_real_uid, 40 int anonymous, struct passwd *pw,
41 const char *proxy_command); 41 const char *proxy_command);
42 42
43/* 43/*
@@ -51,7 +51,7 @@ ssh_connect(const char *host, struct sockaddr_storage * hostaddr,
51 51
52void 52void
53ssh_login(Key *host_key, const char *host, 53ssh_login(Key *host_key, const char *host,
54 struct sockaddr * hostaddr, uid_t original_real_uid); 54 struct sockaddr * hostaddr, struct passwd *pw);
55 55
56 56
57void 57void
diff --git a/uidswap.c b/uidswap.c
index bdfa484fc..7cfacd4a5 100644
--- a/uidswap.c
+++ b/uidswap.c
@@ -12,7 +12,7 @@
12 */ 12 */
13 13
14#include "includes.h" 14#include "includes.h"
15RCSID("$OpenBSD: uidswap.c,v 1.13 2001/01/21 19:06:01 markus Exp $"); 15RCSID("$OpenBSD: uidswap.c,v 1.14 2001/04/06 21:00:16 markus Exp $");
16 16
17#include "log.h" 17#include "log.h"
18#include "uidswap.h" 18#include "uidswap.h"
@@ -26,57 +26,80 @@ RCSID("$OpenBSD: uidswap.c,v 1.13 2001/01/21 19:06:01 markus Exp $");
26 * POSIX saved uids or not. 26 * POSIX saved uids or not.
27 */ 27 */
28 28
29#if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS)
30/* Lets assume that posix saved ids also work with seteuid, even though that 29/* Lets assume that posix saved ids also work with seteuid, even though that
31 is not part of the posix specification. */ 30 is not part of the posix specification. */
32#define SAVED_IDS_WORK_WITH_SETEUID 31
33/* Saved effective uid. */ 32/* Saved effective uid. */
34static uid_t saved_euid = 0; 33static int privileged = 0;
35#endif 34static int temporarily_use_uid_effective = 0;
36 35static uid_t saved_euid = 0;
36static gid_t saved_egid;
37static gid_t saved_egroups[NGROUPS_MAX], user_groups[NGROUPS_MAX];
38static int saved_egroupslen = -1, user_groupslen = -1;
39
37/* 40/*
38 * Temporarily changes to the given uid. If the effective user 41 * Temporarily changes to the given uid. If the effective user
39 * id is not root, this does nothing. This call cannot be nested. 42 * id is not root, this does nothing. This call cannot be nested.
40 */ 43 */
41void 44void
42temporarily_use_uid(uid_t uid) 45temporarily_use_uid(struct passwd *pw)
43{ 46{
44#ifdef SAVED_IDS_WORK_WITH_SETEUID 47 /* Save the current euid, and egroups. */
45 /* Save the current euid. */ 48 saved_euid = geteuid();
46 saved_euid = geteuid(); 49 debug("temporarily_use_uid: %d/%d (e=%d)",
47 50 pw->pw_uid, pw->pw_gid, saved_euid);
48 /* Set the effective uid to the given (unprivileged) uid. */ 51 if (saved_euid != 0) {
49 if (seteuid(uid) == -1) 52 privileged = 0;
50 debug("seteuid %u: %.100s", (u_int) uid, strerror(errno)); 53 return;
51#else /* SAVED_IDS_WORK_WITH_SETEUID */ 54 }
52 /* Propagate the privileged uid to all of our uids. */ 55 privileged = 1;
53 if (setuid(geteuid()) < 0) 56 temporarily_use_uid_effective = 1;
54 debug("setuid %u: %.100s", (u_int) geteuid(), strerror(errno)); 57 saved_egid = getegid();
55 58 saved_egroupslen = getgroups(NGROUPS_MAX, saved_egroups);
56 /* Set the effective uid to the given (unprivileged) uid. */ 59 if (saved_egroupslen < 0)
57 if (seteuid(uid) == -1) 60 fatal("getgroups: %.100s", strerror(errno));
58 debug("seteuid %u: %.100s", (u_int) uid, strerror(errno)); 61
59#endif /* SAVED_IDS_WORK_WITH_SETEUID */ 62 /* set and save the user's groups */
63 if (user_groupslen == -1) {
64 if (initgroups(pw->pw_name, pw->pw_gid) < 0)
65 fatal("initgroups: %s: %.100s", pw->pw_name,
66 strerror(errno));
67 user_groupslen = getgroups(NGROUPS_MAX, user_groups);
68 if (user_groupslen < 0)
69 fatal("getgroups: %.100s", strerror(errno));
70 }
71 /* Set the effective uid to the given (unprivileged) uid. */
72 if (setgroups(user_groupslen, user_groups) < 0)
73 fatal("setgroups: %.100s", strerror(errno));
74 pw->pw_gid = pw->pw_gid;
75 if (setegid(pw->pw_gid) < 0)
76 fatal("setegid %u: %.100s", (u_int) pw->pw_gid,
77 strerror(errno));
78 if (seteuid(pw->pw_uid) == -1)
79 fatal("seteuid %u: %.100s", (u_int) pw->pw_uid,
80 strerror(errno));
60} 81}
61 82
62/* 83/*
63 * Restores to the original uid. 84 * Restores to the original uid.
64 */ 85 */
65void 86void
66restore_uid(void) 87restore_uid(void)
67{ 88{
68#ifdef SAVED_IDS_WORK_WITH_SETEUID 89 debug("restore_uid");
90 /* it's a no-op unless privileged */
91 if (!privileged)
92 return;
93 if (!temporarily_use_uid_effective)
94 fatal("restore_uid: temporarily_use_uid not effective");
69 /* Set the effective uid back to the saved uid. */ 95 /* Set the effective uid back to the saved uid. */
70 if (seteuid(saved_euid) < 0) 96 if (seteuid(saved_euid) < 0)
71 debug("seteuid %u: %.100s", (u_int) saved_euid, strerror(errno)); 97 fatal("seteuid %u: %.100s", (u_int) saved_euid, strerror(errno));
72#else /* SAVED_IDS_WORK_WITH_SETEUID */ 98 if (setgroups(saved_egroupslen, saved_egroups) < 0)
73 /* 99 fatal("setgroups: %.100s", strerror(errno));
74 * We are unable to restore the real uid to its unprivileged value. 100 if (setegid(saved_egid) < 0)
75 * Propagate the real uid (usually more privileged) to effective uid 101 fatal("setegid %u: %.100s", (u_int) saved_egid, strerror(errno));
76 * as well. 102 temporarily_use_uid_effective = 0;
77 */
78 setuid(getuid());
79#endif /* SAVED_IDS_WORK_WITH_SETEUID */
80} 103}
81 104
82/* 105/*
@@ -84,8 +107,12 @@ restore_uid(void)
84 * called while temporarily_use_uid is effective. 107 * called while temporarily_use_uid is effective.
85 */ 108 */
86void 109void
87permanently_set_uid(uid_t uid) 110permanently_set_uid(struct passwd *pw)
88{ 111{
89 if (setuid(uid) < 0) 112 if (temporarily_use_uid_effective)
90 debug("setuid %u: %.100s", (u_int) uid, strerror(errno)); 113 fatal("restore_uid: temporarily_use_uid effective");
114 if (setuid(pw->pw_uid) < 0)
115 fatal("setuid %u: %.100s", (u_int) pw->pw_uid, strerror(errno));
116 if (setgid(pw->pw_gid) < 0)
117 fatal("setgid %u: %.100s", (u_int) pw->pw_gid, strerror(errno));
91} 118}
diff --git a/uidswap.h b/uidswap.h
index ab1a79c88..228e5a5e9 100644
--- a/uidswap.h
+++ b/uidswap.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: uidswap.h,v 1.6 2001/01/29 01:58:19 niklas Exp $ */ 1/* $OpenBSD: uidswap.h,v 1.7 2001/04/06 21:00:17 markus Exp $ */
2 2
3/* 3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -19,7 +19,7 @@
19 * Temporarily changes to the given uid. If the effective user id is not 19 * Temporarily changes to the given uid. If the effective user id is not
20 * root, this does nothing. This call cannot be nested. 20 * root, this does nothing. This call cannot be nested.
21 */ 21 */
22void temporarily_use_uid(uid_t uid); 22void temporarily_use_uid(struct passwd *pw);
23 23
24/* 24/*
25 * Restores the original effective user id after temporarily_use_uid(). 25 * Restores the original effective user id after temporarily_use_uid().
@@ -31,6 +31,6 @@ void restore_uid(void);
31 * Permanently sets all uids to the given uid. This cannot be called while 31 * Permanently sets all uids to the given uid. This cannot be called while
32 * temporarily_use_uid is effective. This must also clear any saved uids. 32 * temporarily_use_uid is effective. This must also clear any saved uids.
33 */ 33 */
34void permanently_set_uid(uid_t uid); 34void permanently_set_uid(struct passwd *pw);
35 35
36#endif /* UIDSWAP_H */ 36#endif /* UIDSWAP_H */