/
storage-windows.c
505 lines (444 loc) · 17.9 KB
/
storage-windows.c
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// -*- mode: C; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
// vim: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
/*************************************************************************
* Copyright 2009-2012 Eucalyptus Systems, Inc.
*
* 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; version 3 of the License.
*
* 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 http://www.gnu.org/licenses/.
*
* Please contact Eucalyptus Systems, Inc., 6755 Hollister Ave., Goleta
* CA 93117, USA or visit http://www.eucalyptus.com/licenses/ if you need
* additional information or have any questions.
*
* This file may incorporate work covered under the following copyright
* and permission notice:
*
* Software License Agreement (BSD License)
*
* Copyright (c) 2008, Regents of the University of California
* All rights reserved.
*
* Redistribution and use of this software in source and binary forms,
* with or without modification, are permitted provided that the
* following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. USERS OF THIS SOFTWARE ACKNOWLEDGE
* THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE LICENSED MATERIAL,
* COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS SOFTWARE,
* AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
* IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA,
* SANTA BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY,
* WHICH IN THE REGENTS' DISCRETION MAY INCLUDE, WITHOUT LIMITATION,
* REPLACEMENT OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO
* IDENTIFIED, OR WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT
* NEEDED TO COMPLY WITH ANY SUCH LICENSES OR RIGHTS.
************************************************************************/
//!
//! @file storage/storage-windows.c
//! Need to provide description
//!
/*----------------------------------------------------------------------------*\
| |
| INCLUDES |
| |
\*----------------------------------------------------------------------------*/
#define _FILE_OFFSET_BITS 64 // so large-file support works on 32-bit systems
#include <stdio.h>
#include <stdlib.h>
#define __USE_GNU /* strnlen */
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h> /* open|read|close dir */
#include <time.h> /* time() */
#include <stdint.h>
#include <arpa/inet.h>
#include <openssl/sha.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <eucalyptus.h>
#include <storage-windows.h>
#include <euca_auth.h>
#include <misc.h>
/*----------------------------------------------------------------------------*\
| |
| DEFINES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| TYPEDEFS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| ENUMERATIONS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STRUCTURES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| EXTERNAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/* Should preferably be handled in header file */
/*----------------------------------------------------------------------------*\
| |
| GLOBAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STATIC VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| EXPORTED PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
int decryptWindowsPassword(char *encpass, int encsize, char *pkfile, char **out);
int encryptWindowsPassword(char *pass, char *key, char **out, int *outsize);
int makeWindowsFloppy(char *euca_home, char *rundir_path, char *keyName, char *instName);
/*----------------------------------------------------------------------------*\
| |
| STATIC PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| MACROS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| IMPLEMENTATION |
| |
\*----------------------------------------------------------------------------*/
//!
//!
//!
//! @param[in] encpass
//! @param[in] encsize
//! @param[in] pkfile
//! @param[out] out
//!
//! @return EUCA_OK on success or proper error code. Known error code returned include: EUCA_ERROR,
//! EUCA_IO_ERROR and EUCA_MEMORY_ERROR.
//!
int decryptWindowsPassword(char *encpass, int encsize, char *pkfile, char **out)
{
FILE *PKFP = NULL;
RSA *pr = NULL;
char *dec64 = NULL;
int rc = -1;
// Make sure we have valid parameters
if ((encpass == NULL) || (encsize <= 0) || (pkfile == NULL) || (*pkfile == '\0') || (out == NULL)) {
return (EUCA_ERROR);
}
// Open the private key file in read mode
if ((PKFP = fopen(pkfile, "r")) == NULL) {
return (EUCA_IO_ERROR);
}
// Give is to SSL
if (PEM_read_RSAPrivateKey(PKFP, &pr, NULL, NULL) == NULL) {
fclose(PKFP);
PKFP = NULL;
return (EUCA_ERROR);
}
// No longer need the file handler
fclose(PKFP);
PKFP = NULL;
if ((dec64 = base64_dec(((unsigned char *)encpass), strlen(encpass))) == NULL) {
return (EUCA_ERROR);
}
if ((*out = EUCA_ZALLOC(512, sizeof(char))) == NULL) {
EUCA_FREE(dec64);
return (EUCA_MEMORY_ERROR);
}
rc = RSA_private_decrypt(encsize, ((unsigned char *)dec64), ((unsigned char *)*out), pr, RSA_PKCS1_PADDING);
EUCA_FREE(dec64);
if (rc) {
EUCA_FREE(*out);
return (EUCA_ERROR);
}
return (EUCA_OK);
}
//!
//!
//!
//! @param[in] pass
//! @param[in] key
//! @param[out] out
//! @param[out] outsize
//!
//! @return EUCA_OK on success or proper error code. Known error code returned include: EUCA_ERROR,
//! and EUCA_MEMORY_ERROR
//!
int encryptWindowsPassword(char *pass, char *key, char **out, int *outsize)
{
char *sshkey_dec = NULL;
char *modbuf = NULL;
char *exponentbuf = NULL;
char *ptr = NULL;
char *tmp = NULL;
char hexstr[4] = { 0 };
char encpassword[512] = { 0 };
uint32_t len = 0;
uint32_t exponent = 0;
int size = 0;
int ilen = 0;
int i = 0;
int encsize = 0;
RSA *r = NULL;
if (!pass || !key || !out || !outsize) {
return (EUCA_ERROR);
}
size = strlen(key);
if ((sshkey_dec = base64_dec(((unsigned char *)key), size)) == NULL) {
return (EUCA_ERROR);
}
ptr = sshkey_dec;
memcpy(&len, ptr, 4);
len = htonl(len);
ptr += 4 + len;
memcpy(&len, ptr, 4);
len = htonl(len);
ptr += 4;
// read public exponent
if ((exponentbuf = EUCA_ZALLOC(32768, sizeof(char))) == NULL) {
EUCA_FREE(sshkey_dec);
return (EUCA_MEMORY_ERROR);
}
exponent = 0;
memcpy(&exponent, ptr, len);
exponent = htonl(exponent);
exponent = htonl(exponent);
snprintf(exponentbuf, 128, "%08X", exponent);
ptr += len;
memcpy(&len, ptr, 4);
len = htonl(len);
ptr += 4;
// read modulus material
if ((modbuf = EUCA_ZALLOC(32768, sizeof(char))) == NULL) {
EUCA_FREE(sshkey_dec);
EUCA_FREE(exponentbuf);
return (EUCA_MEMORY_ERROR);
}
ilen = ((int)len);
for (i = 0; i < ilen; i++) {
if ((tmp = strndup(ptr, 1)) != NULL) {
len = *tmp;
bzero(hexstr, sizeof(char) * 4);
snprintf(hexstr, 3, "%02X", (len << 24) >> 24);
strcat(modbuf, hexstr);
ptr += 1;
EUCA_FREE(tmp);
}
}
//printf("MOD: |%s|\n", modbuf);
//printf("EXPONENT: |%s|\n", exponentbuf);
if ((r = RSA_new()) == NULL) {
EUCA_FREE(sshkey_dec);
EUCA_FREE(exponentbuf);
EUCA_FREE(modbuf);
return (EUCA_MEMORY_ERROR);
}
if (!BN_hex2bn(&(r->e), exponentbuf) || !BN_hex2bn(&(r->n), modbuf)) {
EUCA_FREE(sshkey_dec);
EUCA_FREE(exponentbuf);
EUCA_FREE(modbuf);
return (EUCA_ERROR);
}
bzero(encpassword, 512);
if ((encsize = RSA_public_encrypt(strlen(pass), (unsigned char *)pass, (unsigned char *)encpassword, r, RSA_PKCS1_PADDING)) <= 0) {
EUCA_FREE(sshkey_dec);
EUCA_FREE(exponentbuf);
EUCA_FREE(modbuf);
return (EUCA_ERROR);
}
*out = base64_enc((unsigned char *)encpassword, encsize);
*outsize = encsize;
if (!*out || *outsize <= 0) {
EUCA_FREE(sshkey_dec);
EUCA_FREE(exponentbuf);
EUCA_FREE(modbuf);
return (EUCA_ERROR);
}
EUCA_FREE(sshkey_dec);
EUCA_FREE(exponentbuf);
EUCA_FREE(modbuf);
return (EUCA_OK);
}
//!
//!
//!
//! @param[in] euca_home
//! @param[in] rundir_path
//! @param[in] keyName
//! @param[in] instName
//!
//! @return EUCA_OK on success or proper error code. Known error code returned include: EUCA_ERROR,
//! EUCA_IO_ERROR and EUCA_MEMORY_ERROR.
//!
int makeWindowsFloppy(char *euca_home, char *rundir_path, char *keyName, char *instName)
{
int fd = 0;
int rc = 0;
int rbytes = 0;
int count = 0;
int encsize = 0;
int i = 0;
char *buf = NULL;
char *ptr = NULL;
char *tmp = NULL;
char *newpass = NULL;
char dest_path[1024] = { 0 };
char source_path[1024] = { 0 };
char password[16] = { 0 };
char *encpassword = NULL;
char *newInstName = NULL;
char c[4] = { 0 };
char cmd[MAX_PATH] = { 0 };
char enckey[2048] = { 0 };
char keyNameHolder1[512] = { 0 };
char keyNameHolder2[512] = { 0 };
FILE *FH = NULL;
if (!euca_home || !rundir_path || !strlen(euca_home) || !strlen(rundir_path)) {
return (EUCA_ERROR);
}
snprintf(source_path, 1024, EUCALYPTUS_HELPER_DIR "/floppy", euca_home);
snprintf(dest_path, 1024, "%s/floppy", rundir_path);
if (!keyName || !strlen(keyName) || !strlen(instName)) {
snprintf(cmd, MAX_PATH, "cp -a %s %s", source_path, dest_path);
return (system(cmd));
}
bzero(password, sizeof(char) * 16);
for (i = 0; i < 8; i++) {
c[0] = '0';
while (c[0] == '0' || c[0] == 'O')
snprintf(c, 2, "%c", RANDALPHANUM());
strcat(password, c);
}
// snprintf(source_path, 1024, "%s/usr/share/eucalyptus/floppy", euca_home);
// snprintf(dest_path, 1024, "%s/floppy", rundir_path);
if ((buf = EUCA_ALLOC(1024 * 2048, sizeof(char))) == NULL) {
return (EUCA_MEMORY_ERROR);
}
if ((fd = open(source_path, O_RDONLY)) < 0) {
EUCA_FREE(buf);
return (EUCA_IO_ERROR);
}
rbytes = read(fd, buf, 1024 * 2048);
close(fd);
if (rbytes < 0) {
EUCA_FREE(buf);
return (EUCA_IO_ERROR);
}
ptr = buf;
count = 0;
tmp = EUCA_ZALLOC(strlen("MAGICEUCALYPTUSPASSWORDPLACEHOLDER") + 1, sizeof(char));
newpass = EUCA_ZALLOC(strlen("MAGICEUCALYPTUSPASSWORDPLACEHOLDER") + 1, sizeof(char));
newInstName = EUCA_ZALLOC(strlen("MAGICEUCALYPTUSHOSTNAMEPLACEHOLDER") + 1, sizeof(char));
if (!tmp || !newpass || !newInstName) {
EUCA_FREE(tmp);
EUCA_FREE(newpass);
EUCA_FREE(newInstName);
EUCA_FREE(buf);
return (EUCA_MEMORY_ERROR);
}
snprintf(newpass, strlen(password) + 1, "%s", password);
snprintf(newInstName, strlen(instName) + 1, "%s", instName);
while (count < rbytes) {
memcpy(tmp, ptr, strlen("MAGICEUCALYPTUSPASSWORDPLACEHOLDER"));
if (!strcmp(tmp, "MAGICEUCALYPTUSPASSWORDPLACEHOLDER")) {
memcpy(ptr, newpass, strlen("MAGICEUCALYPTUSPASSWORDPLACEHOLDER"));
}
if (!strcmp(tmp, "MAGICEUCALYPTUSHOSTNAMEPLACEHOLDER")) {
memcpy(ptr, newInstName, strlen("MAGICEUCALYPTUSHOSTNAMEPLACEHOLDER"));
}
ptr++;
count++;
}
if ((fd = open(dest_path, O_CREAT | O_TRUNC | O_RDWR, 0700)) < 0) {
EUCA_FREE(buf);
EUCA_FREE(tmp);
EUCA_FREE(newpass);
EUCA_FREE(newInstName);
return (EUCA_IO_ERROR);
}
rc = write(fd, buf, rbytes);
close(fd);
if (rc != rbytes) {
EUCA_FREE(buf);
EUCA_FREE(tmp);
EUCA_FREE(newpass);
EUCA_FREE(newInstName);
return (EUCA_IO_ERROR);
}
EUCA_FREE(buf);
// encrypt password and write to console log for later retrieval
sscanf(keyName, "%s %s %s", keyNameHolder1, enckey, keyNameHolder2);
rc = encryptWindowsPassword(password, enckey, &encpassword, &encsize);
if (rc) {
EUCA_FREE(tmp);
EUCA_FREE(newpass);
EUCA_FREE(newInstName);
return (EUCA_ERROR);
}
snprintf(dest_path, 1024, "%s/console.append.log", rundir_path);
if ((FH = fopen(dest_path, "w")) != NULL) {
fprintf(FH, "<Password>\r\n%s\r\n</Password>\r\n", encpassword);
fclose(FH);
EUCA_FREE(encpassword);
EUCA_FREE(tmp);
EUCA_FREE(newpass);
EUCA_FREE(newInstName);
return (EUCA_OK);
}
EUCA_FREE(encpassword);
EUCA_FREE(tmp);
EUCA_FREE(newpass);
EUCA_FREE(newInstName);
return (EUCA_ERROR);
}