comparison cinst/main.c @ 163:8cfcd38a9bb3

Change coding style for cinst main / windowsstore to GNU The kdelibs / qt coding style is not really suited for plain C and looks irritiating when combined with standard C functions.
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 25 Mar 2014 10:29:12 +0000
parents a4b1c77f3e6a
children 4a20bab3f806
comparison
equal deleted inserted replaced
162:4a4b5e640d1a 163:8cfcd38a9bb3
57 * @param[out] to_remove strv of remove 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 58 * @param[out] all_certs strv of uninstallation instructions or NULL
59 * 59 *
60 * @returns: 0 on success. An error code otherwise. 60 * @returns: 0 on success. An error code otherwise.
61 */ 61 */
62 int readInput(char **certificate_list, char ***to_install, 62 int
63 char ***to_remove, char ***all_certs) 63 readInput (char **certificate_list, char ***to_install,
64 char ***to_remove, char ***all_certs)
64 { 65 {
65 int lines_read = 0; 66 int lines_read = 0;
66 int readingList = 0; 67 int readingList = 0;
67 size_t list_size = 0; 68 size_t list_size = 0;
68 char buf[MAX_LINE_LENGTH + 2]; 69 char buf[MAX_LINE_LENGTH + 2];
69 70
70 if (*certificate_list || *to_install || *to_remove) { 71 if (*certificate_list || *to_install || *to_remove)
71 printf("Error invalid parameters\n"); 72 {
72 return -1; 73 printf ("Error invalid parameters\n");
73 } 74 return -1;
74 75 }
75 while (fgets(buf, MAX_LINE_LENGTH + 1, stdin)) { 76
76 size_t len = strlen(buf); /* fgets ensures buf is terminated */ 77 while (fgets (buf, MAX_LINE_LENGTH + 1, stdin) )
77 if (len <= 3) { 78 {
78 printf("Line too short.\n"); 79 size_t len = strlen (buf); /* fgets ensures buf is terminated */
79 return ERR_INVALID_INPUT; 80 if (len <= 3)
80 } 81 {
81 if (lines_read ++ > MAX_LINES) { 82 printf ("Line too short.\n");
82 printf("Too many lines\n"); 83 return ERR_INVALID_INPUT;
83 return ERR_TOO_MUCH_INPUT; 84 }
84 } 85 if (lines_read++ > MAX_LINES)
85 86 {
86 if (buf[len-2] != '\r') { 87 printf ("Too many lines\n");
87 if (buf[len-1] != '\n') { 88 return ERR_TOO_MUCH_INPUT;
88 printf("Line too long.\n"); 89 }
89 return ERR_INVALID_INPUT; 90
91 if (buf[len - 2] != '\r')
92 {
93 if (buf[len - 1] != '\n')
94 {
95 printf ("Line too long.\n");
96 return ERR_INVALID_INPUT;
90 } 97 }
91 buf[len-1] = '\r'; 98 buf[len - 1] = '\r';
92 buf[len] = '\n'; 99 buf[len] = '\n';
93 buf[len+1] = '\0'; 100 buf[len + 1] = '\0';
94 len++; 101 len++;
95 } 102 }
96 103
97 if (strcmp("-----BEGIN CERTIFICATE LIST-----\r\n", buf) == 0){ 104 if (strcmp ("-----BEGIN CERTIFICATE LIST-----\r\n", buf) == 0)
98 readingList = 1; 105 {
99 continue; 106 readingList = 1;
100 } 107 continue;
101 if (strcmp("-----END CERTIFICATE LIST-----\r\n", buf) == 0){ 108 }
102 readingList = 0; 109 if (strcmp ("-----END CERTIFICATE LIST-----\r\n", buf) == 0)
103 continue; 110 {
104 } 111 readingList = 0;
105 if (readingList) { 112 continue;
106 str_append_str(certificate_list, &list_size, buf, len); 113 }
107 } else if (strcmp("UNINSTALL\r\n", buf) == 0) { 114 if (readingList)
108 /* Remove trailing \r\n */ 115 {
109 strv_append(to_remove, buf, len - 2); 116 str_append_str (certificate_list, &list_size, buf, len);
110 continue; 117 }
111 } 118 else if (strcmp ("UNINSTALL\r\n", buf) == 0)
112 if (*buf == 'I') { 119 {
113 /* Remove leading I: and trailing \r\n */ 120 /* Remove trailing \r\n */
114 strv_append(readingList ? all_certs : to_install, 121 strv_append (to_remove, buf, len - 2);
115 buf+2, len - 4); 122 continue;
116 continue; 123 }
117 } 124 if (*buf == 'I')
118 if (*buf == 'R') { 125 {
119 /* Remove leading R: and trailing \r\n */ 126 /* Remove leading I: and trailing \r\n */
120 strv_append(readingList ? all_certs : to_remove, 127 strv_append (readingList ? all_certs : to_install,
121 buf+2, len - 4); 128 buf + 2, len - 4);
122 continue; 129 continue;
123 } 130 }
124 } 131 if (*buf == 'R')
125 132 {
126 return 0; 133 /* Remove leading R: and trailing \r\n */
134 strv_append (readingList ? all_certs : to_remove, buf + 2, len - 4);
135 continue;
136 }
137 }
138
139 return 0;
127 } 140 }
128 141
129 /** @brief Check that the insturctions match to the list 142 /** @brief Check that the insturctions match to the list
130 * 143 *
131 * Only certificates part of the certificate_list are allowed 144 * Only certificates part of the certificate_list are allowed
134 * @param[in] all_certs strv of all valid certificates in a list 147 * @param[in] all_certs strv of all valid certificates in a list
135 * @param[in] to_validate strv of instructions 148 * @param[in] to_validate strv of instructions
136 * 149 *
137 * @returns 0 on success, an error otherwise 150 * @returns 0 on success, an error otherwise
138 */ 151 */
139 int validate_instructions(char **all_certs, 152 int
140 char **to_validate) 153 validate_instructions (char **all_certs, char **to_validate)
141 { 154 {
142 int i = 0, 155 int i = 0, j = 0;
143 j = 0; 156
144 157 if (!all_certs || strv_length (all_certs) < 1)
145 if (!all_certs || strv_length(all_certs) < 1) { 158 {
146 /* Invalid parameters */ 159 /* Invalid parameters */
147 return -1; 160 return -1;
148 } 161 }
149 162
150 if (to_validate == NULL) { 163 if (to_validate == NULL)
151 /* Nothing is valid */ 164 {
152 return 0; 165 /* Nothing is valid */
153 } 166 return 0;
154 167 }
155 for (i=0; to_validate[i]; i++) { 168
156 bool found = false; 169 for (i = 0; to_validate[i]; i++)
157 for (j=0; all_certs[j]; j++) { 170 {
158 if (strncmp(to_validate[i], all_certs[j], MAX_LINE_LENGTH - 2) == 0) { 171 bool found = false;
159 found = true; 172 for (j = 0; all_certs[j]; j++)
160 break; 173 {
174 if (strncmp (to_validate[i], all_certs[j], MAX_LINE_LENGTH - 2) ==
175 0)
176 {
177 found = true;
178 break;
161 } 179 }
162 } 180 }
163 if (!found) { 181 if (!found)
164 printf("Install instruction with invalid certificate\n."); 182 {
165 return ERR_INVALID_INSTRUCTIONS; 183 printf ("Install instruction with invalid certificate\n.");
166 } 184 return ERR_INVALID_INSTRUCTIONS;
167 } 185 }
168 186 }
169 return 0; 187
188 return 0;
170 } 189 }
171 190
172 191
173 int main() { 192 int
174 char **to_install = NULL; 193 main ()
175 char **to_remove = NULL; 194 {
176 char **all_certs = NULL; 195 char **to_install = NULL;
177 char *certificate_list = NULL; 196 char **to_remove = NULL;
178 size_t list_len = 0; 197 char **all_certs = NULL;
179 int ret = -1; 198 char *certificate_list = NULL;
180 bool uninstall = false; 199 size_t list_len = 0;
181 200 int ret = -1;
182 ret = readInput(&certificate_list, &to_install, &to_remove, &all_certs); 201 bool uninstall = false;
183 202
184 if (ret) { 203 ret = readInput (&certificate_list, &to_install, &to_remove, &all_certs);
185 return ret; 204
186 } 205 if (ret)
187 206 {
188 if (!certificate_list) { 207 return ret;
189 return ERR_INVALID_INPUT_NO_LIST; 208 }
190 } 209
191 210 if (!certificate_list)
192 list_len = strnlen(certificate_list, MAX_INPUT_SIZE); 211 {
193 212 return ERR_INVALID_INPUT_NO_LIST;
194 ret = verify_list(certificate_list, list_len); 213 }
195 214
196 if (ret) { 215 list_len = strnlen (certificate_list, MAX_INPUT_SIZE);
197 return ERR_INVALID_SIGNATURE; 216
198 } 217 ret = verify_list (certificate_list, list_len);
199 218
200 if (!strv_length(to_install) && !strv_length(to_remove)) { 219 if (ret)
201 return ERR_NO_INSTRUCTIONS; 220 {
202 } 221 return ERR_INVALID_SIGNATURE;
203 222 }
204 223
205 /* Check that the instructions are ok to execute */ 224 if (!strv_length (to_install) && !strv_length (to_remove) )
206 if (to_install) { 225 {
207 ret = validate_instructions(all_certs, to_install); 226 return ERR_NO_INSTRUCTIONS;
208 if (ret) { 227 }
209 return ret; 228
210 } 229
211 } 230 /* Check that the instructions are ok to execute */
212 231 if (to_install)
213 if (to_remove) { 232 {
214 if (to_remove[0] && strncmp("UNINSTALL", to_remove[0], MAX_LINE_LENGTH) == 0) { 233 ret = validate_instructions (all_certs, to_install);
215 uninstall = true; 234 if (ret)
216 strv_free(to_remove); 235 {
217 to_remove = NULL; 236 return ret;
218 } else { 237 }
219 ret = validate_instructions(all_certs, to_remove); 238 }
220 if (ret) { 239
221 return ret; 240 if (to_remove)
241 {
242 if (to_remove[0]
243 && strncmp ("UNINSTALL", to_remove[0], MAX_LINE_LENGTH) == 0)
244 {
245 uninstall = true;
246 strv_free (to_remove);
247 to_remove = NULL;
248 }
249 else
250 {
251 ret = validate_instructions (all_certs, to_remove);
252 if (ret)
253 {
254 return ret;
222 } 255 }
223 } 256 }
224 } 257 }
225 258
226 if (uninstall) { 259 if (uninstall)
227 /* To uninstall does not have to be verified as it part of the 260 {
228 * signed list.*/ 261 /* To uninstall does not have to be verified as it part of the
229 to_remove = all_certs; 262 * signed list.*/
230 } else { 263 to_remove = all_certs;
231 strv_free(all_certs); 264 }
232 all_certs = NULL; 265 else
266 {
267 strv_free (all_certs);
268 all_certs = NULL;
233 } 269 }
234 270
235 #ifdef WIN32 271 #ifdef WIN32
236 return write_stores_win (to_install, to_remove, true); 272 return write_stores_win (to_install, to_remove, true);
237 #endif 273 #endif
238 274
239 /* Make valgrind happy */ 275 /* Make valgrind happy */
240 strv_free(to_install); 276 strv_free (to_install);
241 strv_free(to_remove); 277 strv_free (to_remove);
242 free(certificate_list); 278 free (certificate_list);
243 279
244 return 0; 280 return 0;
245 } 281 }

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