/
tftp.c
499 lines (400 loc) · 13.1 KB
/
tftp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include <curl/curl.h>
#include <glib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <gio/gio.h>
#include <curl/curl.h>
#include "tclient.h"
#include "main_window.h"
#include "settings_window.h"
#include "error_window.h"
#include "quit_dialog.h"
/* This function connects using curl to the tftp server. It grabs needed
* files from there */
void *
get_tftp_file (tclient_c *tclient, gchar *filename)
{
CURL *curl = 0;
FILE *config_file = 0;
GError *error = 0;
gchar *local_filename = 0;
gchar *uri = 0;
gchar *dirs = 0;
gchar *itr = 0;
/* Make the directory structure for the file being fetched */
itr = g_strrstr(filename, "/");
if (itr != NULL)
{
dirs = g_strndup(filename, (strlen(filename) - strlen(itr)));
itr = g_strdup_printf("/tmp/%s", dirs);
g_mkdir_with_parents(itr, 0777);
g_free(dirs);
g_free(itr);
}
local_filename = g_strdup_printf("/tmp/%s", filename);
uri = g_strdup_printf("tftp://%s/tclient/%s", tftp_server, filename);
curl = curl_easy_init();
config_file = fopen (local_filename, "w+b");
if (config_file != NULL)
{
CURLcode result;
curl_easy_setopt (curl, CURLOPT_WRITEDATA, config_file);
curl_easy_setopt (curl, CURLOPT_URL, uri);
curl_easy_setopt (curl, CURLOPT_TIMEOUT, 5);
result = curl_easy_perform(curl);
curl_easy_cleanup(curl);
/* If there is an error code, and it isn't
* CURLE_TFTP_NOTFOUND
* CURLE_TFTP_ILLEGAL
* Both of these can originate on missing or null files which
* are not to be considered errors */
if (result != 0 && result != CURLE_TFTP_NOTFOUND && result != CURLE_TFTP_ILLEGAL)
{
start_error_dialog(tclient, __FUNCTION__, (gchar *)curl_easy_strerror(result));
return NULL;
}
}
else
{
if (error != NULL)
{
start_error_dialog(tclient, __FUNCTION__, error->message);
}
}
/* Cleanup */
g_free(uri);
if (config_file != NULL)
{
fclose(config_file);
return local_filename;
}
return NULL;
}
/* Loads a file from the tftp server */
gpointer *
load_remote_file (tclient_c *tclient, gchar *filename)
{
gchar *buf = 0;
GFile *file = 0;
gchar *file_path = 0;
GError *error = 0;
/* Get the path for the downloaded conf file */
file_path = get_tftp_file(tclient, filename);
if (file_path == NULL)
return NULL;
file = g_file_new_for_path(file_path);
if (!file_path)
return NULL;
/* Load the config_file into memory */
g_file_load_contents(file, NULL, &buf, NULL, NULL, &error);
if (error)
start_error_dialog(tclient, __FUNCTION__, error->message);
return (gpointer *)buf;
}
/* Loads a file from the tftp server */
gchar *
load_remote_icon (tclient_c *tclient, const gchar *filename)
{
GString *tftp_path = 0;
gchar *file_path = 0;
/* Get the path for the downloaded conf file */
tftp_path = g_string_new(NULL);
g_string_printf(tftp_path, "icons/%s", filename);
file_path = get_tftp_file(tclient, tftp_path->str);
g_string_free(tftp_path, TRUE);
if (file_path == NULL)
return NULL;
return file_path;
}
/* Writes a file to the tftp server */
gboolean
set_tftp_file (tclient_c *tclient, gchar *filename, gpointer *data)
{
CURL *curl = 0;
GError *error = 0;
FILE *config_file;
gchar *local_filename = 0;
gchar *uri = 0;
local_filename = g_strdup_printf("/tmp/%s", filename);
uri = g_strdup_printf("tftp://%s/tclient/clients/%s", tftp_server, filename);
curl = curl_easy_init();
/* Write config file to the disk */
config_file = fopen (local_filename, "w+b");
fwrite(data, strlen((gchar *)data), 1, config_file);
fclose(config_file);
/* Open the config file with read */
config_file = fopen (local_filename, "r");
if (config_file != NULL)
{
CURLcode curl_result;
gchar *error = 0;
curl_easy_setopt (curl, CURLOPT_UPLOAD, 1);
curl_easy_setopt (curl, CURLOPT_READDATA, config_file);
curl_easy_setopt (curl, CURLOPT_URL, uri);
curl_easy_setopt (curl, CURLOPT_ERRORBUFFER, error);
curl_result = curl_easy_perform(curl);
if (curl_result != 0)
{
if (curl_result == CURLE_TFTP_ILLEGAL)
{
start_error_dialog(tclient, __FUNCTION__, "Could not write settings file. Check permissions on tftp server\n");
return FALSE;
}
start_error_dialog(tclient, __FUNCTION__, error);
return FALSE;
}
curl_easy_cleanup(curl);
}
else
{
if (error != NULL)
{
start_error_dialog(tclient, __FUNCTION__, error->message);
return FALSE;
}
}
/* Cleanup */
g_free(uri);
if (config_file != NULL)
{
fclose(config_file);
}
return TRUE;
}
/* This function updates the connection_c contexts. */
gboolean
read_connections (tclient_c *tclient)
{
GKeyFile *keys = 0;
gint i = 0;
gchar *buf = 0;
gint buf_size = 0;
tclient->connection = 0;
buf = (gchar *)load_remote_file(tclient, "connections.conf");
if (!buf)
{
start_error_dialog(tclient, __FUNCTION__, "Couldn't read connections\n");
return FALSE;
}
buf_size = strlen(buf);
/* Parse the keys */
keys = g_key_file_new();
if(g_key_file_load_from_data(keys, buf, buf_size, G_KEY_FILE_NONE, NULL))
{
gchar **group = 0;
gsize group_size = 0;
group = g_key_file_get_groups(keys, &group_size);
/* Go through the groups and create connections for them */
for (i = 0; i < group_size; i++)
{
connection_c *connection;
connection = g_malloc0 (sizeof (connection_c));
/* conf file reads like this
* [connection alias]
* host=127.0.0.1 #any resolvable IP/DNS
* icon=windows.png #Loaded from %server%/tclient/icons/
* type=RDP # Type of conneciton, must be defined in connections
* description=Test Station # This will show as a tooltip
* username=root #This is the default username to try
* retry=false #retry the connection or not
* hidden=true #Whether or not it shows up in the list */
connection->alias = g_strdup(group[i]);
/* Get the hostname/ip. Verify that there is text there. Fail
* if this is not there */
if(!(connection->host = g_key_file_get_value(keys, group[i], "host", NULL)))
{
gchar *error_str = 0;
error_str = g_strdup_printf("Malformed connection.conf: Error in group \"%s\". You must provide a valid host to connect to.", group[i]);
start_error_dialog(tclient, __FUNCTION__, error_str);
g_free(error_str);
g_free(connection);
continue;
}
if (strlen(connection->host) <= 0)
{
gchar *error_str = 0;
error_str = g_strdup_printf("Malformed connection.conf: Error in group \"%s\". You must provide a valid host to connect to.", group[i]);
start_error_dialog(tclient, __FUNCTION__, error_str);
g_free(error_str);
g_free(connection);
continue;
}
/* Default username. This can be empty */
connection->username = g_key_file_get_value(keys, group[i], "username", NULL);
/* Type to use. This must exist in the types.conf file. Fail if this
* entry is blank */
if (!(connection->type = g_key_file_get_value(keys, group[i], "type", NULL)))
{
gchar *error_str = 0;
error_str = g_strdup_printf("Malformed connection.conf: Error in group \"%s\". \"%s\" must be set in types.conf", group[i], connection->type);
start_error_dialog(tclient, __FUNCTION__, error_str);
g_free(error_str);
g_free(connection);
continue;
}
if (connection->type == NULL)
{
gchar *error_str = 0;
error_str = g_strdup_printf("Malformed connection.conf: Error in group \"%s\". Type must be set", group[i]);
start_error_dialog(tclient, __FUNCTION__, error_str);
g_free(error_str);
g_free(connection);
continue;
}
/* Set retry setting. If nothing else, say true */
if (g_ascii_strncasecmp(g_key_file_get_value(keys, group[i], "retry", NULL), "true", strlen("true")) == 0)
{
connection->retry = TRUE;
}
else if (g_ascii_strncasecmp(g_key_file_get_value(keys, group[i], "retry", NULL), "false", strlen("false")) == 0)
{
connection->retry = FALSE;
}
else
{
connection->retry = TRUE;
}
/* Get this hidden setting. Set to false by default */
if(g_key_file_get_value(keys, group[i], "hidden", NULL) != NULL)
{
if (g_ascii_strncasecmp(g_key_file_get_value(keys, group[i], "hidden", NULL), "true", strlen("true")) == 0)
{
connection->hidden = TRUE;
}
else if (g_ascii_strncasecmp(g_key_file_get_value(keys, group[i], "hidden", NULL), "false", strlen("false")) == 0)
{
connection->hidden = FALSE;
}
}
else
{
connection->hidden = FALSE;
}
/* Get the icon name, this is optional */
connection->icon = g_key_file_get_value(keys, group[i], "icon", NULL);
/* Get the description, this is optional */
connection->description = g_key_file_get_value(keys, group[i], "description", NULL);
tclient->connection = g_list_append(tclient->connection, connection);
}
g_strfreev(group);
}
g_key_file_free(keys);
return TRUE;
}
gboolean
read_settings (tclient_c *tclient)
{
GKeyFile *keys = 0;
gchar *buf;
gint buf_size = 0;
gchar hostname[_POSIX_HOST_NAME_MAX + 1] = {0};
gchar *filename = 0;
gethostname(hostname, _POSIX_HOST_NAME_MAX);
filename = g_strdup_printf("clients/%s.conf", &hostname[0]);
buf = (gchar *)load_remote_file(tclient, filename);
if (!buf || strlen(buf) <= 1)
{
/* Client settings can be blank, just return true */
return TRUE;
}
buf_size = strlen(buf);
if (buf_size < 1)
return TRUE;
/* Parse the keys */
keys = g_key_file_new();
if(g_key_file_load_from_data(keys, buf, buf_size, G_KEY_FILE_NONE, NULL))
{
gchar **group = 0;
gsize group_size = 0;
group = g_key_file_get_groups(keys, &group_size);
tclient->settings.connection = g_key_file_get_value(keys, group[0], "server", NULL);
tclient->settings.resolution = g_key_file_get_value(keys, group[0], "resolution", NULL);
g_strfreev(group);
}
g_free(filename);
g_key_file_free(keys);
return TRUE;
}
/* This function updates the config file contexts. */
gboolean
update_config_file (tclient_c *tclient, gchar *server, gchar *resolution)
{
gchar *config = 0;
gchar *filename = 0;
gchar hostname[_POSIX_HOST_NAME_MAX + 1] = {0};
gethostname(hostname, _POSIX_HOST_NAME_MAX);
if (resolution == NULL && server == NULL)
{
config = g_strdup_printf("[%s]\nserver=\nresolution=\n", hostname);
}
else if (server == NULL)
{
config = g_strdup_printf("[%s]\nserver=\nresolution=%s\n", hostname, resolution);
}
else if (resolution == NULL)
{
config = g_strdup_printf("[%s]\nserver=%s\nresolution=\n", hostname, server);
}
else
{
config = g_strdup_printf("[%s]\nserver=%s\nresolution=%s\n", hostname, server, resolution);
}
filename = g_strdup_printf("%s.conf", hostname);
if (!set_tftp_file (tclient, filename, (gpointer)config))
return FALSE;
return TRUE;
}
/* This function reads the type_c contexts. */
gboolean
read_types (tclient_c *tclient)
{
GKeyFile *keys = 0;
gint i = 0;
gchar *buf = 0;
gint buf_size = 0;
buf = (gchar *)load_remote_file(tclient, "types.conf");
if (!buf)
{
start_error_dialog(tclient, __FUNCTION__, "Could not load the types file");
return FALSE;
}
buf_size = strlen(buf);
/* Parse the keys */
keys = g_key_file_new();
if(g_key_file_load_from_data(keys, buf, buf_size, G_KEY_FILE_NONE, NULL))
{
gchar **group = 0;
gsize group_size = 0;
group = g_key_file_get_groups(keys, &group_size);
/* Go through the groups and create connections for them */
for (i = 0; i < group_size; i++)
{
type_c *type;
type = g_malloc0 (sizeof (type_c));
type->alias = g_strdup(group[i]);
type->program = g_key_file_get_value(keys, group[i], "program", NULL);
type->arguments = g_key_file_get_value(keys, group[i], "arguments", NULL);
type->description = g_key_file_get_value(keys, group[i], "description", NULL);
tclient->connection_type = g_list_append(tclient->connection_type, type);
}
g_strfreev(group);
}
g_key_file_free(keys);
return TRUE;
}
/* Load all of the config filse */
gboolean
load_remote_configs(tclient_c *tclient)
{
if(!read_types(tclient))
return FALSE;
if(!read_connections(tclient))
return FALSE;
if(!read_settings(tclient))
return FALSE;
return TRUE;
}