Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 369 lines (328 sloc) 11.082 kb
df1fb37c »
2012-07-17 Update GPL file headers
1 /*************************************************************************
2 * Copyright 2009-2012 Eucalyptus Systems, 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; version 3 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see http://www.gnu.org/licenses/.
15 *
16 * Please contact Eucalyptus Systems, Inc., 6755 Hollister Ave., Goleta
17 * CA 93117, USA or visit http://www.eucalyptus.com/licenses/ if you need
18 * additional information or have any questions.
47b5fb52 »
2012-07-25 Tweak GPL headers
19 *
20 * This file may incorporate work covered under the following copyright
21 * and permission notice:
22 *
23 * Software License Agreement (BSD License)
24 *
25 * Copyright (c) 2008, Regents of the University of California
26 * All rights reserved.
27 *
28 * Redistribution and use of this software in source and binary forms,
29 * with or without modification, are permitted provided that the
30 * following conditions are met:
31 *
32 * Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 *
35 * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer
37 * in the documentation and/or other materials provided with the
38 * distribution.
39 *
40 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
43 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
44 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
45 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
46 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
48 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
50 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51 * POSSIBILITY OF SUCH DAMAGE. USERS OF THIS SOFTWARE ACKNOWLEDGE
52 * THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE LICENSED MATERIAL,
53 * COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS SOFTWARE,
54 * AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
55 * IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA,
56 * SANTA BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY,
57 * WHICH IN THE REGENTS' DISCRETION MAY INCLUDE, WITHOUT LIMITATION,
58 * REPLACEMENT OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO
59 * IDENTIFIED, OR WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT
60 * NEEDED TO COMPLY WITH ANY SUCH LICENSES OR RIGHTS.
df1fb37c »
2012-07-17 Update GPL file headers
61 ************************************************************************/
62
319d3e34 »
2010-10-15 moved _FILE_OFFSET_BITS definitions up in files, so they actually work
63 #define _FILE_OFFSET_BITS 64 // so large-file support works on 32-bit systems
11a65e9f »
2010-02-22 some helper functions for windows image preparation
64 #include <stdio.h>
65 #include <stdlib.h>
66 #define __USE_GNU /* strnlen */
67 #include <string.h>
68 #include <sys/types.h>
69 #include <sys/stat.h>
70 #include <unistd.h>
71 #include <sys/types.h>
72 #include <sys/stat.h>
73 #include <fcntl.h>
74 #include <errno.h>
75 #include <dirent.h> /* open|read|close dir */
76 #include <time.h> /* time() */
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
77 #include <stdint.h>
7eb044b8 »
2010-05-11 a few more coverity commits
78 #include <arpa/inet.h>
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
79
80 #include <openssl/sha.h>
81 #include <openssl/rsa.h>
82 #include <openssl/pem.h>
83 #include <openssl/bio.h>
84 #include <openssl/evp.h>
85 #include <openssl/err.h>
11a65e9f »
2010-02-22 some helper functions for windows image preparation
86
87 #include <storage-windows.h>
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
88 #include <euca_auth.h>
b7bee8a5 »
2010-03-26 add random password generation
89 #include <misc.h>
11a65e9f »
2010-02-22 some helper functions for windows image preparation
90
59bc3e02 »
2012-09-11 Remove hard-coded paths from C and Java code
91 #include <eucalyptus.h>
92
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
93 int decryptWindowsPassword(char *encpass, int encsize, char *pkfile, char **out) {
94 FILE *PKFP;
95 RSA *pr=NULL;
96 char *dec64;
97 int rc;
98
99 if (!encpass || encsize <= 0 || !*pkfile || !out) {
100 return(1);
101 }
11a65e9f »
2010-02-22 some helper functions for windows image preparation
102
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
103 PKFP = fopen(pkfile, "r");
104 if (!PKFP) {
105 return(1);
106 }
107 if (PEM_read_RSAPrivateKey(PKFP, &pr, NULL, NULL) == NULL) {
108 return(1);
109 }
110
111 dec64 = base64_dec((unsigned char *)encpass, strlen(encpass));
112 if (!dec64) {
11a65e9f »
2010-02-22 some helper functions for windows image preparation
113 return(1);
114 }
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
115
116 *out = malloc(512);
85835c2e »
2010-05-11 coverity fixes
117 if(!*out) {
118 if (dec64) free(dec64);
119 return(1);
120 }
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
121 bzero(*out, 512);
122 rc = RSA_private_decrypt(encsize, (unsigned char *)dec64, (unsigned char *)*out, pr, RSA_PKCS1_PADDING);
85835c2e »
2010-05-11 coverity fixes
123 if (dec64) free(dec64);
124 if (rc) {
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
125 return(1);
126 }
127 return(0);
128 }
129
130
131 int encryptWindowsPassword(char *pass, char *key, char **out, int *outsize) {
132 char *sshkey_dec, *modbuf, *exponentbuf;
ac8adbc5 »
2010-05-30 large files on 32bit centos
133 char *ptr, *tmp, hexstr[4], *enc64;
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
134 char *dec64, encpassword[512];
135
136 uint32_t len, exponent;
137 int size, ilen, i, encsize=0, rc;
138 RSA *r=NULL;
139
140 if (!pass || !key || !out || !outsize) {
141 return(1);
142 }
143
144 size = strlen(key);
145 sshkey_dec = base64_dec((unsigned char *)key, size);
146 if (!sshkey_dec) {
147 return(1);
148 }
ac8adbc5 »
2010-05-30 large files on 32bit centos
149
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
150 ptr = sshkey_dec;
151 memcpy(&len, ptr, 4);
152 len = htonl(len);
153 ptr+=4+len;
154
155 memcpy(&len, ptr, 4);
156 len = htonl(len);
157 ptr+=4;
158
159 // read public exponent
160 exponentbuf = malloc(32768);
85835c2e »
2010-05-11 coverity fixes
161 if (!exponentbuf) {
162 if (sshkey_dec) free(sshkey_dec);
163 return(1);
164 }
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
165 exponent = 0;
166 memcpy(&exponent, ptr, len);
167 exponent = htonl(exponent);
168 exponent = htonl(exponent);
169 snprintf(exponentbuf, 128, "%08X", exponent);
170 ptr+=len;
171
172 memcpy(&len, ptr, 4);
173 len = htonl(len);
174 ptr+=4;
175
176 // read modulus material
177 modbuf = malloc(32768);
85835c2e »
2010-05-11 coverity fixes
178 if (!modbuf) {
179 if (sshkey_dec) free(sshkey_dec);
180 if (exponentbuf) free(exponentbuf);
181 return(1);
182 }
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
183 bzero(modbuf, 32768);
184 ilen = (int)len;
185 for (i=0; i<ilen; i++) {
186 tmp = strndup(ptr, 1);
85835c2e »
2010-05-11 coverity fixes
187 if (tmp) {
188 len = *tmp;
ac8adbc5 »
2010-05-30 large files on 32bit centos
189 bzero(hexstr, sizeof(char) * 4);
190 snprintf(hexstr, 3, "%02X", (len<<24)>>24);
85835c2e »
2010-05-11 coverity fixes
191 strcat(modbuf, hexstr);
ac8adbc5 »
2010-05-30 large files on 32bit centos
192 ptr+=1;
85835c2e »
2010-05-11 coverity fixes
193 free(tmp);
194 }
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
195 }
ac8adbc5 »
2010-05-30 large files on 32bit centos
196 //printf("MOD: |%s|\n", modbuf);
197 //printf("EXPONENT: |%s|\n", exponentbuf);
198
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
199 r = RSA_new();
200 if (!r) {
85835c2e »
2010-05-11 coverity fixes
201 if (sshkey_dec) free(sshkey_dec);
202 if (exponentbuf) free(exponentbuf);
203 if (modbuf) free(modbuf);
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
204 return(1);
205 }
206 if (!BN_hex2bn(&(r->e), exponentbuf) || !BN_hex2bn(&(r->n), modbuf)) {
85835c2e »
2010-05-11 coverity fixes
207 if (sshkey_dec) free(sshkey_dec);
208 if (exponentbuf) free(exponentbuf);
209 if (modbuf) free(modbuf);
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
210 return(1);
211 }
212
213 bzero(encpassword, 512);
214 encsize = RSA_public_encrypt(strlen(pass), (unsigned char *)pass, (unsigned char *)encpassword, r, RSA_PKCS1_PADDING);
215 if (encsize <= 0) {
85835c2e »
2010-05-11 coverity fixes
216 if (sshkey_dec) free(sshkey_dec);
217 if (exponentbuf) free(exponentbuf);
218 if (modbuf) free(modbuf);
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
219 return(1);
220 }
221
222 *out = base64_enc((unsigned char *)encpassword, encsize);
223 *outsize = encsize;
224 if (!*out || *outsize <= 0) {
85835c2e »
2010-05-11 coverity fixes
225 if (sshkey_dec) free(sshkey_dec);
226 if (exponentbuf) free(exponentbuf);
227 if (modbuf) free(modbuf);
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
228 return(1);
229 }
85835c2e »
2010-05-11 coverity fixes
230 if (sshkey_dec) free(sshkey_dec);
231 if (exponentbuf) free(exponentbuf);
232 if (modbuf) free(modbuf);
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
233 return(0);
234 }
235
236
39f2a406 »
2010-09-17 changed the contents of floppy that's get passed to Windows
237 int makeWindowsFloppy(char *euca_home, char *rundir_path, char *keyName, char *instName) {
e7de87ca »
2010-03-29 8 character passwords for windows VM
238 int fd, rc, rbytes, count, encsize, i;
b7bee8a5 »
2010-03-26 add random password generation
239 char *buf, *ptr, *tmp, *newpass, dest_path[1024], source_path[1024], fname[1024], password[16];
f8253098 »
2010-03-20 set up console output with the right <Password> format
240 char *encpassword;
39f2a406 »
2010-09-17 changed the contents of floppy that's get passed to Windows
241 char *newInstName;
f8253098 »
2010-03-20 set up console output with the right <Password> format
242 FILE *FH;
618a84c9 »
2010-02-22 comment out dead code
243
945256cb »
2010-05-06 add right opts to eucalyptus.conf, set bundlePid==0 on cancel to avoi…
244 if (!euca_home || !rundir_path || !strlen(euca_home) || !strlen(rundir_path)) {
67c8598d »
2010-04-05 add callout to euca-nc-bundle-upload
245 return(1);
246 }
247
59bc3e02 »
2012-09-11 Remove hard-coded paths from C and Java code
248 snprintf(source_path, 1024, EUCALYPTUS_HELPER_DIR "/floppy", euca_home);
3f6b25ba »
2011-09-07 allow windows image to boot even if keypair is not supplied
249 snprintf(dest_path, 1024, "%s/floppy", rundir_path);
250 if (!keyName || !strlen(keyName) || !strlen(instName)) {
251 char cmd[MAX_PATH];
252 snprintf(cmd, MAX_PATH, "cp -a %s %s", source_path, dest_path);
253 return(system(cmd));
254 }
255
e7de87ca »
2010-03-29 8 character passwords for windows VM
256 bzero(password, sizeof(char)*16);
257 for (i=0; i<8; i++) {
ac8adbc5 »
2010-05-30 large files on 32bit centos
258 char c[4];
ea20610e »
2010-11-12 avoid using 0 and O in random windows password generator
259 c[0] = '0';
260 while(c[0] == '0' || c[0] == 'O') snprintf(c, 2, "%c", RANDALPHANUM);
e7de87ca »
2010-03-29 8 character passwords for windows VM
261 strcat(password, c);
262 }
3f6b25ba »
2011-09-07 allow windows image to boot even if keypair is not supplied
263 // snprintf(source_path, 1024, "%s/usr/share/eucalyptus/floppy", euca_home);
264 // snprintf(dest_path, 1024, "%s/floppy", rundir_path);
11a65e9f »
2010-02-22 some helper functions for windows image preparation
265
266 buf = malloc(1024 * 2048);
267 if (!buf) {
268 return(1);
269 }
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
270
11a65e9f »
2010-02-22 some helper functions for windows image preparation
271 fd = open(source_path, O_RDONLY);
272 if (fd < 0) {
273 if (buf) free(buf);
274 return(1);
275 }
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
276
11a65e9f »
2010-02-22 some helper functions for windows image preparation
277 rbytes = read(fd, buf, 1024 * 2048);
278 close(fd);
85835c2e »
2010-05-11 coverity fixes
279 if (rbytes < 0) {
280 if (buf) free(buf);
281 return(1);
282 }
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
283
11a65e9f »
2010-02-22 some helper functions for windows image preparation
284 ptr = buf;
285 count=0;
286 tmp = malloc(sizeof(char) * strlen("MAGICEUCALYPTUSPASSWORDPLACEHOLDER")+1);
287 newpass = malloc(sizeof(char) * strlen("MAGICEUCALYPTUSPASSWORDPLACEHOLDER")+1);
39f2a406 »
2010-09-17 changed the contents of floppy that's get passed to Windows
288 newInstName = malloc(sizeof(char) * strlen("MAGICEUCALYPTUSHOSTNAMEPLACEHOLDER")+1);
289
290 if (!tmp || !newpass || !newInstName) {
492df468 »
2010-05-12 added doc dir with libvirt-windows examples
291 if (tmp) free(tmp);
292 if (newpass) free(newpass);
39f2a406 »
2010-09-17 changed the contents of floppy that's get passed to Windows
293 if (newInstName) free(newInstName);
85835c2e »
2010-05-11 coverity fixes
294 if (buf) free(buf);
295 return(1);
296 }
11a65e9f »
2010-02-22 some helper functions for windows image preparation
297 bzero(tmp, strlen("MAGICEUCALYPTUSPASSWORDPLACEHOLDER")+1);
298 bzero(newpass, strlen("MAGICEUCALYPTUSPASSWORDPLACEHOLDER")+1);
39f2a406 »
2010-09-17 changed the contents of floppy that's get passed to Windows
299 bzero(newInstName, strlen("MAGICEUCALYPTUSHOSTNAMEPLACEHOLDER")+1);
300
11a65e9f »
2010-02-22 some helper functions for windows image preparation
301 snprintf(newpass, strlen(password)+1, "%s", password);
39f2a406 »
2010-09-17 changed the contents of floppy that's get passed to Windows
302 snprintf(newInstName, strlen(instName)+1, "%s", instName);
04fa7d12 »
2010-03-19 add encryptWindowsPassword and decryptWindowsPassword
303
11a65e9f »
2010-02-22 some helper functions for windows image preparation
304 while(count < rbytes) {
305 memcpy(tmp, ptr, strlen("MAGICEUCALYPTUSPASSWORDPLACEHOLDER"));
306 if (!strcmp(tmp, "MAGICEUCALYPTUSPASSWORDPLACEHOLDER")) {
307 memcpy(ptr, newpass, strlen("MAGICEUCALYPTUSPASSWORDPLACEHOLDER"));
308 }
39f2a406 »
2010-09-17 changed the contents of floppy that's get passed to Windows
309
310 if (!strcmp(tmp, "MAGICEUCALYPTUSHOSTNAMEPLACEHOLDER")) {
311 memcpy(ptr, newInstName, strlen("MAGICEUCALYPTUSHOSTNAMEPLACEHOLDER"));
312 }
313
314
11a65e9f »
2010-02-22 some helper functions for windows image preparation
315 ptr++;
316 count++;
317 }
f8253098 »
2010-03-20 set up console output with the right <Password> format
318
ee45ad6f »
2010-05-04 Add getConsoleOutput support for VMwareBroker
319 fd = open(dest_path, O_CREAT | O_TRUNC | O_RDWR, 0700);
11a65e9f »
2010-02-22 some helper functions for windows image preparation
320 if (fd < 0) {
321 if (buf) free(buf);
85835c2e »
2010-05-11 coverity fixes
322 if (tmp) free(tmp);
323 if (newpass) free(newpass);
39f2a406 »
2010-09-17 changed the contents of floppy that's get passed to Windows
324 if (newInstName) free(newInstName);
11a65e9f »
2010-02-22 some helper functions for windows image preparation
325 return(1);
326 }
327 rc = write(fd, buf, rbytes);
328 if (rc != rbytes) {
329 if (buf) free(buf);
85835c2e »
2010-05-11 coverity fixes
330 if (tmp) free(tmp);
331 if (newpass) free(newpass);
39f2a406 »
2010-09-17 changed the contents of floppy that's get passed to Windows
332 if (newInstName) free(newInstName);
555ee70c »
2012-05-15 Fix file descriptor leak from Coverity issue #10289
333 close(fd);
11a65e9f »
2010-02-22 some helper functions for windows image preparation
334 return(1);
335 }
336 close(fd);
337 if (buf) free(buf);
f8253098 »
2010-03-20 set up console output with the right <Password> format
338
339 // encrypt password and write to console log for later retrieval
340 char tmpstr[512], enckey[2048];
c788ee0f »
2011-03-03 fix seg fault issue with makeWindowsFloppy
341 char keyNameHolder1[512], keyNameHolder2[512];
342 sscanf(keyName, "%s %s %s", keyNameHolder1, enckey, keyNameHolder2);
f8253098 »
2010-03-20 set up console output with the right <Password> format
343 rc = encryptWindowsPassword(password, enckey, &encpassword, &encsize);
344 if (rc) {
85835c2e »
2010-05-11 coverity fixes
345 if (tmp) free(tmp);
346 if (newpass) free(newpass);
39f2a406 »
2010-09-17 changed the contents of floppy that's get passed to Windows
347 if (newInstName) free(newInstName);
f8253098 »
2010-03-20 set up console output with the right <Password> format
348 return(1);
349 }
350
351 snprintf(dest_path, 1024, "%s/console.append.log", rundir_path);
352 FH = fopen(dest_path, "w");
353 if (FH) {
354 fprintf(FH, "<Password>\r\n%s\r\n</Password>\r\n", encpassword);
355 fclose(FH);
356 } else {
357 if (encpassword) free(encpassword);
85835c2e »
2010-05-11 coverity fixes
358 if (tmp) free(tmp);
359 if (newpass) free(newpass);
39f2a406 »
2010-09-17 changed the contents of floppy that's get passed to Windows
360 if (newInstName) free(newInstName);
f8253098 »
2010-03-20 set up console output with the right <Password> format
361 return(1);
362 }
363 if (encpassword) free(encpassword);
85835c2e »
2010-05-11 coverity fixes
364 if (tmp) free(tmp);
365 if (newpass) free(newpass);
39f2a406 »
2010-09-17 changed the contents of floppy that's get passed to Windows
366 if (newInstName) free(newInstName);
11a65e9f »
2010-02-22 some helper functions for windows image preparation
367 return(0);
368 }
Something went wrong with that request. Please try again.