]> cygwin.com Git - cygwin-apps/setup.git/blame - root.cc
* net.cc (do_net): Default to direct download.
[cygwin-apps/setup.git] / root.cc
CommitLineData
23c9e63c
DD
1/*
2 * Copyright (c) 2000, Red Hat, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by DJ Delorie <dj@cygnus.com>
13 *
14 */
15
16/* The purpose of this file is to ask the user where they want the
17 root of the installation to be, and to ask whether the user prefers
18 text or binary mounts. */
19
8507f105
DD
20static char *cvsid = "\n%%% $Id$\n";
21
23c9e63c
DD
22#include "win32.h"
23#include <shlobj.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <ctype.h>
27
28#include "dialog.h"
29#include "resource.h"
30#include "state.h"
31#include "msg.h"
32#include "mount.h"
bf1d5889 33#include "concat.h"
89b1a15b 34#include "log.h"
23c9e63c
DD
35
36static int rb[] = { IDC_ROOT_TEXT, IDC_ROOT_BINARY, 0 };
24e259bb 37static int su[] = { IDC_ROOT_SYSTEM, IDC_ROOT_USER, 0 };
23c9e63c
DD
38
39static void
40check_if_enable_next (HWND h)
41{
24e259bb 42 EnableWindow (GetDlgItem (h, IDOK), root_text && root_dir && root_scope);
23c9e63c
DD
43}
44
45static void
46load_dialog (HWND h)
47{
48 rbset (h, rb, root_text);
24e259bb 49 rbset (h, su, root_scope);
23c9e63c
DD
50 eset (h, IDC_ROOT_DIR, root_dir);
51 check_if_enable_next (h);
52}
53
54static void
55save_dialog (HWND h)
56{
57 root_text = rbget (h, rb);
24e259bb 58 root_scope = rbget (h, su);
23c9e63c
DD
59 root_dir = eget (h, IDC_ROOT_DIR, root_dir);
60}
61
fee2a8d0
DD
62/*
63 * is_admin () determines whether or not the current user is a member of the
64 * Administrators group. On Windows 9X, the current user is considered an
65 * Administrator by definition.
66 */
67
68static int
69is_admin ()
70{
71 // Windows 9X users are considered Administrators by definition
72 OSVERSIONINFO verinfo;
73 verinfo.dwOSVersionInfoSize = sizeof (verinfo);
74 GetVersionEx (&verinfo);
75 if (verinfo.dwPlatformId != VER_PLATFORM_WIN32_NT)
76 return 1;
77
78 // Get the process token for the current process
79 HANDLE token;
80 BOOL status = OpenProcessToken (GetCurrentProcess(), TOKEN_QUERY, &token);
81 if (!status)
82 return 0;
83
84 // Get the group token information
85 UCHAR token_info[1024];
86 PTOKEN_GROUPS groups = (PTOKEN_GROUPS) token_info;
87 DWORD token_info_len = sizeof (token_info);
88 status = GetTokenInformation (token, TokenGroups, token_info, token_info_len, &token_info_len);
89 CloseHandle(token);
90 if (!status)
91 return 0;
92
93 // Create the Administrators group SID
94 PSID admin_sid;
95 SID_IDENTIFIER_AUTHORITY authority = SECURITY_NT_AUTHORITY;
96 status = AllocateAndInitializeSid (&authority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &admin_sid);
97 if (!status)
98 return 0;
99
100 // Check to see if the user is a member of the Administrators group
101 status = 0;
102 for (UINT i=0; i<groups->GroupCount; i++) {
103 if (EqualSid(groups->Groups[i].Sid, admin_sid)) {
104 status = 1;
105 break;
106 }
107 }
108
109 // Destroy the Administrators group SID
110 FreeSid (admin_sid);
111
112 // Return whether or not the user is a member of the Administrators group
113 return status;
114}
115
23c9e63c
DD
116static void
117read_mount_table ()
118{
119 int istext;
24e259bb
DD
120 int issystem;
121 root_dir = find_root_mount (&istext, &issystem);
23c9e63c
DD
122 if (root_dir)
123 {
124 if (istext)
125 root_text = IDC_ROOT_TEXT;
126 else
127 root_text = IDC_ROOT_BINARY;
24e259bb
DD
128 if (issystem)
129 root_scope = IDC_ROOT_SYSTEM;
130 else
131 root_scope = IDC_ROOT_USER;
23c9e63c 132 }
bf1d5889
DD
133 else
134 {
135 char windir[_MAX_PATH];
136 GetWindowsDirectory (windir, sizeof (windir));
137 windir[2] = 0;
138 root_dir = concat (windir, "\\cygwin", 0);
139 root_text = IDC_ROOT_BINARY;
fee2a8d0 140 root_scope = (is_admin()) ? IDC_ROOT_SYSTEM : IDC_ROOT_USER;
bf1d5889 141 }
23c9e63c
DD
142}
143
144static int CALLBACK
145browse_cb (HWND h, UINT msg, LPARAM lp, LPARAM data)
146{
147 switch (msg)
148 {
149 case BFFM_INITIALIZED:
bf1d5889
DD
150 if (root_dir)
151 SendMessage (h, BFFM_SETSELECTION, TRUE, (LPARAM)root_dir);
23c9e63c
DD
152 break;
153 }
154 return 0;
155}
156
157static void
158browse (HWND h)
159{
160 BROWSEINFO bi;
161 CHAR name[MAX_PATH];
162 LPITEMIDLIST pidl;
163 memset (&bi, 0, sizeof (bi));
164 bi.hwndOwner = h;
165 bi.pszDisplayName = name;
166 bi.lpszTitle = "Select an installation root directory";
167 bi.ulFlags = BIF_RETURNONLYFSDIRS;
168 bi.lpfn = browse_cb;
169 pidl = SHBrowseForFolder (&bi);
170 if (pidl)
171 {
172 if (SHGetPathFromIDList (pidl, name))
173 eset (h, IDC_ROOT_DIR, name);
174 }
175}
176
177#define isslash(c) ((c) == '\\' || (c) == '/')
178
179static int
180directory_is_absolute ()
181{
182 if (isalpha (root_dir[0])
183 && root_dir[1] == ':'
184 && (root_dir[2] == '\\' || root_dir[2] == '/'))
185 return 1;
186 return 0;
187}
188
189static int
190directory_is_rootdir ()
191{
192 char *c;
193 for (c = root_dir; *c; c++)
1fd6d0a2 194 if (isslash (c[0]) && c[1] && !isslash (c[1]))
23c9e63c
DD
195 return 0;
196 return 1;
197}
198
199static int
200directory_has_spaces ()
201{
202 if (strchr (root_dir, ' '))
203 return 1;
204 return 0;
205}
206
207static BOOL
208dialog_cmd (HWND h, int id, HWND hwndctl, UINT code)
209{
210 switch (id)
211 {
212
213 case IDC_ROOT_DIR:
214 case IDC_ROOT_TEXT:
215 case IDC_ROOT_BINARY:
24e259bb
DD
216 case IDC_ROOT_SYSTEM:
217 case IDC_ROOT_USER:
23c9e63c
DD
218 save_dialog (h);
219 check_if_enable_next (h);
220 break;
221
222 case IDC_ROOT_BROWSE:
223 browse (h);
224 break;
225
226 case IDOK:
1fd6d0a2 227 save_dialog (h);
23c9e63c
DD
228
229 if (! directory_is_absolute ())
230 {
231 note (IDS_ROOT_ABSOLUTE);
232 break;
233 }
234
235 if (directory_is_rootdir ())
236 if (IDNO == yesno (IDS_ROOT_SLASH))
237 break;
238
239 if (directory_has_spaces ())
240 if (IDNO == yesno (IDS_ROOT_SPACE))
241 break;
242
243 switch (source)
244 {
245 case IDC_SOURCE_NETINST:
1fd6d0a2 246 NEXT (IDD_NET);
23c9e63c
DD
247 break;
248 case IDC_SOURCE_CWD:
1fd6d0a2 249 NEXT (IDD_S_FROM_CWD);
23c9e63c
DD
250 break;
251 default:
1fd6d0a2
DD
252 msg ("source is default? %d\n", source);
253 NEXT (0);
23c9e63c
DD
254 }
255 break;
256
257 case IDC_BACK:
1fd6d0a2 258 save_dialog (h);
c92e1307 259 NEXT (IDD_LOCAL_DIR);
23c9e63c
DD
260 break;
261
262 case IDCANCEL:
1fd6d0a2 263 NEXT (0);
23c9e63c
DD
264 break;
265 }
266}
267
268static BOOL CALLBACK
269dialog_proc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
270{
271 switch (message)
272 {
273 case WM_INITDIALOG:
1fd6d0a2 274 load_dialog (h);
23c9e63c
DD
275 return FALSE;
276 case WM_COMMAND:
1fd6d0a2 277 return HANDLE_WM_COMMAND (h, wParam, lParam, dialog_cmd);
23c9e63c
DD
278 }
279 return FALSE;
280}
281
282void
283do_root (HINSTANCE h)
284{
285 int rv = 0;
286 if (!root_dir)
1fd6d0a2 287 read_mount_table ();
23c9e63c
DD
288 rv = DialogBox (h, MAKEINTRESOURCE (IDD_ROOT), 0, dialog_proc);
289 if (rv == -1)
290 fatal (IDS_DIALOG_FAILED);
89b1a15b
DD
291
292 log (0, "root: %s %s %s", root_dir,
293 (root_text == IDC_ROOT_TEXT) ? "text" : "binary",
294 (root_scope == IDC_ROOT_USER) ? "user" : "system");
23c9e63c
DD
295}
296
This page took 0.056199 seconds and 5 git commands to generate.