|
8 | 8 |
|
9 | 9 | #include "diskpart.h" |
10 | 10 |
|
11 | | -BOOL automount_main(INT argc, LPWSTR *argv) |
| 11 | +#define NDEBUG |
| 12 | +#include <debug.h> |
| 13 | + |
| 14 | + |
| 15 | +static |
| 16 | +NTSTATUS |
| 17 | +OpenMountManager( |
| 18 | + _Out_ PHANDLE MountMgrHandle, |
| 19 | + _In_ ACCESS_MASK Access) |
| 20 | +{ |
| 21 | + OBJECT_ATTRIBUTES ObjectAttributes; |
| 22 | + UNICODE_STRING DeviceName; |
| 23 | + IO_STATUS_BLOCK Iosb; |
| 24 | + |
| 25 | + RtlInitUnicodeString(&DeviceName, MOUNTMGR_DEVICE_NAME); |
| 26 | + |
| 27 | + InitializeObjectAttributes(&ObjectAttributes, |
| 28 | + &DeviceName, |
| 29 | + 0, |
| 30 | + NULL, |
| 31 | + NULL); |
| 32 | + |
| 33 | + return NtOpenFile(MountMgrHandle, |
| 34 | + Access | SYNCHRONIZE, |
| 35 | + &ObjectAttributes, |
| 36 | + &Iosb, |
| 37 | + 0, |
| 38 | + FILE_SYNCHRONOUS_IO_NONALERT); |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +static |
| 43 | +BOOL |
| 44 | +ShowAutomountState(VOID) |
| 45 | +{ |
| 46 | + HANDLE MountMgrHandle; |
| 47 | + MOUNTMGR_QUERY_AUTO_MOUNT AutoMount; |
| 48 | + IO_STATUS_BLOCK Iosb; |
| 49 | + NTSTATUS Status; |
| 50 | + |
| 51 | + DPRINT("ShowAutomountState()\n"); |
| 52 | + |
| 53 | + Status = OpenMountManager(&MountMgrHandle, GENERIC_READ); |
| 54 | + if (!NT_SUCCESS(Status)) |
| 55 | + { |
| 56 | + DPRINT1("OpenMountManager() Status 0x%08lx\n", Status); |
| 57 | + return TRUE; |
| 58 | + } |
| 59 | + |
| 60 | + Status = NtDeviceIoControlFile(MountMgrHandle, |
| 61 | + NULL, |
| 62 | + NULL, |
| 63 | + NULL, |
| 64 | + &Iosb, |
| 65 | + IOCTL_MOUNTMGR_QUERY_AUTO_MOUNT, |
| 66 | + NULL, |
| 67 | + 0, |
| 68 | + &AutoMount, |
| 69 | + sizeof(AutoMount)); |
| 70 | + |
| 71 | + NtClose(MountMgrHandle); |
| 72 | + |
| 73 | + if (!NT_SUCCESS(Status)) |
| 74 | + { |
| 75 | + DPRINT1("NtDeviceIoControlFile() Status 0x%08lx\n", Status); |
| 76 | + return TRUE; |
| 77 | + } |
| 78 | + |
| 79 | + if (AutoMount.CurrentState == Enabled) |
| 80 | + ConResPuts(StdOut, IDS_AUTOMOUNT_ENABLED); |
| 81 | + else |
| 82 | + ConResPuts(StdOut, IDS_AUTOMOUNT_DISABLED); |
| 83 | + ConPuts(StdOut, L"\n"); |
| 84 | + |
| 85 | + return TRUE; |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +static |
| 90 | +BOOL |
| 91 | +SetAutomountState( |
| 92 | + _In_ BOOL bEnable) |
| 93 | +{ |
| 94 | + HANDLE MountMgrHandle; |
| 95 | + MOUNTMGR_SET_AUTO_MOUNT AutoMount; |
| 96 | + IO_STATUS_BLOCK Iosb; |
| 97 | + NTSTATUS Status; |
| 98 | + |
| 99 | + DPRINT("SetAutomountState()\n"); |
| 100 | + |
| 101 | + Status = OpenMountManager(&MountMgrHandle, GENERIC_READ | GENERIC_WRITE); |
| 102 | + if (!NT_SUCCESS(Status)) |
| 103 | + { |
| 104 | + DPRINT1("OpenMountManager() Status 0x%08lx\n", Status); |
| 105 | + return TRUE; |
| 106 | + } |
| 107 | + |
| 108 | + if (bEnable) |
| 109 | + AutoMount.NewState = Enabled; |
| 110 | + else |
| 111 | + AutoMount.NewState = Disabled; |
| 112 | + |
| 113 | + Status = NtDeviceIoControlFile(MountMgrHandle, |
| 114 | + NULL, |
| 115 | + NULL, |
| 116 | + NULL, |
| 117 | + &Iosb, |
| 118 | + IOCTL_MOUNTMGR_SET_AUTO_MOUNT, |
| 119 | + &AutoMount, |
| 120 | + sizeof(AutoMount), |
| 121 | + NULL, |
| 122 | + 0); |
| 123 | + |
| 124 | + NtClose(MountMgrHandle); |
| 125 | + |
| 126 | + if (!NT_SUCCESS(Status)) |
| 127 | + { |
| 128 | + DPRINT1("NtDeviceIoControlFile() Status 0x%08lx\n", Status); |
| 129 | + return TRUE; |
| 130 | + } |
| 131 | + |
| 132 | + if (AutoMount.NewState == Enabled) |
| 133 | + ConResPuts(StdOut, IDS_AUTOMOUNT_ENABLED); |
| 134 | + else |
| 135 | + ConResPuts(StdOut, IDS_AUTOMOUNT_DISABLED); |
| 136 | + ConPuts(StdOut, L"\n"); |
| 137 | + |
| 138 | + return TRUE; |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | +static |
| 143 | +BOOL |
| 144 | +ScrubAutomount(VOID) |
| 145 | +{ |
| 146 | + HANDLE MountMgrHandle; |
| 147 | + IO_STATUS_BLOCK Iosb; |
| 148 | + NTSTATUS Status; |
| 149 | + |
| 150 | + DPRINT("ScrubAutomount()\n"); |
| 151 | + |
| 152 | + Status = OpenMountManager(&MountMgrHandle, GENERIC_READ | GENERIC_WRITE); |
| 153 | + if (!NT_SUCCESS(Status)) |
| 154 | + { |
| 155 | + DPRINT1("OpenMountManager() Status 0x%08lx\n", Status); |
| 156 | + return TRUE; |
| 157 | + } |
| 158 | + |
| 159 | + Status = NtDeviceIoControlFile(MountMgrHandle, |
| 160 | + NULL, |
| 161 | + NULL, |
| 162 | + NULL, |
| 163 | + &Iosb, |
| 164 | + IOCTL_MOUNTMGR_SCRUB_REGISTRY, |
| 165 | + NULL, |
| 166 | + 0, |
| 167 | + NULL, |
| 168 | + 0); |
| 169 | + |
| 170 | + NtClose(MountMgrHandle); |
| 171 | + |
| 172 | + if (!NT_SUCCESS(Status)) |
| 173 | + { |
| 174 | + DPRINT1("NtDeviceIoControlFile() Status 0x%08lx\n", Status); |
| 175 | + return TRUE; |
| 176 | + } |
| 177 | + |
| 178 | + ConResPuts(StdOut, IDS_AUTOMOUNT_SCRUBBED); |
| 179 | + ConPuts(StdOut, L"\n"); |
| 180 | + |
| 181 | + return TRUE; |
| 182 | +} |
| 183 | + |
| 184 | + |
| 185 | +BOOL |
| 186 | +automount_main( |
| 187 | + INT argc, |
| 188 | + LPWSTR *argv) |
12 | 189 | { |
13 | | - ConPuts(StdOut, L"Automount\n"); |
| 190 | + BOOL bDisable = FALSE, bEnable = FALSE, bScrub = FALSE; |
| 191 | +#if 0 |
| 192 | + BOOL bNoErr = FALSE; |
| 193 | +#endif |
| 194 | + INT i; |
| 195 | + |
| 196 | + DPRINT("Automount()\n"); |
| 197 | + |
| 198 | +#if 0 |
| 199 | + for (i = 1; i < argc; i++) |
| 200 | + { |
| 201 | + if (_wcsicmp(argv[i], L"noerr") == 0) |
| 202 | + { |
| 203 | + /* noerr */ |
| 204 | + bNoErr = TRUE; |
| 205 | + } |
| 206 | + } |
| 207 | +#endif |
| 208 | + |
| 209 | + for (i = 1; i < argc; i++) |
| 210 | + { |
| 211 | + if (_wcsicmp(argv[i], L"disable") == 0) |
| 212 | + { |
| 213 | + /* set automount state */ |
| 214 | + bDisable = TRUE; |
| 215 | + } |
| 216 | + else if (_wcsicmp(argv[i], L"enable") == 0) |
| 217 | + { |
| 218 | + /* set automount state */ |
| 219 | + bEnable = TRUE; |
| 220 | + } |
| 221 | + else if (_wcsicmp(argv[i], L"scrub") == 0) |
| 222 | + { |
| 223 | + /* scrub automount */ |
| 224 | + bScrub = TRUE; |
| 225 | + } |
| 226 | + else if (_wcsicmp(argv[i], L"noerr") == 0) |
| 227 | + { |
| 228 | + /* already handled */ |
| 229 | + } |
| 230 | + else |
| 231 | + { |
| 232 | + ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS); |
| 233 | + return TRUE; |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + DPRINT("bDisable %u\n", bDisable); |
| 238 | + DPRINT("bEnable %u\n", bEnable); |
| 239 | + DPRINT("bScrub %u\n", bScrub); |
| 240 | + |
| 241 | + if (bDisable && bEnable) |
| 242 | + { |
| 243 | + ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS); |
| 244 | + return TRUE; |
| 245 | + } |
| 246 | + |
| 247 | + if ((bDisable == FALSE) && (bEnable == FALSE) && (bScrub == FALSE)) |
| 248 | + { |
| 249 | + DPRINT("Show automount\n"); |
| 250 | + return ShowAutomountState(); |
| 251 | + } |
| 252 | + |
| 253 | + if (bDisable) |
| 254 | + { |
| 255 | + DPRINT("Disable automount\n"); |
| 256 | + return SetAutomountState(FALSE); |
| 257 | + } |
| 258 | + |
| 259 | + if (bEnable) |
| 260 | + { |
| 261 | + DPRINT("Enable automount\n"); |
| 262 | + return SetAutomountState(TRUE); |
| 263 | + } |
| 264 | + |
| 265 | + if (bScrub) |
| 266 | + { |
| 267 | + DPRINT("Scrub automount\n"); |
| 268 | + return ScrubAutomount(); |
| 269 | + } |
| 270 | + |
14 | 271 | return TRUE; |
15 | 272 | } |
0 commit comments