Fix: scanf unbounded input
[lttng-tools.git] / src / bin / lttng / conf.c
1 /*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 #include <common/error.h>
28 #include <common/utils.h>
29
30 #include "conf.h"
31
32 /*
33 * Returns the path with '/CONFIG_FILENAME' added to it;
34 * path will be NULL if an error occurs.
35 */
36 char *config_get_file_path(char *path)
37 {
38 int ret;
39 char *file_path;
40
41 ret = asprintf(&file_path, "%s/%s", path, CONFIG_FILENAME);
42 if (ret < 0) {
43 ERR("Fail allocating config file path");
44 file_path = NULL;
45 }
46
47 return file_path;
48 }
49
50 /*
51 * Returns an open FILE pointer to the config file;
52 * on error, NULL is returned.
53 */
54 static FILE *open_config(char *path, const char *mode)
55 {
56 FILE *fp = NULL;
57 char *file_path;
58
59 file_path = config_get_file_path(path);
60 if (file_path == NULL) {
61 goto error;
62 }
63
64 fp = fopen(file_path, mode);
65 if (fp == NULL) {
66 goto error;
67 }
68
69 error:
70 free(file_path);
71 return fp;
72 }
73
74 /*
75 * Creates the empty config file at the path.
76 * On success, returns 0;
77 * on error, returns -1.
78 */
79 static int create_config_file(char *path)
80 {
81 int ret;
82 FILE *fp;
83
84 fp = open_config(path, "w+");
85 if (fp == NULL) {
86 ERR("Unable to create config file");
87 ret = -1;
88 goto error;
89 }
90
91 ret = fclose(fp);
92
93 error:
94 return ret;
95 }
96
97 /*
98 * Append data to the config file in file_path
99 * On success, returns 0;
100 * on error, returns -1.
101 */
102 static int write_config(char *file_path, size_t size, char *data)
103 {
104 FILE *fp;
105 size_t len;
106 int ret = 0;
107
108 fp = open_config(file_path, "a");
109 if (fp == NULL) {
110 ret = -1;
111 goto end;
112 }
113
114 /* Write session name into config file */
115 len = fwrite(data, size, 1, fp);
116 if (len != 1) {
117 ret = -1;
118 }
119 if (fclose(fp)) {
120 PERROR("close write_config");
121 }
122 end:
123 return ret;
124 }
125
126 /*
127 * Destroys directory config and file config.
128 */
129 void config_destroy(char *path)
130 {
131 int ret;
132 char *config_path;
133
134 config_path = config_get_file_path(path);
135 if (config_path == NULL) {
136 return;
137 }
138
139 if (!config_exists(config_path)) {
140 goto end;
141 }
142
143 DBG("Removing %s\n", config_path);
144 ret = remove(config_path);
145 if (ret < 0) {
146 perror("remove config file");
147 }
148 end:
149 free(config_path);
150 }
151
152 /*
153 * Destroys the default config
154 */
155 void config_destroy_default(void)
156 {
157 char *path = utils_get_home_dir();
158 if (path == NULL) {
159 return;
160 }
161 config_destroy(path);
162 }
163
164 /*
165 * Returns 1 if config exists, 0 otherwise
166 */
167 int config_exists(const char *path)
168 {
169 int ret;
170 struct stat info;
171
172 ret = stat(path, &info);
173 if (ret < 0) {
174 return 0;
175 }
176 return S_ISREG(info.st_mode) || S_ISDIR(info.st_mode);
177 }
178
179 /*
180 * Returns the session name from the config file.
181 * The caller is responsible for freeing the returned string.
182 * On error, NULL is returned.
183 */
184 char *config_read_session_name(char *path)
185 {
186 int ret;
187 FILE *fp;
188 char var[NAME_MAX], *session_name;
189 #if (NAME_MAX == 255)
190 #define NAME_MAX_SCANF_IS_A_BROKEN_API "254"
191 #endif
192
193 session_name = malloc(NAME_MAX);
194 if (session_name == NULL) {
195 ERR("Out of memory");
196 goto error;
197 }
198
199 fp = open_config(path, "r");
200 if (fp == NULL) {
201 ERR("Can't find valid lttng config %s/.lttngrc", path);
202 MSG("Did you create a session? (lttng create <my_session>)");
203 free(session_name);
204 goto error;
205 }
206
207 while (!feof(fp)) {
208 if ((ret = fscanf(fp, "%" NAME_MAX_SCANF_IS_A_BROKEN_API
209 "[^'=']=%" NAME_MAX_SCANF_IS_A_BROKEN_API "s\n",
210 var, session_name)) != 2) {
211 if (ret == -1) {
212 ERR("Missing session=NAME in config file.");
213 goto error_close;
214 }
215 continue;
216 }
217
218 if (strcmp(var, "session") == 0) {
219 goto found;
220 }
221 }
222
223 error_close:
224 free(session_name);
225 ret = fclose(fp);
226 if (ret < 0) {
227 PERROR("close config read session name");
228 }
229
230 error:
231 return NULL;
232
233 found:
234 ret = fclose(fp);
235 if (ret < 0) {
236 PERROR("close config read session name found");
237 }
238 return session_name;
239
240 }
241
242 /*
243 * Write session name option to the config file.
244 * On success, returns 0;
245 * on error, returns -1.
246 */
247 int config_add_session_name(char *path, char *name)
248 {
249 int ret;
250 char *attr = "session=";
251 /* Max name len accepted plus attribute's len and the NULL byte. */
252 char session_name[NAME_MAX + strlen(attr) + 1];
253
254 /*
255 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
256 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
257 */
258 ret = snprintf(session_name, sizeof(session_name), "%s%s\n", attr, name);
259 if (ret < 0) {
260 ret = -1;
261 goto error;
262 }
263 ret = write_config(path, ret, session_name);
264 error:
265 return ret;
266 }
267
268 /*
269 * Init configuration directory and file.
270 * On success, returns 0;
271 * on error, returns -1.
272 */
273 int config_init(char *session_name)
274 {
275 int ret;
276 char *path;
277
278 path = utils_get_home_dir();
279 if (path == NULL) {
280 ret = -1;
281 goto error;
282 }
283
284 /* Create default config file */
285 ret = create_config_file(path);
286 if (ret < 0) {
287 goto error;
288 }
289
290 ret = config_add_session_name(path, session_name);
291 if (ret < 0) {
292 goto error;
293 }
294
295 DBG("Init config session in %s", path);
296
297 error:
298 return ret;
299 }
This page took 0.036484 seconds and 5 git commands to generate.