comparison cinst/nssstore_win.c @ 489:a9da8e4eeff7

Fix instruction writing for Windows. Back to native winapi. What happens if you convert a Winapi handle to a c handle and how closing etc. works is to badly documented for me to be comfortable to use it.
author Andre Heinecke <aheinecke@intevation.de>
date Thu, 24 Apr 2014 15:46:35 +0000
parents 17e1c8f37d72
children e551de11d8b6
comparison
equal deleted inserted replaced
488:b8b0f9685ffa 489:a9da8e4eeff7
73 /**@brief Write strv of instructions to a handle 73 /**@brief Write strv of instructions to a handle
74 * 74 *
75 * Writes the null terminated list of instructions to 75 * Writes the null terminated list of instructions to
76 * the handle. 76 * the handle.
77 * 77 *
78 * @param [in] base64 encoded der certificates to write 78 * @param [in] certificates base64 encoded der certificate to write
79 * @param [in] write_handle to write to 79 * @param [in] write_handle handle to write to
80 * @param [in] remove weather the certificate should be installed or removed 80 * @param [in] remove weather the certificate should be installed or removed
81 * 81 *
82 * @returns true on success, false on failure 82 * @returns true on success, false on failure
83 */ 83 */
84 static bool 84 static bool
85 write_instructions(char **certificates, HANDLE write_handle, 85 write_instructions(char **certificates, HANDLE write_handle,
86 bool remove) 86 bool remove)
87 { 87 {
88 bool retval = false;
88 int i = 0; 89 int i = 0;
89 int cHandle = -1; 90 const char *line_end = "\r\n";
90 FILE *write_stream = NULL; 91 char *line_start = NULL;
91 92
92 if (!certificates) 93 if (!certificates)
93 { 94 {
94 return true; 95 return true;
95 } 96 }
96 97
97 cHandle = _open_osfhandle ((intptr_t)write_handle, 0); 98 line_start = remove ? "R:" : "I:";
98 99
99 if (cHandle == -1)
100 {
101 ERRORPRINTF ("Failed to open write handle.\n");
102 }
103
104 write_stream = _fdopen(cHandle, "w");
105 for (i = 0; certificates[i]; i++) 100 for (i = 0; certificates[i]; i++)
106 { 101 {
107 int ret = 0; 102 DWORD written = 0;
108 if (remove) 103 DWORD inst_len = strlen (certificates[i]);
109 ret = fprintf (write_stream, "R:%s\n", certificates[i]); 104 retval = WriteFile (write_handle, (LPCVOID) line_start, 2, &written, NULL);
110 else 105 if (!retval)
111 ret = fprintf (write_stream, "I:%s\n", certificates[i]); 106 {
112 107 PRINTLASTERROR ("Failed to write line start\n");
113 if (ret <= 0) 108 return false;
114 { 109 }
115 DEBUGPRINTF ("Failed to write everything.\n"); 110 if (written != 2)
116 break; 111 {
117 } 112 ERRORPRINTF ("Failed to write line start\n");
118 } 113 retval = false;
119 114 return false;
115 }
116 written = 0;
117 retval = WriteFile (write_handle, (LPCVOID) certificates[i], inst_len, &written, NULL);
118 if (!retval)
119 {
120 PRINTLASTERROR ("Failed to write certificate\n");
121 return false;
122 }
123 if (inst_len != written)
124 {
125 ERRORPRINTF ("Failed to write everything\n");
126 retval = false;
127 return false;
128 }
129 written = 0;
130 retval = WriteFile (write_handle, (LPCVOID) line_end, 2, &written, NULL);
131 if (!retval)
132 {
133 PRINTLASTERROR ("Failed to write line end\n");
134 return false;
135 }
136 if (written != 2)
137 {
138 ERRORPRINTF ("Failed to write full line end\n");
139 retval = false;
140 return false;
141 }
142 }
120 return true; 143 return true;
121 } 144 }
122 145
123 /**@brief Start the process to install / remove 146 /**@brief Start the process to install / remove
124 * 147 *
451 474
452 hFile = CreateFileW(path, 475 hFile = CreateFileW(path,
453 GENERIC_WRITE, 476 GENERIC_WRITE,
454 0, /* don't share */ 477 0, /* don't share */
455 NULL, /* use the security attributes from the folder */ 478 NULL, /* use the security attributes from the folder */
456 OPEN_ALWAYS, 479 OPEN_ALWAYS | TRUNCATE_EXISTING,
457 0, 480 0,
458 NULL); 481 NULL);
459 482
460 if (hFile == INVALID_HANDLE_VALUE) 483 if (hFile == INVALID_HANDLE_VALUE)
461 { 484 {

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