Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * cygwin_util.c |
| 4 | * |
| 5 | * Author: Corinna Vinschen <vinschen@cygnus.com> |
| 6 | * |
| 7 | * Copyright (c) 2000 Corinna Vinschen <vinschen@cygnus.com>, Duisburg, Germany |
| 8 | * All rights reserved |
| 9 | * |
| 10 | * Created: Sat Sep 02 12:17:00 2000 cv |
| 11 | * |
| 12 | * This file contains functions for forcing opened file descriptors to |
| 13 | * binary mode on Windows systems. |
| 14 | */ |
| 15 | |
| 16 | #include "config.h" |
| 17 | |
| 18 | #ifdef HAVE_CYGWIN |
Damien Miller | 72c9a7e | 2000-09-24 11:10:13 +1100 | [diff] [blame] | 19 | |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <io.h> |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <sys/vfs.h> |
| 24 | #include <windows.h> |
| 25 | #define is_winnt (GetVersion() < 0x80000000) |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 26 | |
| 27 | int binary_open(const char *filename, int flags, mode_t mode) |
| 28 | { |
Damien Miller | 72c9a7e | 2000-09-24 11:10:13 +1100 | [diff] [blame] | 29 | return open(filename, flags | O_BINARY, mode); |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | int binary_pipe(int fd[2]) |
| 33 | { |
Damien Miller | 72c9a7e | 2000-09-24 11:10:13 +1100 | [diff] [blame] | 34 | int ret = pipe(fd); |
| 35 | |
| 36 | if (!ret) { |
| 37 | setmode (fd[0], O_BINARY); |
| 38 | setmode (fd[1], O_BINARY); |
| 39 | } |
| 40 | return ret; |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 41 | } |
| 42 | |
Damien Miller | 72c9a7e | 2000-09-24 11:10:13 +1100 | [diff] [blame] | 43 | int check_nt_auth(int pwd_authenticated, uid_t uid) |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 44 | { |
| 45 | /* |
Damien Miller | 72c9a7e | 2000-09-24 11:10:13 +1100 | [diff] [blame] | 46 | * The only authentication which is able to change the user |
| 47 | * context on NT systems is the password authentication. So |
| 48 | * we deny all requsts for changing the user context if another |
| 49 | * authentication method is used. |
| 50 | * This may change in future when a special openssh |
| 51 | * subauthentication package is available. |
| 52 | */ |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 53 | if (is_winnt && !pwd_authenticated && geteuid() != uid) |
| 54 | return 0; |
Damien Miller | 72c9a7e | 2000-09-24 11:10:13 +1100 | [diff] [blame] | 55 | |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 56 | return 1; |
| 57 | } |
| 58 | |
Damien Miller | 72c9a7e | 2000-09-24 11:10:13 +1100 | [diff] [blame] | 59 | int check_ntsec(const char *filename) |
Damien Miller | b70b61f | 2000-09-16 16:25:12 +1100 | [diff] [blame] | 60 | { |
| 61 | char *cygwin; |
| 62 | int allow_ntea = 0; |
| 63 | int allow_ntsec = 0; |
| 64 | struct statfs fsstat; |
| 65 | |
| 66 | /* Windows 95/98/ME don't support file system security at all. */ |
| 67 | if (!is_winnt) |
| 68 | return 0; |
| 69 | |
| 70 | /* Evaluate current CYGWIN settings. */ |
| 71 | if ((cygwin = getenv("CYGWIN")) != NULL) { |
| 72 | if (strstr(cygwin, "ntea") && !strstr(cygwin, "nontea")) |
| 73 | allow_ntea = 1; |
| 74 | if (strstr(cygwin, "ntsec") && !strstr(cygwin, "nontsec")) |
| 75 | allow_ntsec = 1; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * `ntea' is an emulation of POSIX attributes. It doesn't support |
| 80 | * real file level security as ntsec on NTFS file systems does |
| 81 | * but it supports FAT filesystems. `ntea' is minimum requirement |
| 82 | * for security checks. |
| 83 | */ |
| 84 | if (allow_ntea) |
| 85 | return 1; |
| 86 | |
| 87 | /* |
| 88 | * Retrieve file system flags. In Cygwin, file system flags are |
| 89 | * copied to f_type which has no meaning in Win32 itself. |
| 90 | */ |
| 91 | if (statfs(filename, &fsstat)) |
| 92 | return 1; |
| 93 | |
| 94 | /* |
| 95 | * Only file systems supporting ACLs are able to set permissions. |
| 96 | * `ntsec' is the setting in Cygwin which switches using of NTFS |
| 97 | * ACLs to support POSIX permissions on files. |
| 98 | */ |
| 99 | if (fsstat.f_type & FS_PERSISTENT_ACLS) |
| 100 | return allow_ntsec; |
| 101 | |
| 102 | return 0; |
Damien Miller | bac2d8a | 2000-09-05 16:13:06 +1100 | [diff] [blame] | 103 | } |
Damien Miller | 72c9a7e | 2000-09-24 11:10:13 +1100 | [diff] [blame] | 104 | |
| 105 | #endif /* HAVE_CYGWIN */ |