comparison cinst/main.c @ 148:095d0e7f8ed4

Add instruction verification and handle uninstall command in input
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 24 Mar 2014 17:21:25 +0000
parents 4904fe01055d
children bd5a5d3e5674
comparison
equal deleted inserted replaced
147:fc9af77b06b9 148:095d0e7f8ed4
28 **/ 28 **/
29 #include <stdio.h> 29 #include <stdio.h>
30 #include <stdlib.h> 30 #include <stdlib.h>
31 #include <string.h> 31 #include <string.h>
32 #include <assert.h> 32 #include <assert.h>
33 #include <stdbool.h>
33 34
34 #include "strhelp.h" 35 #include "strhelp.h"
35 #include "listutil.h" 36 #include "listutil.h"
36 #include "errorcodes.h" 37 #include "errorcodes.h"
37 #include "windowsstore.h" 38 #include "windowsstore.h"
47 * Terminates in OOM conditions. 48 * Terminates in OOM conditions.
48 * 49 *
49 * The caller needs to free the memory allocated by this function 50 * The caller needs to free the memory allocated by this function
50 * even when an error is returned. 51 * even when an error is returned.
51 * 52 *
53 * Uninstall certificates are all certificates that are pa
54 *
55 * @param[out] certificate_list the parsed certificate list
56 * @param[out] to_install strv of installation instructions or NULL
57 * @param[out] to_remove strv of remove instructions or NULL
58 * @param[out] all_certs strv of uninstallation instructions or NULL
59 *
52 * @returns: 0 on success. An error code otherwise. 60 * @returns: 0 on success. An error code otherwise.
53 */ 61 */
54 int readInput(char **certificate_list, char ***to_install, 62 int readInput(char **certificate_list, char ***to_install,
55 char ***to_remove) 63 char ***to_remove, char ***all_certs)
56 { 64 {
57 int lines_read = 0; 65 int lines_read = 0;
58 int readingList = 0; 66 int readingList = 0;
59 size_t list_size = 0; 67 size_t list_size = 0;
60 char buf[MAX_LINE_LENGTH + 2]; 68 char buf[MAX_LINE_LENGTH + 2];
94 readingList = 0; 102 readingList = 0;
95 continue; 103 continue;
96 } 104 }
97 if (readingList) { 105 if (readingList) {
98 str_append_str(certificate_list, &list_size, buf, len); 106 str_append_str(certificate_list, &list_size, buf, len);
107 } else if (strcmp("UNINSTALL", buf) == 0) {
108 /* Remove trailing \r\n */
109 strv_append(to_remove, buf, len - 2);
99 continue; 110 continue;
100 } 111 }
101 if (*buf == 'I') { 112 if (*buf == 'I') {
102 /* Remove leading I: and trailing \r\n */ 113 /* Remove leading I: and trailing \r\n */
103 strv_append(to_install, buf+2, len - 4); 114 strv_append(readingList ? all_certs : to_install,
115 buf+2, len - 4);
104 continue; 116 continue;
105 } 117 }
106 if (*buf == 'R') { 118 if (*buf == 'R') {
107 /* Remove leading R: and trailing \r\n */ 119 /* Remove leading R: and trailing \r\n */
108 strv_append(to_remove, buf+2, len - 4); 120 strv_append(readingList ? all_certs : to_remove,
109 continue; 121 buf+2, len - 4);
110 } 122 continue;
111 if (strcmp("UNINSTALL", buf) == 0) {
112 /* Remove trailing \r\n */
113 strv_append(to_remove, buf, len - 2);
114 } 123 }
115 } 124 }
116 125
117 return 0; 126 return 0;
118 } 127 }
119 /* 128
120 int validate_instructions(const char *certificate_list, 129 /** @brief Check that the insturctions match to the list
121 const size_t list_len, 130 *
122 const char **to_install, 131 * Only certificates part of the certificate_list are allowed
123 const char **to_remove) 132 * for installation.
133 *
134 * @param[in] all_certs strv of all valid certificates in a list
135 * @param[in] to_validate strv of instructions
136 *
137 * @returns 0 on success, an error otherwise
138 */
139 int validate_instructions(char **all_certs,
140 char **to_validate)
124 { 141 {
125 TODO 142 int i = 0,
126 (void *) certificate_list; 143 j = 0;
127 (void **) to_install; 144
128 (void **) to_remove; 145 if (!all_certs || strv_length(all_certs) < 1) {
129 (void) list_len; 146 /* Invalid parameters */
147 return -1;
148 }
149
150 if (to_validate == NULL) {
151 /* Nothing is valid */
152 return 0;
153 }
154
155 for (i=0; to_validate[i]; i++) {
156 bool found = false;
157 for (j=0; all_certs[j]; j++) {
158 if (strncmp(to_validate[i], all_certs[j], MAX_LINE_LENGTH - 2) == 0) {
159 found = true;
160 break;
161 }
162 }
163 if (!found) {
164 printf("Install instruction with invalid certificate\n.");
165 return ERR_INVALID_INSTRUCTIONS;
166 }
167 }
130 168
131 return 0; 169 return 0;
132 } 170 }
133 */ 171
134 172
135 int main() { 173 int main() {
136 char **to_install = NULL; 174 char **to_install = NULL;
137 char **to_remove = NULL; 175 char **to_remove = NULL;
176 char **all_certs = NULL;
138 char *certificate_list = NULL; 177 char *certificate_list = NULL;
139 size_t list_len = 0; 178 size_t list_len = 0;
140 int ret = -1; 179 int ret = -1;
141 /* 180 bool uninstall = false;
142 i = 0 , 181
143 uninstall = 0; 182 ret = readInput(&certificate_list, &to_install, &to_remove, &all_certs);
144 */ 183
145 ret = readInput(&certificate_list, &to_install, &to_remove); 184 if (ret) {
146
147 if (ret != 0) {
148 return ret; 185 return ret;
149 } 186 }
150 187
151 if (!certificate_list) { 188 if (!certificate_list) {
152 return ERR_INVALID_INPUT_NO_LIST; 189 return ERR_INVALID_INPUT_NO_LIST;
154 191
155 list_len = strnlen(certificate_list, MAX_INPUT_SIZE); 192 list_len = strnlen(certificate_list, MAX_INPUT_SIZE);
156 193
157 ret = verify_list(certificate_list, list_len); 194 ret = verify_list(certificate_list, list_len);
158 195
159 if (ret != 0) { 196 if (ret) {
160 return ERR_INVALID_SIGNATURE; 197 return ERR_INVALID_SIGNATURE;
161 } 198 }
162 199
163 if (!strv_length(to_install) && !strv_length(to_remove)) { 200 if (!strv_length(to_install) && !strv_length(to_remove)) {
164 return ERR_NO_INSTRUCTIONS; 201 return ERR_NO_INSTRUCTIONS;
165 } 202 }
166 203
167 204
168 /* Check that the instructions are ok to execute 205 /* Check that the instructions are ok to execute */
169 ret = validate_instructions(certificate_list, list_len, to_install, 206 if (to_install) {
170 to_remove); 207 ret = validate_instructions(all_certs, to_install);
171 if (ret != 0) { 208 if (ret) {
172 return ERR_INVALID_INSTRUCTIONS; 209 return ret;
210 }
173 } 211 }
174 212
175 if (to_remove) { 213 if (to_remove) {
176 for (i=0; to_remove[i]; i++) { 214 if (to_remove[0] && strncmp("UNINSTALL", to_remove[0], MAX_LINE_LENGTH) == 0) {
177 if (strncmp("UNINSTALL", to_remove[i], MAX_LINE_LENGTH)) { 215 uninstall = true;
178 uninstall = 1; 216 strv_free(to_remove);
179 break; 217 to_remove = NULL;
218 } else {
219 ret = validate_instructions(all_certs, to_remove);
220 if (ret) {
221 return ret;
180 } 222 }
181 } 223 }
182 } 224 }
183 225
184 if (uninstall) { 226 if (uninstall) {
185 227 /* To uninstall does not have to be verified as it part of the
186 } 228 * signed list.*/
187 */ 229 to_remove = all_certs;
230 } else {
231 strv_free(all_certs);
232 all_certs = NULL;
233 }
188 234
189 #ifdef WIN32 235 #ifdef WIN32
190 return install_certificates_win((const char**) to_install, 1); 236 return install_certificates_win((const char**) to_install, 1);
191 //remove_certificates_win((const char**) to_remove, 1); 237 //remove_certificates_win((const char**) to_remove, 1);
192 #endif 238 #endif

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