Mercurial > trustbridge
annotate cinst/main.c @ 60:6acb1dae6185
Use strn functions and improve error handling.
Even if we know the strings are NULL terminated we use
length terminated string functions after a first strlen
this makes it easier to assert that at one point we know
the string is terminated and afterwards use the length
of that.
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Tue, 18 Mar 2014 11:28:02 +0000 |
parents | 3f6378647371 |
children | fb9f78f7ab2f |
rev | line source |
---|---|
25
92108a2120f1
Move certificatelist to common. Add cinst directory
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
1 /** @brief Main entry point for the cinst process. |
92108a2120f1
Move certificatelist to common. Add cinst directory
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
2 * |
92108a2120f1
Move certificatelist to common. Add cinst directory
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
3 * The cinst process may or may not be run with elevated |
92108a2120f1
Move certificatelist to common. Add cinst directory
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
4 * privileges. When run with elevated privileges this |
92108a2120f1
Move certificatelist to common. Add cinst directory
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
5 * process will modify system wide certificate stores. |
92108a2120f1
Move certificatelist to common. Add cinst directory
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
6 * Otherwise only the users certificate stores are modified. |
92108a2120f1
Move certificatelist to common. Add cinst directory
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
7 * |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
8 * It expects a certificatelist on stdin enclosed in a |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
9 * -----BEGIN CERTIFICATE LIST----- |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
10 * ... |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
11 * -----END CERTIFICATE LIST----- |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
12 * |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
13 * Followed by additional instruction lines of: |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
14 * I:<certificate> |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
15 * R:<certificate> |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
16 * |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
17 * It will only execute the instructions if the |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
18 * I and R instructions are also part of the signed |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
19 * certificate list. The signature is validated with the |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
20 * built in key. |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
21 * |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
22 * The special instruction "UNINSTALL" will cause the installer |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
23 * to remove all certificates (Even those marked with I) that |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
24 * are part of the list to be removed. |
25
92108a2120f1
Move certificatelist to common. Add cinst directory
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
25 * |
92108a2120f1
Move certificatelist to common. Add cinst directory
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
26 **/ |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
27 #define MAX_LINE_LENGTH 1000 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
28 #define MAX_LINES 1000 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
29 #define MAX_INPUT_SIZE 2000000 /* MAX_LINE_LENGTH * (MAX_LINES *2) */ |
25
92108a2120f1
Move certificatelist to common. Add cinst directory
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
30 |
92108a2120f1
Move certificatelist to common. Add cinst directory
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
31 #include <stdio.h> |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
32 #include <string.h> |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
33 #include <assert.h> |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
34 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
35 #include "strhelp.h" |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
36 #include "listutil.h" |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
37 #include "errorcodes.h" |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
38 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
39 /* @brief Read stdin into data structures. |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
40 * |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
41 * Reads from stdin and sorts the input into the respective |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
42 * variables. The pointers returned need to be freed by the caller. |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
43 * Terminates in OOM conditions. |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
44 * |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
45 * The caller needs to free the memory allocated by this function |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
46 * even when an error is returned. |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
47 * |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
48 * @returns: 0 on success. An error code otherwise. |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
49 */ |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
50 int readInput(char **certificateList, char ***to_install, |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
51 char ***to_remove) |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
52 { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
53 int lines_read = 0; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
54 int readingList = 0; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
55 char buf[MAX_LINE_LENGTH + 1]; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
56 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
57 while (fgets(buf, MAX_LINE_LENGTH + 1, stdin)) { |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
58 size_t len = strlen(buf); /* fgets ensures buf is terminated */ |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
59 if (lines_read ++ > MAX_LINES) { |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
60 printf("Too many lines\n"); |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
61 return ERR_TOO_MUCH_INPUT; |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
62 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
63 if (strcmp("-----BEGIN CERTIFICATE LIST-----", buf) == 0){ |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
64 readingList = 1; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
65 continue; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
66 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
67 if (strcmp("-----END CERTIFICATE LIST-----", buf) == 0){ |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
68 readingList = 0; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
69 continue; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
70 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
71 if (readingList) { |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
72 str_append_str(certificateList, buf, len); |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
73 continue; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
74 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
75 if (*buf == 'I') { |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
76 array_append_str(to_install, buf+2, len - 2); |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
77 continue; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
78 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
79 if (*buf == 'R') { |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
80 array_append_str(to_remove, buf+2, len - 2); |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
81 continue; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
82 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
83 if (strcmp("UNINSTALL", buf) == 0) { |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
84 array_append_str(to_remove, buf, len - 2); |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
85 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
86 } |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
87 |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
88 return 0; |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
89 } |
25
92108a2120f1
Move certificatelist to common. Add cinst directory
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
90 |
26
cbd57d767dfa
Move layout around. Restructure CMakeLists
Andre Heinecke <andre.heinecke@intevation.de>
parents:
25
diff
changeset
|
91 int main() { |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
92 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
93 char **to_install = NULL; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
94 char **to_remove = NULL; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
95 char *certificateList = NULL; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
96 int ret = -1; |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
97 |
60
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
98 ret = readInput(&certificateList, &to_install, &to_remove); |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
99 |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
100 if (ret != 0) { |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
101 return ret; |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
102 } |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
103 |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
104 if (!certificateList) { |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
105 return ERR_INVALID_INPUT_NO_LIST; |
6acb1dae6185
Use strn functions and improve error handling.
Andre Heinecke <aheinecke@intevation.de>
parents:
59
diff
changeset
|
106 } |
59
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
107 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
108 ret = verify_list(certificateList, strlen(certificateList)); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
109 |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
110 printf ("Verify List returned %i\n", ret); |
3f6378647371
Start work on cinst. Strhelp new helpers to work with C String
Andre Heinecke <aheinecke@intevation.de>
parents:
26
diff
changeset
|
111 |
25
92108a2120f1
Move certificatelist to common. Add cinst directory
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
112 return 0; |
92108a2120f1
Move certificatelist to common. Add cinst directory
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
113 } |