d3372da9 |
1 | /* |
2 | * Dolda Connect - Modular multiuser Direct Connect-style client |
3 | * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com) |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program; if not, write to the Free Software |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | */ |
19 | #define _GNU_SOURCE |
20 | #include <unistd.h> |
21 | #include <stdlib.h> |
d3372da9 |
22 | #include <sys/socket.h> |
23 | #include <netinet/in.h> |
24 | #include <netinet/ip6.h> |
504ba854 |
25 | #include <arpa/inet.h> |
ddb933f1 |
26 | #include <sys/un.h> |
d3372da9 |
27 | #include <errno.h> |
28 | #include <string.h> |
29 | #include <stdarg.h> |
30 | #include <wchar.h> |
31 | #include <wctype.h> |
32 | #include <iconv.h> |
33 | #include <pwd.h> |
34 | #include <time.h> |
35 | #include <fcntl.h> |
36 | #include <signal.h> |
37 | |
38 | #ifdef HAVE_CONFIG_H |
39 | #include <config.h> |
40 | #endif |
41 | #include "conf.h" |
42 | #include "auth.h" |
43 | #include "utils.h" |
44 | #include "net.h" |
45 | #include "module.h" |
46 | #include "sysevents.h" |
47 | #include "filenet.h" |
48 | #include "transfer.h" |
49 | #include "search.h" |
50 | #include "client.h" |
51 | |
52 | #define PERM_DISALLOW 1 |
53 | #define PERM_ADMIN 2 |
54 | #define PERM_FNETCTL 4 |
55 | #define PERM_TRANS 8 |
56 | #define PERM_TRANSCU 16 |
57 | #define PERM_CHAT 32 |
58 | #define PERM_SRCH 64 |
59 | |
60 | #define NOTIF_END 0 |
61 | #define NOTIF_INT 1 |
62 | #define NOTIF_STR 2 |
63 | #define NOTIF_FLOAT 3 |
64 | #define NOTIF_ID 4 |
65 | #define NOTIF_PEND 0 |
66 | #define NOTIF_WAIT 1 |
67 | |
68 | struct uidata; |
69 | |
70 | struct command |
71 | { |
72 | wchar_t *name; |
73 | void (*handler)(struct socket *sk, struct uidata *data, int argc, wchar_t **argv); |
74 | }; |
75 | |
76 | struct qcommand |
77 | { |
78 | struct qcommand *next; |
79 | struct command *cmd; |
80 | int argc; |
81 | wchar_t **argv; |
82 | }; |
83 | |
84 | struct uiuser |
85 | { |
86 | struct uiuser *next, *prev; |
87 | int used; |
88 | wchar_t *name; |
89 | unsigned long perms; |
90 | int delete; |
91 | }; |
92 | |
93 | struct notif |
94 | { |
95 | struct notif *next, *prev; |
96 | struct uidata *ui; |
97 | int state; |
98 | int code; |
99 | double rlimit; |
100 | size_t argc; |
101 | struct timer *exptimer; |
102 | struct notifarg |
103 | { |
104 | int dt; |
105 | union |
106 | { |
107 | int n; |
108 | wchar_t *s; |
109 | double d; |
110 | } d; |
111 | } *argv; |
112 | }; |
113 | |
114 | struct uidata |
115 | { |
116 | struct uidata *next, *prev; |
117 | struct socket *sk; |
118 | struct qcommand *queue, *queuelast; |
119 | struct authhandle *auth; |
120 | int close; |
121 | union |
122 | { |
123 | struct |
124 | { |
125 | int fnact:1; |
126 | int fnchat:1; |
ba86d49a |
127 | int fnpeer:1; |
d3372da9 |
128 | int tract:1; |
129 | int trprog:1; |
130 | int srch:1; |
5f151349 |
131 | int msg:1; |
d3372da9 |
132 | } b; |
133 | int w; |
134 | } notify; |
135 | wchar_t *username; |
136 | struct uiuser *userinfo; |
5f151349 |
137 | int id; |
138 | wchar_t *regname; |
d3372da9 |
139 | uid_t uid; |
140 | struct notif *fnotif, *lnotif; |
141 | char *fcmdbuf; |
142 | size_t fcmdbufdata, fcmdbufsize; |
143 | pid_t fcmdpid; |
144 | struct socket *fcmdsk; |
145 | /* Read buffer */ |
146 | char *inbuf; |
147 | size_t inbufsize, indata; |
148 | /* Wordset storage */ |
149 | wchar_t **argv; |
76236762 |
150 | size_t argc, args; |
d3372da9 |
151 | /* WCS conversation stuff */ |
152 | wchar_t *cb; /* Conversation buffer */ |
153 | size_t cbsize, cbdata; |
154 | iconv_t ichandle; |
155 | /* Parser data */ |
156 | int ps; /* Parser state */ |
157 | wchar_t *pp; /* Current parse pointer */ |
158 | wchar_t *cw; /* Current word (building storage) */ |
159 | size_t cwsize, cwdata; |
160 | }; |
161 | |
162 | static int srcheta(struct search *srch, void *uudata); |
163 | static int srchcommit(struct search *srch, void *uudata); |
164 | static int srchres(struct search *srch, struct srchres *sr, void *uudata); |
5f151349 |
165 | static struct notif *newnotif(struct uidata *data, int code, ...); |
166 | static void notifappend(struct notif *notif, ...); |
d3372da9 |
167 | |
168 | struct uiuser *users = NULL; |
169 | struct uidata *actives = NULL; |
ddb933f1 |
170 | struct socket *tcpsocket = NULL; |
171 | struct socket *unixsocket = NULL; |
9a64a4f7 |
172 | static time_t starttime; |
d3372da9 |
173 | |
174 | static wchar_t *quoteword(wchar_t *word) |
175 | { |
176 | wchar_t *wp, *buf, *bp; |
177 | int dq, numbs, numc; |
178 | |
179 | dq = 0; |
180 | numbs = 0; |
181 | numc = 0; |
182 | if(*word == L'\0') |
183 | { |
184 | dq = 1; |
185 | } else { |
186 | for(wp = word; *wp != L'\0'; wp++) |
187 | { |
188 | if(!dq && iswspace(*wp)) |
189 | dq = 1; |
190 | if((*wp == L'\\') || (*wp == L'\"')) |
191 | numbs++; |
192 | numc++; |
193 | } |
194 | } |
195 | if(!dq && !numbs) |
196 | return(NULL); |
197 | bp = buf = smalloc(sizeof(wchar_t) * (numc + numbs + (dq?2:0) + 1)); |
198 | if(dq) |
199 | *(bp++) = L'\"'; |
200 | for(wp = word; *wp != L'\0'; wp++) |
201 | { |
202 | if((*wp == L'\\') || (*wp == L'\"')) |
203 | *(bp++) = L'\\'; |
204 | *(bp++) = *wp; |
205 | } |
206 | if(dq) |
207 | *(bp++) = L'\"'; |
208 | *(bp++) = L'\0'; |
209 | return(buf); |
210 | } |
211 | |
212 | static void sq(struct socket *sk, int cont, ...) |
213 | { |
214 | int num, freepart; |
215 | va_list al; |
f7292a8b |
216 | char *final, *sarg; |
d3372da9 |
217 | wchar_t *buf; |
218 | wchar_t *part, *tpart; |
219 | size_t bufsize, bufdata; |
220 | |
221 | buf = NULL; |
222 | bufsize = bufdata = 0; |
223 | num = 0; |
224 | va_start(al, cont); |
225 | while((part = va_arg(al, wchar_t *)) != NULL) |
226 | { |
f7292a8b |
227 | freepart = 0; |
d3372da9 |
228 | if(*part == L'%') |
229 | { |
f7292a8b |
230 | tpart = part + 1; |
231 | if(!wcscmp(tpart, L"i")) |
d3372da9 |
232 | { |
f7292a8b |
233 | freepart = 1; |
234 | part = swprintf2(L"%i", va_arg(al, int)); |
235 | } else if(!wcscmp(tpart, L"s")) { |
236 | freepart = 1; |
237 | part = icmbstowcs(sarg = va_arg(al, char *), NULL); |
238 | if(part == NULL) |
d3372da9 |
239 | { |
f7292a8b |
240 | freepart = 0; |
241 | part = L"ERROR"; |
242 | flog(LOG_ERR, "could not convert local string to wcs: %s", sarg); |
d3372da9 |
243 | } |
f7292a8b |
244 | } else if(!wcscmp(tpart, L"ls")) { |
245 | part = va_arg(al, wchar_t *); |
246 | } else if(!wcscmp(tpart, L"ll")) { |
247 | freepart = 1; |
248 | part = swprintf2(L"%lli", va_arg(al, long long)); |
249 | } else if(!wcscmp(tpart, L"f")) { |
250 | freepart = 1; |
251 | part = swprintf2(L"%f", va_arg(al, double)); |
252 | } else if(!wcscmp(tpart, L"x")) { |
253 | freepart = 1; |
254 | part = swprintf2(L"%x", va_arg(al, int)); |
255 | } else { |
256 | flog(LOG_CRIT, "BUG: unknown type code in sq: %ls", tpart); |
257 | abort(); |
d3372da9 |
258 | } |
d3372da9 |
259 | } |
260 | if((tpart = quoteword(part)) != NULL) |
261 | { |
262 | if(freepart) |
263 | free(part); |
264 | part = tpart; |
265 | freepart = 1; |
266 | } |
267 | if((num > 1) || ((num == 1) && !(cont & 1))) |
268 | addtobuf(buf, L' '); |
269 | bufcat(buf, part, wcslen(part)); |
270 | if((num == 0) && (cont & 1)) |
271 | addtobuf(buf, L'-'); |
272 | num++; |
273 | if(freepart) |
274 | free(part); |
275 | } |
276 | if(cont & 2) |
277 | bufcat(buf, L" \0", 2); |
278 | else |
279 | bufcat(buf, L"\r\n\0", 3); |
280 | if((final = icwcstombs(buf, "utf-8")) == NULL) |
281 | { |
282 | flog(LOG_CRIT, "could not convert \"%ls\" into utf-8: %s", buf, strerror(errno)); |
283 | free(buf); |
284 | return; |
285 | } |
286 | va_end(al); |
287 | free(buf); |
288 | sockqueue(sk, final, strlen(final)); |
289 | free(final); |
290 | } |
291 | |
292 | struct uiuser *finduser(wchar_t *name) |
293 | { |
294 | struct uiuser *user; |
295 | |
296 | for(user = users; user != NULL; user = user->next) |
297 | { |
298 | if(!wcscmp(user->name, name)) |
299 | break; |
300 | } |
301 | return(user); |
302 | } |
303 | |
304 | static void logout(struct uidata *data) |
305 | { |
306 | data->userinfo = NULL; |
307 | if(data->username != NULL) |
308 | free(data->username); |
309 | data->username = NULL; |
310 | if(data->auth != NULL) |
311 | authputhandle(data->auth); |
312 | data->auth = NULL; |
313 | } |
314 | |
315 | static int haspriv(struct uidata *data, int perm) |
316 | { |
317 | if(data->userinfo == NULL) |
318 | return(0); |
319 | if(data->userinfo->perms & perm) |
320 | return(1); |
321 | else |
322 | return(0); |
323 | } |
324 | |
325 | /* Useful macros for the command functions: */ |
326 | #define haveargs(n) do { if(argc < n) { sq(sk, 0, L"501", L"Wrong number of arguments", NULL); return; } } while(0) |
f7292a8b |
327 | #define havepriv(p) do { if((data->userinfo == NULL) || ((data->userinfo->perms & (p)) != (p))) { sq(sk, 0, L"502", L"Unauthorized request", L"needed", L"%x", (p), L"had", L"%x", data->userinfo->perms, NULL); return; } } while(0) |
d3372da9 |
328 | |
329 | static void cmd_connect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
330 | { |
331 | int valid; |
504ba854 |
332 | struct in6_addr mv4lo; |
d3372da9 |
333 | |
334 | if(confgetint("ui", "onlylocal")) |
335 | { |
336 | switch(sk->remote->sa_family) |
337 | { |
338 | case AF_INET: |
339 | valid = ((struct sockaddr_in *)sk->remote)->sin_addr.s_addr == INADDR_LOOPBACK; |
340 | break; |
341 | case AF_INET6: |
504ba854 |
342 | inet_pton(AF_INET6, "::ffff:127.0.0.1", &mv4lo); |
343 | valid = 0; |
344 | if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback))) |
345 | valid = 1; |
346 | if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &mv4lo, sizeof(in6addr_loopback))) |
347 | valid = 1; |
d3372da9 |
348 | break; |
ddb933f1 |
349 | case AF_UNIX: |
350 | valid = 1; |
351 | break; |
d3372da9 |
352 | default: |
353 | valid = 0; |
354 | break; |
355 | } |
356 | if(!valid) |
357 | { |
358 | sq(sk, 0, L"502", L"Only localhost connections allowed to this host", NULL); |
359 | sk->close = 1; |
360 | data->close = 1; |
361 | return; |
362 | } |
363 | } |
f7292a8b |
364 | sq(sk, 0, L"201", L"1", L"1", L"Dolda Connect daemon v" VERSION, NULL); |
d3372da9 |
365 | } |
366 | |
367 | static void cmd_notfound(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
368 | { |
369 | if((argv != NULL) && (argv[0] != NULL)) |
f7292a8b |
370 | sq(sk, 0, L"500", L"Command not found", NULL); |
d3372da9 |
371 | else |
372 | sq(sk, 0, L"500", L"No command", NULL); |
373 | } |
374 | |
375 | static void cmd_shutdown(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
376 | { |
377 | extern volatile int running; |
378 | |
379 | havepriv(PERM_ADMIN); |
380 | flog(LOG_NOTICE, "UI shutdown request from %ls, shutting down", data->username); |
381 | running = 0; |
382 | sq(sk, 0, L"200", L"Daemon shutting down", NULL); |
383 | } |
384 | |
385 | static void cmd_quit(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
386 | { |
387 | sq(sk, 0, L"200", L"Closing connection", NULL); |
388 | data->close = 1; |
389 | } |
390 | |
391 | static void cmd_lsauth(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
392 | { |
393 | struct authmech *mech, *prev; |
394 | |
395 | prev = NULL; |
396 | for(mech = mechs; mech != NULL; mech = mech->next) |
397 | { |
3616b334 |
398 | if(mech->enabled && authavailable(mech, sk)) |
d3372da9 |
399 | { |
400 | if(prev != NULL) |
401 | sq(sk, 1, L"200", prev->name, NULL); |
402 | prev = mech; |
403 | } |
404 | } |
405 | if(prev == NULL) |
406 | sq(sk, 0, L"201", L"No authentication methods supported", NULL); |
407 | else |
408 | sq(sk, 0, L"200", prev->name, NULL); |
409 | } |
410 | |
411 | static void cmd_login(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
412 | { |
413 | char *buf; |
414 | int code; |
415 | struct passwd *pwd; |
416 | |
417 | haveargs(3); |
418 | if(data->username != NULL) |
419 | { |
420 | if(data->userinfo != NULL) |
421 | sq(sk, 0, L"503", L"Already logged in", NULL); |
422 | else |
423 | sq(sk, 0, L"503", L"Already logging in", NULL); |
424 | return; |
425 | } |
426 | if((buf = icwcstombs(argv[2], NULL)) == NULL) |
427 | { |
428 | sq(sk, 0, L"504", L"Could not convert username to locale charset", NULL); |
429 | return; |
430 | } |
431 | data->username = swcsdup(argv[2]); |
432 | if((pwd = getpwnam(buf)) == NULL) |
433 | data->uid = -1; |
434 | else |
435 | data->uid = pwd->pw_uid; |
436 | if((data->auth = initauth(argv[1], buf)) == NULL) |
437 | { |
438 | if(errno == ENOENT) |
439 | sq(sk, 0, L"508", L"No such authentication mechanism", NULL); |
440 | else |
f7292a8b |
441 | sq(sk, 0, L"505", L"Could not initialize authentication system", L"%s", strerror(errno), NULL); |
d3372da9 |
442 | free(buf); |
443 | logout(data); |
444 | return; |
445 | } |
446 | free(buf); |
3616b334 |
447 | switch(authenticate(data->auth, sk, NULL)) |
d3372da9 |
448 | { |
449 | case AUTH_SUCCESS: |
450 | data->userinfo = finduser(data->username); |
451 | if(data->userinfo == NULL) |
452 | data->userinfo = finduser(L"default"); |
453 | if(data->uid == -1) |
454 | { |
455 | sq(sk, 0, L"506", L"Authentication error", NULL); |
6c9eee05 |
456 | flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen)); |
d3372da9 |
457 | logout(data); |
458 | } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) { |
459 | sq(sk, 0, L"506", L"Authentication error", NULL); |
6c9eee05 |
460 | flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen)); |
d3372da9 |
461 | logout(data); |
462 | } else { |
463 | sq(sk, 0, L"200", L"Welcome", NULL); |
6c9eee05 |
464 | flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen)); |
d3372da9 |
465 | } |
466 | break; |
467 | case AUTH_DENIED: |
f7292a8b |
468 | sq(sk, 0, L"506", L"Authentication error", L"%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL); |
6c9eee05 |
469 | flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen)); |
d3372da9 |
470 | logout(data); |
471 | break; |
472 | case AUTH_PASS: |
473 | switch(data->auth->prompt) |
474 | { |
475 | case AUTH_PR_AUTO: |
476 | code = 300; |
477 | break; |
478 | case AUTH_PR_NOECHO: |
479 | code = 301; |
480 | break; |
481 | case AUTH_PR_ECHO: |
482 | code = 302; |
483 | break; |
484 | case AUTH_PR_INFO: |
485 | code = 303; |
486 | break; |
487 | case AUTH_PR_ERROR: |
488 | code = 304; |
489 | break; |
490 | } |
f7292a8b |
491 | sq(sk, 0, L"%i", code, data->auth->text, NULL); |
d3372da9 |
492 | break; |
493 | case AUTH_ERR: |
f7292a8b |
494 | sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL); |
d3372da9 |
495 | logout(data); |
496 | break; |
497 | default: |
498 | flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_login"); |
f7292a8b |
499 | sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL); |
d3372da9 |
500 | logout(data); |
501 | break; |
502 | } |
503 | } |
504 | |
505 | static void cmd_pass(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
506 | { |
507 | char *buf; |
508 | int code; |
509 | |
510 | haveargs(2); |
511 | if((buf = icwcstombs(argv[1], NULL)) == NULL) |
512 | { |
513 | sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL); |
514 | return; |
515 | } |
516 | if((data->auth == NULL) || (data->userinfo != NULL)) |
517 | { |
518 | sq(sk, 0, L"507", L"Data not expected", NULL); |
519 | return; |
520 | } |
3616b334 |
521 | switch(authenticate(data->auth, sk, buf)) |
d3372da9 |
522 | { |
523 | case AUTH_SUCCESS: |
524 | data->userinfo = finduser(data->username); |
525 | if(data->userinfo == NULL) |
526 | data->userinfo = finduser(L"default"); |
527 | if(data->uid == -1) |
528 | { |
529 | sq(sk, 0, L"506", L"Authentication error", NULL); |
6c9eee05 |
530 | flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen)); |
d3372da9 |
531 | logout(data); |
532 | } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) { |
533 | sq(sk, 0, L"506", L"Authentication error", NULL); |
6c9eee05 |
534 | flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen)); |
d3372da9 |
535 | logout(data); |
536 | } else { |
537 | sq(sk, 0, L"200", L"Welcome", NULL); |
6c9eee05 |
538 | flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen)); |
d3372da9 |
539 | } |
540 | break; |
541 | case AUTH_DENIED: |
f7292a8b |
542 | sq(sk, 0, L"506", L"Authentication error", L"%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL); |
6c9eee05 |
543 | flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen)); |
d3372da9 |
544 | logout(data); |
545 | break; |
546 | case AUTH_PASS: |
547 | switch(data->auth->prompt) |
548 | { |
549 | case AUTH_PR_AUTO: |
550 | code = 300; |
551 | break; |
552 | case AUTH_PR_NOECHO: |
553 | code = 301; |
554 | break; |
555 | case AUTH_PR_ECHO: |
556 | code = 302; |
557 | break; |
558 | case AUTH_PR_INFO: |
559 | code = 303; |
560 | break; |
561 | case AUTH_PR_ERROR: |
562 | code = 304; |
563 | break; |
564 | } |
f7292a8b |
565 | sq(sk, 0, L"%i", code, data->auth->text, NULL); |
d3372da9 |
566 | break; |
567 | case AUTH_ERR: |
f7292a8b |
568 | sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL); |
d3372da9 |
569 | logout(data); |
570 | break; |
571 | default: |
572 | flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_pass"); |
f7292a8b |
573 | sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL); |
d3372da9 |
574 | logout(data); |
575 | break; |
576 | } |
577 | free(buf); |
578 | } |
579 | |
580 | static void cmd_fnetconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
581 | { |
582 | int i; |
583 | char *buf; |
584 | int err; |
585 | struct fnetnode *fn; |
efc05613 |
586 | struct wcspair *args; |
d3372da9 |
587 | |
588 | haveargs(3); |
589 | havepriv(PERM_FNETCTL); |
dd31fcfa |
590 | for(i = 0, fn = fnetnodes; fn != NULL; i++, fn = fn->next); |
591 | if((confgetint("fnet", "maxnodes") > 0) && (i >= confgetint("fnet", "maxnodes"))) { |
592 | sq(sk, 0, L"515", L"Too many fnetnodes connected already", NULL); |
593 | return; |
594 | } |
d3372da9 |
595 | if((buf = icwcstombs(argv[2], NULL)) == NULL) |
596 | { |
597 | sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL); |
598 | return; |
599 | } |
efc05613 |
600 | args = NULL; |
601 | for(i = 3; i < argc - 1; i += 2) |
602 | newwcspair(argv[i], argv[i + 1], &args); |
a3a4a3a7 |
603 | fn = fnetinitconnect(argv[1], data->userinfo->name, buf, args); |
d3372da9 |
604 | err = errno; |
605 | free(buf); |
606 | if(fn == NULL) |
607 | { |
608 | if(errno == EPROTONOSUPPORT) |
609 | sq(sk, 0, L"511", L"No such network name", NULL); |
610 | else |
f7292a8b |
611 | sq(sk, 0, L"509", L"Could not parse the address", L"%s", strerror(err), NULL); |
d3372da9 |
612 | return; |
613 | } |
d3372da9 |
614 | linkfnetnode(fn); |
615 | fnetsetname(fn, argv[2]); |
f7292a8b |
616 | sq(sk, 0, L"200", L"%i", fn->id, L"Connection under way", NULL); |
d3372da9 |
617 | putfnetnode(fn); |
d3372da9 |
618 | } |
619 | |
620 | static void cmd_lsnodes(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
621 | { |
622 | struct fnetnode *fn; |
623 | |
624 | if(fnetnodes == NULL) |
625 | { |
626 | sq(sk, 0, L"201", L"No connected nodes", NULL); |
627 | return; |
628 | } |
629 | for(fn = fnetnodes; fn != NULL; fn = fn->next) |
630 | { |
f7292a8b |
631 | sq(sk, (fn->next != NULL)?1:0, L"200", L"%i", fn->id, fn->fnet->name, (fn->name == NULL)?L"":fn->name, L"%i", fn->numpeers, L"%i", fn->state, L"%ls", fn->pubid, NULL); |
d3372da9 |
632 | } |
633 | } |
634 | |
635 | static void cmd_disconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
636 | { |
637 | struct fnetnode *fn; |
638 | int i; |
639 | |
640 | haveargs(2); |
641 | havepriv(PERM_FNETCTL); |
642 | /* Note - Programmatical user interfaces must only give one |
643 | * argument per command, the multiple argument form is only for |
644 | * convenience when manually controlling the daemon via |
645 | * eg. telnet. The reason is that the return codes aren't clear |
646 | * enough for the multiple argument form. */ |
647 | for(i = 1; i < argc; i++) |
648 | { |
649 | if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL) |
650 | { |
651 | sq(sk, 0, L"510", L"No such node", NULL); |
652 | return; |
653 | } |
18603153 |
654 | if(wpfind(fn->args, L"locked") && !((data->userinfo->perms & PERM_ADMIN) || !wcscmp(data->userinfo->name, fn->owner))) |
655 | { |
656 | sq(sk, 0, L"502", L"This node is locked and you are neither administrator nor its owner", NULL); |
657 | return; |
658 | } |
d3372da9 |
659 | killfnetnode(fn); |
660 | unlinkfnetnode(fn); |
661 | } |
662 | sq(sk, 0, L"200", L"Node flagged for disconnection", NULL); |
663 | } |
664 | |
665 | static void cmd_lspa(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
666 | { |
667 | struct fnetnode *fn; |
668 | struct fnetpeerdatum *datum; |
669 | |
670 | haveargs(2); |
671 | if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL) |
672 | { |
673 | sq(sk, 0, L"510", L"No such node", NULL); |
674 | return; |
675 | } |
676 | if(fn->peerdata == NULL) |
677 | { |
678 | sq(sk, 0, L"201", L"No data available", NULL); |
679 | } else { |
680 | for(datum = fn->peerdata; datum != NULL; datum = datum->next) |
f7292a8b |
681 | sq(sk, (datum->next != NULL)?1:0, L"200", datum->id, L"%i", datum->datatype, NULL); |
d3372da9 |
682 | } |
683 | } |
684 | |
685 | static void cmd_lspeers(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
686 | { |
687 | int i; |
688 | struct fnetnode *fn; |
689 | struct fnetpeer *peer; |
d3372da9 |
690 | |
691 | haveargs(2); |
692 | if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL) |
693 | { |
694 | sq(sk, 0, L"510", L"No such node", NULL); |
695 | return; |
696 | } |
697 | if(fn->peers == NULL) |
698 | { |
699 | sq(sk, 0, L"201", L"No peers avaiable", NULL); |
700 | } else { |
701 | for(peer = fn->peers; peer != NULL; peer = peer->next) |
702 | { |
f7292a8b |
703 | sq(sk, 2 | ((peer->next != NULL)?1:0), L"200", L"%ls", peer->id, L"%ls", peer->nick, NULL); |
d3372da9 |
704 | for(i = 0; i < peer->dinum; i++) |
705 | { |
706 | if(peer->peerdi[i].datum->datatype == FNPD_INT) |
f7292a8b |
707 | sq(sk, 2, peer->peerdi[i].datum->id, L"%i", peer->peerdi[i].data.num, NULL); |
d3372da9 |
708 | if(peer->peerdi[i].datum->datatype == FNPD_LL) |
f7292a8b |
709 | sq(sk, 2, peer->peerdi[i].datum->id, L"%ll", peer->peerdi[i].data.lnum, NULL); |
d3372da9 |
710 | if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL)) |
f7292a8b |
711 | sq(sk, 2, peer->peerdi[i].datum->id, L"%ls", peer->peerdi[i].data.str, NULL); |
d3372da9 |
712 | } |
713 | sq(sk, 0, NULL); |
714 | } |
715 | } |
716 | } |
717 | |
718 | static void cmd_download(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
719 | { |
720 | int i; |
721 | struct fnet *net; |
722 | struct fnetnode *fn; |
723 | struct transfer *transfer; |
724 | struct fnetpeer *peer; |
725 | |
726 | haveargs(4); |
727 | if((argc > 5) && ((argc % 2) == 0)) |
728 | { |
729 | sq(sk, 0, L"501", L"Must have an even number of arguments", NULL); |
730 | return; |
731 | } |
732 | havepriv(PERM_TRANS); |
733 | if((*(argv[1]) >= L'0') && (*(argv[1]) <= L'9')) |
734 | { |
735 | if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL) |
736 | { |
737 | sq(sk, 0, L"510", L"No such node", NULL); |
738 | return; |
739 | } |
740 | net = fn->fnet; |
741 | } else { |
742 | fn = NULL; |
743 | if((net = findfnet(argv[1])) == NULL) |
744 | { |
745 | sq(sk, 0, L"511", L"No such network name", NULL); |
746 | return; |
747 | } |
748 | } |
749 | transfer = newtransfer(); |
750 | authgethandle(transfer->auth = data->auth); |
751 | transfer->fnet = net; |
752 | transfer->peerid = swcsdup(argv[2]); |
753 | transfer->path = swcsdup(argv[3]); |
754 | transfer->dir = TRNSD_DOWN; |
755 | transfer->owner = data->uid; |
756 | if(fn != NULL) |
757 | { |
758 | transfer->fn = fn; |
759 | getfnetnode(fn); |
760 | linktransfer(transfer); |
761 | if(((peer = fnetfindpeer(fn, transfer->peerid)) != NULL) && (peer->nick != NULL)) |
762 | transfersetnick(transfer, peer->nick); |
763 | } else { |
764 | linktransfer(transfer); |
765 | } |
766 | if(argc > 4) |
767 | transfersetsize(transfer, wcstol(argv[4], NULL, 0)); |
768 | if(argc > 5) |
769 | { |
770 | for(i = 5; i < argc; i += 2) |
5123bec0 |
771 | { |
772 | if(!wcscmp(argv[i], L"hash")) |
773 | { |
a04fc9ff |
774 | transfersethash(transfer, parsehash(argv[i + 1])); |
5123bec0 |
775 | } else { |
9ec790e8 |
776 | newwcspair(argv[i], argv[i + 1], &transfer->args); |
5123bec0 |
777 | } |
778 | } |
d3372da9 |
779 | } |
f7292a8b |
780 | sq(sk, 0, L"200", L"%i", transfer->id, L"Download queued", NULL); |
d3372da9 |
781 | transfersetactivity(transfer, L"create"); |
782 | } |
783 | |
784 | static void cmd_lstrans(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
785 | { |
786 | struct transfer *transfer, *pt; |
787 | |
788 | havepriv(PERM_TRANS); |
789 | pt = NULL; |
790 | for(transfer = transfers; transfer != NULL; transfer = transfer->next) |
791 | { |
792 | if((transfer->dir != TRNSD_DOWN) || (transfer->owner == data->uid)) |
793 | { |
794 | if(pt != NULL) |
f7292a8b |
795 | sq(sk, 1, L"200", L"%i", pt->id, L"%i", pt->dir, |
796 | L"%i", pt->state, pt->peerid, |
d3372da9 |
797 | (pt->peernick == NULL)?L"":(pt->peernick), |
798 | (pt->path == NULL)?L"":(pt->path), |
f7292a8b |
799 | L"%i", pt->size, L"%i", pt->curpos, |
5123bec0 |
800 | (pt->hash == NULL)?L"":unparsehash(pt->hash), |
d3372da9 |
801 | NULL); |
802 | pt = transfer; |
803 | } |
804 | } |
805 | if(pt == NULL) |
806 | sq(sk, 0, L"201", L"No transfers", NULL); |
807 | else |
f7292a8b |
808 | sq(sk, 0, L"200", L"%i", pt->id, L"%i", pt->dir, |
809 | L"%i", pt->state, pt->peerid, |
d3372da9 |
810 | (pt->peernick == NULL)?L"":(pt->peernick), |
811 | (pt->path == NULL)?L"":(pt->path), |
f7292a8b |
812 | L"%i", pt->size, L"%i", pt->curpos, |
5123bec0 |
813 | (pt->hash == NULL)?L"":unparsehash(pt->hash), |
d3372da9 |
814 | NULL); |
815 | } |
816 | |
817 | static void cmd_cancel(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
818 | { |
819 | struct transfer *transfer; |
820 | |
821 | haveargs(2); |
822 | havepriv(PERM_TRANS); |
823 | if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL) |
824 | { |
825 | sq(sk, 0, L"512", L"No such transfer", NULL); |
826 | return; |
827 | } |
828 | if((transfer->dir == TRNSD_UP) && !(data->userinfo->perms & PERM_TRANSCU)) |
829 | { |
830 | sq(sk, 0, L"502", L"You are not allowed to cancel uploads", NULL); |
831 | return; |
832 | } |
833 | if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid)) |
834 | { |
835 | sq(sk, 0, L"502", L"You do not own that transfer", NULL); |
836 | return; |
837 | } |
838 | transfer->close = 1; |
839 | sq(sk, 0, L"200", L"Transfer cancelled", NULL); |
840 | } |
841 | |
842 | static void cmd_notify(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
843 | { |
844 | int i, val; |
845 | |
846 | if((argc % 2) != 1) |
847 | { |
848 | sq(sk, 0, L"501", L"Must have an even number of arguments", NULL); |
849 | return; |
850 | } |
851 | for(i = 1; i < argc; i += 2) |
852 | { |
853 | if(!wcscasecmp(argv[i + 1], L"on")) |
854 | val = 1; |
855 | else |
856 | val = 0; |
857 | if(!wcscasecmp(argv[i], L"all")) |
858 | { |
859 | if(val) |
860 | data->notify.w = ~0; |
861 | else |
862 | data->notify.w = 0; |
863 | } else if(!wcscasecmp(argv[i], L"fn:chat")) { |
864 | data->notify.b.fnchat = val; |
865 | } else if(!wcscasecmp(argv[i], L"fn:act")) { |
866 | data->notify.b.fnact = val; |
ba86d49a |
867 | } else if(!wcscasecmp(argv[i], L"fn:peer")) { |
868 | data->notify.b.fnpeer = val; |
d3372da9 |
869 | } else if(!wcscasecmp(argv[i], L"trans:act")) { |
870 | data->notify.b.tract = val; |
871 | } else if(!wcscasecmp(argv[i], L"trans:prog")) { |
872 | data->notify.b.trprog = val; |
873 | } else if(!wcscasecmp(argv[i], L"srch:act")) { |
874 | data->notify.b.srch = val; |
5f151349 |
875 | } else if(!wcscasecmp(argv[i], L"msg")) { |
876 | data->notify.b.msg = val; |
d3372da9 |
877 | } |
878 | } |
879 | sq(sk, 0, L"200", L"Notification alteration succeeded", NULL); |
880 | } |
881 | |
882 | static void cmd_sendchat(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
883 | { |
884 | struct fnetnode *fn; |
885 | int public; |
886 | |
887 | haveargs(5); |
888 | havepriv(PERM_CHAT); |
889 | if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL) |
890 | { |
891 | sq(sk, 0, L"510", L"No such node", NULL); |
892 | return; |
893 | } |
894 | public = wcstol(argv[2], NULL, 0); |
895 | if((public != 0) && (public != 1)) |
896 | { |
897 | sq(sk, 0, L"509", L"Second argument must be 0 or 1", NULL); |
898 | return; |
899 | } |
900 | if(fn->state != FNN_EST) |
901 | { |
902 | sq(sk, 0, L"513", L"Hub is in state FNN_EST", NULL); |
903 | return; |
904 | } |
905 | if(fnetsendchat(fn, public, argv[3], argv[4])) |
906 | { |
907 | if(errno == ENOTSUP) |
908 | sq(sk, 0, L"513", L"This network does not support chatting", NULL); |
909 | else if(errno == EPERM) |
910 | sq(sk, 0, L"502", L"This node does not allow you to chat", NULL); |
911 | else if(errno == EILSEQ) |
912 | sq(sk, 0, L"504", L"This network could not support all the characters in that message", NULL); |
913 | else |
914 | sq(sk, 0, L"505", L"Could not chat", NULL); |
915 | return; |
916 | } |
917 | sq(sk, 0, L"200", L"Chat string sent", NULL); |
918 | } |
919 | |
920 | static void cmd_search(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
921 | { |
922 | struct search *srch; |
923 | struct fnetnode *fn; |
924 | struct sexpr *sexpr; |
925 | int i; |
926 | |
927 | haveargs(3); |
928 | havepriv(PERM_SRCH); |
929 | srch = newsearch(data->username, NULL); |
930 | for(i = 1; i < argc; i++) |
931 | { |
932 | if(!wcscmp(argv[i], L"all")) |
933 | { |
934 | for(fn = fnetnodes; fn != NULL; fn = fn->next) |
935 | { |
936 | if(fn->state == FNN_EST) |
937 | searchaddfn(srch, fn); |
938 | } |
939 | i++; |
940 | break; |
941 | } else if(!wcscmp(argv[i], L"prio")) { |
942 | if(++i == argc) |
943 | { |
944 | sq(sk, 0, L"501", L"No argument to prio", NULL); |
945 | freesearch(srch); |
946 | return; |
947 | } |
948 | srch->prio = wcstol(argv[i], NULL, 0); |
949 | } else if(iswdigit(*argv[i])) { |
950 | if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL) |
951 | { |
952 | sq(sk, 0, L"510", L"No such node", NULL); |
953 | freesearch(srch); |
954 | return; |
955 | } |
956 | searchaddfn(srch, fn); |
957 | } else { |
958 | break; |
959 | } |
960 | } |
961 | if(srch->fnl == NULL) |
962 | { |
963 | sq(sk, 0, L"501", L"No fnetnodes to search found on line", NULL); |
964 | freesearch(srch); |
965 | return; |
966 | } |
967 | if(i == argc) |
968 | { |
969 | sq(sk, 0, L"501", L"No search expression found on line", NULL); |
970 | freesearch(srch); |
971 | return; |
972 | } |
973 | if((sexpr = parsesexpr(argc - i, argv + i)) == NULL) |
974 | { |
975 | sq(sk, 0, L"509", L"Could not parse search expression", NULL); |
976 | freesearch(srch); |
977 | return; |
978 | } |
979 | optsexpr(sexpr); |
980 | getsexpr(srch->sexpr = sexpr); |
981 | queuesearch(srch); |
982 | CBREG(srch, search_eta, srcheta, NULL, NULL); |
983 | CBREG(srch, search_commit, srchcommit, NULL, NULL); |
984 | CBREG(srch, search_result, srchres, NULL, NULL); |
f7292a8b |
985 | sq(sk, 0, L"200", L"%i", srch->id, L"%i", srch->eta - time(NULL), NULL); |
d3372da9 |
986 | putsexpr(sexpr); |
987 | } |
988 | |
989 | static void cmd_lssrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
990 | { |
991 | struct search *srch, *pt; |
992 | time_t now; |
993 | |
994 | havepriv(PERM_SRCH); |
995 | pt = NULL; |
996 | now = time(NULL); |
997 | for(srch = searches; srch != NULL; srch = srch->next) |
998 | { |
999 | if(!wcscmp(srch->owner, data->username)) |
1000 | { |
1001 | if(pt != NULL) |
f7292a8b |
1002 | sq(sk, 1, L"200", L"%i", pt->id, L"%i", pt->state, L"%i", pt->eta - now, L"%i", pt->numres, NULL); |
d3372da9 |
1003 | pt = srch; |
1004 | } |
1005 | } |
1006 | if(pt == NULL) |
1007 | sq(sk, 0, L"201", L"No searches", NULL); |
1008 | else |
f7292a8b |
1009 | sq(sk, 0, L"200", L"%i", pt->id, L"%i", pt->state, L"%i", pt->eta - now, L"%i", pt->numres, NULL); |
d3372da9 |
1010 | } |
1011 | |
1012 | static void cmd_lssr(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
1013 | { |
1014 | struct search *srch; |
1015 | struct srchres *sr; |
d3372da9 |
1016 | |
1017 | haveargs(2); |
1018 | havepriv(PERM_SRCH); |
1019 | if((srch = findsearch(wcstol(argv[1], NULL, 0))) == NULL) |
1020 | { |
1021 | sq(sk, 0, L"514", L"No such search", NULL); |
1022 | return; |
1023 | } |
1024 | if(srch->results == NULL) |
1025 | { |
1026 | sq(sk, 0, L"201", L"No results", NULL); |
1027 | } else { |
1028 | for(sr = srch->results; sr != NULL; sr = sr->next) |
1029 | { |
f7292a8b |
1030 | sq(sk, (sr->next != NULL)?1:0, L"200", L"%ls", sr->filename, |
1031 | sr->fnet->name, L"%ls", sr->peerid, L"%i", sr->size, |
1032 | L"%i", sr->slots, L"%i", (sr->fn == NULL)?-1:(sr->fn->id), |
1033 | L"%f", sr->time, |
1034 | L"%ls", (sr->hash == NULL)?L"":unparsehash(sr->hash), NULL); |
d3372da9 |
1035 | } |
1036 | } |
1037 | } |
1038 | |
1039 | static void cmd_cansrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
1040 | { |
1041 | struct search *srch; |
1042 | int i; |
1043 | |
1044 | haveargs(2); |
1045 | havepriv(PERM_SRCH); |
1046 | /* Note - Programmatical user interfaces must only give one |
1047 | * argument per command, the multiple argument form is only for |
1048 | * convenience when manually controlling the daemon via |
1049 | * eg. telnet. The reason is that the return codes aren't clear |
1050 | * enough for the multiple argument form. */ |
1051 | for(i = 1; i < argc; i++) |
1052 | { |
1053 | if((srch = findsearch(wcstol(argv[i], NULL, 0))) == NULL) |
1054 | { |
1055 | sq(sk, 0, L"514", L"No such search", NULL); |
1056 | return; |
1057 | } |
1058 | freesearch(srch); |
1059 | } |
1060 | sq(sk, 0, L"200", L"Search cancelled", NULL); |
1061 | } |
1062 | |
cab0b442 |
1063 | static void fcmdread(struct socket *sk, struct uidata *data) |
d3372da9 |
1064 | { |
1065 | char *buf; |
1066 | size_t bufsize; |
1067 | |
1068 | if((buf = sockgetinbuf(sk, &bufsize)) == NULL) |
cab0b442 |
1069 | return; |
d3372da9 |
1070 | bufcat(data->fcmdbuf, buf, bufsize); |
1071 | free(buf); |
1072 | } |
1073 | |
cab0b442 |
1074 | static void fcmderr(struct socket *sk, int err, struct uidata *data) |
d3372da9 |
1075 | { |
1076 | wchar_t *wbuf, *p, *p2; |
1077 | |
1078 | if(err) |
1079 | { |
1080 | flog(LOG_WARNING, "error occurred on filtercmd pipe socket: %s", strerror(err)); |
1081 | kill(-data->fcmdpid, SIGHUP); |
1082 | putsock(data->fcmdsk); |
1083 | data->fcmdsk = NULL; |
1084 | if(data->fcmdbuf != NULL) |
1085 | { |
1086 | free(data->fcmdbuf); |
1087 | data->fcmdbuf = NULL; |
1088 | } |
1089 | data->fcmdbufsize = data->fcmdbufdata = 0; |
f7292a8b |
1090 | sq(data->sk, 0, L"505", L"An error occurred on the pipe to the filtercmd", L"%s", strerror(err), NULL); |
cab0b442 |
1091 | return; |
d3372da9 |
1092 | } |
1093 | putsock(data->fcmdsk); |
1094 | data->fcmdsk = NULL; |
1095 | data->fcmdpid = 0; |
1096 | if(data->fcmdbuf == NULL) |
1097 | { |
1098 | wbuf = swcsdup(L""); |
1099 | } else { |
1100 | addtobuf(data->fcmdbuf, 0); |
1101 | wbuf = icmbstowcs(data->fcmdbuf, NULL); |
1102 | free(data->fcmdbuf); |
1103 | } |
1104 | data->fcmdbuf = NULL; |
1105 | data->fcmdbufsize = data->fcmdbufdata = 0; |
1106 | if(wbuf == NULL) |
1107 | { |
1108 | sq(data->sk, 0, L"504", L"Filtercmd sent data which could not be converted from the local charset", NULL); |
cab0b442 |
1109 | return; |
d3372da9 |
1110 | } |
1111 | p = wbuf; |
1112 | for(p2 = wcschr(p, L'\n'); p2 != NULL; p2 = wcschr(p, L'\n')) |
1113 | { |
1114 | *(p2++) = L'\0'; |
f7292a8b |
1115 | sq(data->sk, (*p2 == L'\0')?0:1, L"200", L"%ls", p, NULL); |
d3372da9 |
1116 | p = p2; |
1117 | } |
1118 | if(*p == L'\0') |
1119 | { |
1120 | if(p == wbuf) |
1121 | sq(data->sk, 0, L"201", L"No data returned", NULL); |
1122 | } else { |
f7292a8b |
1123 | sq(data->sk, 0, L"200", L"%ls", p, NULL); |
d3372da9 |
1124 | } |
1125 | free(wbuf); |
1126 | } |
1127 | |
1128 | static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
1129 | { |
1130 | int i; |
1131 | pid_t pid; |
1132 | int pipe; |
1133 | char **cargv, **pp; |
1134 | char *filtercmd, *argbuf; |
1135 | size_t cargvsize, cargvdata; |
1136 | struct passwd *pwent; |
1137 | |
1138 | haveargs(2); |
1139 | havepriv(PERM_TRANS); |
1140 | if((pwent = getpwuid(data->uid)) == NULL) |
1141 | { |
1142 | flog(LOG_WARNING, "no passwd entry for UI user %i", data->uid); |
1143 | sq(sk, 0, L"505", L"System error - Could not fork session", "Internal error", NULL); |
1144 | return; |
1145 | } |
ddb933f1 |
1146 | filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), NULL, 0); |
1147 | if(filtercmd == NULL) |
1148 | filtercmd = findfile("dc-filtercmd", pwent->pw_dir, 0); |
1149 | if(filtercmd == NULL) |
d3372da9 |
1150 | { |
1151 | flog(LOG_WARNING, "could not find filtercmd executable for user %s", pwent->pw_name); |
1152 | sq(sk, 0, L"505", L"System error - Could not fork session", L"Could not find filtercmd executable", NULL); |
1153 | return; |
1154 | } |
1155 | cargv = NULL; |
1156 | cargvsize = cargvdata = 0; |
1157 | addtobuf(cargv, filtercmd); |
1158 | for(i = 1; i < argc; i++) |
1159 | { |
1160 | if((argbuf = icwcstombs(argv[i], NULL)) == NULL) |
1161 | { |
1162 | for(i = 0; i < cargvdata; i++) |
1163 | free(cargv[i]); |
1164 | free(cargv); |
f7292a8b |
1165 | sq(sk, 0, L"504", L"Could not convert argument into local character set", L"%i", i, L"%s", strerror(errno), NULL); |
d3372da9 |
1166 | return; |
1167 | } |
1168 | addtobuf(cargv, argbuf); |
1169 | } |
1170 | addtobuf(cargv, NULL); |
1171 | if((pid = forksess(data->uid, data->auth, NULL, NULL, FD_FILE, 0, O_RDWR, "/dev/null", FD_PIPE, 1, O_RDONLY, &pipe, FD_FILE, 2, O_RDWR, "/dev/null", FD_END)) < 0) |
1172 | { |
1173 | flog(LOG_WARNING, "could not fork session in filtercmd: %s", strerror(errno)); |
f7292a8b |
1174 | sq(sk, 0, L"505", L"System error - Could not fork session", L"%s", strerror(errno), NULL); |
d3372da9 |
1175 | return; |
1176 | } |
1177 | if(pid == 0) |
1178 | { |
1179 | execv(filtercmd, cargv); |
1180 | flog(LOG_WARNING, "could not exec filtercmd %s: %s", filtercmd, strerror(errno)); |
1181 | exit(127); |
1182 | } |
1183 | for(pp = cargv; *pp; pp++) |
1184 | free(*pp); |
1185 | free(cargv); |
1186 | data->fcmdsk = wrapsock(pipe); |
1187 | data->fcmdpid = pid; |
1188 | if(data->fcmdbuf != NULL) |
1189 | { |
1190 | free(data->fcmdbuf); |
1191 | data->fcmdbuf = NULL; |
1192 | } |
1193 | data->fcmdbufsize = data->fcmdbufdata = 0; |
cab0b442 |
1194 | data->fcmdsk->data = data; |
1195 | data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread; |
1196 | data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr; |
d3372da9 |
1197 | } |
1198 | |
1199 | static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
1200 | { |
1201 | struct transfer *transfer; |
9ec790e8 |
1202 | struct wcspair *ta; |
d3372da9 |
1203 | |
1204 | haveargs(2); |
1205 | havepriv(PERM_TRANS); |
1206 | if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL) |
1207 | { |
1208 | sq(sk, 0, L"512", L"No such transfer", NULL); |
1209 | return; |
1210 | } |
1211 | if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid)) |
1212 | { |
1213 | sq(sk, 0, L"502", L"You do not own that transfer", NULL); |
1214 | return; |
1215 | } |
1216 | if(transfer->args == NULL) |
1217 | { |
1218 | sq(sk, 0, L"201", L"Transfer has no arguments", NULL); |
1219 | } else { |
1220 | for(ta = transfer->args; ta != NULL; ta = ta->next) |
f7292a8b |
1221 | sq(sk, ta->next != NULL, L"200", L"%ls", ta->key, L"%ls", ta->val, NULL); |
d3372da9 |
1222 | } |
1223 | } |
1224 | |
1225 | static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
1226 | { |
1227 | struct sharecache *node; |
1228 | int total, hashed; |
1229 | |
1230 | total = hashed = 0; |
1231 | for(node = shareroot->child; node != NULL; node = nextscnode(node)) |
1232 | { |
1233 | if(node->f.b.type == FILE_REG) |
1234 | { |
1235 | total++; |
1236 | if(node->f.b.hastth) |
1237 | hashed++; |
1238 | } |
1239 | } |
f7292a8b |
1240 | sq(sk, 0, L"200", L"%i", total, L"tth", L"%i", hashed, NULL); |
d3372da9 |
1241 | } |
1242 | |
796283c6 |
1243 | static void cmd_transstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
1244 | { |
796283c6 |
1245 | havepriv(PERM_TRANS); |
f7292a8b |
1246 | sq(sk, 0, L"200", L"down", L"%ll", bytesdownload, L"up", L"%ll", bytesupload, NULL); |
796283c6 |
1247 | } |
1248 | |
5f151349 |
1249 | static void cmd_register(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
1250 | { |
1251 | struct uidata *d2; |
1252 | |
1253 | haveargs(2); |
1254 | if(data->userinfo == NULL) { |
1255 | sq(sk, 0, L"502", L"Must be logged in", NULL); |
1256 | return; |
1257 | } |
1258 | if(argv[1][0] == L'#') { |
1259 | sq(sk, 0, L"509", L"Name must not begin with a hash sign", NULL); |
1260 | return; |
1261 | } |
1262 | for(d2 = actives; d2 != NULL; d2 = d2->next) { |
7b2663a4 |
1263 | if((d2 != data) && (d2->userinfo == data->userinfo) && d2->regname && !wcscmp(d2->regname, argv[1])) { |
5f151349 |
1264 | sq(sk, 0, L"516", L"Name already in use", NULL); |
1265 | return; |
1266 | } |
1267 | } |
1268 | if(data->regname != NULL) |
1269 | free(data->regname); |
1270 | data->regname = swcsdup(argv[1]); |
1271 | sq(sk, 0, L"200", L"Registered", NULL); |
1272 | } |
1273 | |
1274 | static void cmd_sendmsg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
1275 | { |
1276 | int i, rcptid; |
1277 | struct uidata *rcpt; |
1278 | wchar_t *myname; |
1279 | struct notif *notif; |
1280 | |
1281 | haveargs(2); |
1282 | if(data->userinfo == NULL) { |
1283 | sq(sk, 0, L"502", L"Must be logged in", NULL); |
1284 | return; |
1285 | } |
1286 | if(argv[1][0] == L'#') { |
1287 | rcptid = wcstol(argv[1] + 1, NULL, 0); |
1288 | for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) { |
7b2663a4 |
1289 | if((rcpt->userinfo == data->userinfo) && (rcpt->id == rcptid)) |
5f151349 |
1290 | break; |
1291 | } |
1292 | } else { |
1293 | for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) { |
7b2663a4 |
1294 | if((rcpt->userinfo == data->userinfo) && rcpt->regname && !wcscmp(rcpt->regname, argv[1])) |
5f151349 |
1295 | break; |
1296 | } |
1297 | } |
1298 | if(rcpt == NULL) { |
1299 | sq(sk, 0, L"517", L"No such recipient", NULL); |
1300 | return; |
1301 | } |
1302 | if(!rcpt->notify.b.msg) { |
1303 | sq(sk, 0, L"518", L"Recipient not listening for messages", NULL); |
1304 | return; |
1305 | } |
1306 | if(data->regname != NULL) |
1307 | myname = swcsdup(data->regname); |
1308 | else |
1309 | myname = swprintf2(L"#%i", data->id); |
1310 | notif = newnotif(rcpt, 640, NOTIF_STR, myname, NOTIF_END); |
1311 | for(i = 2; i < argc; i++) |
1312 | notifappend(notif, NOTIF_STR, argv[i], NOTIF_END); |
1313 | sq(sk, 0, L"200", L"Message sent", NULL); |
1314 | } |
1315 | |
9a64a4f7 |
1316 | static void cmd_uptime(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
1317 | { |
f7292a8b |
1318 | sq(sk, 0, L"200", L"%i", time(NULL) - starttime, NULL); |
9a64a4f7 |
1319 | } |
1320 | |
d3372da9 |
1321 | #undef haveargs |
1322 | #undef havepriv |
1323 | |
1324 | /* |
1325 | * Reserved command numbers for nameless commands: |
1326 | * 0: Issued when a client has connected |
1327 | * 1: Issued when a named command couldn't be found |
1328 | */ |
1329 | |
1330 | static struct command commands[] = |
1331 | { |
1332 | {NULL, cmd_connect}, |
1333 | {NULL, cmd_notfound}, |
1334 | {L"shutdown", cmd_shutdown}, |
1335 | {L"quit", cmd_quit}, |
1336 | {L"lsauth", cmd_lsauth}, |
1337 | {L"login", cmd_login}, |
1338 | {L"pass", cmd_pass}, |
1339 | {L"cnct", cmd_fnetconnect}, |
1340 | {L"lsnodes", cmd_lsnodes}, |
1341 | {L"dcnct", cmd_disconnect}, |
1342 | {L"lspa", cmd_lspa}, |
1343 | {L"lspeers", cmd_lspeers}, |
1344 | {L"download", cmd_download}, |
1345 | {L"lstrans", cmd_lstrans}, |
1346 | {L"cancel", cmd_cancel}, |
1347 | {L"notify", cmd_notify}, |
1348 | {L"sendchat", cmd_sendchat}, |
1349 | {L"search", cmd_search}, |
1350 | {L"lssrch", cmd_lssrch}, |
1351 | {L"lssr", cmd_lssr}, |
1352 | {L"cansrch", cmd_cansrch}, |
1353 | {L"filtercmd", cmd_filtercmd}, |
1354 | {L"lstrarg", cmd_lstrarg}, |
1355 | {L"hashstatus", cmd_hashstatus}, |
796283c6 |
1356 | {L"transstatus", cmd_transstatus}, |
5f151349 |
1357 | {L"register", cmd_register}, |
1358 | {L"sendmsg", cmd_sendmsg}, |
9a64a4f7 |
1359 | {L"uptime", cmd_uptime}, |
d3372da9 |
1360 | {NULL, NULL} |
1361 | }; |
1362 | |
1363 | static void freequeuecmd(struct qcommand *qcmd) |
1364 | { |
1365 | int i; |
1366 | |
1367 | if(qcmd->argv != NULL) |
1368 | { |
1369 | for(i = 0; i < qcmd->argc; i++) |
1370 | free(qcmd->argv[i]); |
1371 | free(qcmd->argv); |
1372 | } |
1373 | free(qcmd); |
1374 | } |
1375 | |
1376 | static struct qcommand *unlinkqcmd(struct uidata *data) |
1377 | { |
1378 | struct qcommand *qcmd; |
1379 | |
1380 | qcmd = data->queue; |
1381 | if(qcmd != NULL) |
1382 | { |
1383 | data->queue = qcmd->next; |
1384 | if(qcmd == data->queuelast) |
1385 | data->queuelast = qcmd->next; |
1386 | } |
1387 | return(qcmd); |
1388 | } |
1389 | |
ba86d49a |
1390 | static void notifappendv(struct notif *notif, va_list args) |
d3372da9 |
1391 | { |
d3372da9 |
1392 | int dt, ca; |
1393 | |
d3372da9 |
1394 | while((dt = va_arg(args, int)) != NOTIF_END) |
1395 | { |
1396 | ca = notif->argc; |
1397 | notif->argv = realloc(notif->argv, sizeof(*notif->argv) * ++notif->argc); |
1398 | notif->argv[ca].dt = dt; |
1399 | switch(dt) |
1400 | { |
1401 | case NOTIF_INT: |
1402 | case NOTIF_ID: |
1403 | notif->argv[ca].d.n = va_arg(args, int); |
1404 | break; |
1405 | case NOTIF_STR: |
1406 | notif->argv[ca].d.s = wcsdup(va_arg(args, wchar_t *)); |
1407 | break; |
1408 | case NOTIF_FLOAT: |
1409 | notif->argv[ca].d.d = va_arg(args, double); |
1410 | break; |
1411 | } |
1412 | } |
ba86d49a |
1413 | } |
1414 | |
1415 | static void notifappend(struct notif *notif, ...) |
1416 | { |
1417 | va_list args; |
1418 | |
1419 | va_start(args, notif); |
1420 | notifappendv(notif, args); |
1421 | va_end(args); |
1422 | } |
1423 | |
1424 | static struct notif *newnotif(struct uidata *data, int code, ...) |
1425 | { |
1426 | struct notif *notif; |
1427 | va_list args; |
1428 | |
1429 | notif = smalloc(sizeof(*notif)); |
1430 | memset(notif, 0, sizeof(*notif)); |
1431 | notif->rlimit = 0.0; |
1432 | notif->ui = data; |
1433 | notif->code = code; |
1434 | va_start(args, code); |
1435 | notifappendv(notif, args); |
d3372da9 |
1436 | va_end(args); |
1437 | notif->next = NULL; |
1438 | notif->prev = data->lnotif; |
1439 | if(data->lnotif != NULL) |
1440 | data->lnotif->next = notif; |
1441 | else |
1442 | data->fnotif = notif; |
1443 | data->lnotif = notif; |
1444 | return(notif); |
1445 | } |
1446 | |
1447 | static void freenotif(struct notif *notif) |
1448 | { |
1449 | int i; |
1450 | |
1451 | if(notif->next != NULL) |
1452 | notif->next->prev = notif->prev; |
1453 | if(notif->prev != NULL) |
1454 | notif->prev->next = notif->next; |
1455 | if(notif == notif->ui->fnotif) |
1456 | notif->ui->fnotif = notif->next; |
1457 | if(notif == notif->ui->lnotif) |
1458 | notif->ui->lnotif = notif->prev; |
1459 | if(notif->exptimer != NULL) |
1460 | canceltimer(notif->exptimer); |
1461 | for(i = 0; i < notif->argc; i++) |
1462 | { |
1463 | if(notif->argv[i].dt == NOTIF_STR) |
1464 | free(notif->argv[i].d.s); |
1465 | } |
1466 | if(notif->argv != NULL) |
1467 | free(notif->argv); |
1468 | free(notif); |
1469 | } |
1470 | |
1471 | static void notifexpire(int cancelled, struct notif *notif) |
1472 | { |
1473 | notif->exptimer = NULL; |
1474 | if(!cancelled) |
1475 | freenotif(notif); |
1476 | } |
1477 | |
1478 | static struct notif *findnotif(struct notif *notif, int dir, int state, int code, int id) |
1479 | { |
1480 | int i, cont; |
1481 | |
1482 | for(; notif != NULL; notif = (dir?notif->next:notif->prev)) |
1483 | { |
1484 | if((notif->code == code) && ((state < 0) || (state == notif->state))) |
1485 | { |
1486 | cont = 0; |
1487 | if(id >= 0) |
1488 | { |
1489 | for(i = 0; i < notif->argc; i++) |
1490 | { |
1491 | if((notif->argv[i].dt == NOTIF_ID) && (notif->argv[i].d.n != id)) |
1492 | { |
1493 | cont = 1; |
1494 | break; |
1495 | } |
1496 | } |
1497 | } |
1498 | if(cont) |
1499 | continue; |
1500 | break; |
1501 | } |
1502 | } |
1503 | return(notif); |
1504 | } |
1505 | |
1506 | static void freeuidata(struct uidata *data) |
1507 | { |
1508 | int i; |
1509 | struct qcommand *qcmd; |
1510 | |
1511 | if(data->next != NULL) |
1512 | data->next->prev = data->prev; |
1513 | if(data->prev != NULL) |
1514 | data->prev->next = data->next; |
1515 | if(data == actives) |
1516 | actives = data->next; |
cab0b442 |
1517 | data->sk->readcb = NULL; |
1518 | data->sk->errcb = NULL; |
d3372da9 |
1519 | putsock(data->sk); |
1520 | while((qcmd = unlinkqcmd(data)) != NULL) |
1521 | freequeuecmd(qcmd); |
1522 | iconv_close(data->ichandle); |
1523 | if(data->cw != NULL) |
1524 | free(data->cw); |
1525 | if(data->cb != NULL) |
1526 | free(data->cb); |
1527 | if(data->argv != NULL) |
1528 | { |
1529 | for(i = 0; i < data->argc; i++) |
1530 | free(data->argv[i]); |
1531 | free(data->argv); |
1532 | } |
1533 | if(data->auth != NULL) |
1534 | authputhandle(data->auth); |
5f151349 |
1535 | if(data->regname != NULL) |
1536 | free(data->regname); |
d3372da9 |
1537 | if(data->username != NULL) |
1538 | { |
1539 | if(data->userinfo != NULL) |
1540 | flog(LOG_INFO, "%ls logged out", data->username); |
1541 | free(data->username); |
1542 | } |
1543 | free(data->inbuf); |
1544 | while(data->fnotif != NULL) |
1545 | freenotif(data->fnotif); |
1546 | if(data->fcmdbuf != NULL) |
1547 | free(data->fcmdbuf); |
1548 | if(data->fcmdpid != 0) |
1549 | kill(-data->fcmdpid, SIGHUP); |
1550 | if(data->fcmdsk != NULL) |
1551 | putsock(data->fcmdsk); |
1552 | free(data); |
1553 | } |
1554 | |
1555 | static void queuecmd(struct uidata *data, struct command *cmd, int argc, wchar_t **argv) |
1556 | { |
1557 | struct qcommand *new; |
1558 | |
1559 | new = smalloc(sizeof(*new)); |
1560 | new->cmd = cmd; |
1561 | new->argc = argc; |
1562 | new->argv = argv; |
1563 | new->next = NULL; |
1564 | if(data->queuelast != NULL) |
1565 | data->queuelast->next = new; |
1566 | data->queuelast = new; |
1567 | if(data->queue == NULL) |
1568 | data->queue = new; |
1569 | } |
1570 | |
1571 | static struct uidata *newuidata(struct socket *sk) |
1572 | { |
1573 | struct uidata *data; |
5f151349 |
1574 | static int curid = 0; |
d3372da9 |
1575 | |
1576 | data = smalloc(sizeof(*data)); |
1577 | memset(data, 0, sizeof(*data)); |
5f151349 |
1578 | data->id = curid++; |
d3372da9 |
1579 | data->sk = sk; |
1580 | getsock(sk); |
1581 | data->inbuf = smalloc(1024); |
1582 | data->uid = -1; |
1583 | if((data->ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1) |
1584 | { |
1585 | flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno)); |
1586 | return(NULL); |
1587 | } |
1588 | data->next = actives; |
1589 | data->prev = NULL; |
1590 | if(actives != NULL) |
1591 | actives->prev = data; |
1592 | actives = data; |
1593 | return(data); |
1594 | } |
1595 | |
cab0b442 |
1596 | static void uiread(struct socket *sk, struct uidata *data) |
d3372da9 |
1597 | { |
1598 | int ret, done; |
1599 | char *newbuf; |
1600 | char *p1, *p2; |
1601 | wchar_t *porig; |
1602 | size_t datalen, len2; |
1603 | struct command *cur; |
1604 | |
1605 | if(data->indata > 1024) |
1606 | data->indata = 0; |
1607 | if((newbuf = sockgetinbuf(sk, &datalen)) == NULL) |
cab0b442 |
1608 | return; |
d3372da9 |
1609 | sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1); |
1610 | memcpy(data->inbuf + data->indata, newbuf, datalen); |
1611 | free(newbuf); |
1612 | data->indata += datalen; |
1613 | if(data->cb == NULL) |
1614 | { |
1615 | data->cb = smalloc(sizeof(wchar_t) * (data->cbsize = 64)); |
1616 | data->cbdata = 0; |
1617 | data->pp = data->cb; |
1618 | } |
1619 | done = 0; |
1620 | while(!done) |
1621 | { |
1622 | if(data->cbsize == data->cbdata) |
1623 | { |
1624 | len2 = data->pp - data->cb; |
1625 | data->cb = srealloc(data->cb, sizeof(wchar_t) * (data->cbsize *= 2)); |
1626 | data->pp = data->cb + len2; |
1627 | } |
1628 | p1 = data->inbuf; |
1629 | p2 = (char *)(porig = (data->cb + data->cbdata)); |
1630 | len2 = sizeof(wchar_t) * (data->cbsize - data->cbdata); |
1631 | ret = iconv(data->ichandle, &p1, &data->indata, &p2, &len2); |
1632 | memmove(data->inbuf, p1, data->indata); |
1633 | /* Just a sanity check */ |
1634 | if(((p2 - ((char *)data->cb)) % sizeof(wchar_t)) != 0) |
1635 | { |
1636 | flog(LOG_CRIT, "Aiya! iconv does strange things to our wchar_t's!"); |
1637 | abort(); |
1638 | } |
1639 | data->cbdata += (((wchar_t *)p2) - porig); |
1640 | if(ret < 0) |
1641 | { |
1642 | switch(errno) |
1643 | { |
1644 | case EILSEQ: |
1645 | /* XXX: Should this really just ignore it? */ |
1646 | data->indata = 0; |
1647 | done = 1; |
1648 | break; |
1649 | case EINVAL: |
1650 | done = 1; |
1651 | break; |
1652 | case E2BIG: |
1653 | /* Just a sanity check */ |
1654 | if(data->cbsize != data->cbdata) |
1655 | { |
1656 | flog(LOG_CRIT, "Aiya! iconv doesn't give us wchar_t's!"); |
1657 | abort(); |
1658 | } |
1659 | break; |
1660 | default: |
1661 | flog(LOG_WARNING, "bug: strange error from iconv in uiread: %s", strerror(errno)); |
1662 | break; |
1663 | } |
1664 | } else { |
1665 | done = 1; |
1666 | } |
1667 | } |
1668 | done = 0; |
1669 | while(!done && (data->pp - data->cb < data->cbdata)) |
1670 | { |
1671 | switch(data->ps) |
1672 | { |
1673 | case 0: |
1674 | if(iswspace(*data->pp)) |
1675 | { |
1676 | if(*data->pp == L'\r') |
1677 | { |
1678 | if(data->pp == data->cb + data->cbdata - 1) |
1679 | { |
1680 | done = 1; |
1681 | break; |
1682 | } |
1683 | if(*(++data->pp) == L'\n') |
1684 | { |
1685 | if((data->argv != NULL) && (data->argv[0] != NULL)) |
1686 | { |
1687 | for(cur = commands; cur->handler != NULL; cur++) |
1688 | { |
1689 | if(cur->name == NULL) |
1690 | continue; |
1691 | if(!wcscasecmp(cur->name, data->argv[0])) |
1692 | { |
1693 | queuecmd(data, cur, data->argc, data->argv); |
1694 | break; |
1695 | } |
1696 | } |
1697 | if(cur->handler == NULL) |
1698 | queuecmd(data, &commands[1], data->argc, data->argv); |
1699 | } else { |
1700 | queuecmd(data, &commands[1], data->argc, data->argv); |
1701 | } |
1702 | data->argv = NULL; |
1703 | data->args = 0; |
1704 | data->argc = 0; |
1705 | wmemmove(data->cb, data->pp, data->cbdata -= (data->pp - data->cb)); |
1706 | data->pp = data->cb; |
1707 | } else { |
1708 | data->pp++; |
1709 | } |
1710 | } else { |
1711 | data->pp++; |
1712 | } |
1713 | } else { |
1714 | data->ps = 1; |
1715 | data->cwdata = 0; |
1716 | } |
1717 | break; |
1718 | case 1: |
1719 | if(iswspace(*data->pp)) |
1720 | { |
1721 | addtobuf(data->cw, L'\0'); |
1722 | sizebuf(&data->argv, &data->args, data->argc + 1, sizeof(*data->argv), 1); |
1723 | data->argv[data->argc++] = data->cw; |
1724 | data->cw = NULL; |
1725 | data->cwsize = 0; |
1726 | data->cwdata = 0; |
1727 | data->ps = 0; |
1728 | } else if(*data->pp == L'\"') { |
1729 | data->ps = 2; |
1730 | data->pp++; |
1731 | } else if(*data->pp == L'\\') { |
1732 | if(data->pp == data->cb + data->cbdata - 1) |
1733 | { |
1734 | done = 1; |
1735 | break; |
1736 | } |
1737 | addtobuf(data->cw, *(++data->pp)); |
1738 | data->pp++; |
1739 | } else { |
1740 | addtobuf(data->cw, *(data->pp++)); |
1741 | } |
1742 | break; |
1743 | case 2: |
1744 | if(*data->pp == L'\"') |
1745 | { |
1746 | data->ps = 1; |
1747 | } else if(*data->pp == L'\\') { |
1748 | if(data->pp == data->cb + data->cbdata - 1) |
1749 | { |
1750 | done = 1; |
1751 | break; |
1752 | } |
1753 | addtobuf(data->cw, *(++(data->pp))); |
1754 | } else { |
1755 | addtobuf(data->cw, *data->pp); |
1756 | } |
1757 | data->pp++; |
1758 | break; |
1759 | } |
1760 | } |
1761 | } |
1762 | |
cab0b442 |
1763 | static void uierror(struct socket *sk, int err, struct uidata *data) |
d3372da9 |
1764 | { |
1765 | if(err) |
1766 | flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err)); |
1767 | freeuidata(data); |
1768 | } |
1769 | |
cab0b442 |
1770 | static void uiaccept(struct socket *sk, struct socket *newsk, void *data) |
d3372da9 |
1771 | { |
1772 | struct uidata *uidata; |
1773 | |
cab0b442 |
1774 | newsk->data = uidata = newuidata(newsk); |
d3372da9 |
1775 | socksettos(newsk, confgetint("ui", "uitos")); |
1776 | if(uidata == NULL) |
cab0b442 |
1777 | return; |
1778 | newsk->errcb = (void (*)(struct socket *, int, void *))uierror; |
1779 | newsk->readcb = (void (*)(struct socket *, void *))uiread; |
d3372da9 |
1780 | queuecmd(uidata, &commands[0], 0, NULL); |
1781 | } |
1782 | |
1783 | static int srcheta(struct search *srch, void *uudata) |
1784 | { |
1785 | struct uidata *data; |
1786 | |
1787 | for(data = actives; data != NULL; data = data->next) |
1788 | { |
1789 | if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username)) |
1790 | newnotif(data, 620, NOTIF_ID, srch->id, NOTIF_INT, srch->eta - time(NULL), NOTIF_END); |
1791 | } |
1792 | return(0); |
1793 | } |
1794 | |
1795 | static int srchcommit(struct search *srch, void *uudata) |
1796 | { |
1797 | struct uidata *data; |
1798 | |
1799 | for(data = actives; data != NULL; data = data->next) |
1800 | { |
1801 | if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username)) |
1802 | newnotif(data, 621, NOTIF_ID, srch->id, NOTIF_END); |
1803 | } |
1804 | return(0); |
1805 | } |
1806 | |
1807 | static int srchres(struct search *srch, struct srchres *sr, void *uudata) |
1808 | { |
1809 | struct uidata *data; |
1810 | |
1811 | for(data = actives; data != NULL; data = data->next) |
1812 | { |
1813 | if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username)) |
ebbc13f3 |
1814 | { |
ebbc13f3 |
1815 | newnotif(data, 622, NOTIF_ID, srch->id, NOTIF_STR, sr->filename, NOTIF_STR, sr->fnet->name, NOTIF_STR, sr->peerid, NOTIF_INT, sr->size, |
5123bec0 |
1816 | NOTIF_INT, sr->slots, NOTIF_INT, (sr->fn == NULL)?-1:(sr->fn->id), NOTIF_FLOAT, sr->time, NOTIF_STR, (sr->hash == NULL)?L"":unparsehash(sr->hash), NOTIF_END); |
ebbc13f3 |
1817 | } |
d3372da9 |
1818 | } |
1819 | return(0); |
1820 | } |
1821 | |
1822 | static int recvchat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *string, void *uudata) |
1823 | { |
1824 | struct uidata *data; |
1825 | |
1826 | for(data = actives; data != NULL; data = data->next) |
1827 | { |
1828 | if(haspriv(data, PERM_CHAT) && data->notify.b.fnchat) |
1829 | newnotif(data, 600, NOTIF_ID, fn->id, NOTIF_INT, public, NOTIF_STR, name, NOTIF_STR, peer, NOTIF_STR, string, NOTIF_END); |
1830 | } |
1831 | return(0); |
1832 | } |
1833 | |
1834 | static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata) |
1835 | { |
1836 | struct uidata *data; |
1837 | struct notif *notif; |
1838 | |
1839 | if(!wcscmp(attrib, L"state")) |
1840 | { |
1841 | for(data = actives; data != NULL; data = data->next) |
1842 | { |
1843 | if(data->notify.b.fnact) |
1844 | newnotif(data, 601, NOTIF_ID, fn->id, NOTIF_INT, fn->state, NOTIF_END); |
1845 | } |
1846 | } else if(!wcscmp(attrib, L"name")) { |
1847 | for(data = actives; data != NULL; data = data->next) |
1848 | { |
1849 | if(data->notify.b.fnact) |
1850 | newnotif(data, 602, NOTIF_ID, fn->id, NOTIF_STR, fn->name, NOTIF_END); |
1851 | } |
1852 | } else if(!wcscmp(attrib, L"numpeers")) { |
1853 | for(data = actives; data != NULL; data = data->next) |
1854 | { |
1855 | if(data->notify.b.fnact) |
1856 | { |
1857 | if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL) |
1858 | notif->argv[1].d.n = fn->numpeers; |
1859 | else |
ba86d49a |
1860 | newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END)->rlimit = 0.5; |
d3372da9 |
1861 | } |
1862 | } |
1863 | } |
1864 | return(0); |
1865 | } |
1866 | |
1867 | static int fnunlink(struct fnetnode *fn, void *uudata) |
1868 | { |
1869 | struct uidata *data; |
1870 | |
1871 | for(data = actives; data != NULL; data = data->next) |
1872 | { |
1873 | if(data->notify.b.fnact) |
1874 | newnotif(data, 603, NOTIF_ID, fn->id, NOTIF_END); |
1875 | } |
1876 | return(0); |
1877 | } |
1878 | |
ba86d49a |
1879 | static int peernew(struct fnetnode *fn, struct fnetpeer *peer, void *uudata) |
1880 | { |
1881 | struct uidata *data; |
1882 | |
1883 | for(data = actives; data != NULL; data = data->next) |
1884 | { |
1885 | if(data->notify.b.fnpeer) |
1886 | newnotif(data, 630, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END); |
1887 | } |
1888 | return(0); |
1889 | } |
1890 | |
1891 | static int peerdel(struct fnetnode *fn, struct fnetpeer *peer, void *uudata) |
1892 | { |
1893 | struct uidata *data; |
1894 | |
1895 | for(data = actives; data != NULL; data = data->next) |
1896 | { |
1897 | if(data->notify.b.fnpeer) |
1898 | newnotif(data, 631, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_END); |
1899 | } |
1900 | return(0); |
1901 | } |
1902 | |
1903 | static int peerchange(struct fnetnode *fn, struct fnetpeer *peer, struct fnetpeerdi *di, void *uudata) |
1904 | { |
1905 | struct uidata *data; |
1906 | struct notif *notif; |
1907 | wchar_t buf[32]; |
1908 | |
1909 | for(data = actives; data != NULL; data = data->next) |
1910 | { |
1911 | if(data->notify.b.fnpeer) |
1912 | { |
1913 | for(notif = data->fnotif; notif != NULL; notif = notif->next) |
1914 | { |
1915 | if((notif->code == 632) && (notif->state == NOTIF_PEND) && (notif->argv[0].d.n == fn->id) && !wcscmp(notif->argv[1].d.s, peer->id)) |
1916 | break; |
1917 | } |
1918 | if(notif == NULL) |
1919 | notif = newnotif(data, 632, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END); |
1920 | notifappend(notif, NOTIF_STR, di->datum->id, NOTIF_INT, di->datum->datatype, NOTIF_END); |
1921 | switch(di->datum->datatype) |
1922 | { |
1923 | case FNPD_INT: |
1924 | notifappend(notif, NOTIF_INT, di->data.num, NOTIF_END); |
1925 | break; |
1926 | case FNPD_STR: |
1927 | notifappend(notif, NOTIF_STR, di->data.str, NOTIF_END); |
1928 | break; |
1929 | case FNPD_LL: |
1930 | swprintf(buf, sizeof(buf) / sizeof(*buf), L"%lli", di->data.lnum); |
1931 | notifappend(notif, NOTIF_STR, buf, NOTIF_END); |
1932 | break; |
1933 | } |
1934 | } |
1935 | } |
1936 | return(0); |
1937 | } |
1938 | |
d3372da9 |
1939 | static int newfnetnode(struct fnetnode *fn, void *uudata) |
1940 | { |
1941 | struct uidata *data; |
1942 | |
1943 | for(data = actives; data != NULL; data = data->next) |
1944 | { |
1945 | if(data->notify.b.fnact) |
1946 | newnotif(data, 604, NOTIF_ID, fn->id, NOTIF_STR, fn->fnet->name, NOTIF_END); |
1947 | } |
1948 | CBREG(fn, fnetnode_ac, fnactive, NULL, NULL); |
1949 | CBREG(fn, fnetnode_chat, recvchat, NULL, NULL); |
1950 | CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL); |
ba86d49a |
1951 | CBREG(fn, fnetpeer_new, peernew, NULL, NULL); |
1952 | CBREG(fn, fnetpeer_del, peerdel, NULL, NULL); |
1953 | CBREG(fn, fnetpeer_chdi, peerchange, NULL, NULL); |
d3372da9 |
1954 | return(0); |
1955 | } |
1956 | |
1957 | static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uudata) |
1958 | { |
1959 | struct uidata *data; |
1960 | |
1961 | if(!wcscmp(attrib, L"state")) |
1962 | { |
1963 | for(data = actives; data != NULL; data = data->next) |
1964 | { |
1965 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) |
1966 | newnotif(data, 611, NOTIF_ID, transfer->id, NOTIF_INT, transfer->state, NOTIF_END); |
1967 | } |
1968 | } else if(!wcscmp(attrib, L"nick")) { |
1969 | for(data = actives; data != NULL; data = data->next) |
1970 | { |
1971 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) |
1972 | newnotif(data, 612, NOTIF_ID, transfer->id, NOTIF_STR, transfer->peernick, NOTIF_END); |
1973 | } |
1974 | } else if(!wcscmp(attrib, L"size")) { |
1975 | for(data = actives; data != NULL; data = data->next) |
1976 | { |
1977 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) |
1978 | newnotif(data, 613, NOTIF_ID, transfer->id, NOTIF_INT, transfer->size, NOTIF_END); |
1979 | } |
1980 | } else if(!wcscmp(attrib, L"error")) { |
1981 | for(data = actives; data != NULL; data = data->next) |
1982 | { |
1983 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) |
1984 | newnotif(data, 614, NOTIF_ID, transfer->id, NOTIF_INT, transfer->error, NOTIF_END); |
1985 | } |
1986 | } else if(!wcscmp(attrib, L"path")) { |
1987 | for(data = actives; data != NULL; data = data->next) |
1988 | { |
1989 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) |
1990 | newnotif(data, 616, NOTIF_ID, transfer->id, NOTIF_STR, transfer->path, NOTIF_END); |
1991 | } |
a04fc9ff |
1992 | } else if(!wcscmp(attrib, L"hash")) { |
1993 | for(data = actives; data != NULL; data = data->next) |
1994 | { |
1995 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) |
5e13cdca |
1996 | newnotif(data, 618, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->hash == NULL)?L"":unparsehash(transfer->hash), NOTIF_END); |
a04fc9ff |
1997 | } |
d3372da9 |
1998 | } |
1999 | return(0); |
2000 | } |
2001 | |
2002 | static int transferprog(struct transfer *transfer, void *uudata) |
2003 | { |
2004 | struct uidata *data; |
2005 | struct notif *notif; |
2006 | |
2007 | for(data = actives; data != NULL; data = data->next) |
2008 | { |
2009 | if(haspriv(data, PERM_TRANS) && data->notify.b.trprog && ((transfer->owner == 0) || (transfer->owner == data->uid))) |
2010 | { |
2011 | if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 615, transfer->id)) != NULL) |
2012 | notif->argv[1].d.n = transfer->curpos; |
2013 | else |
2014 | newnotif(data, 615, NOTIF_ID, transfer->id, NOTIF_INT, transfer->curpos, NOTIF_END)->rlimit = 0.5; |
2015 | } |
2016 | } |
2017 | return(0); |
2018 | } |
2019 | |
2020 | static int transferdestroyed(struct transfer *transfer, void *uudata) |
2021 | { |
2022 | struct uidata *data; |
2023 | |
2024 | for(data = actives; data != NULL; data = data->next) |
2025 | { |
2026 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) |
3d204c2d |
2027 | newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->exitstatus == NULL)?L"":(transfer->exitstatus), NOTIF_END); |
d3372da9 |
2028 | } |
2029 | return(0); |
2030 | } |
2031 | |
2032 | static int newtransfernotify(struct transfer *transfer, void *uudata) |
2033 | { |
2034 | struct uidata *data; |
2035 | |
2036 | for(data = actives; data != NULL; data = data->next) |
2037 | { |
2038 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) |
2039 | newnotif(data, 610, NOTIF_ID, transfer->id, NOTIF_INT, transfer->dir, NOTIF_STR, transfer->peerid, NOTIF_STR, (transfer->path == NULL)?L"":transfer->path, NOTIF_END); |
2040 | } |
2041 | CBREG(transfer, trans_ac, transferchattr, NULL, NULL); |
2042 | CBREG(transfer, trans_p, transferprog, NULL, NULL); |
2043 | CBREG(transfer, trans_destroy, transferdestroyed, NULL, NULL); |
2044 | return(0); |
2045 | } |
2046 | |
2047 | static struct uiuser *newuser(wchar_t *name, unsigned long perms) |
2048 | { |
2049 | struct uiuser *new; |
2050 | |
2051 | new = smalloc(sizeof(*new)); |
2052 | new->used = 0; |
2053 | new->name = swcsdup(name); |
2054 | new->perms = perms; |
2055 | new->delete = 0; |
2056 | new->next = users; |
2057 | new->prev = NULL; |
2058 | if(users != NULL) |
2059 | users->prev = new; |
2060 | users = new; |
2061 | return(new); |
2062 | } |
2063 | |
2064 | static void freeuser(struct uiuser *user) |
2065 | { |
2066 | if(user->next != NULL) |
2067 | user->next->prev = user->prev; |
2068 | if(user->prev != NULL) |
2069 | user->prev->next = user->next; |
2070 | if(user == users) |
2071 | users = user->next; |
2072 | free(user->name); |
2073 | free(user); |
2074 | } |
2075 | |
2076 | static int conf_user(int argc, wchar_t **argv) |
2077 | { |
2078 | int i, perms, permmod; |
2079 | struct uiuser *user; |
2080 | wchar_t *p; |
2081 | |
2082 | if(argc < 3) |
2083 | { |
2084 | flog(LOG_WARNING, "not enough arguments given for user command"); |
2085 | return(1); |
2086 | } |
2087 | perms = 0; |
2088 | for(i = 2; i < argc; i++) |
2089 | { |
2090 | if(!iswalpha(argv[i][0])) |
2091 | p = argv[i] + 1; |
2092 | else |
2093 | p = argv[i]; |
2094 | if(!wcscmp(p, L"disallow")) |
2095 | permmod = PERM_DISALLOW; |
2096 | if(!wcscmp(p, L"admin")) |
2097 | permmod = PERM_ADMIN; |
2098 | if(!wcscmp(p, L"fnetctl")) |
2099 | permmod = PERM_FNETCTL; |
2100 | if(!wcscmp(p, L"trans")) |
2101 | permmod = PERM_TRANS; |
2102 | if(!wcscmp(p, L"transcu")) |
2103 | permmod = PERM_TRANSCU; |
2104 | if(!wcscmp(p, L"chat")) |
2105 | permmod = PERM_CHAT; |
2106 | if(!wcscmp(p, L"srch")) |
2107 | permmod = PERM_SRCH; |
2108 | if(!wcscmp(p, L"all")) |
2109 | permmod = ~0; |
2110 | if(argv[i][0] == L'-') |
2111 | perms &= ~permmod; |
2112 | else |
2113 | perms |= permmod; |
2114 | } |
2115 | if((user = finduser(argv[1])) == NULL) |
2116 | { |
2117 | newuser(argv[1], perms); |
2118 | } else { |
2119 | user->delete = 0; |
2120 | user->perms = perms; |
2121 | } |
2122 | return(0); |
2123 | } |
2124 | |
2125 | static void preinit(int hup) |
2126 | { |
2127 | struct uiuser *user; |
2128 | |
2129 | if(!hup) |
2130 | { |
8754fb85 |
2131 | newuser(L"default", PERM_DISALLOW); |
d3372da9 |
2132 | } else { |
2133 | for(user = users; user != NULL; user = user->next) |
2134 | { |
2135 | if(!wcscmp(user->name, L"default")) |
2136 | user->delete = 1; |
2137 | } |
2138 | } |
2139 | } |
2140 | |
ddb933f1 |
2141 | static struct sockaddr_un *makeunixname(void) |
2142 | { |
2143 | static struct sockaddr_un buf; |
2144 | char *val; |
2145 | struct passwd *pwd; |
2146 | uid_t uid; |
2147 | |
2148 | memset(&buf, 0, sizeof(buf)); |
2149 | buf.sun_family = PF_UNIX; |
2150 | if((val = icswcstombs(confgetstr("ui", "unixsock"), NULL, NULL)) == NULL) { |
2151 | flog(LOG_WARNING, "could not map Unix socket name into local charset: %s", strerror(errno)); |
2152 | return(NULL); |
2153 | } |
2154 | if(!strcmp(val, "none")) |
2155 | return(NULL); |
2156 | if(!strcmp(val, "default")) |
2157 | { |
2158 | if((uid = getuid()) == 0) |
2159 | { |
2160 | strcpy(buf.sun_path, "/var/run/doldacond.sock"); |
2161 | return(&buf); |
2162 | } else { |
2163 | if((pwd = getpwuid(uid)) == NULL) |
2164 | { |
2165 | flog(LOG_ERR, "could not get passwd entry for current user: %s", strerror(errno)); |
2166 | return(NULL); |
2167 | } |
2168 | strcpy(buf.sun_path, "/tmp/doldacond-"); |
2169 | strcat(buf.sun_path, pwd->pw_name); |
2170 | return(&buf); |
2171 | } |
2172 | } |
2173 | if(strchr(val, '/')) |
2174 | { |
2175 | strcpy(buf.sun_path, val); |
2176 | return(&buf); |
2177 | } |
2178 | flog(LOG_WARNING, "invalid Unix socket name: %s", val); |
2179 | return(NULL); |
2180 | } |
2181 | |
2182 | static int tcpportupdate(struct configvar *var, void *uudata) |
d3372da9 |
2183 | { |
d3372da9 |
2184 | struct socket *newsock; |
2185 | |
ddb933f1 |
2186 | newsock = NULL; |
2187 | if((var->val.num != -1) && ((newsock = netcstcplisten(var->val.num, 1, uiaccept, NULL)) == NULL)) |
d3372da9 |
2188 | { |
ddb933f1 |
2189 | flog(LOG_WARNING, "could not create new TCP UI socket, reverting to old: %s", strerror(errno)); |
d3372da9 |
2190 | return(0); |
2191 | } |
ddb933f1 |
2192 | if(tcpsocket != NULL) |
2193 | { |
2194 | putsock(tcpsocket); |
2195 | tcpsocket = NULL; |
2196 | } |
2197 | tcpsocket = newsock; |
2198 | return(0); |
2199 | } |
2200 | |
2201 | static int unixsockupdate(struct configvar *var, void *uudata) |
2202 | { |
2203 | struct socket *newsock; |
2204 | struct sockaddr_un *un; |
2205 | |
2206 | newsock = NULL; |
2207 | if(((un = makeunixname()) != NULL) && ((newsock = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL)) |
2208 | { |
2209 | flog(LOG_WARNING, "could not create new Unix UI socket, reverting to old: %s", strerror(errno)); |
2210 | return(0); |
2211 | } |
2212 | if(unixsocket != NULL) |
2213 | { |
2214 | putsock(unixsocket); |
2215 | unixsocket = NULL; |
2216 | } |
2217 | unixsocket = newsock; |
d3372da9 |
2218 | return(0); |
2219 | } |
2220 | |
2221 | static int init(int hup) |
2222 | { |
d3372da9 |
2223 | struct uiuser *user, *next; |
ddb933f1 |
2224 | struct sockaddr_un *un; |
8754fb85 |
2225 | struct passwd *pwd; |
2226 | wchar_t *wcsname; |
d3372da9 |
2227 | |
2228 | if(hup) |
2229 | { |
2230 | for(user = users; user != NULL; user = next) |
2231 | { |
2232 | next = user->next; |
2233 | if(user->delete) |
2234 | freeuser(user); |
2235 | } |
2236 | } |
2237 | if(!hup) |
2238 | { |
9a64a4f7 |
2239 | starttime = time(NULL); |
ddb933f1 |
2240 | if((confgetint("ui", "port") != -1) && ((tcpsocket = netcstcplisten(confgetint("ui", "port"), 1, uiaccept, NULL)) == NULL)) |
2241 | { |
2242 | flog(LOG_CRIT, "could not create TCP UI socket: %s", strerror(errno)); |
2243 | return(1); |
2244 | } |
2245 | CBREG(confgetvar("ui", "port"), conf_update, tcpportupdate, NULL, NULL); |
2246 | if(((un = makeunixname()) != NULL) && ((unixsocket = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL)) |
d3372da9 |
2247 | { |
ddb933f1 |
2248 | flog(LOG_CRIT, "could not create Unix UI socket: %s", strerror(errno)); |
d3372da9 |
2249 | return(1); |
2250 | } |
ddb933f1 |
2251 | CBREG(confgetvar("ui", "unixsock"), conf_update, unixsockupdate, NULL, NULL); |
d3372da9 |
2252 | GCBREG(newfncb, newfnetnode, NULL); |
2253 | GCBREG(newtransfercb, newtransfernotify, NULL); |
2254 | } |
8754fb85 |
2255 | if(getuid() != 0) |
2256 | { |
2257 | for(user = users; user != NULL; user = user->next) |
2258 | { |
2259 | if(wcscmp(user->name, L"default")) |
2260 | break; |
2261 | } |
2262 | if(!user) |
2263 | { |
2264 | if((pwd = getpwuid(getuid())) == NULL) |
2265 | { |
2266 | flog(LOG_CRIT, "could not get login info: %s", strerror(errno)); |
2267 | return(1); |
2268 | } |
2269 | if((wcsname = icmbstowcs(pwd->pw_name, NULL)) == NULL) |
2270 | { |
2271 | flog(LOG_CRIT, "could not convert user name into wcs: %s", strerror(errno)); |
2272 | return(1); |
2273 | } |
2274 | newuser(wcsname, ~PERM_DISALLOW); |
2275 | free(wcsname); |
2276 | } |
2277 | } |
d3372da9 |
2278 | return(0); |
2279 | } |
2280 | |
2281 | static int run(void) |
2282 | { |
2283 | int i, id; |
2284 | struct uidata *data, *next; |
2285 | struct qcommand *qcmd; |
2286 | struct notif *notif, *nnotif; |
d3372da9 |
2287 | |
2288 | for(data = actives; data != NULL; data = next) |
2289 | { |
2290 | next = data->next; |
2291 | if(data->close) |
2292 | freeuidata(data); |
2293 | } |
2294 | for(data = actives; data != NULL; data = data->next) |
2295 | { |
2296 | for(notif = data->fnotif; notif != NULL; notif = nnotif) |
2297 | { |
2298 | nnotif = notif->next; |
2299 | if(notif->state == NOTIF_WAIT) |
2300 | continue; |
2301 | id = -1; |
2302 | for(i = 0; i < notif->argc; i++) |
2303 | { |
2304 | if(notif->argv[i].dt == NOTIF_ID) |
2305 | { |
2306 | id = notif->argv[i].d.n; |
2307 | break; |
2308 | } |
2309 | } |
2310 | if(findnotif(notif->prev, 0, -1, notif->code, id) != NULL) |
2311 | continue; |
f7292a8b |
2312 | sq(data->sk, 2, L"%i", notif->code, NULL); |
d3372da9 |
2313 | for(i = 0; i < notif->argc; i++) |
2314 | { |
2315 | switch(notif->argv[i].dt) |
2316 | { |
2317 | case NOTIF_INT: |
2318 | case NOTIF_ID: |
f7292a8b |
2319 | sq(data->sk, 2, L"%i", notif->argv[i].d.n, NULL); |
d3372da9 |
2320 | break; |
2321 | case NOTIF_STR: |
2322 | if(notif->argv[i].d.s[0] == L'%') |
f7292a8b |
2323 | sq(data->sk, 2, L"%ls", notif->argv[i].d.s, NULL); |
d3372da9 |
2324 | else |
2325 | sq(data->sk, 2, notif->argv[i].d.s, NULL); |
2326 | break; |
2327 | case NOTIF_FLOAT: |
f7292a8b |
2328 | sq(data->sk, 2, L"%f", notif->argv[i].d.d, NULL); |
d3372da9 |
2329 | break; |
2330 | } |
2331 | } |
2332 | sq(data->sk, 0, NULL); |
2333 | if(notif->rlimit != 0) |
2334 | { |
2335 | notif->state = NOTIF_WAIT; |
2336 | notif->exptimer = timercallback(ntime() + notif->rlimit, (void (*)(int, void *))notifexpire, notif); |
2337 | } else { |
2338 | freenotif(notif); |
2339 | } |
2340 | } |
2341 | if((qcmd = unlinkqcmd(data)) != NULL) |
2342 | { |
2343 | qcmd->cmd->handler(data->sk, data, qcmd->argc, qcmd->argv); |
2344 | freequeuecmd(qcmd); |
2345 | return(1); |
2346 | } |
2347 | } |
2348 | return(0); |
2349 | } |
2350 | |
2351 | static void terminate(void) |
2352 | { |
2353 | while(users != NULL) |
2354 | freeuser(users); |
ddb933f1 |
2355 | if(tcpsocket != NULL) |
2356 | putsock(tcpsocket); |
2357 | if(unixsocket != NULL) |
2358 | putsock(unixsocket); |
d3372da9 |
2359 | } |
2360 | |
2361 | static struct configvar myvars[] = |
2362 | { |
d9f89ef5 |
2363 | /** If true, UI connections will only be accepted from localhost |
2364 | * addresses (127.0.0.1, ::1 or ::ffff:127.0.0.1). Unless you are |
2365 | * completely sure that you know what you are doing, never turn |
2366 | * this off when auth.authless is on. */ |
d3372da9 |
2367 | {CONF_VAR_BOOL, "onlylocal", {.num = 1}}, |
ddb933f1 |
2368 | /** The TCP port number on which to accept UI client connections, |
2369 | * or -1 to not listen on TCP. */ |
d3372da9 |
2370 | {CONF_VAR_INT, "port", {.num = 1500}}, |
ddb933f1 |
2371 | /** |
2372 | * Controls the the name to use for the Unix socket on which to |
2373 | * accept UI client connections. If the name contains a slash, it |
2374 | * is treated as a file name to bind on. If the name is "default", |
2375 | * the file name will be "/var/run/doldacond.sock" if doldacond |
2376 | * runs with UID == 0, or "/tmp/doldacond-NAME" otherwise, where |
2377 | * NAME is the user name of the UID which doldacond runs as. If |
2378 | * the name is "none", no Unix socket will be used. Otherwise, an |
2379 | * error is signaled. |
2380 | */ |
2381 | {CONF_VAR_STRING, "unixsock", {.str = L"default"}}, |
d9f89ef5 |
2382 | /** The TOS value to use for UI connections (see the TOS VALUES |
2383 | * section). */ |
d3372da9 |
2384 | {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}}, |
d9f89ef5 |
2385 | /** The name of the filtercmd script (see the FILES section for |
2386 | * lookup information). */ |
d3372da9 |
2387 | {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}}, |
2388 | {CONF_VAR_END} |
2389 | }; |
2390 | |
2391 | static struct configcmd mycmds[] = |
2392 | { |
2393 | {"user", conf_user}, |
2394 | {NULL} |
2395 | }; |
2396 | |
2397 | static struct module me = |
2398 | { |
2399 | .name = "ui", |
2400 | .conf = |
2401 | { |
2402 | .vars = myvars, |
2403 | .cmds = mycmds |
2404 | }, |
2405 | .preinit = preinit, |
2406 | .init = init, |
2407 | .run = run, |
2408 | .terminate = terminate |
2409 | }; |
2410 | |
2411 | MODULE(me) |