-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.c
1397 lines (1089 loc) · 31.3 KB
/
session.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* rpcd - UBUS RPC server
*
* Copyright (C) 2013 Felix Fietkau <[email protected]>
* Copyright (C) 2013-2014 Jo-Philipp Wich <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#define _GNU_SOURCE /* crypt() */
#include <libubox/avl-cmp.h>
#include <libubox/blobmsg.h>
#include <libubox/utils.h>
#include <libubus.h>
#include <fnmatch.h>
#include <glob.h>
#include <uci.h>
#include <limits.h>
#ifdef HAVE_SHADOW
#include <shadow.h>
#endif
#include <rpcd/session.h>
static struct avl_tree sessions;
static struct blob_buf buf;
static LIST_HEAD(create_callbacks);
static LIST_HEAD(destroy_callbacks);
enum {
RPC_SN_TIMEOUT,
__RPC_SN_MAX,
};
static const struct blobmsg_policy new_policy[__RPC_SN_MAX] = {
[RPC_SN_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
};
enum {
RPC_SI_SID,
__RPC_SI_MAX,
};
static const struct blobmsg_policy sid_policy[__RPC_SI_MAX] = {
[RPC_SI_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
};
enum {
RPC_SS_SID,
RPC_SS_VALUES,
__RPC_SS_MAX,
};
static const struct blobmsg_policy set_policy[__RPC_SS_MAX] = {
[RPC_SS_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
[RPC_SS_VALUES] = { .name = "values", .type = BLOBMSG_TYPE_TABLE },
};
enum {
RPC_SG_SID,
RPC_SG_KEYS,
__RPC_SG_MAX,
};
static const struct blobmsg_policy get_policy[__RPC_SG_MAX] = {
[RPC_SG_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
[RPC_SG_KEYS] = { .name = "keys", .type = BLOBMSG_TYPE_ARRAY },
};
enum {
RPC_SA_SID,
RPC_SA_SCOPE,
RPC_SA_OBJECTS,
__RPC_SA_MAX,
};
static const struct blobmsg_policy acl_policy[__RPC_SA_MAX] = {
[RPC_SA_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
[RPC_SA_SCOPE] = { .name = "scope", .type = BLOBMSG_TYPE_STRING },
[RPC_SA_OBJECTS] = { .name = "objects", .type = BLOBMSG_TYPE_ARRAY },
};
enum {
RPC_SP_SID,
RPC_SP_SCOPE,
RPC_SP_OBJECT,
RPC_SP_FUNCTION,
__RPC_SP_MAX,
};
static const struct blobmsg_policy perm_policy[__RPC_SP_MAX] = {
[RPC_SP_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
[RPC_SP_SCOPE] = { .name = "scope", .type = BLOBMSG_TYPE_STRING },
[RPC_SP_OBJECT] = { .name = "object", .type = BLOBMSG_TYPE_STRING },
[RPC_SP_FUNCTION] = { .name = "function", .type = BLOBMSG_TYPE_STRING },
};
enum {
RPC_DUMP_SID,
RPC_DUMP_TIMEOUT,
RPC_DUMP_EXPIRES,
RPC_DUMP_DATA,
__RPC_DUMP_MAX,
};
static const struct blobmsg_policy dump_policy[__RPC_DUMP_MAX] = {
[RPC_DUMP_SID] = { .name = "ubus_rpc_session", .type = BLOBMSG_TYPE_STRING },
[RPC_DUMP_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
[RPC_DUMP_EXPIRES] = { .name = "expires", .type = BLOBMSG_TYPE_INT32 },
[RPC_DUMP_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE },
};
enum {
RPC_L_USERNAME,
RPC_L_PASSWORD,
RPC_L_TIMEOUT,
__RPC_L_MAX,
};
static const struct blobmsg_policy login_policy[__RPC_L_MAX] = {
[RPC_L_USERNAME] = { .name = "username", .type = BLOBMSG_TYPE_STRING },
[RPC_L_PASSWORD] = { .name = "password", .type = BLOBMSG_TYPE_STRING },
[RPC_L_TIMEOUT] = { .name = "timeout", .type = BLOBMSG_TYPE_INT32 },
};
/*
* Keys in the AVL tree contain all pattern characters up to the first wildcard.
* To look up entries, start with the last entry that has a key less than or
* equal to the method name, then work backwards as long as the AVL key still
* matches its counterpart in the object name
*/
#define uh_foreach_matching_acl_prefix(_acl, _avl, _obj, _func) \
for (_acl = avl_find_le_element(_avl, _obj, _acl, avl); \
_acl; \
_acl = avl_is_first(_avl, &(_acl)->avl) ? NULL : \
avl_prev_element((_acl), avl))
#define uh_foreach_matching_acl(_acl, _avl, _obj, _func) \
uh_foreach_matching_acl_prefix(_acl, _avl, _obj, _func) \
if (!strncmp((_acl)->object, _obj, (_acl)->sort_len) && \
!fnmatch((_acl)->object, (_obj), FNM_NOESCAPE) && \
!fnmatch((_acl)->function, (_func), FNM_NOESCAPE))
static int
rpc_random(char *dest)
{
unsigned char buf[16] = { 0 };
FILE *f;
int i;
int ret;
f = fopen("/dev/urandom", "r");
if (!f)
return -1;
ret = fread(buf, 1, sizeof(buf), f);
fclose(f);
if (ret < 0)
return ret;
for (i = 0; i < sizeof(buf); i++)
sprintf(dest + (i<<1), "%02x", buf[i]);
return 0;
}
static void
rpc_session_dump_data(struct rpc_session *ses, struct blob_buf *b)
{
struct rpc_session_data *d;
avl_for_each_element(&ses->data, d, avl) {
blobmsg_add_field(b, blobmsg_type(d->attr), blobmsg_name(d->attr),
blobmsg_data(d->attr), blobmsg_data_len(d->attr));
}
}
static void
rpc_session_dump_acls(struct rpc_session *ses, struct blob_buf *b)
{
struct rpc_session_acl *acl;
struct rpc_session_acl_scope *acl_scope;
const char *lastobj = NULL;
const char *lastscope = NULL;
void *c = NULL, *d = NULL;
avl_for_each_element(&ses->acls, acl_scope, avl) {
if (!lastscope || strcmp(acl_scope->avl.key, lastscope))
{
if (c) blobmsg_close_table(b, c);
c = blobmsg_open_table(b, acl_scope->avl.key);
lastobj = NULL;
}
d = NULL;
avl_for_each_element(&acl_scope->acls, acl, avl) {
if (!lastobj || strcmp(acl->object, lastobj))
{
if (d) blobmsg_close_array(b, d);
d = blobmsg_open_array(b, acl->object);
}
blobmsg_add_string(b, NULL, acl->function);
lastobj = acl->object;
}
if (d) blobmsg_close_array(b, d);
}
if (c) blobmsg_close_table(b, c);
}
static void
rpc_session_to_blob(struct rpc_session *ses, bool acls)
{
void *c;
blob_buf_init(&buf, 0);
blobmsg_add_string(&buf, "ubus_rpc_session", ses->id);
blobmsg_add_u32(&buf, "timeout", ses->timeout);
blobmsg_add_u32(&buf, "expires", uloop_timeout_remaining(&ses->t) / 1000);
if (acls) {
c = blobmsg_open_table(&buf, "acls");
rpc_session_dump_acls(ses, &buf);
blobmsg_close_table(&buf, c);
}
c = blobmsg_open_table(&buf, "data");
rpc_session_dump_data(ses, &buf);
blobmsg_close_table(&buf, c);
}
static void
rpc_session_dump(struct rpc_session *ses, struct ubus_context *ctx,
struct ubus_request_data *req)
{
rpc_session_to_blob(ses, true);
ubus_send_reply(ctx, req, buf.head);
}
static void
rpc_touch_session(struct rpc_session *ses)
{
if (ses->timeout > 0)
uloop_timeout_set(&ses->t, ses->timeout * 1000);
}
static void
rpc_session_destroy(struct rpc_session *ses)
{
struct rpc_session_acl *acl, *nacl;
struct rpc_session_acl_scope *acl_scope, *nacl_scope;
struct rpc_session_data *data, *ndata;
struct rpc_session_cb *cb;
list_for_each_entry(cb, &destroy_callbacks, list)
cb->cb(ses, cb->priv);
uloop_timeout_cancel(&ses->t);
avl_for_each_element_safe(&ses->acls, acl_scope, avl, nacl_scope) {
avl_remove_all_elements(&acl_scope->acls, acl, avl, nacl)
free(acl);
avl_delete(&ses->acls, &acl_scope->avl);
free(acl_scope);
}
avl_remove_all_elements(&ses->data, data, avl, ndata)
free(data);
avl_delete(&sessions, &ses->avl);
free(ses);
}
static void rpc_session_timeout(struct uloop_timeout *t)
{
struct rpc_session *ses;
ses = container_of(t, struct rpc_session, t);
rpc_session_destroy(ses);
}
static struct rpc_session *
rpc_session_new(void)
{
struct rpc_session *ses;
ses = calloc(1, sizeof(*ses));
if (!ses)
return NULL;
ses->avl.key = ses->id;
avl_init(&ses->acls, avl_strcmp, true, NULL);
avl_init(&ses->data, avl_strcmp, false, NULL);
ses->t.cb = rpc_session_timeout;
return ses;
}
static struct rpc_session *
rpc_session_create(int timeout)
{
struct rpc_session *ses;
struct rpc_session_cb *cb;
ses = rpc_session_new();
if (!ses)
return NULL;
if (rpc_random(ses->id))
return NULL;
ses->timeout = timeout;
avl_insert(&sessions, &ses->avl);
rpc_touch_session(ses);
list_for_each_entry(cb, &create_callbacks, list)
cb->cb(ses, cb->priv);
return ses;
}
static struct rpc_session *
rpc_session_get(const char *id)
{
struct rpc_session *ses;
ses = avl_find_element(&sessions, id, ses, avl);
if (!ses)
return NULL;
rpc_touch_session(ses);
return ses;
}
static int
rpc_handle_create(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct rpc_session *ses;
struct blob_attr *tb;
int timeout = RPC_DEFAULT_SESSION_TIMEOUT;
blobmsg_parse(new_policy, __RPC_SN_MAX, &tb, blob_data(msg), blob_len(msg));
if (tb)
timeout = blobmsg_get_u32(tb);
ses = rpc_session_create(timeout);
if (ses)
rpc_session_dump(ses, ctx, req);
return 0;
}
static int
rpc_handle_list(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct rpc_session *ses;
struct blob_attr *tb;
blobmsg_parse(sid_policy, __RPC_SI_MAX, &tb, blob_data(msg), blob_len(msg));
if (!tb) {
avl_for_each_element(&sessions, ses, avl)
rpc_session_dump(ses, ctx, req);
return 0;
}
ses = rpc_session_get(blobmsg_data(tb));
if (!ses)
return UBUS_STATUS_NOT_FOUND;
rpc_session_dump(ses, ctx, req);
return 0;
}
static int
uh_id_len(const char *str)
{
return strcspn(str, "*?[");
}
static int
rpc_session_grant(struct rpc_session *ses,
const char *scope, const char *object, const char *function)
{
struct rpc_session_acl *acl;
struct rpc_session_acl_scope *acl_scope;
char *new_scope, *new_obj, *new_func, *new_id;
int id_len;
if (!object || !function)
return UBUS_STATUS_INVALID_ARGUMENT;
acl_scope = avl_find_element(&ses->acls, scope, acl_scope, avl);
if (acl_scope) {
uh_foreach_matching_acl_prefix(acl, &acl_scope->acls, object, function) {
if (!strcmp(acl->object, object) &&
!strcmp(acl->function, function))
return 0;
}
}
if (!acl_scope) {
acl_scope = calloc_a(sizeof(*acl_scope),
&new_scope, strlen(scope) + 1);
if (!acl_scope)
return UBUS_STATUS_UNKNOWN_ERROR;
acl_scope->avl.key = strcpy(new_scope, scope);
avl_init(&acl_scope->acls, avl_strcmp, true, NULL);
avl_insert(&ses->acls, &acl_scope->avl);
}
id_len = uh_id_len(object);
acl = calloc_a(sizeof(*acl),
&new_obj, strlen(object) + 1,
&new_func, strlen(function) + 1,
&new_id, id_len + 1);
if (!acl)
return UBUS_STATUS_UNKNOWN_ERROR;
acl->object = strcpy(new_obj, object);
acl->function = strcpy(new_func, function);
acl->avl.key = strncpy(new_id, object, id_len);
avl_insert(&acl_scope->acls, &acl->avl);
return 0;
}
static int
rpc_session_revoke(struct rpc_session *ses,
const char *scope, const char *object, const char *function)
{
struct rpc_session_acl *acl, *next;
struct rpc_session_acl_scope *acl_scope;
int id_len;
char *id;
acl_scope = avl_find_element(&ses->acls, scope, acl_scope, avl);
if (!acl_scope)
return 0;
if (!object && !function) {
avl_remove_all_elements(&acl_scope->acls, acl, avl, next)
free(acl);
avl_delete(&ses->acls, &acl_scope->avl);
free(acl_scope);
return 0;
}
id_len = uh_id_len(object);
id = alloca(id_len + 1);
strncpy(id, object, id_len);
id[id_len] = 0;
acl = avl_find_element(&acl_scope->acls, id, acl, avl);
while (acl) {
if (!avl_is_last(&acl_scope->acls, &acl->avl))
next = avl_next_element(acl, avl);
else
next = NULL;
if (strcmp(id, acl->avl.key) != 0)
break;
if (!strcmp(acl->object, object) &&
!strcmp(acl->function, function)) {
avl_delete(&acl_scope->acls, &acl->avl);
free(acl);
}
acl = next;
}
if (avl_is_empty(&acl_scope->acls)) {
avl_delete(&ses->acls, &acl_scope->avl);
free(acl_scope);
}
return 0;
}
static int
rpc_handle_acl(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct rpc_session *ses;
struct blob_attr *tb[__RPC_SA_MAX];
struct blob_attr *attr, *sattr;
const char *object, *function;
const char *scope = "ubus";
int rem1, rem2;
int (*cb)(struct rpc_session *ses,
const char *scope, const char *object, const char *function);
blobmsg_parse(acl_policy, __RPC_SA_MAX, tb, blob_data(msg), blob_len(msg));
if (!tb[RPC_SA_SID])
return UBUS_STATUS_INVALID_ARGUMENT;
ses = rpc_session_get(blobmsg_data(tb[RPC_SA_SID]));
if (!ses)
return UBUS_STATUS_NOT_FOUND;
if (tb[RPC_SA_SCOPE])
scope = blobmsg_data(tb[RPC_SA_SCOPE]);
if (!strcmp(method, "grant"))
cb = rpc_session_grant;
else
cb = rpc_session_revoke;
if (!tb[RPC_SA_OBJECTS])
return cb(ses, scope, NULL, NULL);
blobmsg_for_each_attr(attr, tb[RPC_SA_OBJECTS], rem1) {
if (blobmsg_type(attr) != BLOBMSG_TYPE_ARRAY)
continue;
object = NULL;
function = NULL;
blobmsg_for_each_attr(sattr, attr, rem2) {
if (blobmsg_type(sattr) != BLOBMSG_TYPE_STRING)
continue;
if (!object)
object = blobmsg_data(sattr);
else if (!function)
function = blobmsg_data(sattr);
else
break;
}
if (object && function)
cb(ses, scope, object, function);
}
return 0;
}
static bool
rpc_session_acl_allowed(struct rpc_session *ses, const char *scope,
const char *obj, const char *fun)
{
struct rpc_session_acl *acl;
struct rpc_session_acl_scope *acl_scope;
acl_scope = avl_find_element(&ses->acls, scope, acl_scope, avl);
if (acl_scope) {
uh_foreach_matching_acl(acl, &acl_scope->acls, obj, fun)
return true;
}
return false;
}
static int
rpc_handle_access(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct rpc_session *ses;
struct blob_attr *tb[__RPC_SP_MAX];
const char *scope = "ubus";
bool allow;
blobmsg_parse(perm_policy, __RPC_SP_MAX, tb, blob_data(msg), blob_len(msg));
if (!tb[RPC_SP_SID])
return UBUS_STATUS_INVALID_ARGUMENT;
ses = rpc_session_get(blobmsg_data(tb[RPC_SP_SID]));
if (!ses)
return UBUS_STATUS_NOT_FOUND;
blob_buf_init(&buf, 0);
if (tb[RPC_SP_OBJECT] && tb[RPC_SP_FUNCTION])
{
if (tb[RPC_SP_SCOPE])
scope = blobmsg_data(tb[RPC_SP_SCOPE]);
allow = rpc_session_acl_allowed(ses, scope,
blobmsg_data(tb[RPC_SP_OBJECT]),
blobmsg_data(tb[RPC_SP_FUNCTION]));
blobmsg_add_u8(&buf, "access", allow);
}
else
{
rpc_session_dump_acls(ses, &buf);
}
ubus_send_reply(ctx, req, buf.head);
return 0;
}
static void
rpc_session_set(struct rpc_session *ses, const char *key, struct blob_attr *val)
{
struct rpc_session_data *data;
data = avl_find_element(&ses->data, key, data, avl);
if (data) {
avl_delete(&ses->data, &data->avl);
free(data);
}
data = calloc(1, sizeof(*data) + blob_pad_len(val));
if (!data)
return;
memcpy(data->attr, val, blob_pad_len(val));
data->avl.key = blobmsg_name(data->attr);
avl_insert(&ses->data, &data->avl);
}
static int
rpc_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct rpc_session *ses;
struct blob_attr *tb[__RPC_SS_MAX];
struct blob_attr *attr;
int rem;
blobmsg_parse(set_policy, __RPC_SS_MAX, tb, blob_data(msg), blob_len(msg));
if (!tb[RPC_SS_SID] || !tb[RPC_SS_VALUES])
return UBUS_STATUS_INVALID_ARGUMENT;
ses = rpc_session_get(blobmsg_data(tb[RPC_SS_SID]));
if (!ses)
return UBUS_STATUS_NOT_FOUND;
blobmsg_for_each_attr(attr, tb[RPC_SS_VALUES], rem) {
if (!blobmsg_name(attr)[0])
continue;
rpc_session_set(ses, blobmsg_name(attr), attr);
}
return 0;
}
static int
rpc_handle_get(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct rpc_session *ses;
struct rpc_session_data *data;
struct blob_attr *tb[__RPC_SG_MAX];
struct blob_attr *attr;
void *c;
int rem;
blobmsg_parse(get_policy, __RPC_SG_MAX, tb, blob_data(msg), blob_len(msg));
if (!tb[RPC_SG_SID])
return UBUS_STATUS_INVALID_ARGUMENT;
ses = rpc_session_get(blobmsg_data(tb[RPC_SG_SID]));
if (!ses)
return UBUS_STATUS_NOT_FOUND;
blob_buf_init(&buf, 0);
c = blobmsg_open_table(&buf, "values");
if (tb[RPC_SG_KEYS])
blobmsg_for_each_attr(attr, tb[RPC_SG_KEYS], rem) {
if (blobmsg_type(attr) != BLOBMSG_TYPE_STRING)
continue;
data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
if (!data)
continue;
blobmsg_add_field(&buf, blobmsg_type(data->attr),
blobmsg_name(data->attr),
blobmsg_data(data->attr),
blobmsg_data_len(data->attr));
}
else
rpc_session_dump_data(ses, &buf);
blobmsg_close_table(&buf, c);
ubus_send_reply(ctx, req, buf.head);
return 0;
}
static int
rpc_handle_unset(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct rpc_session *ses;
struct rpc_session_data *data, *ndata;
struct blob_attr *tb[__RPC_SA_MAX];
struct blob_attr *attr;
int rem;
blobmsg_parse(get_policy, __RPC_SG_MAX, tb, blob_data(msg), blob_len(msg));
if (!tb[RPC_SG_SID])
return UBUS_STATUS_INVALID_ARGUMENT;
ses = rpc_session_get(blobmsg_data(tb[RPC_SG_SID]));
if (!ses)
return UBUS_STATUS_NOT_FOUND;
if (!tb[RPC_SG_KEYS]) {
avl_remove_all_elements(&ses->data, data, avl, ndata)
free(data);
return 0;
}
blobmsg_for_each_attr(attr, tb[RPC_SG_KEYS], rem) {
if (blobmsg_type(attr) != BLOBMSG_TYPE_STRING)
continue;
data = avl_find_element(&ses->data, blobmsg_data(attr), data, avl);
if (!data)
continue;
avl_delete(&ses->data, &data->avl);
free(data);
}
return 0;
}
static int
rpc_handle_destroy(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct rpc_session *ses;
struct blob_attr *tb;
blobmsg_parse(sid_policy, __RPC_SI_MAX, &tb, blob_data(msg), blob_len(msg));
if (!tb)
return UBUS_STATUS_INVALID_ARGUMENT;
if (!strcmp(blobmsg_get_string(tb), RPC_DEFAULT_SESSION_ID))
return UBUS_STATUS_PERMISSION_DENIED;
ses = rpc_session_get(blobmsg_data(tb));
if (!ses)
return UBUS_STATUS_NOT_FOUND;
rpc_session_destroy(ses);
return 0;
}
static bool
rpc_login_test_password(const char *hash, const char *password)
{
char *crypt_hash;
/* password is not set */
if (!hash || !*hash || !strcmp(hash, "!") || !strcmp(hash, "x"))
{
return true;
}
/* password hash refers to shadow/passwd */
else if (!strncmp(hash, "$p$", 3))
{
#ifdef HAVE_SHADOW
struct spwd *sp = getspnam(hash + 3);
if (!sp)
return false;
return rpc_login_test_password(sp->sp_pwdp, password);
#else
struct passwd *pw = getpwnam(hash + 3);
if (!pw)
return false;
return rpc_login_test_password(pw->pw_passwd, password);
#endif
}
crypt_hash = crypt(password, hash);
return !strcmp(crypt_hash, hash);
}
static struct uci_section *
rpc_login_test_login(struct uci_context *uci,
const char *username, const char *password)
{
struct uci_package *p = NULL;
struct uci_section *s;
struct uci_element *e;
struct uci_ptr ptr = { .package = "rpcd" };
uci_load(uci, ptr.package, &p);
if (!p)
return false;
uci_foreach_element(&p->sections, e)
{
s = uci_to_section(e);
if (strcmp(s->type, "login"))
continue;
ptr.section = s->e.name;
ptr.s = NULL;
/* test for matching username */
ptr.option = "username";
ptr.o = NULL;
if (uci_lookup_ptr(uci, &ptr, NULL, true))
continue;
if (ptr.o->type != UCI_TYPE_STRING)
continue;
if (strcmp(ptr.o->v.string, username))
continue;
/* If password is NULL, we're restoring ACLs for an existing session,
* in this case do not check the password again. */
if (!password)
return ptr.s;
/* test for matching password */
ptr.option = "password";
ptr.o = NULL;
if (uci_lookup_ptr(uci, &ptr, NULL, true))
continue;
if (ptr.o->type != UCI_TYPE_STRING)
continue;
if (rpc_login_test_password(ptr.o->v.string, password))
return ptr.s;
}
return NULL;
}
static bool
rpc_login_test_permission(struct uci_section *s,
const char *perm, const char *group)
{
const char *p;
struct uci_option *o;
struct uci_element *e, *l;
/* If the login section is not provided, we're setting up acls for the
* default session, in this case uncondionally allow access to the
* "unauthenticated" access group */
if (!s) {
return !strcmp(group, "unauthenticated");
}
uci_foreach_element(&s->options, e)
{
o = uci_to_option(e);
if (o->type != UCI_TYPE_LIST)
continue;
if (strcmp(o->e.name, perm))
continue;
/* Match negative expressions first. If a negative expression matches
* the current group name then deny access. */
uci_foreach_element(&o->v.list, l) {
p = l->name;
if (!p || *p != '!')
continue;
while (isspace(*++p));
if (!*p)
continue;
if (!fnmatch(p, group, 0))
return false;
}
uci_foreach_element(&o->v.list, l) {
if (!l->name || !*l->name || *l->name == '!')
continue;
if (!fnmatch(l->name, group, 0))
return true;
}
}
/* make sure that write permission implies read permission */
if (!strcmp(perm, "read"))
return rpc_login_test_permission(s, "write", group);
return false;
}
static void
rpc_login_setup_acl_scope(struct rpc_session *ses,
struct blob_attr *acl_perm,
struct blob_attr *acl_scope)
{
struct blob_attr *acl_obj, *acl_func;
int rem, rem2;
/*
* Parse ACL scopes in table notation.
*
* "<scope>": {
* "<object>": [
* "<function>",
* "<function>",
* ...
* ]
* }
*/
if (blobmsg_type(acl_scope) == BLOBMSG_TYPE_TABLE) {
blobmsg_for_each_attr(acl_obj, acl_scope, rem) {
if (blobmsg_type(acl_obj) != BLOBMSG_TYPE_ARRAY)
continue;
blobmsg_for_each_attr(acl_func, acl_obj, rem2) {
if (blobmsg_type(acl_func) != BLOBMSG_TYPE_STRING)
continue;
rpc_session_grant(ses, blobmsg_name(acl_scope),
blobmsg_name(acl_obj),
blobmsg_data(acl_func));
}
}
}
/*
* Parse ACL scopes in array notation. The permission ("read" or "write")
* will be used as function name for each object.
*
* "<scope>": [
* "<object>",
* "<object>",
* ...
* ]
*/
else if (blobmsg_type(acl_scope) == BLOBMSG_TYPE_ARRAY) {
blobmsg_for_each_attr(acl_obj, acl_scope, rem) {
if (blobmsg_type(acl_obj) != BLOBMSG_TYPE_STRING)
continue;
rpc_session_grant(ses, blobmsg_name(acl_scope),
blobmsg_data(acl_obj),
blobmsg_name(acl_perm));
}
}
}