aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/main.cpp
blob: 757982669f8cce461213351434e32a1f2b9f442d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) 2016 Egor Tensin <egor@tensin.name>
// This file is part of the "Privilege check" project.
// For details, see https://github.com/egor-tensin/privilege-check.
// Distributed under the MIT License.

#include "error.hpp"
#include "os.hpp"
#include "process.hpp"
#include "resource.hpp"
#include "resource_ids.h"
#include "sid.hpp"
#include "token.hpp"

/* clang-format off */
#include <windows.h>
#include <commctrl.h>
#include <windowsx.h>
/* clang-format on */

#include <exception>
#include <string>

#if defined(_MSC_VER)
#pragma comment(                                                                                                                                                               \
    linker,                                                                                                                                                                    \
    "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"" \
)
#endif

namespace {

HWND get_dialog_item(HWND wnd, int id) {
    const auto lbl = GetDlgItem(wnd, id);
    if (lbl == NULL)
        error::raise("GetDlgItem");
    return lbl;
}

bool is_administrator() {
    auto token = token::open_for_current_process(
        token::permissions::query() | token::permissions::duplicate()
    );
    if (!token::get_linked(token))
        token = token::get_for_identification(token);

    return token::check_belongs(token, sid::builtin_administrators());
}

bool is_run_as_administrator() {
    return token::check_belongs(token::dumb(), sid::builtin_administrators());
}

bool is_elevated() {
    return token::query_elevation(token::open_for_current_process());
}

void decorate_elevate_button(HWND wnd, bool decorate) {
    const auto btn = get_dialog_item(wnd, IDC_BUTTON_ELEVATE);
    Button_SetElevationRequiredState(btn, !decorate);
}

DWORD get_integrity_level() {
    return token::query_integrity_level(token::open_for_current_process());
}

std::wstring get_integrity_level_as_string() {
    return token::integrity_level_to_string(get_integrity_level());
}

HWND get_label(HWND wnd, int id) {
    return get_dialog_item(wnd, id);
}

void set_label(HWND wnd, int id, bool val) {
    SetWindowTextW(get_label(wnd, id), val ? L"True" : L"False");
}

void set_label(HWND wnd, int id, const wchar_t* s) {
    SetWindowTextW(get_label(wnd, id), s);
}

void set_label(HWND wnd, int id, const std::wstring& s) {
    SetWindowTextW(get_label(wnd, id), s.c_str());
}

BOOL on_init_dialog(HWND wnd, HWND, LPARAM) {
    try {
        set_label(wnd, IDC_ADMINISTRATOR, is_administrator());
    } catch (const Error& e) {
        set_label(wnd, IDC_ADMINISTRATOR, L"N/A");
        error::report(e);
    }

    try {
        set_label(wnd, IDC_RUN_AS_ADMINISTRATOR, is_run_as_administrator());
    } catch (const Error& e) {
        set_label(wnd, IDC_RUN_AS_ADMINISTRATOR, L"N/A");
        error::report(e);
    }

    if (os::is_vista_or_later()) {
        try {
            const auto elevated = is_elevated();
            set_label(wnd, IDC_ELEVATED, elevated);
            decorate_elevate_button(wnd, elevated);
        } catch (const Error& e) {
            set_label(wnd, IDC_ELEVATED, L"N/A");
            error::report(e);
        }

        try {
            set_label(wnd, IDC_INTEGRITY_LEVEL, get_integrity_level_as_string());
        } catch (const Error& e) {
            set_label(wnd, IDC_INTEGRITY_LEVEL, L"N/A");
            error::report(e);
        }
    } else {
        set_label(wnd, IDC_ELEVATED, L"N/A");
        set_label(wnd, IDC_INTEGRITY_LEVEL, L"N/A");
    }

    return TRUE;
}

void report_already_elevated(HWND wnd) {
    MessageBoxW(
        wnd,
        resource::load_string(IDS_ALREADY_ELEVATED_TEXT).c_str(),
        resource::load_string(IDS_ALREADY_ELEVATED_CAPTION).c_str(),
        MB_OK
    );
}

void on_button_elevate_click(HWND wnd) {
    bool as_admin = false;

    try {
        as_admin = is_run_as_administrator();
    } catch (const Error& e) {
        error::report(e);
        return;
    }

    if (as_admin) {
        report_already_elevated(wnd);
        return;
    }

    try {
        process::runas_self(wnd);
    } catch (const Error& e) {
        if (error::get_code(e) != ERROR_CANCELLED)
            error::report(e);
        return;
    }

    EndDialog(wnd, 1);
}

void on_command(HWND wnd, int id, HWND, unsigned int) {
    switch (id) {
        case IDC_BUTTON_ELEVATE:
            on_button_elevate_click(wnd);
            break;

        case IDOK:
        case IDCANCEL:
            EndDialog(wnd, 0);
            break;
    }
}

void on_close(HWND wnd) {
    EndDialog(wnd, 0);
}

INT_PTR CALLBACK dialog_main(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
        HANDLE_MSG(wnd, WM_INITDIALOG, on_init_dialog);
        HANDLE_MSG(wnd, WM_COMMAND, on_command);
        HANDLE_MSG(wnd, WM_CLOSE, on_close);

        default:
            return FALSE;
    }
}

} // namespace

int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t*, int) {
    try {
        const auto ret = DialogBoxW(instance, MAKEINTRESOURCE(IDD_MAINDIALOG), NULL, dialog_main);
        if (ret == -1)
            error::report(error::make("DialogBoxW"));
        return static_cast<int>(ret);
    } catch (const std::exception& e) {
        error::report(e);
        return 1;
    }
}