-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuse.c
597 lines (549 loc) · 13.4 KB
/
fuse.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#include "common.h"
GHashTable *FDt;
unsigned FDn;
static void * mainloopthread(void *loop);
pthread_t thr;
gchar *filter_str(gchar *str)
{
gchar *ch = strchr(str, '\n');
if (ch)
str[ch - str] = 0;
g_strstrip(str);
return str;
}
int fileexists(const char *path) /* TODO remove/rewrite */
{
if (strcmp(path, "/log") == 0) return 1;
if (strncmp(path, "/roster/", 8) == 0) return 1;
return 0;
}
rosteritem * getri(const char *path)
{
char *p;
rosteritem *ri;
if (*path == '/')
path ++;
gchar *ch = strchr(path, '/');
if (ch) {
p = g_strndup(path, ch - path);
} else {
p = g_strdup(path);
}
ri = g_hash_table_lookup(roster, p);
g_free(p);
return ri;
}
FD * addfd(rosteritem *ri)
{
FD *fd;
int *id;
fd = g_malloc0(sizeof(fd));
fd->id = ++FDn;
fd->ri = ri;
fd->writebuf = g_array_new(TRUE, FALSE, 1);
id = g_malloc(sizeof(int));
*id = fd->id;
g_hash_table_insert(FDt, id, fd);
return fd;
}
void destroyfd(FD *fd)
{
// g_array_free(fd->readbuf, TRUE); /* TODO? */
g_array_free(fd->writebuf, TRUE);
g_free(fd);
}
/* FS calls */
static int fstruncate(const char *path, off_t size)
{
return 0;
}
static int fsrmdir(const char *path)
{
if (strcmp(path, "/config") == 0) {
g_hash_table_destroy(config);
config = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
return 0;
}
if (strcmp(path, "/roster") == 0) {
if (connection_state != OFFLINE) {
xmpp_disconnect();
return 0;
}
}
if (strncmp(path, "/roster/", 8) == 0) {
rosteritem *ri;
path += 8;
ri = getri(path);
if (ri) {
if (ri->type == MUC) {
partmuc(path, NULL, "TODO here must be default leave message :-)");
g_hash_table_remove(roster, path);
} else logstr("Roster items removal isn't implemented\n");; /* TODO */
return 0;
} else return -ENOENT;
}
return -EPERM;
}
static int fscreate(const char *path, mode_t mode, struct fuse_file_info *fi)
{
/* TODO add roster items */
if (strncmp(path, "/roster/", 8) == 0) {
path += 8;
xmpp_add_to_roster(path);
}
return 0;
}
static int fsmknod(const char *path, mode_t mode, dev_t type)
{
/* TODO */
return 0;
}
static int fsmkdir(const char *path, mode_t mode)
{
if (strcmp(path, "/roster") == 0) {
logstr("Make roster!\n");
if (connection && lm_connection_is_open(connection)) {
return -EPERM;
} else {
pthread_create(&thr, NULL, mainloopthread, NULL);
usleep(100);
while (connection_state == CONNECTING) usleep(100); // FIXME
return 0;
}
}
if (strncmp(path, "/roster/", 8) == 0) {
path += 8;
logf("join conference %s!\n", path);
joinmuc(path, NULL, NULL);
return 0;
}
return -EPERM;
}
static int fsgetattr(const char *path, struct stat *stbuf)
{
memset(stbuf, 0, sizeof(struct stat));
if (strcmp(path, "/events") == 0) {
stbuf->st_mode = S_IFIFO | 0444;
stbuf->st_nlink = 1;
return 0;
}
if (strcmp(path, "/log") == 0) {
stbuf->st_mode = S_IFREG | 0666;
stbuf->st_nlink = 1;
stbuf->st_size = LogBuf->len;
return 0;
}
if (connection_state != OFFLINE) {
if (strcmp(path, "/roster") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
return 0;
}
if (strcmp(path, "/rawxmpp") == 0) {
stbuf->st_mode = S_IFREG | 0222;
stbuf->st_nlink = 1;
return 0;
}
}
if ((strcmp(path, "/config") == 0) ||
(strcmp(path, "/") == 0)) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
return 0;
}
if (strncmp(path, "/config/", 8) == 0) {
char *conf;
path += 8;
stbuf->st_mode = S_IFREG | 0644;
stbuf->st_nlink = 1;
conf = g_hash_table_lookup(config, path);
if (conf) stbuf->st_size = strlen(conf);
return 0;
}
if (strncmp(path, "/roster/", 8) == 0) {
rosteritem *ri;
path += 8;
ri = getri(path);
if (ri) {
if (strlen(path) == strlen(get_jid(path))) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
return 0;
}
stbuf->st_mode = S_IFREG | 0666; /* TODO fixable perms */
stbuf->st_nlink = 1;
stbuf->st_size = ri->log->len;
return 0;
}
}
return -ENOENT;
}
static int fsreaddir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
(void) offset;
(void) fi;
if (strcmp(path, "/") == 0) {
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
filler(buf, "events", NULL, 0);
filler(buf, "log", NULL, 0);
filler(buf, "config", NULL, 0);
if (roster && (connection_state == ONLINE)) {
filler(buf, "roster", NULL, 0);
filler(buf, "rawxmpp", NULL, 0);
}
return 0;
}
if (strcmp(path, "/config") == 0) {
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
GHashTableIter iter;
gchar *key;
g_hash_table_iter_init(&iter, config);
while (g_hash_table_iter_next(&iter, (gpointer *)&key, NULL)) {
filler(buf, key, NULL, 0);
}
filler(buf, "server", NULL, 0);
filler(buf, "port", NULL, 0);
filler(buf, "ssl", NULL, 0);
}
if (strcmp(path, "/roster") == 0) {
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
GHashTableIter iter;
rosteritem *ri;
g_hash_table_iter_init(&iter, roster);
while (g_hash_table_iter_next(&iter, NULL, (gpointer) &ri))
filler(buf, ri->jid, NULL, 0);
return 0;
}
if (strncmp(path, "/roster/", 8) == 0) {
path += 8;
rosteritem *ri;
ri = getri(path);
if (ri) {
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
filler(buf, "__chat", NULL, 0);
if (ri->type == MUC)
filler(buf, "__nick", NULL, 0);
GHashTableIter iter;
gchar *res;
g_hash_table_iter_init(&iter, ri->resources);
while (g_hash_table_iter_next(&iter, (gpointer) &res, NULL))
filler(buf, res, NULL, 0);
}
}
return 0;
}
static int fsopen(const char *path, struct fuse_file_info *fi)
{
/* FDs are introduced only for writing ops */
if ((fi->flags & O_WRONLY) || (fi->flags & O_RDWR)) {
if (strncmp(path, "/roster/", 8) == 0) {
rosteritem *ri;
path += 8;
ri = getri(path);
if (ri) {
/* TODO */
}
}
}
/* TODO */
return 0;
}
static char *get_option(const char *option)
{
if (strcmp(option, "server") == 0) {
const char *r = lm_connection_get_server(connection);
if (r)
return strdup(r);
} else if (strcmp(option, "ssl") == 0) {
if (ssl) {
if (lm_ssl_get_use_starttls(ssl)) {
if (lm_ssl_get_require_starttls(ssl)) {
return strdup("required_starttls");
} else {
return strdup("starttls");
}
} else {
return strdup("enabled");
}
} else {
return strdup("disabled");
}
} else if (strcmp(option, "port") == 0) {
int p = lm_connection_get_port(connection);
return g_strdup_printf("%d", p);
} else {
char *r = g_hash_table_lookup(config, option);
if (r)
return strdup(r);
}
return NULL;
}
static int fsread(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
if (strcmp(path, "/log") == 0) {
// write(fi->fh, LogBuf->data+offset, size);
memcpy(buf, LogBuf->data + offset, size);
/* TODO: checks, lock */
return size;
}
if (strncmp(path, "/config/", 8) == 0) {
size_t len;
path += 8;
char *val = get_option(path);
if (val) {
len = strlen(val);
int retval;
if (offset + size < len) {
memcpy(buf, val + offset, size);
retval = size;
} else {
memcpy(buf, val + offset, len - offset);
retval = len - offset;
}
free(val);
return retval;
} else return -ENOENT;
}
if (strncmp(path, "/roster/", 8) == 0) {
GArray *log;
rosteritem *ri;
path += 8;
ri = getri(path);
gchar *resource = get_resource(path);
if (ri && ((strcmp(path, "__nick") == 0) || (resource && strcmp(resource, "__nick") == 0)) && ri->type == MUC) {
size_t len = strlen(ri->self_resource->name);
if (offset + size < len) {
memcpy(buf, ri->self_resource->name + offset, size);
return size;
} else {
memcpy(buf, ri->self_resource->name + offset, len - offset);
return len - offset;
}
}
if (ri) {
// if ((ri->type == MUC) && (strcmp(path + strlen(ri->jid)+1)));
log = ri->log;
if (offset + size < log->len) {
memcpy(buf, log->data + offset, size);
return size;
} else {
memcpy(buf, log->data + offset, log->len - offset);
return log->len - offset;
}
} else return -ENOENT;
}
return -ENOENT;
}
// FIXME fuse with set_option
static char *prepare_option(const char *option, const char *buf, size_t size)
{
char *val;
// bool options
if ((strcmp(option, "events") == 0) ||
(strcmp(option, "raw_logs") == 0)) {
val = strdup("1"); // just something :)
} else {
val = filter_str(strndup(buf, size));
bool pr;
if (strcmp(option, "show") == 0 &&
strcmp(val, "") &&
strcmp(val, "away") &&
strcmp(val, "chat") &&
strcmp(val, "dnd") &&
strcmp(val, "xa")) {
logf("Failed to set %s to incorrect value %s\n", option, val);
return 0;
} else if (strcmp(option, "ssl") == 0) {
/* A kludge to avoid double string checking */
if (strcmp(val, "disabled") == 0) return (char *)1;
else if (strcmp(val, "enabled") == 0) return (char *)2;
else if (strcmp(val, "starttls") == 0) return (char *)3;
else if (strcmp(val, "required_starttls") == 0) return (char *)4;
else {
logf("Failed to set %s to incorrect value %s\n", option, val);
return 0;
}
} else if ((pr = (strcmp(option, "priority") == 0)) ||
((strcmp(option, "port") == 0))) {
/* Suggest a better way to validate the integer */
char *not_ok;
long long i = strtoll(val, ¬_ok, 10);
if (*not_ok) {
logf("The %s option must be set to a numeric value, %s received instead!\n", option, val);
return 0;
}
if (pr) {
if (i != (int8_t)i) {
logf("Failed to set priority to way too big value %s\n", val);
return 0;
}
} else {
if (i != (uint16_t)i) {
logf("Value %s is an incorrect port number!\n", val);
return 0;
}
}
}
}
return val;
}
static void set_option(char *option, char *val)
{
bool used = false;
// FIXME: in the case of ssl, the argument is not a string but a number
if (strcmp(option, "ssl")) {
logf("Setting %s = %s\n", option, val);
} else {
logf("Setting %s = %" PRIdPTR "\n", option, (intptr_t)val);
}
if (strcmp(option, "server") == 0) {
lm_connection_set_server(connection, val);
} else if (strcmp(option, "port") == 0) {
lm_connection_set_port(connection, atoi(val));
} else if (strcmp(option, "ssl") == 0) {
if (val) {
bool st = (long)val & 2, str = ((long)val & 3) == 3;
while (!ssl) {
ssl = lm_ssl_new(NULL, NULL, NULL, NULL);
assert(ssl);
lm_connection_set_ssl(connection, ssl);
}
lm_ssl_use_starttls(ssl, st, str);
} else {
lm_connection_set_ssl(connection, NULL);
lm_ssl_unref(ssl);
ssl = NULL;
}
} else {
g_hash_table_replace(config, option, val);
used = true;
}
if (connection_state == ONLINE && (
strcmp(option, "priority") == 0 ||
strcmp(option, "show") == 0 ||
strcmp(option, "status") == 0))
xmpp_send_presence();
if (!used) {
if (strcmp(option, "ssl")) {
free(val);
}
free(option);
}
}
static int fswrite(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
if (strncmp(path, "/roster/", 8) == 0) {
char *msg;
size_t msg_len = size;
path += 8;
gchar *res = get_resource(path);
if (res && (strncmp(res, "__chat", 2) == 0)) {
path = get_jid(path);
}
msg = g_malloc(msg_len + 1);
memcpy(msg, buf, msg_len);
msg[msg_len] = 0;
if (res && (strncmp(res, "__nick", 6) == 0))
xmpp_muc_change_nick(get_jid(path), msg);
else
xmpp_send(path, msg);
g_free(msg);
return size;
}
if (strncmp(path, "/config/", 8) == 0) {
path += 8;
char *option = g_strdup(path);
char *val = prepare_option(option, buf, size);
if (val) {
set_option(option, val);
return size;
} else return 0;
}
if ((strcmp(path, "/rawxmpp") == 0) && connection_state == ONLINE) {
gchar *str = g_strndup(buf, size);
lm_connection_send_raw(connection, str, NULL);
g_free(str);
return size;
}
return 0;
}
static int fssetxattr(const char *path, const char *a, const char *aa, size_t size, int aaa)
{
/* Stub. Do we need this? */
return 0;
}
static int fsunlink(const char *path)
{
if (strncmp(path, "/roster/", 8) == 0) {
path += 8;
char *sl = strchr(path, '/');
if (sl) { /* We're in the conference and wanna ban! */
char *who = g_strdup(sl + 1);
char *where = g_strndup(path, who - path);
where[who - path] = 0;
// TODO!
banmuc(where, who);
g_free(where);
g_free(who);
} else {
xmpp_del_from_roster(path);
}
}
// delete options from /config/, maybe need some checks before actual deleting
if (strncmp(path, "/config/", 8) == 0) {
path += 8;
g_hash_table_remove(config, path);
}
return 0;
}
static void fsdestroy(void *privdata)
{
if (connection)
xmpp_disconnect();
free_all();
if (main_loop) {
g_main_loop_quit(main_loop);
g_main_loop_unref(main_loop);
}
return;
}
static void * mainloopthread(void *loop)
{
xmpp_connect();
g_main_loop_run(main_loop);
return NULL;
}
static void * fsinit(struct fuse_conn_info *conn)
{
FDt = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, (GDestroyNotify)destroyfd); //TODO check the cast
return NULL;
}
int fuseinit(int argc, char **argv)
{
int ret;
// fifo = open("fs/log", O_WRONLY | O_NONBLOCK);
ret = fuse_main(argc, argv, &fuseoper, NULL);
return ret;
}
struct fuse_operations fuseoper = {
.getattr = fsgetattr,
.readdir = fsreaddir,
.open = fsopen,
.read = fsread,
.write = fswrite,
.setxattr = fssetxattr,
.mkdir = fsmkdir,
.rmdir = fsrmdir,
.mknod = fsmknod,
.create = fscreate,
.unlink = fsunlink,
.init = fsinit,
.destroy = fsdestroy,
.truncate = fstruncate,
};