/
idmapper.c
1430 lines (1260 loc) · 35.8 KB
/
idmapper.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
// SPDX-License-Identifier: LGPL-3.0-or-later
/*
* Copyright CEA/DAM/DIF (2008)
* contributeur : Philippe DENIEL philippe.deniel@cea.fr
* Thomas LEIBOVICI thomas.leibovici@cea.fr
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*
* ---------------------------------------
*/
/**
* @addtogroup idmapper
* @{
*/
/**
* @file idmapper.c
* @brief Id mapping functions
*/
#include "config.h"
#include <unistd.h> /* for using gethostname */
#include <stdlib.h> /* for using exit */
#include <strings.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef USE_NFSIDMAP
#include <nfsidmap.h>
#include "nfs_exports.h"
#endif /* USE_NFSIDMAP */
#ifdef _MSPAC_SUPPORT
#include <wbclient.h>
#endif
#include "common_utils.h"
#include "gsh_rpc.h"
#include "gsh_types.h"
#include "gsh_list.h"
#ifdef USE_DBUS
#include "gsh_dbus.h"
#endif
#include "nfs_core.h"
#include "idmapper.h"
#include "server_stats_private.h"
struct owner_domain_holder {
struct gsh_buffdesc domain;
/* Lock to synchronise reads and writes to owner domain */
pthread_rwlock_t lock;
};
static struct owner_domain_holder owner_domain;
/* winbind auth stats information */
struct auth_stats winbind_auth_stats;
pthread_rwlock_t winbind_auth_lock;
/*group cache auth stats information */
struct auth_stats gc_auth_stats;
pthread_rwlock_t gc_auth_lock;
/*DNS auth stats information */
struct auth_stats dns_auth_stats;
pthread_rwlock_t dns_auth_lock;
/* Cleanup on shutdown */
struct cleanup_list_element idmapper_cleanup_element;
/* Struct representing threads that reap idmapper caches */
static struct fridgethr *cache_reaper_fridge;
/* Switch to enable or disable idmapping */
bool idmapping_enabled = true;
/* Mutex to protect set/reset of idmapping status */
static mutex_t idmapping_status_lock = MUTEX_INITIALIZER;
/**
* @brief Set the ID Mapper's owner-domain
*
* @return true on success, false on failure
*/
static bool idmapper_set_owner_domain(void)
{
char domain_addr[NFS4_MAX_DOMAIN_LEN + 1] = {};
#ifdef USE_NFSIDMAP
if (!nfs_param.nfsv4_param.use_getpwnam) {
/* Note: The libnfsidmap function `nfs4_init_name_mapping` has
* no effect if called after it has been previously called once
* during the lifetime of the process. Any subsequent call to
* override libnfsidmap's global state must be preceded by
* clearing the earlier state within libnfsidmap, which is
* currently not possible.
*/
if (nfs4_init_name_mapping(nfs_param.nfsv4_param.idmapconf)
!= 0) {
LogCrit(COMPONENT_IDMAPPER,
"Failed to init idmapping via nfsidmap");
return false;
}
if (nfs4_get_default_domain
(NULL, domain_addr, NFS4_MAX_DOMAIN_LEN) != 0) {
LogCrit(COMPONENT_IDMAPPER,
"Failed to get default domain via nfsidmap");
return false;
}
}
#endif /* USE_NFSIDMAP */
if (nfs_param.nfsv4_param.use_getpwnam)
strcpy(domain_addr,
nfs_param.directory_services_param.domainname);
/* Return false if domain was not initialised through above
* conditions
*/
if (domain_addr[0] == '\0') {
LogCrit(COMPONENT_IDMAPPER,
"Owner domain was not found or initialised");
return false;
}
PTHREAD_RWLOCK_wrlock(&owner_domain.lock);
owner_domain.domain.addr = gsh_strdup(domain_addr);
owner_domain.domain.len = strlen(owner_domain.domain.addr);
PTHREAD_RWLOCK_unlock(&owner_domain.lock);
return true;
}
/**
* @brief Remove and free the ID Mapper's owner-domain
*/
static void idmapper_clear_owner_domain(void)
{
PTHREAD_RWLOCK_wrlock(&owner_domain.lock);
if (owner_domain.domain.len == 0) {
PTHREAD_RWLOCK_unlock(&owner_domain.lock);
return;
}
gsh_free(owner_domain.domain.addr);
owner_domain.domain.addr = NULL;
owner_domain.domain.len = 0;
PTHREAD_RWLOCK_unlock(&owner_domain.lock);
}
/**
* This function sets the idmapping status within Ganesha.
* If the status is OFF, it performs existing data cleanup in uid2grp.c and
* idmapper.c
*/
bool set_idmapping_status(bool status_enabled)
{
bool rc;
/* Acquire mutex to prevent interference by another invocation */
mutex_lock(&idmapping_status_lock);
if (idmapping_enabled == status_enabled) {
mutex_unlock(&idmapping_status_lock);
LogInfo(COMPONENT_IDMAPPER,
"Idmapping status is already set to %d",
status_enabled);
return true;
}
if (status_enabled) {
/* Set the domainname for idmapping */
rc = idmapper_set_owner_domain();
if (!rc) {
mutex_unlock(&idmapping_status_lock);
LogWarn(COMPONENT_IDMAPPER,
"Could not set owner-domain while enabling Idmapping");
return false;
}
idmapping_enabled = true;
mutex_unlock(&idmapping_status_lock);
LogInfo(COMPONENT_IDMAPPER, "Idmapping is now enabled");
return true;
}
idmapping_enabled = false;
/* Clear idmapper data */
idmapper_clear_cache();
idmapper_clear_owner_domain();
/* Clear uid2grp data */
uid2grp_clear_cache();
mutex_unlock(&idmapping_status_lock);
LogInfo(COMPONENT_IDMAPPER, "Idmapping is now disabled");
return true;
}
/**
* @brief Cleanup idmapper on shutdown
*
* This should happen only once in the process lifetime, during shutdown.
*/
void idmapper_cleanup(void)
{
if (cache_reaper_fridge != NULL) {
fridgethr_destroy(cache_reaper_fridge);
cache_reaper_fridge = NULL;
}
idmapper_clear_owner_domain();
idmapper_destroy_cache();
idmapper_negative_cache_destroy();
PTHREAD_RWLOCK_destroy(&owner_domain.lock);
PTHREAD_RWLOCK_destroy(&winbind_auth_lock);
PTHREAD_RWLOCK_destroy(&gc_auth_lock);
PTHREAD_RWLOCK_destroy(&dns_auth_lock);
}
/**
* @brief Reaps the positive and negative idmapper cache entries.
*/
static void cache_reaper_run(struct fridgethr_context *unused_ctx)
{
idmapper_cache_reap();
idmapper_negative_cache_reap();
uid2grp_cache_reap();
}
/**
* @brief Initialise the reaper thread for reaping expired cache entries
*/
static void idmapper_reaper_init(void)
{
struct fridgethr_params thread_params;
int rc;
int reaping_interval =
nfs_param.directory_services_param.cache_reaping_interval;
if (reaping_interval == 0) {
LogInfo(COMPONENT_IDMAPPER,
"Idmapper cache reaper is disabled");
return;
}
memset(&thread_params, 0, sizeof(struct fridgethr_params));
thread_params.thr_max = 1;
thread_params.thr_min = 1;
thread_params.thread_delay = reaping_interval;
thread_params.flavor = fridgethr_flavor_looper;
assert(cache_reaper_fridge == NULL);
rc = fridgethr_init(&cache_reaper_fridge, "idmapper_reaper",
&thread_params);
if (rc != 0) {
LogCrit(COMPONENT_IDMAPPER,
"Idmapper reaper fridge init failed. Error: %d", rc);
return;
}
rc = fridgethr_submit(cache_reaper_fridge, cache_reaper_run, NULL);
if (rc != 0) {
LogCrit(COMPONENT_IDMAPPER,
"Unable to start reaper for idmapper. Error: %d.", rc);
fridgethr_destroy(cache_reaper_fridge);
cache_reaper_fridge = NULL;
return;
}
LogInfo(COMPONENT_IDMAPPER, "Idmapper reaper initialized");
}
/**
* @brief Initialize the ID Mapper
*
* This should happen only once in the process lifetime, during startup.
*
* @return true on success, false on failure
*/
bool idmapper_init(void)
{
bool rc;
PTHREAD_RWLOCK_init(&winbind_auth_lock, NULL);
PTHREAD_RWLOCK_init(&gc_auth_lock, NULL);
PTHREAD_RWLOCK_init(&dns_auth_lock, NULL);
PTHREAD_RWLOCK_init(&owner_domain.lock, NULL);
rc = idmapper_set_owner_domain();
if (!rc) {
LogWarn(COMPONENT_IDMAPPER,
"Unable to set owner-domain required for idmapping.");
return false;
}
idmapper_cache_init();
idmapper_negative_cache_init();
idmapper_reaper_init();
idmapper_cleanup_element.clean = idmapper_cleanup;
RegisterCleanup(&idmapper_cleanup_element);
return true;
}
/**
* @brief Add user to the idmapper-user cache
*
* @param[in] name user name
* @param[in] uid user ID
* @param[in] gid user GID
* @param[in] gss_princ true if the name is a gss principal
*/
static void add_user_to_cache(const struct gsh_buffdesc *name, uid_t uid,
const gid_t *gid, bool gss_princ)
{
bool success;
if (!idmapping_enabled) {
LogWarn(COMPONENT_IDMAPPER,
"Idmapping is disabled. Add user(uid: %u) skipped.",
uid);
return;
}
PTHREAD_RWLOCK_wrlock(&idmapper_user_lock);
/* Recheck after obtaining the lock */
if (!idmapping_enabled) {
PTHREAD_RWLOCK_unlock(&idmapper_user_lock);
LogWarn(COMPONENT_IDMAPPER,
"Idmapping is disabled. Add user(uid: %u) skipped.",
uid);
return;
}
success = idmapper_add_user(name, uid, gid, gss_princ);
PTHREAD_RWLOCK_unlock(&idmapper_user_lock);
if (unlikely(!success)) {
LogMajor(COMPONENT_IDMAPPER,
"idmapper_add_user (uid: %u) failed.", uid);
}
}
/**
* @brief Add user to the idmapper-user negative cache
*
* @param[in] name user name
*/
static void add_user_to_negative_cache(const struct gsh_buffdesc *name)
{
PTHREAD_RWLOCK_wrlock(&idmapper_negative_cache_user_lock);
idmapper_negative_cache_add_user_by_name(name);
PTHREAD_RWLOCK_unlock(&idmapper_negative_cache_user_lock);
}
/**
* @brief Add group to the idmapper-group cache
*
* @param[in] name group name
* @param[in] gid group ID
*/
static void add_group_to_cache(const struct gsh_buffdesc *name, gid_t gid)
{
bool success;
if (!idmapping_enabled) {
LogWarn(COMPONENT_IDMAPPER,
"Idmapping is disabled. Add group(gid: %u) skipped.",
gid);
return;
}
PTHREAD_RWLOCK_wrlock(&idmapper_group_lock);
/* Recheck after obtaining the lock */
if (!idmapping_enabled) {
PTHREAD_RWLOCK_unlock(&idmapper_group_lock);
LogWarn(COMPONENT_IDMAPPER,
"Idmapping is disabled. Add group(gid: %u) skipped.",
gid);
return;
}
success = idmapper_add_group(name, gid);
PTHREAD_RWLOCK_unlock(&idmapper_group_lock);
if (unlikely(!success)) {
LogMajor(COMPONENT_IDMAPPER,
"idmapper_add_group (gid: %u) failed.", gid);
}
}
/**
* @brief Add group to the idmapper-group negative cache
*
* @param[in] name group name
*/
static void add_group_to_negative_cache(const struct gsh_buffdesc *name)
{
PTHREAD_RWLOCK_wrlock(&idmapper_negative_cache_group_lock);
idmapper_negative_cache_add_group_by_name(name);
PTHREAD_RWLOCK_unlock(&idmapper_negative_cache_group_lock);
}
/**
* @brief Encode a UID or GID as a string
*
* @param[in,out] xdrs XDR stream to which to encode
* @param[in] id UID or GID
* @param[in] group True if this is a GID, false for a UID
*
* @retval true on success.
* @retval false on failure.
*/
static bool xdr_encode_nfs4_princ(XDR *xdrs, uint32_t id, bool group)
{
const struct gsh_buffdesc *found;
uint32_t not_a_size_t;
bool success = false;
if (nfs_param.nfsv4_param.only_numeric_owners) {
/* 2**32 is 10 digits long in decimal */
struct gsh_buffdesc name;
char namebuf[11];
name.addr = namebuf;
name.len = sprintf(namebuf, "%"PRIu32, id);
not_a_size_t = name.len;
return inline_xdr_bytes(xdrs, (char **)&name.addr,
¬_a_size_t, UINT32_MAX);
}
if (!idmapping_enabled) {
LogWarn(COMPONENT_IDMAPPER,
"Idmapping is disabled, encode-nfs4-principal skipped");
return false;
}
PTHREAD_RWLOCK_rdlock(group ? &idmapper_group_lock :
&idmapper_user_lock);
if (group)
success = idmapper_lookup_by_gid(id, &found);
else
success = idmapper_lookup_by_uid(id, &found, NULL);
if (likely(success)) {
not_a_size_t = found->len;
/* Fully qualified owners are always stored in the
hash table, no matter what our lookup method. */
success =
inline_xdr_bytes(xdrs, (char **)&found->addr, ¬_a_size_t,
UINT32_MAX);
PTHREAD_RWLOCK_unlock(group ? &idmapper_group_lock :
&idmapper_user_lock);
return success;
} else {
PTHREAD_RWLOCK_unlock(group ? &idmapper_group_lock :
&idmapper_user_lock);
int rc;
int size;
bool looked_up = false;
char *namebuff = NULL;
struct gsh_buffdesc new_name;
/* We copy owner_domain to a static buffer to:
* 1. Avoid holding owner_domain read lock during network calls,
* and avoid possible writes starvation (when using libnfsidmap)
* 2. Avoid inconsistencies across the usage points of
* owner_domain, if owner_domain gets updated in the meantime
*/
PTHREAD_RWLOCK_rdlock(&owner_domain.lock);
size_t owner_domain_len = owner_domain.domain.len;
char owner_domain_addr[owner_domain_len + 1];
memcpy(owner_domain_addr, owner_domain.domain.addr,
owner_domain_len);
owner_domain_addr[owner_domain_len] = '\0';
PTHREAD_RWLOCK_unlock(&owner_domain.lock);
if (nfs_param.nfsv4_param.use_getpwnam) {
if (group)
size = sysconf(_SC_GETGR_R_SIZE_MAX);
else
size = sysconf(_SC_GETPW_R_SIZE_MAX);
if (size == -1)
size = PWENT_BEST_GUESS_LEN;
new_name.len = size;
if (owner_domain_len == 0) {
LogInfo(COMPONENT_IDMAPPER,
"owner_domain.domain is NULL, cannot encode nfs4 principal");
return false;
}
size += owner_domain_len + 2;
} else {
size = NFS4_MAX_DOMAIN_LEN + 2;
}
namebuff = alloca(size);
new_name.addr = namebuff;
if (nfs_param.nfsv4_param.use_getpwnam) {
char *cursor;
bool nulled;
if (group) {
struct group g;
struct group *gres;
rc = getgrgid_r(id, &g, namebuff, new_name.len,
&gres);
nulled = (gres == NULL);
} else {
struct passwd p;
struct passwd *pres;
rc = getpwuid_r(id, &p, namebuff, new_name.len,
&pres);
nulled = (pres == NULL);
}
if ((rc == 0) && !nulled) {
new_name.len = strlen(namebuff);
cursor = namebuff + new_name.len;
*(cursor++) = '@';
++new_name.len;
if (owner_domain_len == 0) {
LogInfo(COMPONENT_IDMAPPER,
"owner_domain.domain is NULL, cannot encode nfs4 principal");
return false;
}
memcpy(cursor, owner_domain_addr,
owner_domain_len);
new_name.len += owner_domain_len;
looked_up = true;
} else {
LogInfo(COMPONENT_IDMAPPER,
"%s failed with code %d.",
(group ? "getgrgid_r" : "getpwuid_r"),
rc);
}
} else {
#ifdef USE_NFSIDMAP
if (group) {
rc = nfs4_gid_to_name(id, owner_domain_addr,
namebuff,
NFS4_MAX_DOMAIN_LEN + 1);
} else {
rc = nfs4_uid_to_name(id, owner_domain_addr,
namebuff,
NFS4_MAX_DOMAIN_LEN + 1);
}
if (rc == 0) {
new_name.len = strlen(namebuff);
looked_up = true;
} else {
LogInfo(COMPONENT_IDMAPPER,
"%s failed with code %d.",
(group ? "nfs4_gid_to_name" :
"nfs4_uid_to_name"), rc);
}
#else /* USE_NFSIDMAP */
looked_up = false;
#endif /* !USE_NFSIDMAP */
}
if (!looked_up) {
if (nfs_param.nfsv4_param.allow_numeric_owners) {
LogInfo(COMPONENT_IDMAPPER,
"Lookup for %d failed, using numeric %s",
id, (group ? "group" : "owner"));
/* 2**32 is 10 digits long in decimal */
new_name.len = sprintf(namebuff, "%"PRIu32, id);
} else {
LogInfo(COMPONENT_IDMAPPER,
"Lookup for %d failed, using nobody.",
id);
memcpy(new_name.addr, "nobody", 6);
new_name.len = 6;
}
}
/* Add to the cache and encode the result. */
if (group)
add_group_to_cache(&new_name, id);
else
add_user_to_cache(&new_name, id, NULL, false);
not_a_size_t = new_name.len;
return inline_xdr_bytes(xdrs, (char **)&new_name.addr,
¬_a_size_t, UINT32_MAX);
}
}
/**
* @brief Encode a UID as a string
*
* @param[in,out] xdrs XDR stream to which to encode
* @param[in] uid UID
*
* @retval true on success.
* @retval false on failure.
*/
bool xdr_encode_nfs4_owner(XDR *xdrs, uid_t uid)
{
return xdr_encode_nfs4_princ(xdrs, uid, false);
}
/**
* @brief Encode a GID as a string
*
* @param[in,out] xdrs XDR stream to which to encode
* @param[in] gid GID
*
* @retval true on success.
* @retval false on failure.
*/
bool xdr_encode_nfs4_group(XDR *xdrs, gid_t gid)
{
return xdr_encode_nfs4_princ(xdrs, gid, true);
}
/**
* @brief Handle unqualified names
*
* @param[in] name C string of name
* @param[in] len Length of name
* @param[out] id ID found
* @param[in] anon ID to use in case of nobody
*
* @return true on success, false on just phoning it in.
*/
static bool atless2id(char *name, size_t len, uint32_t *id,
const uint32_t anon)
{
if ((len == 6) && (!memcmp(name, "nobody", 6))) {
*id = anon;
return true;
} else if (nfs_param.nfsv4_param.allow_numeric_owners) {
char *end = NULL;
*id = strtol(name, &end, 10);
if (!(end && *end != '\0'))
return true;
}
/* Nothing else without an @ is allowed. */
return false;
}
/**
* @brief Return gid given a group name
*
* @param[in] name group name
* @param[out] gid address for gid to be filled in
*
* @return 0 on success and errno on failure and -1 if idmapping is disabled.
*
* NOTE: If a group name doesn't exist, getgrnam_r returns 0 with the
* result pointer set to NULL. We turn that into ENOENT error! Also,
* getgrnam_r fails with ERANGE if there is a group with a large number
* of users that it can't fill all those users into the supplied buffer.
* This need not be the group we are asking for! ERANGE is handled here,
* so this function never ends up returning ERANGE back to the caller.
*/
static int name_to_gid(const char *name, gid_t *gid)
{
struct group g;
struct group *gres = NULL;
char *buf;
size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
int err;
if (buflen == -1)
buflen = PWENT_BEST_GUESS_LEN;
if (!idmapping_enabled) {
LogWarn(COMPONENT_IDMAPPER,
"Idmapping is disabled. name-to-gid skipped.");
/* Return -1 as the pw-functions return >= 0 return codes */
return -1;
}
do {
buf = gsh_malloc(buflen);
err = getgrnam_r(name, &g, buf, buflen, &gres);
if (err == ERANGE) {
buflen *= 16;
gsh_free(buf);
}
} while (buflen <= GROUP_MAX_SIZE && err == ERANGE);
if (err == 0) {
if (gres == NULL)
err = ENOENT;
else
*gid = gres->gr_gid;
}
if (err != ERANGE)
gsh_free(buf);
return err;
}
/**
* @brief Populate uid and gid given a user name
*
* @param[in] name user name
* @param[out] uid address for uid to be filled in
* @param[out] gid address for gid to be filled in
*
* @return 0 on success and errno on failure and -1 if idmapping is disabled.
*
* NOTE: If a user name doesn't exist, getpwnam_r returns 0 with the
* result pointer set to NULL. We turn that into ENOENT error! Also,
* getpwnam_r fails with ERANGE if it can't fill all user passwd fields
* into the supplied buffer. ERANGE is handled here,
* so this function never ends up returning ERANGE back to the caller.
*/
static int name_to_uid(const char *name, uint32_t *uid, gid_t *gid)
{
struct passwd p;
struct passwd *pres = NULL;
char *buf;
size_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
int err = ERANGE;
if (!idmapping_enabled) {
LogWarn(COMPONENT_IDMAPPER,
"Idmapping is disabled. name-to-uid skipped.");
/* Return -1 as the pw-functions return >= 0 return codes */
return -1;
}
if (buflen == -1)
buflen = PWENT_BEST_GUESS_LEN;
while (buflen <= PWENT_MAX_SIZE) {
buf = gsh_malloc(buflen);
err = getpwnam_r(name, &p, buf, buflen, &pres);
/* We don't use any strings from the buffer, so free it */
gsh_free(buf);
if (err != ERANGE)
break;
buflen *= 16;
}
if (err == 0) {
if (pres == NULL)
err = ENOENT;
else {
*uid = pres->pw_uid;
*gid = pres->pw_gid;
}
}
return err;
}
/**
* @brief Lookup a name using PAM
*
* @param[in] name C string of name
* @param[out] id ID found
* @param[in] group Whether this a group lookup
* @param[out] gss_gid Found GID
* @param[out] gss_uid Found UID
* @apram[out] gotgss_gid Found a GID.
* @param[in] at Location of the @
*
* @return true on success, false not making the grade
*/
static bool pwentname2id(char *name, uint32_t *id, bool group,
gid_t *gid, bool *got_gid, char *at)
{
int err;
if (at != NULL) {
PTHREAD_RWLOCK_rdlock(&owner_domain.lock);
if (owner_domain.domain.len == 0) {
PTHREAD_RWLOCK_unlock(&owner_domain.lock);
LogWarn(COMPONENT_IDMAPPER,
"owner_domain.domain is NULL, cannot validate the input domain");
return false;
}
if (strcmp(at + 1, owner_domain.domain.addr) != 0) {
PTHREAD_RWLOCK_unlock(&owner_domain.lock);
/* We won't map what isn't even in the right domain */
return false;
}
PTHREAD_RWLOCK_unlock(&owner_domain.lock);
*at = '\0';
}
if (group) {
err = name_to_gid(name, id);
if (err == 0)
return true;
if (err != ENOENT) {
LogWarn(COMPONENT_IDMAPPER,
"getgrnam_r %s failed, error: %d", name, err);
return false;
}
#ifndef USE_NFSIDMAP
char *end = NULL;
gid_t gid;
gid = strtol(name, &end, 10);
if (end && *end != '\0')
return 0;
*id = gid;
return true;
#endif
} else {
err = name_to_uid(name, id, gid);
if (err == 0) {
*got_gid = true;
return true;
}
if (err != ENOENT) {
LogWarn(COMPONENT_IDMAPPER,
"getpwnam_r %s failed, error: %d", name, err);
return false;
}
#ifndef USE_NFSIDMAP
char *end = NULL;
uid_t uid;
uid = strtol(name, &end, 10);
if (end && *end != '\0')
return 0;
*id = uid;
*got_gid = false;
return true;
#endif
}
return false;
}
/**
* @brief Lookup a name NFS ID Mapper
*
* @param[in] name C string of name
* @param[in] len Length of name
* @param[out] id ID found
* @param[in] anon ID to use in case of nobody
* @param[in] group Whether this a group lookup
* @param[out] gss_gid Found GID
* @param[out] gss_uid Found UID
* @apram[out] gotgss_gid Found a GID.
* @param[in] at Location of the @
*
* @return true on success, false not making the grade
*/
static bool idmapname2id(char *name, size_t len, uint32_t *id,
const uint32_t anon, bool group, gid_t *gid,
bool *got_gid, char *at)
{
#ifdef USE_NFSIDMAP
int rc;
if (!idmapping_enabled) {
LogWarn(COMPONENT_IDMAPPER,
"Idmapping is disabled. idmap-name-to-id skipped.");
return false;
}
if (group)
rc = nfs4_name_to_gid(name, id);
else
rc = nfs4_name_to_uid(name, id);
if (rc == 0) {
return true;
} else {
LogInfo(COMPONENT_IDMAPPER,
"%s %s failed with %d, using anonymous.",
(group ? "nfs4_name_to_gid" : "nfs4_name_to_uid"), name,
-rc);
return false;
}
#else /* USE_NFSIDMAP */
return false;
#endif /* USE_NFSIDMAP */
}
/**
* @brief Convert a name to an ID
*
* @param[in] name The name of the user
* @param[out] id The resulting id
* @param[in] group True if this is a group name
* @param[in] anon ID to return if look up fails
*
* @return true if successful, false otherwise
*/
static bool name2id(const struct gsh_buffdesc *name, uint32_t *id, bool group,
const uint32_t anon)
{
bool success;
gid_t gid;
char *namebuff;
char *at;
bool got_gid = false;
bool looked_up = false;
PTHREAD_RWLOCK_rdlock(group ? &idmapper_group_lock :
&idmapper_user_lock);
if (group)
success = idmapper_lookup_by_gname(name, id);
else
success = idmapper_lookup_by_uname(name, id, NULL, false);
PTHREAD_RWLOCK_unlock(group ? &idmapper_group_lock :
&idmapper_user_lock);
if (success)
return true;
/* Lookup negative cache */
PTHREAD_RWLOCK_rdlock(group ? &idmapper_negative_cache_group_lock :
&idmapper_negative_cache_user_lock);
if (group)
success = idmapper_negative_cache_lookup_group_by_name(name);
else
success = idmapper_negative_cache_lookup_user_by_name(name);
PTHREAD_RWLOCK_unlock(group ? &idmapper_negative_cache_group_lock :
&idmapper_negative_cache_user_lock);
if (success) {
*id = anon;
return true;
}
/* Something we can mutate and count on as terminated */
namebuff = alloca(name->len + 1);
memcpy(namebuff, name->addr, name->len);
*(namebuff + name->len) = '\0';
at = memchr(namebuff, '@', name->len);
if (at == NULL) {
if (pwentname2id(namebuff, id, group, &gid, &got_gid, NULL))
looked_up = true;
else if (atless2id(namebuff, name->len, id, anon))
looked_up = true;
else
return false;
} else if (nfs_param.nfsv4_param.use_getpwnam) {
looked_up =
pwentname2id(namebuff, id, group, &gid, &got_gid, at);
} else {
looked_up = idmapname2id(namebuff, name->len, id, anon, group,
&gid, &got_gid, at);
}
if (!looked_up) {
LogInfo(COMPONENT_IDMAPPER,
"All lookups failed for %s, using anonymous.",
namebuff);
*id = anon;
/* Add to negative cache */
if (group)
add_group_to_negative_cache(name);
else
add_user_to_negative_cache(name);
return true;
}
if (group)
add_group_to_cache(name, *id);
else
add_user_to_cache(name, *id, got_gid ? &gid : NULL, false);
return true;
}
/**
* @brief Convert a name to a uid
*
* @param[in] name The name of the user
* @param[out] uid The resulting UID