Mercurial > trustbridge
comparison cinst/main.c @ 64:fb9f78f7ab2f
Improve error handling free memory before exiting. Include line endings in Marker lines
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Tue, 18 Mar 2014 14:14:15 +0000 |
parents | 6acb1dae6185 |
children | e4088afd5281 |
comparison
equal
deleted
inserted
replaced
63:355800cdefcc | 64:fb9f78f7ab2f |
---|---|
27 #define MAX_LINE_LENGTH 1000 | 27 #define MAX_LINE_LENGTH 1000 |
28 #define MAX_LINES 1000 | 28 #define MAX_LINES 1000 |
29 #define MAX_INPUT_SIZE 2000000 /* MAX_LINE_LENGTH * (MAX_LINES *2) */ | 29 #define MAX_INPUT_SIZE 2000000 /* MAX_LINE_LENGTH * (MAX_LINES *2) */ |
30 | 30 |
31 #include <stdio.h> | 31 #include <stdio.h> |
32 #include <stdlib.h> | |
32 #include <string.h> | 33 #include <string.h> |
33 #include <assert.h> | 34 #include <assert.h> |
34 | 35 |
35 #include "strhelp.h" | 36 #include "strhelp.h" |
36 #include "listutil.h" | 37 #include "listutil.h" |
45 * The caller needs to free the memory allocated by this function | 46 * The caller needs to free the memory allocated by this function |
46 * even when an error is returned. | 47 * even when an error is returned. |
47 * | 48 * |
48 * @returns: 0 on success. An error code otherwise. | 49 * @returns: 0 on success. An error code otherwise. |
49 */ | 50 */ |
50 int readInput(char **certificateList, char ***to_install, | 51 int readInput(char **certificate_list, char ***to_install, |
51 char ***to_remove) | 52 char ***to_remove) |
52 { | 53 { |
53 int lines_read = 0; | 54 int lines_read = 0; |
54 int readingList = 0; | 55 int readingList = 0; |
55 char buf[MAX_LINE_LENGTH + 1]; | 56 char buf[MAX_LINE_LENGTH + 1]; |
58 size_t len = strlen(buf); /* fgets ensures buf is terminated */ | 59 size_t len = strlen(buf); /* fgets ensures buf is terminated */ |
59 if (lines_read ++ > MAX_LINES) { | 60 if (lines_read ++ > MAX_LINES) { |
60 printf("Too many lines\n"); | 61 printf("Too many lines\n"); |
61 return ERR_TOO_MUCH_INPUT; | 62 return ERR_TOO_MUCH_INPUT; |
62 } | 63 } |
63 if (strcmp("-----BEGIN CERTIFICATE LIST-----", buf) == 0){ | 64 if (strcmp("-----BEGIN CERTIFICATE LIST-----\r\n", buf) == 0){ |
64 readingList = 1; | 65 readingList = 1; |
65 continue; | 66 continue; |
66 } | 67 } |
67 if (strcmp("-----END CERTIFICATE LIST-----", buf) == 0){ | 68 if (strcmp("-----END CERTIFICATE LIST-----\r\n", buf) == 0){ |
68 readingList = 0; | 69 readingList = 0; |
69 continue; | 70 continue; |
70 } | 71 } |
71 if (readingList) { | 72 if (readingList) { |
72 str_append_str(certificateList, buf, len); | 73 str_append_str(certificate_list, buf, len); |
73 continue; | 74 continue; |
74 } | 75 } |
75 if (*buf == 'I') { | 76 if (*buf == 'I') { |
76 array_append_str(to_install, buf+2, len - 2); | 77 array_append_str(to_install, buf+2, len - 2); |
77 continue; | 78 continue; |
86 } | 87 } |
87 | 88 |
88 return 0; | 89 return 0; |
89 } | 90 } |
90 | 91 |
92 int validate_instructions(const char *certificate_list, | |
93 const size_t listLen, | |
94 const char **to_install, | |
95 const char **to_remove) | |
96 { | |
97 /* TODO */ | |
98 return 0; | |
99 } | |
100 | |
91 int main() { | 101 int main() { |
92 | |
93 char **to_install = NULL; | 102 char **to_install = NULL; |
94 char **to_remove = NULL; | 103 char **to_remove = NULL; |
95 char *certificateList = NULL; | 104 char *certificate_list = NULL; |
105 size_t listLen = 0; | |
96 int ret = -1; | 106 int ret = -1; |
97 | 107 |
98 ret = readInput(&certificateList, &to_install, &to_remove); | 108 ret = readInput(&certificate_list, &to_install, &to_remove); |
99 | 109 |
100 if (ret != 0) { | 110 if (ret != 0) { |
101 return ret; | 111 return ret; |
102 } | 112 } |
103 | 113 |
104 if (!certificateList) { | 114 if (!certificate_list) { |
105 return ERR_INVALID_INPUT_NO_LIST; | 115 return ERR_INVALID_INPUT_NO_LIST; |
106 } | 116 } |
107 | 117 |
108 ret = verify_list(certificateList, strlen(certificateList)); | 118 listLen = strnlen(certificate_list, MAX_INPUT_SIZE); |
109 | 119 |
110 printf ("Verify List returned %i\n", ret); | 120 ret = verify_list(certificate_list, listLen); |
121 | |
122 if (ret != 0) { | |
123 return ERR_INVALID_SIGNATURE; | |
124 } | |
125 | |
126 if (!strv_length(to_install) && !strv_length(to_remove)) { | |
127 return ERR_NO_INSTRUCTIONS; | |
128 } | |
129 | |
130 /* Check that the instructions are ok to execute */ | |
131 ret = validate_instructions(certificate_list, to_install, to_remove); | |
132 | |
133 if (ret != 0) { | |
134 return ERR_INVALID_INSTRUCTIONS; | |
135 } | |
136 | |
137 /* Make valgrind happy */ | |
138 strfreev (to_install); | |
139 strfreev (to_remove); | |
140 free (certificate_list); | |
111 | 141 |
112 return 0; | 142 return 0; |
113 } | 143 } |