comparison cinst/nssstore_win.c @ 675:4ad764bfb39c

Add writing of the NSS line into the registry
author Andre Heinecke <andre.heinecke@intevation.de>
date Fri, 27 Jun 2014 18:53:52 +0200
parents f1795a232418
children cb40af11ec3a
comparison
equal deleted inserted replaced
674:f1795a232418 675:4ad764bfb39c
53 #include "strhelp.h" 53 #include "strhelp.h"
54 54
55 #ifndef APPNAME 55 #ifndef APPNAME
56 #define APPNAME L"cinst" 56 #define APPNAME L"cinst"
57 #endif 57 #endif
58
59 /**@def The name of the nss installation process */
60 #define NSS_APP_NAME L"mozilla.exe"
58 61
59 #ifndef SELECTION_FILE_NAME 62 #ifndef SELECTION_FILE_NAME
60 #define SELECTION_FILE_NAME L"currently_selected.txt" 63 #define SELECTION_FILE_NAME L"currently_selected.txt"
61 #endif 64 #endif
62 65
356 { 359 {
357 ERRORPRINTF ("Failed to enumeratre profile list. Error: %i", ret); 360 ERRORPRINTF ("Failed to enumeratre profile list. Error: %i", ret);
358 goto done; 361 goto done;
359 } 362 }
360 363
364 error = false;
365
361 done: 366 done:
362 xfree (current_user); 367 xfree (current_user);
363 368
364 RegCloseKey (profile_list); 369 RegCloseKey (profile_list);
365 370
371 if (error) 376 if (error)
372 { 377 {
373 strv_free (retval); 378 strv_free (retval);
374 retval = NULL; 379 retval = NULL;
375 } 380 }
381
382 return retval;
383 }
384
385 /** @brief Build the command line for the NSS installation process
386 *
387 * Caller has to free the return value
388 *
389 * @param [in] selection_file the certificates to install
390 *
391 * @returns the command line to install the certificates. */
392 static wchar_t*
393 get_command_line(wchar_t *selection_file)
394 {
395 LPWSTR retval;
396 char *install_dir = get_install_dir();
397 wchar_t *w_inst_dir;
398 size_t cmd_line_len = 0;
399
400 if (install_dir == NULL)
401 {
402 ERRORPRINTF ("Failed to get installation directory");
403 return NULL;
404 }
405
406 w_inst_dir = utf8_to_wchar (install_dir, strlen(install_dir));
407 xfree (install_dir);
408
409 if (w_inst_dir == NULL)
410 {
411 ERRORPRINTF ("Failed to convert installation directory");
412 return NULL;
413 }
414
415 /* installdir + dirsep + quotes + process name + space + quotes + selection_file
416 + NULL */
417 cmd_line_len = wcslen (w_inst_dir) + 1 + 2 + wcslen (NSS_APP_NAME) +
418 + 1 + 2 + wcslen(selection_file) + 1;
419 retval = xmalloc (cmd_line_len * sizeof(wchar_t));
420
421 wcscpy_s (retval, cmd_line_len, L"\"");
422 wcscat_s (retval, cmd_line_len, w_inst_dir);
423 wcscat_s (retval, cmd_line_len, L"\\");
424 wcscat_s (retval, cmd_line_len, NSS_APP_NAME);
425 wcscat_s (retval, cmd_line_len, L"\" \"");
426 wcscat_s (retval, cmd_line_len, selection_file);
427 wcscat_s (retval, cmd_line_len, L"\"");
376 428
377 return retval; 429 return retval;
378 } 430 }
379 431
380 /**@brief Register NSS process as runOnce for other users 432 /**@brief Register NSS process as runOnce for other users
394 */ 446 */
395 void 447 void
396 register_proccesses_for_others (wchar_t *selection_file) 448 register_proccesses_for_others (wchar_t *selection_file)
397 { 449 {
398 char **hives = locate_other_hives(); 450 char **hives = locate_other_hives();
399 451 int i = 0;
452 wchar_t *run_command = NULL;
453
454 if (hives == NULL)
455 {
456 DEBUGPRINTF ("No hives found.");
457 return;
458 }
459 run_command = get_command_line (selection_file);
460 for (i = 0; hives[i] != NULL; i++)
461 {
462 LONG ret = 0;
463 wchar_t *hivepath = utf8_to_wchar (hives[i], strlen(hives[i]));
464 HKEY key_handle = NULL;
465
466 if (hivepath == NULL)
467 {
468 ERRORPRINTF ("Failed to read hive path");
469 continue;
470 }
471 ret = RegLoadKeyW (HKEY_LOCAL_MACHINE, APPNAME L"_tmphive", hivepath);
472
473 xfree (hivepath);
474 hivepath = NULL;
475
476 if (ret != ERROR_SUCCESS)
477 {
478 /* This is somewhat expected if the registry is not located
479 in the standard location. Failure is accepted in that case. */
480 PRINTLASTERROR ("Failed to load hive.");
481 continue;
482 }
483
484 ret = RegOpenKeyExW (HKEY_LOCAL_MACHINE,
485 APPNAME L"_tmphive\\Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce",
486 0,
487 KEY_WRITE,
488 &key_handle);
489
490 if (ret != ERROR_SUCCESS)
491 {
492 ERRORPRINTF ("Failed to find RunOnce key in other registry.");
493 RegUnLoadKey (HKEY_LOCAL_MACHINE, APPNAME L"_tmphive");
494 continue;
495 }
496
497 ret = RegSetValueExW (key_handle, APPNAME, 0, REG_SZ, (LPBYTE) run_command,
498 (wcslen(run_command) + 1) * sizeof(wchar_t));
499
500 if (ret != ERROR_SUCCESS)
501 {
502 ERRORPRINTF ("Failed to write RunOnce key.");
503 }
504
505 RegCloseKey (key_handle);
506 RegUnLoadKey (HKEY_LOCAL_MACHINE, APPNAME L"_tmphive");
507 }
508
509 xfree (run_command);
400 strv_free (hives); 510 strv_free (hives);
401 printf("Selection file %S", selection_file);
402 } 511 }
403 512
404 /**@brief Start the process to install / remove 513 /**@brief Start the process to install / remove
405 * 514 *
406 * Starts the NSS installation process for the current user 515 * Starts the NSS installation process for the current user
419 LPWSTR lpApplicationName = L"mozilla.exe", 528 LPWSTR lpApplicationName = L"mozilla.exe",
420 lpCommandLine; 529 lpCommandLine;
421 PROCESS_INFORMATION piProcInfo = {0}; 530 PROCESS_INFORMATION piProcInfo = {0};
422 STARTUPINFOW siStartInfo = {0}; 531 STARTUPINFOW siStartInfo = {0};
423 BOOL success = FALSE; 532 BOOL success = FALSE;
424 size_t cmd_line_len = 0;
425 533
426 if (!selection_file) 534 if (!selection_file)
427 { 535 {
428 ERRORPRINTF ("Invalid call\n"); 536 ERRORPRINTF ("Invalid call\n");
429 return false; 537 return false;
439 } 547 }
440 /* TODO! if (is_elevated()) 548 /* TODO! if (is_elevated())
441 restrict token -> hChildToken 549 restrict token -> hChildToken
442 */ 550 */
443 551
444 cmd_line_len = wcslen (lpApplicationName) + wcslen(selection_file) + 2; 552 lpCommandLine = get_command_line (selection_file);
445 lpCommandLine = xmalloc (cmd_line_len * sizeof(wchar_t)); 553
446 554 if (lpCommandLine == NULL)
447 wcscpy_s (lpCommandLine, cmd_line_len, lpApplicationName); 555 {
448 wcscpy_s (lpCommandLine, cmd_line_len, L" "); 556 ERRORPRINTF ("Failed to build command line.");
449 wcscat_s (lpCommandLine, cmd_line_len, selection_file); 557 return false;
558 }
450 559
451 DEBUGPRINTF ("Starting %S with command line %S\n", lpApplicationName, lpCommandLine); 560 DEBUGPRINTF ("Starting %S with command line %S\n", lpApplicationName, lpCommandLine);
452 561
453 success = CreateProcessAsUserW (hToken, 562 success = CreateProcessAsUserW (hToken,
454 lpApplicationName, 563 lpApplicationName,

http://wald.intevation.org/projects/trustbridge/