BSD-style license.
[olsrd.git] / gui / win32 / Main / Frontend.cpp
1 /*\r
2  * The olsr.org Optimized Link-State Routing daemon (olsrd)\r
3  * Copyright (c) 2004, Thomas Lopatic (thomas@lopatic.de)\r
4  * All rights reserved.\r
5  *\r
6  * Redistribution and use in source and binary forms, with or without \r
7  * modification, are permitted provided that the following conditions \r
8  * are met:\r
9  *\r
10  * * Redistributions of source code must retain the above copyright \r
11  *   notice, this list of conditions and the following disclaimer.\r
12  * * Redistributions in binary form must reproduce the above copyright \r
13  *   notice, this list of conditions and the following disclaimer in \r
14  *   the documentation and/or other materials provided with the \r
15  *   distribution.\r
16  * * Neither the name of olsr.org, olsrd nor the names of its \r
17  *   contributors may be used to endorse or promote products derived \r
18  *   from this software without specific prior written permission.\r
19  *\r
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \r
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT \r
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS \r
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE \r
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, \r
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, \r
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; \r
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER \r
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT \r
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN \r
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE \r
31  * POSSIBILITY OF SUCH DAMAGE.\r
32  *\r
33  * Visit http://www.olsr.org for more information.\r
34  *\r
35  * If you find this software useful feel free to make a donation\r
36  * to the project. For more information see the website or contact\r
37  * the copyright holders.\r
38  *\r
39  * $Id: Frontend.cpp,v 1.4 2004/11/21 01:21:10 tlopatic Exp $\r
40  */\r
41 \r
42 #include "stdafx.h"\r
43 #include "Frontend.h"\r
44 #include "FrontendDlg.h"\r
45 \r
46 #ifdef _DEBUG\r
47 #define new DEBUG_NEW\r
48 #undef THIS_FILE\r
49 static char THIS_FILE[] = __FILE__;\r
50 #endif\r
51 \r
52 BEGIN_MESSAGE_MAP(CFrontendApp, CWinApp)\r
53         //{{AFX_MSG_MAP(CFrontendApp)\r
54         //}}AFX_MSG\r
55         ON_COMMAND(ID_HELP, CWinApp::OnHelp)\r
56 END_MESSAGE_MAP()\r
57 \r
58 CFrontendApp::CFrontendApp()\r
59 {\r
60 }\r
61 \r
62 CFrontendApp theApp;\r
63 \r
64 static int SetEnableRedirKey(unsigned long New)\r
65 {\r
66         HKEY Key;\r
67         unsigned long Type;\r
68         unsigned long Len;\r
69         unsigned long Old;\r
70         \r
71         if (::RegOpenKeyEx(HKEY_LOCAL_MACHINE,\r
72                 "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters",\r
73                 0, KEY_READ | KEY_WRITE, &Key) != ERROR_SUCCESS)\r
74                 return -1;\r
75         \r
76         Len = sizeof (Old);\r
77         \r
78         if (::RegQueryValueEx(Key, "EnableICMPRedirect", NULL, &Type,\r
79                 (unsigned char *)&Old, &Len) != ERROR_SUCCESS ||\r
80                 Type != REG_DWORD)\r
81                 Old = 1;\r
82         \r
83         if (::RegSetValueEx(Key, "EnableICMPRedirect", 0, REG_DWORD,\r
84                 (unsigned char *)&New, sizeof (New)))\r
85         {\r
86                 ::RegCloseKey(Key);\r
87                 return -1;\r
88         }\r
89         \r
90         ::RegCloseKey(Key);\r
91         return Old;\r
92 }\r
93 \r
94 BOOL CFrontendApp::InitInstance()\r
95 {\r
96         int Res;\r
97 \r
98 #ifdef _AFXDLL\r
99         Enable3dControls();\r
100 #else\r
101         Enable3dControlsStatic();\r
102 #endif\r
103 \r
104         CCommandLineInfo CmdLineInfo;\r
105         ParseCommandLine(CmdLineInfo);\r
106 \r
107         CFrontendDlg dlg;\r
108 \r
109         dlg.ConfigFile = CmdLineInfo.m_strFileName;\r
110 \r
111         m_pMainWnd = &dlg;\r
112 \r
113         Res = SetEnableRedirKey(0);\r
114 \r
115         if (Res == 1)\r
116         {\r
117                 Res = AfxMessageBox("- WARNING -\n\n"\r
118                         "The OLSR software has just switched off the processing of incoming ICMP "\r
119                         " redirect messages in your registry.\n\n"\r
120                         " Please REBOOT your computer for this change to take effect.\n\n"\r
121                         " Do you want to allow the OLSR software to reboot your computer now?\n\n"\r
122                         " (Please say \"Yes\".)", MB_YESNO | MB_ICONEXCLAMATION);\r
123 \r
124                 if (Res == IDYES)\r
125                 {\r
126                         HANDLE Proc;\r
127                         HMODULE Lib;\r
128                         BOOL (*Open)(HANDLE, DWORD, HANDLE *);\r
129                         BOOL (*Lookup)(char *, char *, LUID *);\r
130                         BOOL (*Adjust)(HANDLE, BOOL, TOKEN_PRIVILEGES *, DWORD,\r
131                                 TOKEN_PRIVILEGES *, DWORD *);\r
132                         HANDLE Token;\r
133 \r
134                         Lib = ::LoadLibrary("advapi32.dll");\r
135 \r
136                         if (Lib != NULL)\r
137                         {\r
138                                 Open = (BOOL (*)(HANDLE, DWORD, HANDLE *))\r
139                                         ::GetProcAddress(Lib, "OpenProcessToken");\r
140 \r
141                                 Lookup = (BOOL (*)(char *, char *, LUID *))\r
142                                         ::GetProcAddress(Lib, "LookupPrivilegeValueA");\r
143 \r
144                                 Adjust = (BOOL (*)(HANDLE, BOOL, TOKEN_PRIVILEGES *, DWORD,\r
145                                         TOKEN_PRIVILEGES *, DWORD *))\r
146                                         ::GetProcAddress(Lib, "AdjustTokenPrivileges");\r
147 \r
148                                 if (Open != NULL && Lookup != NULL && Adjust != NULL)\r
149                                 {\r
150                                         struct\r
151                                         {\r
152                                                 DWORD Count;\r
153                                                 LUID_AND_ATTRIBUTES Priv;\r
154                                         }\r
155                                         TokPriv;\r
156 \r
157                                         Proc = ::GetCurrentProcess();\r
158 \r
159                                         if (!Open(Proc, TOKEN_ALL_ACCESS, &Token))\r
160                                                 AfxMessageBox("OpenProcessToken() failed.");\r
161 \r
162                                         else if (!Lookup("", "SeShutdownPrivilege", &TokPriv.Priv.Luid))\r
163                                                 AfxMessageBox("LookupPrivilegeValue() failed.");\r
164 \r
165                                         else\r
166                                         {\r
167                                                 TokPriv.Count = 1;\r
168                                                 TokPriv.Priv.Attributes = SE_PRIVILEGE_ENABLED;\r
169 \r
170                                                 if (!Adjust(Token, FALSE, (TOKEN_PRIVILEGES *)&TokPriv,\r
171                                                         0, NULL, NULL))\r
172                                                         AfxMessageBox("AdjustTokenPrivilege() failed.");\r
173                                         }\r
174                                 }\r
175 \r
176                                 ::FreeLibrary(Lib);\r
177                         }\r
178 \r
179                         ::ExitWindowsEx(EWX_REBOOT, 0);\r
180                         ::TerminateProcess(Proc, 0);\r
181                 }\r
182         }\r
183 \r
184         dlg.DoModal();\r
185 \r
186         return FALSE;\r
187 }\r