| 1 | /****************************************************************************** |
| 2 | * * |
| 3 | * Copyright (C) 2006-2009 by Tor Andersson. * |
| 4 | * * |
| 5 | * This file is part of Gargoyle. * |
| 6 | * * |
| 7 | * Gargoyle is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU General Public License as published by * |
| 9 | * the Free Software Foundation; either version 2 of the License, or * |
| 10 | * (at your option) any later version. * |
| 11 | * * |
| 12 | * Gargoyle is distributed in the hope that it will be useful, * |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 | * GNU General Public License for more details. * |
| 16 | * * |
| 17 | * You should have received a copy of the GNU General Public License * |
| 18 | * along with Gargoyle; if not, write to the Free Software * |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
| 20 | * * |
| 21 | *****************************************************************************/ |
| 22 | |
| 23 | #include <ctype.h> |
| 24 | #include <stddef.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #ifdef _WIN32 |
| 28 | #include <windows.h> |
| 29 | #endif |
| 30 | |
| 31 | #include "os.h" |
| 32 | #include "osextra.h" |
| 33 | |
| 34 | /* |
| 35 | * This file contains extra OS-specific functionality which is required |
| 36 | * but is not part of osportable.cc. |
| 37 | */ |
| 38 | |
| 39 | #ifdef _WIN32 |
| 40 | /* |
| 41 | * Copied and adapted from tads2/osnoui.c. |
| 42 | * Get the DOS attributes for a file |
| 43 | * |
| 44 | * We don't use either of the versions provided in tads2/osnoui.c directly, |
| 45 | * even though the version provided for the MICROSOFT compiler is pretty |
| 46 | * much what we need, because defining the MICROSOFT precompiler symbol |
| 47 | * would include a bunch of other stuff that we don't necessarily want. |
| 48 | */ |
| 49 | unsigned long oss_get_file_attrs(const char *fname) |
| 50 | { |
| 51 | unsigned long attrs = 0; |
| 52 | |
| 53 | /* get the DOS attribute flags */ |
| 54 | DWORD dos_attrib = GetFileAttributes(fname); |
| 55 | |
| 56 | /* translate the HIDDEN and SYSTEM bits */ |
| 57 | if ((dos_attrib & _A_HIDDEN) != 0) |
| 58 | attrs |= OSFATTR_HIDDEN; |
| 59 | if ((dos_attrib & _A_SYSTEM) != 0) |
| 60 | attrs |= OSFATTR_SYSTEM; |
| 61 | |
| 62 | /* if the RDONLY bit is set, it's readable, otherwise read/write */ |
| 63 | attrs |= OSFATTR_READ; |
| 64 | if ((dos_attrib & _A_RDONLY) == 0) |
| 65 | attrs |= OSFATTR_WRITE; |
| 66 | |
| 67 | /* |
| 68 | * On Windows, attempt to do a full security manager check to determine |
| 69 | * our actual access rights to the file. If we fail to get the |
| 70 | * security info, we'll give up and return the simple RDONLY |
| 71 | * determination we just made above. In most cases, failure to check |
| 72 | * the security information will simply be because the file has no ACL, |
| 73 | * which means that anyone can access it, hence our tentative result |
| 74 | * from the RDONLY attribute is correct after all. |
| 75 | */ |
| 76 | { |
| 77 | /* |
| 78 | * get the file's DACL and owner/group security info; first, ask |
| 79 | * how much space we need to allocate for the returned information |
| 80 | */ |
| 81 | DWORD len = 0; |
| 82 | SECURITY_INFORMATION info = (SECURITY_INFORMATION)( |
| 83 | OWNER_SECURITY_INFORMATION |
| 84 | | GROUP_SECURITY_INFORMATION |
| 85 | | DACL_SECURITY_INFORMATION); |
| 86 | GetFileSecurity(fname, info, 0, 0, &len); |
| 87 | if (len != 0) |
| 88 | { |
| 89 | /* allocate a buffer for the security info */ |
| 90 | SECURITY_DESCRIPTOR *sd = (SECURITY_DESCRIPTOR *)malloc(len); |
| 91 | if (sd != 0) |
| 92 | { |
| 93 | /* now actually retrieve the security info into our buffer */ |
| 94 | if (GetFileSecurity(fname, info, sd, len, &len)) |
| 95 | { |
| 96 | HANDLE ttok; |
| 97 | |
| 98 | /* impersonate myself for security purposes */ |
| 99 | ImpersonateSelf(SecurityImpersonation); |
| 100 | |
| 101 | /* |
| 102 | * get the security token for the current thread, which |
| 103 | * is the context in which the caller will presumably |
| 104 | * eventually attempt to access the file |
| 105 | */ |
| 106 | if (OpenThreadToken( |
| 107 | GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE, &ttok)) |
| 108 | { |
| 109 | GENERIC_MAPPING genmap = { |
| 110 | FILE_GENERIC_READ, |
| 111 | FILE_GENERIC_WRITE, |
| 112 | FILE_GENERIC_EXECUTE, |
| 113 | FILE_ALL_ACCESS |
| 114 | }; |
| 115 | PRIVILEGE_SET privs; |
| 116 | DWORD privlen = sizeof(privs); |
| 117 | BOOL granted = FALSE; |
| 118 | DWORD desired; |
| 119 | DWORD allowed; |
| 120 | |
| 121 | /* test read access */ |
| 122 | desired = GENERIC_READ; |
| 123 | MapGenericMask(&desired, &genmap); |
| 124 | if (AccessCheck(sd, ttok, desired, &genmap, |
| 125 | &privs, &privlen, &allowed, &granted)) |
| 126 | { |
| 127 | /* clear the read bit if reading isn't allowed */ |
| 128 | if (allowed == 0) |
| 129 | attrs &= ~OSFATTR_READ; |
| 130 | } |
| 131 | |
| 132 | /* test write access */ |
| 133 | desired = GENERIC_WRITE; |
| 134 | MapGenericMask(&desired, &genmap); |
| 135 | if (AccessCheck(sd, ttok, desired, &genmap, |
| 136 | &privs, &privlen, &allowed, &granted)) |
| 137 | { |
| 138 | /* clear the read bit if reading isn't allowed */ |
| 139 | if (allowed == 0) |
| 140 | attrs &= ~OSFATTR_WRITE; |
| 141 | } |
| 142 | |
| 143 | /* done with the thread token */ |
| 144 | CloseHandle(ttok); |
| 145 | } |
| 146 | |
| 147 | /* terminate our security self-impersonation */ |
| 148 | RevertToSelf(); |
| 149 | } |
| 150 | |
| 151 | /* free the allocated security info buffer */ |
| 152 | free(sd); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* return the attributes */ |
| 158 | return attrs; |
| 159 | } |
| 160 | #endif /* _WIN32 */ |