comparison cinst/mozilla.c @ 113:02ad0922c01f

Start over (only leave comments).
author Sascha Wilde <wilde@intevation.de>
date Fri, 21 Mar 2014 17:12:53 +0100
parents 0c8ea71a89cd
children 24ca8e2ceecf
comparison
equal deleted inserted replaced
112:9bfa2a60b9b8 113:02ad0922c01f
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/stat.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <fcntl.h>
7 #include <errno.h>
8
9 /* @file Mozilla installation process 1 /* @file Mozilla installation process
10 * 2 *
11 * Reads from stdin a list of instructions in the form: 3 * Reads from stdin a list of instructions in the form:
12 * 4 *
13 * I:<base64 DER econded certificate>\r\n 5 * I:<base64 DER econded certificate>\r\n
52 * 44 *
53 * Mozilla also accepts the ini file on Windows even if it is UTF-16 45 * Mozilla also accepts the ini file on Windows even if it is UTF-16
54 * encoded. 46 * encoded.
55 * */ 47 * */
56 48
49 #include <errorcodes.h>
50 #include <stdlib.h>
51
52 #ifndef _WIN32
53 #define UNIX 1
54 #else
55 #define UNIX 0
56 #endif
57
57 58
58 /** 59 /**
59 * @brief Read a file into memory. 60 * @brief Get a list of all mozilla profile directories
60 *
61 * @param[in] fileName Name of the file (UTF-8 encoded).
62 * @param[out] data Newly allocated pointer to the file content.
63 * @param[out] size Size in Bytes of the file content.
64 * @param[in] maxSize the maximum size to read.
65 *
66 * @return 0 on success an error code otherwise.
67 */
68
69 #define RAF_UNKNOWN -1
70 #define RAF_UNREADABLE -2
71 #define RAF_STATFAILED -3
72 #define RAF_TOOLARGE -4
73 #define RAF_OUTOFCORE -5
74 int readFile(const char *fileName, char **data, size_t *size,
75 const size_t maxSize)
76 {
77 /* TODO
78 * split out the read file from common/listutil.c and use that. */
79 int fd = -1;
80 struct stat fileStat;
81 int rc = 0;
82 ssize_t bRead = 0;
83 int retval = -1;
84
85 memset(&fileStat, 0, sizeof(fileStat));
86
87 fd = open(fileName, O_RDONLY);
88 if (fd == -1) {
89 retval = RAF_UNREADABLE;
90 goto cleanup;
91 }
92
93 rc = fstat(fd, &fileStat);
94 if (rc < 0) {
95 retval = RAF_STATFAILED;
96 goto cleanup;
97 }
98
99 // Check the size of the file
100 if (!fileStat.st_size) {
101 retval = RAF_STATFAILED;
102 goto cleanup;
103 }
104
105 if (fileStat.st_size > maxSize &&
106 fileStat.st_size > 0) {
107 retval = RAF_TOOLARGE;
108 goto cleanup;
109 }
110
111 *size = (size_t) fileStat.st_size;
112
113 *data = (char*) malloc(*size);
114
115 if (*data == NULL) {
116 retval = RAF_OUTOFCORE;
117 goto cleanup;
118 }
119
120 bRead = read(fd, *data, *size);
121
122 if (bRead < 0 || (size_t) bRead != *size) {
123 if (bRead == -1) {
124 printf("Error: %s \n", strerror(errno));
125 }
126 retval = RAF_UNKNOWN;
127 *size = 0;
128 if (*data) {
129 free(*data);
130 *data = NULL;
131 }
132 goto cleanup;
133 }
134
135 cleanup:
136
137 if (fd && fd != -1) {
138 close(fd);
139 fd = -1;
140 }
141
142 return retval;
143 }
144
145
146 #ifndef _WIN32
147
148 #define INI_LOCATIONS { \
149 "/.mozilla/firefox/profiles.ini", \
150 "/.mozilla/thunderbird/profiles.ini", \
151 NULL }
152
153 /**
154 * @brief Get a list of all mozilla profile directories for this user
155 * 61 *
156 * Read the profiles.ini and extract all profile paths from that. 62 * Read the profiles.ini and extract all profile paths from that.
157 * 63 *
64 * @param[inifile] path of the profile.ini to read.
158 * @return NULL terminated array of strings containing containing the 65 * @return NULL terminated array of strings containing containing the
159 * absolute path of the profile directories. The array needs to 66 * absolute path of the profile directories. The array needs to
160 * be freed by the caller. 67 * be freed by the caller.
161 */ 68 */
162 char **getProfilePaths() { 69 //char**
163 char *homedir = NULL, 70 //get_profile_dirs(char* inifile)
164 **retval = NULL;
165 const char* const iniLocations[] = INI_LOCATIONS;
166 int i = 0;
167 71
168 homedir = getenv ("HOME");
169 72
170 if (!homedir) { 73 int
171 printf ("Could not get HOME\n"); 74 main()
172 return NULL; 75 {
173 } 76 exit(0);
174
175 for (i = 0; iniLocations[i] != NULL; i++) {
176 char *candidate[MAX_PATH_LEN],
177 *fileContent = NULL;
178 const size_t needed = strnlen (homedir, MAX_PATH_LEN) +
179 strnlen (iniLocations[i], MAX_PATH_LEN);
180 fileSize = 0;
181 int err = 0;
182
183 memset (candidate, 0, MAX_PATH_LEN);
184
185 /* Verify that addition of strlen did not overflow and
186 * that the buffer is large enough */
187 if (needed < strnlen (homedir, MAX_PATH_LEN_LEN) || needed >= MAX_PATH - 1) {
188 printf ("Error invalid HOME environment variable");
189 return NULL;
190 }
191
192 strncpy (candidate, homedir, MAX_PATH_LEN);
193 /* Environment might have been modified */
194 if (candidate[MAX_PATH_LEN - 1] != '\0') {
195 printf ("Error invalid HOME");
196 return NULL;
197 }
198 strncat (candidate, iniLocations[i], MAX_PATH_LEN - strnlen(candidate,
199 MAX_PATH_LEN) - 1);
200
201 rc = readFile (candidate, &fileContent, &fileSize, MAX_FILESIZE);
202
203 if (err) {
204 printf ("Failed to read file.\n");
205 continue;
206 }
207 parseIni (fileContent, &retval);
208 }
209 } 77 }
210 #else /* _WIN32 */
211 char **getProfilePaths() {
212 return NULL;
213 }
214 #endif
215
216 int main(int argc, char *argv) {
217 }

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