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
|
/*
SSSD
NSS Responder Interface for ID-SID mappings
Authors:
Sumit Bose <sbose@redhat.com>
Copyright (C) 2013 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 <errno.h>
#include <nss.h>
#include "sss_client/sss_cli.h"
#include "sss_client/nss_mc.h"
#include "sss_client/idmap/sss_nss_idmap.h"
#include "sss_client/idmap/sss_nss_idmap_private.h"
#include "util/strtonum.h"
#define DATA_START (3 * sizeof(uint32_t))
#define LIST_START (2 * sizeof(uint32_t))
#define NO_TIMEOUT ((unsigned int) -1)
union input {
const char *str;
uint32_t id;
};
struct output {
enum sss_id_type type;
enum sss_id_type *types;
union {
char *str;
uint32_t id;
struct sss_nss_kv *kv_list;
char **names;
} d;
};
static int sss_nss_status_to_errno(enum nss_status nret) {
switch (nret) {
case NSS_STATUS_TRYAGAIN:
return EAGAIN;
case NSS_STATUS_SUCCESS:
return EOK;
case NSS_STATUS_UNAVAIL:
default:
return ENOENT;
}
return EINVAL;
}
void sss_nss_free_kv(struct sss_nss_kv *kv_list)
{
size_t c;
if (kv_list != NULL) {
for (c = 0; kv_list[c].key != NULL; c++) {
free(kv_list[c].key);
free(kv_list[c].value);
}
free(kv_list);
}
}
void sss_nss_free_list(char **l)
{
size_t c;
if (l != NULL) {
for (c = 0; l[c] != NULL; c++) {
free(l[c]);
}
free(l);
}
}
static int buf_to_name_type_list(uint8_t *buf, size_t buf_len, uint32_t num,
char ***names, enum sss_id_type **types)
{
int ret;
size_t c;
char **n = NULL;
enum sss_id_type *t = NULL;
size_t rp = 0;
n = calloc(num + 1, sizeof(char *));
if (n == NULL) {
ret = ENOMEM;
goto done;
}
t = calloc(num + 1, sizeof(enum sss_id_type));
if (t == NULL) {
ret = ENOMEM;
goto done;
}
for (c = 0; c < num; c++) {
SAFEALIGN_COPY_UINT32(&(t[c]), buf + rp, &rp);
n[c] = strdup((char *) buf + rp);
if (n[c] == NULL) {
ret = ENOMEM;
goto done;
}
rp += strlen(n[c]) + 1;
}
ret = EOK;
done:
if (ret != EOK) {
sss_nss_free_list(n);
free(t);
} else {
*names = n;
*types = t;
}
return ret;
}
static int buf_to_kv_list(uint8_t *buf, size_t buf_len,
struct sss_nss_kv **kv_list)
{
size_t c;
size_t count = 0;
struct sss_nss_kv *list;
uint8_t *p;
int ret;
for (c = 0; c < buf_len; c++) {
if (buf[c] == '\0') {
count++;
}
}
if ((count % 2) != 0) {
return EINVAL;
}
count /= 2;
list = calloc((count + 1), sizeof(struct sss_nss_kv));
if (list == NULL) {
return ENOMEM;
}
p = buf;
for (c = 0; c < count; c++) {
list[c].key = strdup((char *) p);
if (list[c].key == NULL) {
ret = ENOMEM;
goto done;
}
p = memchr(p, '\0', buf_len - (p - buf));
if (p == NULL) {
ret = EINVAL;
goto done;
}
p++;
list[c].value = strdup((char *) p);
if (list[c].value == NULL) {
ret = ENOMEM;
goto done;
}
p = memchr(p, '\0', buf_len - (p - buf));
if (p == NULL) {
ret = EINVAL;
goto done;
}
p++;
}
*kv_list = list;
ret = EOK;
done:
if (ret != EOK) {
sss_nss_free_kv(list);
}
return ret;
}
static errno_t sss_nss_mc_get(union input inp, enum sss_cli_command cmd,
struct output *out)
{
switch (cmd) {
case SSS_NSS_GETSIDBYID:
return sss_nss_mc_get_sid_by_id(inp.id, &out->d.str, &out->type);
case SSS_NSS_GETSIDBYUID:
return sss_nss_mc_get_sid_by_uid(inp.id, &out->d.str, &out->type);
case SSS_NSS_GETSIDBYGID:
return sss_nss_mc_get_sid_by_gid(inp.id, &out->d.str, &out->type);
case SSS_NSS_GETIDBYSID:
return sss_nss_mc_get_id_by_sid(inp.str, &out->d.id, &out->type);
default:
return ENOENT;
}
}
static int sss_nss_getyyybyxxx(union input inp, enum sss_cli_command cmd,
unsigned int timeout, struct output *out)
{
int ret;
size_t inp_len;
struct sss_cli_req_data rd;
uint8_t *repbuf = NULL;
size_t replen;
int errnop;
enum nss_status nret;
uint32_t num_results;
char *str = NULL;
size_t data_len;
uint32_t c;
struct sss_nss_kv *kv_list;
char **names;
enum sss_id_type *types;
int time_left = SSS_CLI_SOCKET_TIMEOUT;
ret = sss_nss_mc_get(inp, cmd, out);
if (ret == EOK) {
return 0;
}
switch (cmd) {
case SSS_NSS_GETSIDBYNAME:
case SSS_NSS_GETSIDBYUSERNAME:
case SSS_NSS_GETSIDBYGROUPNAME:
case SSS_NSS_GETNAMEBYSID:
case SSS_NSS_GETIDBYSID:
case SSS_NSS_GETORIGBYNAME:
case SSS_NSS_GETORIGBYUSERNAME:
case SSS_NSS_GETORIGBYGROUPNAME:
ret = sss_strnlen(inp.str, 2048, &inp_len);
if (ret != EOK) {
return EINVAL;
}
rd.len = inp_len + 1;
rd.data = inp.str;
break;
case SSS_NSS_GETNAMEBYCERT:
case SSS_NSS_GETLISTBYCERT:
ret = sss_strnlen(inp.str, 10 * 1024 , &inp_len);
if (ret != EOK) {
return EINVAL;
}
rd.len = inp_len + 1;
rd.data = inp.str;
break;
case SSS_NSS_GETSIDBYID:
case SSS_NSS_GETSIDBYUID:
case SSS_NSS_GETSIDBYGID:
rd.len = sizeof(uint32_t);
rd.data = &inp.id;
break;
default:
return EINVAL;
}
if (timeout == NO_TIMEOUT) {
sss_nss_lock();
} else {
ret = sss_nss_timedlock(timeout, &time_left);
if (ret != 0) {
return ret;
}
}
/* previous thread might already initialize entry in mmap cache */
ret = sss_nss_mc_get(inp, cmd, out);
if (ret == EOK) {
sss_nss_unlock();
return 0;
}
nret = sss_nss_make_request_timeout(cmd, &rd, time_left, &repbuf, &replen,
&errnop);
if (nret != NSS_STATUS_SUCCESS) {
ret = sss_nss_status_to_errno(nret);
goto done;
}
if (replen < 8) {
ret = EBADMSG;
goto done;
}
SAFEALIGN_COPY_UINT32(&num_results, repbuf, NULL);
if (num_results == 0) {
ret = ENOENT;
goto done;
} else if (num_results > 1 && cmd != SSS_NSS_GETLISTBYCERT) {
ret = EBADMSG;
goto done;
}
/* Skip first two 32 bit values (number of results and
* reserved padding) */
SAFEALIGN_COPY_UINT32(&out->type, repbuf + 2 * sizeof(uint32_t), NULL);
data_len = replen - DATA_START;
switch(cmd) {
case SSS_NSS_GETSIDBYID:
case SSS_NSS_GETSIDBYUID:
case SSS_NSS_GETSIDBYGID:
case SSS_NSS_GETSIDBYNAME:
case SSS_NSS_GETSIDBYUSERNAME:
case SSS_NSS_GETSIDBYGROUPNAME:
case SSS_NSS_GETNAMEBYSID:
case SSS_NSS_GETNAMEBYCERT:
if (data_len <= 1 || repbuf[replen - 1] != '\0') {
ret = EBADMSG;
goto done;
}
str = malloc(sizeof(char) * data_len);
if (str == NULL) {
ret = ENOMEM;
goto done;
}
strncpy(str, (char *) repbuf + DATA_START, data_len-1);
str[data_len-1] = '\0';
out->d.str = str;
break;
case SSS_NSS_GETIDBYSID:
if (data_len != sizeof(uint32_t)) {
ret = EBADMSG;
goto done;
}
SAFEALIGN_COPY_UINT32(&c, repbuf + DATA_START, NULL);
out->d.id = c;
break;
case SSS_NSS_GETLISTBYCERT:
ret = buf_to_name_type_list(repbuf + LIST_START, replen - LIST_START,
num_results,
&names, &types);
if (ret != EOK) {
goto done;
}
out->types = types;
out->d.names = names;
break;
case SSS_NSS_GETORIGBYNAME:
case SSS_NSS_GETORIGBYUSERNAME:
case SSS_NSS_GETORIGBYGROUPNAME:
ret = buf_to_kv_list(repbuf + DATA_START, data_len, &kv_list);
if (ret != EOK) {
goto done;
}
out->d.kv_list = kv_list;
break;
default:
ret = EINVAL;
goto done;
}
ret = EOK;
done:
sss_nss_unlock();
free(repbuf);
if (ret != EOK) {
free(str);
}
return ret;
}
static int _sss_nss_getsidbyxxxname_timeout(enum sss_cli_command cmd,
const char *fq_name,
unsigned int timeout,
char **sid,
enum sss_id_type *type)
{
int ret;
union input inp;
struct output out;
if (sid == NULL || fq_name == NULL || *fq_name == '\0') {
return EINVAL;
}
inp.str = fq_name;
ret = sss_nss_getyyybyxxx(inp, cmd, timeout, &out);
if (ret == EOK) {
*sid = out.d.str;
*type = out.type;
}
return ret;
}
int sss_nss_getsidbyname_timeout(const char *fq_name, unsigned int timeout,
char **sid, enum sss_id_type *type)
{
return _sss_nss_getsidbyxxxname_timeout(SSS_NSS_GETSIDBYNAME, fq_name,
timeout, sid, type);
}
int sss_nss_getsidbyname(const char *fq_name, char **sid,
enum sss_id_type *type)
{
return _sss_nss_getsidbyxxxname_timeout(SSS_NSS_GETSIDBYNAME, fq_name,
NO_TIMEOUT, sid, type);
}
int sss_nss_getsidbyusername_timeout(const char *fq_name,
unsigned int timeout,
char **sid,
enum sss_id_type *type)
{
return _sss_nss_getsidbyxxxname_timeout(SSS_NSS_GETSIDBYUSERNAME, fq_name,
timeout, sid, type);
}
int sss_nss_getsidbyusername(const char *fq_name,
char **sid,
enum sss_id_type *type)
{
return _sss_nss_getsidbyxxxname_timeout(SSS_NSS_GETSIDBYUSERNAME, fq_name,
NO_TIMEOUT, sid, type);
}
int sss_nss_getsidbygroupname_timeout(const char *fq_name,
unsigned int timeout,
char **sid,
enum sss_id_type *type)
{
return _sss_nss_getsidbyxxxname_timeout(SSS_NSS_GETSIDBYGROUPNAME, fq_name,
timeout, sid, type);
}
int sss_nss_getsidbygroupname(const char *fq_name,
char **sid,
enum sss_id_type *type)
{
return _sss_nss_getsidbyxxxname_timeout(SSS_NSS_GETSIDBYGROUPNAME, fq_name,
NO_TIMEOUT, sid, type);
}
int sss_nss_getsidbyid_timeout(uint32_t id, unsigned int timeout,
char **sid, enum sss_id_type *type)
{
int ret;
union input inp;
struct output out;
if (sid == NULL) {
return EINVAL;
}
inp.id = id;
ret = sss_nss_getyyybyxxx(inp, SSS_NSS_GETSIDBYID, timeout, &out);
if (ret == EOK) {
*sid = out.d.str;
*type = out.type;
}
return ret;
}
int sss_nss_getsidbyid(uint32_t id, char **sid, enum sss_id_type *type)
{
return sss_nss_getsidbyid_timeout(id, NO_TIMEOUT, sid, type);
}
int sss_nss_getsidbyuid_timeout(uint32_t uid, unsigned int timeout,
char **sid, enum sss_id_type *type)
{
int ret;
union input inp;
struct output out;
if (sid == NULL) {
return EINVAL;
}
inp.id = uid;
ret = sss_nss_getyyybyxxx(inp, SSS_NSS_GETSIDBYUID, timeout, &out);
if (ret == EOK) {
*sid = out.d.str;
*type = out.type;
}
return ret;
}
int sss_nss_getsidbyuid(uint32_t uid, char **sid, enum sss_id_type *type)
{
return sss_nss_getsidbyuid_timeout(uid, NO_TIMEOUT, sid, type);
}
int sss_nss_getsidbygid_timeout(uint32_t gid, unsigned int timeout,
char **sid, enum sss_id_type *type)
{
int ret;
union input inp;
struct output out;
if (sid == NULL) {
return EINVAL;
}
inp.id = gid;
ret = sss_nss_getyyybyxxx(inp, SSS_NSS_GETSIDBYGID, timeout, &out);
if (ret == EOK) {
*sid = out.d.str;
*type = out.type;
}
return ret;
}
int sss_nss_getsidbygid(uint32_t gid, char **sid, enum sss_id_type *type)
{
return sss_nss_getsidbygid_timeout(gid, NO_TIMEOUT, sid, type);
}
int sss_nss_getnamebysid_timeout(const char *sid, unsigned int timeout,
char **fq_name, enum sss_id_type *type)
{
int ret;
union input inp;
struct output out;
if (fq_name == NULL || sid == NULL || *sid == '\0') {
return EINVAL;
}
inp.str = sid;
ret = sss_nss_getyyybyxxx(inp, SSS_NSS_GETNAMEBYSID, timeout, &out);
if (ret == EOK) {
*fq_name = out.d.str;
*type = out.type;
}
return ret;
}
int sss_nss_getnamebysid(const char *sid, char **fq_name,
enum sss_id_type *type)
{
return sss_nss_getnamebysid_timeout(sid, NO_TIMEOUT, fq_name, type);
}
int sss_nss_getidbysid_timeout(const char *sid, unsigned int timeout,
uint32_t *id, enum sss_id_type *id_type)
{
int ret;
union input inp;
struct output out;
if (id == NULL || id_type == NULL || sid == NULL || *sid == '\0') {
return EINVAL;
}
inp.str = sid;
ret = sss_nss_getyyybyxxx(inp, SSS_NSS_GETIDBYSID, timeout, &out);
if (ret == EOK) {
*id = out.d.id;
*id_type = out.type;
}
return ret;
}
int sss_nss_getidbysid(const char *sid, uint32_t *id, enum sss_id_type *id_type)
{
return sss_nss_getidbysid_timeout(sid, NO_TIMEOUT, id, id_type);
}
int sss_nss_getorigbyname_timeout_common(const char *fq_name,
unsigned int timeout,
enum sss_cli_command cmd,
struct sss_nss_kv **kv_list,
enum sss_id_type *type)
{
int ret;
union input inp;
struct output out;
if (kv_list == NULL || fq_name == NULL || *fq_name == '\0') {
return EINVAL;
}
inp.str = fq_name;
ret = sss_nss_getyyybyxxx(inp, cmd, timeout, &out);
if (ret == EOK) {
*kv_list = out.d.kv_list;
*type = out.type;
}
return ret;
}
int sss_nss_getorigbyname_timeout(const char *fq_name, unsigned int timeout,
struct sss_nss_kv **kv_list,
enum sss_id_type *type)
{
return sss_nss_getorigbyname_timeout_common(fq_name, timeout,
SSS_NSS_GETORIGBYNAME, kv_list,
type);
}
int sss_nss_getorigbyname(const char *fq_name, struct sss_nss_kv **kv_list,
enum sss_id_type *type)
{
return sss_nss_getorigbyname_timeout(fq_name, NO_TIMEOUT, kv_list, type);
}
int sss_nss_getorigbyusername_timeout(const char *fq_name, unsigned int timeout,
struct sss_nss_kv **kv_list,
enum sss_id_type *type)
{
return sss_nss_getorigbyname_timeout_common(fq_name, timeout,
SSS_NSS_GETORIGBYUSERNAME,
kv_list, type);
}
int sss_nss_getorigbyusername(const char *fq_name, struct sss_nss_kv **kv_list,
enum sss_id_type *type)
{
return sss_nss_getorigbyusername_timeout(fq_name, NO_TIMEOUT, kv_list, type);
}
int sss_nss_getorigbygroupname_timeout(const char *fq_name, unsigned int timeout,
struct sss_nss_kv **kv_list,
enum sss_id_type *type)
{
return sss_nss_getorigbyname_timeout_common(fq_name, timeout,
SSS_NSS_GETORIGBYGROUPNAME,
kv_list, type);
}
int sss_nss_getorigbygroupname(const char *fq_name, struct sss_nss_kv **kv_list,
enum sss_id_type *type)
{
return sss_nss_getorigbygroupname_timeout(fq_name, NO_TIMEOUT, kv_list, type);
}
int sss_nss_getnamebycert_timeout(const char *cert, unsigned int timeout,
char **fq_name, enum sss_id_type *type)
{
int ret;
union input inp;
struct output out;
if (fq_name == NULL || cert == NULL || *cert == '\0') {
return EINVAL;
}
inp.str = cert;
ret = sss_nss_getyyybyxxx(inp, SSS_NSS_GETNAMEBYCERT, timeout, &out);
if (ret == EOK) {
*fq_name = out.d.str;
*type = out.type;
}
return ret;
}
int sss_nss_getnamebycert(const char *cert, char **fq_name,
enum sss_id_type *type)
{
return sss_nss_getnamebycert_timeout(cert, NO_TIMEOUT, fq_name, type);
}
int sss_nss_getlistbycert_timeout(const char *cert, unsigned int timeout,
char ***fq_name, enum sss_id_type **type)
{
int ret;
union input inp;
struct output out;
if (fq_name == NULL || cert == NULL || *cert == '\0') {
return EINVAL;
}
inp.str = cert;
ret = sss_nss_getyyybyxxx(inp, SSS_NSS_GETLISTBYCERT, timeout, &out);
if (ret == EOK) {
*fq_name = out.d.names;
*type = out.types;
}
return ret;
}
int sss_nss_getlistbycert(const char *cert, char ***fq_name,
enum sss_id_type **type)
{
return sss_nss_getlistbycert_timeout(cert, NO_TIMEOUT, fq_name, type);
}
|