/* HIDP implementation for Linux Bluetooth stack (BlueZ). Copyright (C) 2003-2004 Marcel Holtmann This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL 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. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS SOFTWARE IS DISCLAIMED. */ #ifndef __HIDP_H #define __HIDP_H #include #include /* HIDP header masks */ #define HIDP_HEADER_TRANS_MASK 0xf0 #define HIDP_HEADER_PARAM_MASK 0x0f /* HIDP transaction types */ #define HIDP_TRANS_HANDSHAKE 0x00 #define HIDP_TRANS_HID_CONTROL 0x10 #define HIDP_TRANS_GET_REPORT 0x40 #define HIDP_TRANS_SET_REPORT 0x50 #define HIDP_TRANS_GET_PROTOCOL 0x60 #define HIDP_TRANS_SET_PROTOCOL 0x70 #define HIDP_TRANS_GET_IDLE 0x80 #define HIDP_TRANS_SET_IDLE 0x90 #define HIDP_TRANS_DATA 0xa0 #define HIDP_TRANS_DATC 0xb0 /* HIDP handshake results */ #define HIDP_HSHK_SUCCESSFUL 0x00 #define HIDP_HSHK_NOT_READY 0x01 #define HIDP_HSHK_ERR_INVALID_REPORT_ID 0x02 #define HIDP_HSHK_ERR_UNSUPPORTED_REQUEST 0x03 #define HIDP_HSHK_ERR_INVALID_PARAMETER 0x04 #define HIDP_HSHK_ERR_UNKNOWN 0x0e #define HIDP_HSHK_ERR_FATAL 0x0f /* HIDP control operation parameters */ #define HIDP_CTRL_NOP 0x00 #define HIDP_CTRL_HARD_RESET 0x01 #define HIDP_CTRL_SOFT_RESET 0x02 #define HIDP_CTRL_SUSPEND 0x03 #define HIDP_CTRL_EXIT_SUSPEND 0x04 #define HIDP_CTRL_VIRTUAL_CABLE_UNPLUG 0x05 /* HIDP data transaction headers */ #define HIDP_DATA_RTYPE_MASK 0x03 #define HIDP_DATA_RSRVD_MASK 0x0c #define HIDP_DATA_RTYPE_OTHER 0x00 #define HIDP_DATA_RTYPE_INPUT 0x01 #define HIDP_DATA_RTYPE_OUPUT 0x02 #define HIDP_DATA_RTYPE_FEATURE 0x03 /* HIDP protocol header parameters */ #define HIDP_PROTO_BOOT 0x00 #define HIDP_PROTO_REPORT 0x01 /* HIDP ioctl defines */ #define HIDPCONNADD _IOW('H', 200, int) #define HIDPCONNDEL _IOW('H', 201, int) #define HIDPGETCONNLIST _IOR('H', 210, int) #define HIDPGETCONNINFO _IOR('H', 211, int) #define HIDP_VIRTUAL_CABLE_UNPLUG 0 #define HIDP_BOOT_PROTOCOL_MODE 1 #define HIDP_BLUETOOTH_VENDOR_ID 9 struct hidp_connadd_req { int ctrl_sock; // Connected control socket int intr_sock; // Connteted interrupt socket __u16 parser; __u16 rd_size; __u8 __user *rd_data; __u8 country; __u8 subclass; __u16 vendor; __u16 product; __u16 version; __u32 flags; __u32 idle_to; char name[128]; }; struct hidp_conndel_req { bdaddr_t bdaddr; __u32 flags; }; struct hidp_conninfo { bdaddr_t bdaddr; __u32 flags; __u16 state; __u16 vendor; __u16 product; __u16 version; char name[128]; }; struct hidp_connlist_req { __u32 cnum; struct hidp_conninfo __user *ci; }; int hidp_add_connection(struct hidp_connadd_req *req, struct socket *ctrl_sock, struct socket *intr_sock); int hidp_del_connection(struct hidp_conndel_req *req); int hidp_get_connlist(struct hidp_connlist_req *req); int hidp_get_conninfo(struct hidp_conninfo *ci); /* HIDP session defines */ struct hidp_session { struct list_head list; struct hci_conn *conn; struct socket *ctrl_sock; struct socket *intr_sock; bdaddr_t bdaddr; unsigned long state; unsigned long flags; unsigned long idle_to; uint ctrl_mtu; uint intr_mtu; atomic_t terminate; unsigned char keys[8]; unsigned char leds; struct input_dev *input; struct hid_device *hid; struct timer_list timer; struct sk_buff_head ctrl_transmit; struct sk_buff_head intr_transmit; struct hidp_connadd_req *req; }; static inline void hidp_schedule(struct hidp_session *session) { struct sock *ctrl_sk = session->ctrl_sock->sk; struct sock *intr_sk = session->intr_sock->sk; wake_up_interruptible(ctrl_sk->sk_sleep); wake_up_interruptible(intr_sk->sk_sleep); } /* HIDP init defines */ extern int __init hidp_init_sockets(void); extern void __exit hidp_cleanup_sockets(void); #endif /* __HIDP_H */ >
blob: 3c64fe044a2902622ae46b56687da920e8872c33 (plain)
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
/*
    SSSD

    Authors:
        Stephen Gallagher <sgallagh@redhat.com>

    Copyright (C) 2011 Red Hat

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <check.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <talloc.h>

#include "tests/common_check.h"
#include "providers/ipa/ipa_hbac.h"

#define HBAC_TEST_USER "testuser"
#define HBAC_TEST_INVALID_USER "nosuchuser"

#define HBAC_TEST_GROUP1 "testgroup1"
#define HBAC_TEST_GROUP2 "testgroup2"
#define HBAC_TEST_INVALID_GROUP "nosuchgroup"

#define HBAC_TEST_SERVICE "testservice"
#define HBAC_TEST_INVALID_SERVICE "nosuchservice"

#define HBAC_TEST_SERVICEGROUP1 "login_services"
#define HBAC_TEST_SERVICEGROUP2 "all_services"
#define HBAC_TEST_INVALID_SERVICEGROUP "nosuchservicegroup"

#define HBAC_TEST_SRCHOST "client.example.com"
#define HBAC_TEST_INVALID_SRCHOST "nosuchsrchost"

#define HBAC_TEST_SRCHOSTGROUP1 "site_hosts"
#define HBAC_TEST_SRCHOSTGROUP2 "corp_hosts"
#define HBAC_TEST_INVALID_SRCHOSTGROUP "nosuchsrchostgroup"


/* These don't make sense for a user/group/service but they do the job and
 * every one is from a different codepage */
/* Latin Extended A - "Czech" */
const uint8_t user_utf8_lowcase[] = { 0xC4, 0x8D, 'e', 'c', 'h', 0x0 };
const uint8_t user_utf8_upcase[] = { 0xC4, 0x8C, 'e', 'c', 'h', 0x0 };
const uint8_t user_utf8_lowcase_neg[] = { 0xC4, 0x8E, 'e', 'c', 'h', 0x0 };
/* Latin 1 Supplement - "Munchen" */
const uint8_t service_utf8_lowcase[] = { 'm', 0xC3, 0xBC, 'n', 'c', 'h', 'e', 'n', 0x0 };
const uint8_t service_utf8_upcase[] = { 'M', 0xC3, 0x9C, 'N', 'C', 'H', 'E', 'N', 0x0 };
/* Greek - "AlphaBetaGamma" */
const uint8_t srchost_utf8_lowcase[] = { 0xCE, 0xB1, 0xCE, 0xB2, 0xCE, 0xB3, 0x0  };
const uint8_t srchost_utf8_upcase[] = { 0xCE, 0x91, 0xCE, 0x92, 0xCE, 0x93, 0x0 };
/* Turkish "capital I" and "dotless i" */
const uint8_t user_lowcase_tr[] = { 0xC4, 0xB1, 0x0 };
const uint8_t user_upcase_tr[] = { 0x49, 0x0 };

static void get_allow_all_rule(TALLOC_CTX *mem_ctx,
                               struct hbac_rule **allow_rule)
{
    struct hbac_rule *rule;
    /* Create a rule that ALLOWs all services, users and
     * remote hosts.
     */
    rule = talloc_zero(mem_ctx, struct hbac_rule);
    fail_if (rule == NULL);

    rule->enabled = true;

    rule->services = talloc_zero(rule, struct hbac_rule_element);
    fail_if (rule->services == NULL);
    rule->services->category = HBAC_CATEGORY_ALL;
    rule->services->names = NULL;
    rule->services->groups = NULL;

    rule->users = talloc_zero(rule, struct hbac_rule_element);
    fail_if (rule->users == NULL);
    rule->users->category = HBAC_CATEGORY_ALL;
    rule->users->names = NULL;
    rule->users->groups = NULL;

    rule->targethosts = talloc_zero(rule, struct hbac_rule_element);
    fail_if (rule->targethosts == NULL);
    rule->targethosts->category = HBAC_CATEGORY_ALL;
    rule->targethosts->names = NULL;
    rule->targethosts->groups = NULL;

    rule->srchosts = talloc_zero(rule, struct hbac_rule_element);
    fail_if (rule->srchosts == NULL);
    rule->srchosts->category = HBAC_CATEGORY_ALL;
    rule->srchosts->names = NULL;
    rule->srchosts->groups = NULL;

    *allow_rule = rule;
}

static void get_test_user(TALLOC_CTX *mem_ctx,
                          struct hbac_request_element **user)
{
    struct hbac_request_element *new_user;

    new_user = talloc_zero(mem_ctx, struct hbac_request_element);
    fail_if (new_user == NULL);

    new_user->name = talloc_strdup(new_user, HBAC_TEST_USER);
    fail_if(new_user->name == NULL);

    new_user->groups = talloc_array(new_user, const char *, 3);
    fail_if(new_user->groups == NULL);

    new_user->groups[0] = talloc_strdup(new_user->groups, HBAC_TEST_GROUP1);
    fail_if(new_user->groups[0] == NULL);

    new_user->groups[1] = talloc_strdup(new_user->groups, HBAC_TEST_GROUP2);
    fail_if(new_user->groups[1] == NULL);

    new_user->groups[2] = NULL;

    *user = new_user;
}

static void get_test_service(TALLOC_CTX *mem_ctx,
                             struct hbac_request_element **service)
{
    struct hbac_request_element *new_service;

    new_service = talloc_zero(mem_ctx, struct hbac_request_element);
    fail_if (new_service == NULL);

    new_service->name = talloc_strdup(new_service, HBAC_TEST_SERVICE);
    fail_if(new_service->name == NULL);

    new_service->groups = talloc_array(new_service, const char *, 3);
    fail_if(new_service->groups == NULL);

    new_service->groups[0] = talloc_strdup(new_service->groups, HBAC_TEST_SERVICEGROUP1);
    fail_if(new_service->groups[0] == NULL);

    new_service->groups[1] = talloc_strdup(new_service->groups, HBAC_TEST_SERVICEGROUP2);
    fail_if(new_service->groups[1] == NULL);

    new_service->groups[2] = NULL;

    *service = new_service;
}

static void get_test_srchost(TALLOC_CTX *mem_ctx,
                             struct hbac_request_element **srchost)
{
    struct hbac_request_element *new_srchost;

    new_srchost = talloc_zero(mem_ctx, struct hbac_request_element);
    fail_if (new_srchost == NULL);

    new_srchost->name = talloc_strdup(new_srchost, "client.example.com");
    fail_if(new_srchost->name == NULL);

    new_srchost->groups = talloc_array(new_srchost, const char *, 3);
    fail_if(new_srchost->groups == NULL);

    new_srchost->groups[0] = talloc_strdup(new_srchost->groups, "site_hosts");
    fail_if(new_srchost->groups[0] == NULL);

    new_srchost->groups[1] = talloc_strdup(new_srchost->groups, "corp_hosts");
    fail_if(new_srchost->groups[1] == NULL);

    new_srchost->groups[2] = NULL;

    *srchost = new_srchost;
}

START_TEST(ipa_hbac_test_allow_all)
{
    enum hbac_eval_result result;
    TALLOC_CTX *test_ctx;
    struct hbac_rule **rules;
    struct hbac_eval_req *eval_req;
    struct hbac_info *info;