summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c69
1 files changed, 68 insertions, 1 deletions
diff --git a/misc.c b/misc.c
index 9421b4d39..68efb2b38 100644
--- a/misc.c
+++ b/misc.c
@@ -51,8 +51,9 @@
51#include <netdb.h> 51#include <netdb.h>
52#ifdef HAVE_PATHS_H 52#ifdef HAVE_PATHS_H
53# include <paths.h> 53# include <paths.h>
54#include <pwd.h>
55#endif 54#endif
55#include <pwd.h>
56#include <grp.h>
56#ifdef SSH_TUN_OPENBSD 57#ifdef SSH_TUN_OPENBSD
57#include <net/if.h> 58#include <net/if.h>
58#endif 59#endif
@@ -61,6 +62,7 @@
61#include "misc.h" 62#include "misc.h"
62#include "log.h" 63#include "log.h"
63#include "ssh.h" 64#include "ssh.h"
65#include "platform.h"
64 66
65/* remove newline at end of string */ 67/* remove newline at end of string */
66char * 68char *
@@ -708,6 +710,71 @@ read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
708 return -1; 710 return -1;
709} 711}
710 712
713/*
714 * return 1 if the specified uid is a uid that may own a system directory
715 * otherwise 0.
716 */
717int
718platform_sys_dir_uid(uid_t uid)
719{
720 if (uid == 0)
721 return 1;
722#ifdef PLATFORM_SYS_DIR_UID
723 if (uid == PLATFORM_SYS_DIR_UID)
724 return 1;
725#endif
726 return 0;
727}
728
729int
730secure_permissions(struct stat *st, uid_t uid)
731{
732 if (!platform_sys_dir_uid(st->st_uid) && st->st_uid != uid)
733 return 0;
734 if ((st->st_mode & 002) != 0)
735 return 0;
736 if ((st->st_mode & 020) != 0) {
737 /* If the file is group-writable, the group in question must
738 * have exactly one member, namely the file's owner.
739 * (Zero-member groups are typically used by setgid
740 * binaries, and are unlikely to be suitable.)
741 */
742 struct passwd *pw;
743 struct group *gr;
744 int members = 0;
745
746 gr = getgrgid(st->st_gid);
747 if (!gr)
748 return 0;
749
750 /* Check primary group memberships. */
751 while ((pw = getpwent()) != NULL) {
752 if (pw->pw_gid == gr->gr_gid) {
753 ++members;
754 if (pw->pw_uid != uid)
755 return 0;
756 }
757 }
758 endpwent();
759
760 pw = getpwuid(st->st_uid);
761 if (!pw)
762 return 0;
763
764 /* Check supplementary group memberships. */
765 if (gr->gr_mem[0]) {
766 ++members;
767 if (strcmp(pw->pw_name, gr->gr_mem[0]) ||
768 gr->gr_mem[1])
769 return 0;
770 }
771
772 if (!members)
773 return 0;
774 }
775 return 1;
776}
777
711int 778int
712tun_open(int tun, int mode) 779tun_open(int tun, int mode)
713{ 780{