-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.cpp
More file actions
279 lines (239 loc) · 8.82 KB
/
library.cpp
File metadata and controls
279 lines (239 loc) · 8.82 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* pam_sed - unlock self-encrypting drives via PAM
* Copyright (C) 2020 Fabian Stiewitz <fabian@stiewitz.pw>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "library.h"
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <sys/mount.h>
#include <DtaLexicon.h>
#include <DtaDevGeneric.h>
#include <DtaDevOpal2.h>
#include <DtaDevOpal1.h>
#include <DtaDevEnterprise.h>
#include <security/pam_ext.h>
#include <syslog.h>
sedutiloutput outputFormat = sedutilNormal;
int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv) {
const char *user;
const char *pass;
auto err = pam_get_item(pamh, PAM_USER, reinterpret_cast<const void **>(&user));
if (err != PAM_SUCCESS) {
pam_syslog(pamh, LOG_NOTICE, "pam error (libpam_sed): %s\n", pam_strerror(pamh, err));
return PAM_AUTH_ERR;
}
if (user == nullptr) {
pam_syslog(pamh, LOG_NOTICE, "pam error (libpam_sed): no user specified\n");
return PAM_AUTH_ERR;
}
err = pam_get_item(pamh, PAM_AUTHTOK, reinterpret_cast<const void **>(&pass));
if (err != PAM_SUCCESS) {
pam_syslog(pamh, LOG_NOTICE, "pam error (libpam_sed): %s\n", pam_strerror(pamh, err));
return PAM_AUTH_ERR;
}
if (pass == nullptr) {
pam_syslog(pamh, LOG_NOTICE, "pam error (libpam_sed): no password specified\n");
return PAM_AUTH_ERR;
}
return pam_sed_unlock(pamh, user, pass);
}
int pam_sm_setcred(pam_handle_t *, int, int, const char **) {
return PAM_IGNORE;
}
int pam_sm_acct_mgmt(pam_handle_t *, int, int, const char **) {
return PAM_IGNORE;
}
int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv) {
const char *user;
auto err = pam_get_item(pamh, PAM_USER, reinterpret_cast<const void **>(&user));
if (err != PAM_SUCCESS) {
pam_syslog(pamh, LOG_NOTICE, "pam error (libpam_sed): %s\n", pam_strerror(pamh, err));
return PAM_AUTH_ERR;
}
if (user == nullptr) {
pam_syslog(pamh, LOG_NOTICE, "pam error (libpam_sed): no user specified\n");
return PAM_AUTH_ERR;
}
return pam_sed_mount(pamh, user);
}
int pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char **argv) {
return PAM_IGNORE;
const char *user;
auto err = pam_get_item(pamh, PAM_USER, reinterpret_cast<const void **>(&user));
if (err != PAM_SUCCESS) {
pam_syslog(pamh, LOG_NOTICE, "pam error (libpam_sed): %s\n", pam_strerror(pamh, err));
return PAM_AUTH_ERR;
}
if (user == nullptr) {
pam_syslog(pamh, LOG_NOTICE, "pam error (libpam_sed): no user specified\n");
return PAM_AUTH_ERR;
}
return pam_sed_umount(pamh, user);
}
int pam_sm_chauthtok(pam_handle_t *, int, int, const char **) {
return PAM_IGNORE;
}
int pam_sed_unlock(pam_handle_t *handle, const char *user, const char *password) {
std::vector<drive_line_t> drives{};
std::vector<mount_line_t> mounts{};
auto err = pam_sed_read_config(drives, mounts);
if (err != PAM_SUCCESS) return err;
bool all_unlocked = true;
for (const auto &mount : mounts) {
if (strcmp(mount.user, user) == 0) {
if (!pam_sed_is_mounted(handle, mount)) {
all_unlocked = false;
break;
}
}
}
if (all_unlocked) return PAM_SUCCESS;
pam_syslog(handle, LOG_NOTICE, "unlocking SED drives\n");
for (const auto &drive : drives) {
if (strcmp(drive.user, user) == 0) {
err = pam_sed_try_drive_unlock(handle, drive, password);
if (err != PAM_SUCCESS) return err;
}
}
return PAM_SUCCESS;
}
int pam_sed_mount(pam_handle_t *handle, const char *user) {
std::vector<drive_line_t> drives{};
std::vector<mount_line_t> mounts{};
auto err = pam_sed_read_config(drives, mounts);
if (err != PAM_SUCCESS) return err;
for (const auto &mount : mounts) {
if (strcmp(mount.user, user) == 0) {
err = pam_sed_try_mount(handle, mount);
if (err != PAM_SUCCESS) return err;
}
}
return PAM_SUCCESS;
}
int pam_sed_umount(pam_handle_t *handle, const char *user) {
std::vector<drive_line_t> drives{};
std::vector<mount_line_t> mounts{};
auto err = pam_sed_read_config(drives, mounts);
if (err != PAM_SUCCESS) return err;
for (const auto &mount : mounts) {
if (strcmp(mount.user, user) == 0) {
err = pam_sed_try_umount(handle, mount);
if (err != PAM_SUCCESS) return err;
}
}
return PAM_SUCCESS;
}
int pam_sed_read_config(std::vector<drive_line_t> &drives, std::vector<mount_line_t> &mounts) {
std::ifstream ifd{SEDTAB, std::ios_base::in};
if (!ifd.is_open()) {
return PAM_AUTH_ERR;
}
for (std::string line; std::getline(ifd, line, '\n');) {
if (memcmp(line.c_str(), "drive", 5) == 0) {
drive_line_t drive{};
if (sscanf(line.c_str() + 6, DRIVE_LINE_FORMAT, drive.user, drive.path) == 2) {
drives.push_back(drive);
}
} else if (memcmp(line.c_str(), "mount", 5) == 0) {
mount_line_t mount{};
if (sscanf(line.c_str() + 6, MOUNT_LINE_FORMAT, mount.user, mount.drive, mount.path, mount.type) == 4) {
mounts.push_back(mount);
}
}
}
return PAM_SUCCESS;
}
int pam_sed_try_drive_unlock(pam_handle_t *handle, const drive_line_t &drive, const char *password) {
auto lockingOption = OPAL_LOCKINGSTATE::READWRITE;
auto range = 0;
DtaDev *d = nullptr;
auto tempDev = new DtaDevGeneric(drive.path);
if ((!tempDev->isPresent()) || (!tempDev->isAnySSC())) {
pam_syslog(handle, LOG_NOTICE, "pam error (libpam_sed): Invalid or unsupported disk %s", drive.path);
delete tempDev;
return PAM_AUTH_ERR;
}
if (tempDev->isOpal2())
d = new DtaDevOpal2(drive.path);
else if (tempDev->isOpal1())
d = new DtaDevOpal1(drive.path);
else if (tempDev->isEprise())
d = new DtaDevEnterprise(drive.path);
else {
delete tempDev;
pam_syslog(handle, LOG_NOTICE, "pam error (libpam_sed): Unknown OPAL SSC");
return PAM_AUTH_ERR;
}
delete tempDev;
if (nullptr == d) {
pam_syslog(handle, LOG_NOTICE, "pam error (libpam_sed): Create device object failed");
return PAM_AUTH_ERR;
}
d->output_format = sedutilNormal;
d->no_hash_passwords = false;
if (!d->Locked()) {
pam_syslog(handle, LOG_NOTICE, "sed already unlocked");
delete d;
return PAM_SUCCESS;
}
pam_syslog(handle, LOG_NOTICE, "unlocking sed %s", drive.path);
auto err = d->setLockingRange(range, lockingOption, const_cast<char *>(password));
if (err != 0) {
delete d;
pam_syslog(handle, LOG_NOTICE, "set locking range failed on device %s (ret %i)", drive.path, err);
return PAM_AUTH_ERR;
}
err = d->setMBRDone(1, const_cast<char *>(password));
if (err != 0) {
delete d;
pam_syslog(handle, LOG_NOTICE, "set mbr done failed on device %s (ret %i)", drive.path, err);
return PAM_AUTH_ERR;
}
delete d;
#if SLEEP_AFTER_UNLOCK > 0
sleep(SLEEP_AFTER_UNLOCK); // is there a better way?
#endif
return PAM_SUCCESS;
}
int pam_sed_try_mount(pam_handle_t *handle, const mount_line_t &drive) {
if (pam_sed_is_mounted(handle, drive)) {
pam_syslog(handle, LOG_NOTICE, "sed already mounted");
return PAM_SUCCESS;
}
if (mount(drive.drive, drive.path, drive.type, 0, nullptr)) {
pam_syslog(handle, LOG_NOTICE, "error during mount: %s", strerror(errno));
return PAM_AUTH_ERR;
}
pam_syslog(handle, LOG_NOTICE, "libpam_sed: mounting drive\n");
return PAM_SUCCESS;
}
int pam_sed_try_umount(pam_handle_t *handle, const mount_line_t &drive) {
return PAM_IGNORE;
}
int pam_sed_is_mounted(pam_handle_t *handle, const mount_line_t &drive) {
std::ifstream ifd{"/proc/mounts", std::ios_base::in};
for (std::string line; std::getline(ifd, line, '\n');) {
auto it = line.find(drive.path);
if (it != std::string::npos && it > 0 && it < line.size() - 2) {
if (isspace(line[it - 1])) {
return 1;
}
}
}
return 0;
}