2 * Copyright (c) 2008-2015 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <si_module.h>
34 #include <arpa/inet.h>
35 #include <sys/param.h>
36 #include <sys/mount.h>
39 #include <dispatch/dispatch.h>
40 #include <TargetConditionals.h>
43 uint32_t notify_peek(int token
, uint32_t *val
);
45 extern uint32_t gL1CacheEnabled
;
47 /* These really should be in netdb.h & etc. */
48 #define _PATH_RPCS "/etc/rpc"
49 #define _PATH_ALIASES "/etc/aliases"
50 #define _PATH_ETHERS "/etc/ethers"
51 #define _PATH_NETGROUP "/etc/netgroup"
53 static dispatch_once_t rootfs_once
;
54 static si_item_t
*rootfs
= NULL
;
57 #define FNG_MEM 0x00000010
58 #define FNG_GRP 0x00000020
60 #define forever for(;;)
62 #define VALIDATION_PASSWD 0
63 #define VALIDATION_MASTER_PASSWD 1
64 #define VALIDATION_GROUP 2
65 #define VALIDATION_NETGROUP 3
66 #define VALIDATION_ALIASES 4
67 #define VALIDATION_HOSTS 5
68 #define VALIDATION_NETWORKS 6
69 #define VALIDATION_SERVICES 7
70 #define VALIDATION_PROTOCOLS 8
71 #define VALIDATION_RPC 9
72 #define VALIDATION_FSTAB 10
73 #define VALIDATION_ETHERS 11
74 #define VALIDATION_COUNT 12
76 #define VALIDATION_MASK_PASSWD 0x00000001
77 #define VALIDATION_MASK_MASTER_PASSWD 0x00000002
78 #define VALIDATION_MASK_MASK_GROUP 0x00000004
79 #define VALIDATION_MASK_NETGROUP 0x00000008
80 #define VALIDATION_MASK_ALIASES 0x00000010
81 #define VALIDATION_MASK_HOSTS 0x00000020
82 #define VALIDATION_MASK_NETWORKS 0x00000040
83 #define VALIDATION_MASK_SERVICES 0x00000080
84 #define VALIDATION_MASK_PROTOCOLS 0x00000100
85 #define VALIDATION_MASK_RPC 0x00000200
86 #define VALIDATION_MASK_FSTAB 0x00000400
87 #define VALIDATION_MASK_ETHERS 0x00000800
89 typedef struct file_netgroup_member_s
95 struct file_netgroup_member_s
*next
;
96 } file_netgroup_member_t
;
98 typedef struct file_netgroup_s
102 file_netgroup_member_t
*members
;
103 struct file_netgroup_s
*next
;
108 uint32_t validation_notify_mask
;
109 int notify_token
[VALIDATION_COUNT
];
110 file_netgroup_t
*file_netgroup_cache
;
111 uint64_t netgroup_validation_a
;
112 uint64_t netgroup_validation_b
;
115 static pthread_mutex_t file_mutex
= PTHREAD_MUTEX_INITIALIZER
;
118 _fsi_copy_string(char *s
)
123 if (s
== NULL
) return NULL
;
127 if (t
== NULL
) return NULL
;
134 _fsi_append_string(char *s
, char **l
)
138 if (s
== NULL
) return l
;
140 for (i
= 0; l
[i
] != NULL
; i
++);
146 l
= (char **) reallocf(l
, (len
+ 2) * sizeof(char *));
147 if (l
== NULL
) return NULL
;
155 _fsi_tokenize(char *data
, const char *sep
, int trailing_empty
, int *ntokens
)
158 int p
, i
, start
, end
, more
, len
, end_on_sep
;
164 if (data
== NULL
) return NULL
;
166 if (ntokens
!= NULL
) *ntokens
= 0;
169 tokens
= _fsi_append_string(data
, tokens
);
170 if (ntokens
!= NULL
) *ntokens
= *ntokens
+ 1;
177 while (data
[p
] != '\0')
180 /* skip leading white space */
181 while ((data
[p
] == ' ') || (data
[p
] == '\t') || (data
[p
] == '\n')) p
++;
183 /* check for end of line */
184 if (data
[p
] == '\0') break;
186 /* scan for separator */
192 while (scanning
== 1)
194 if (data
[p
] == '\0') break;
196 for (i
= 0; i
< len
; i
++)
198 if (data
[p
] == sep
[i
])
206 /* end is last non-whitespace character */
207 if ((scanning
== 1) && (data
[p
] != ' ') && (data
[p
] != '\t') && (data
[p
] != '\n')) end
= p
;
212 /* see if there's data left after p */
214 if (data
[p
] != '\0') more
= 1;
216 /* set the character following the token to nul */
217 if (start
== p
) data
[p
] = '\0';
218 else data
[end
+ 1] = '\0';
220 tokens
= _fsi_append_string(data
+ start
, tokens
);
221 if (ntokens
!= NULL
) *ntokens
= *ntokens
+ 1;
225 if ((end_on_sep
== 1) && (trailing_empty
!= 0))
227 /* if the scan ended on an empty token, add a null string */
228 tokens
= _fsi_append_string(data
+ p
, tokens
);
229 if (ntokens
!= NULL
) *ntokens
= *ntokens
+ 1;
236 _fsi_get_line(FILE *fp
)
243 x
= fgets(s
, sizeof(s
), fp
);
244 if ((x
== NULL
) || (s
[0] == '\0')) return NULL
;
246 if (s
[0] != '#') s
[strlen(s
) - 1] = '\0';
248 out
= _fsi_copy_string(s
);
253 _fsi_validation_path(int vtype
)
255 if (vtype
== VALIDATION_PASSWD
) return _PATH_PASSWD
;
256 else if (vtype
== VALIDATION_MASTER_PASSWD
) return _PATH_MASTERPASSWD
;
257 else if (vtype
== VALIDATION_GROUP
) return _PATH_GROUP
;
258 else if (vtype
== VALIDATION_NETGROUP
) return _PATH_NETGROUP
;
259 else if (vtype
== VALIDATION_ALIASES
) return _PATH_ALIASES
;
260 else if (vtype
== VALIDATION_HOSTS
) return _PATH_HOSTS
;
261 else if (vtype
== VALIDATION_NETWORKS
) return _PATH_NETWORKS
;
262 else if (vtype
== VALIDATION_SERVICES
) return _PATH_SERVICES
;
263 else if (vtype
== VALIDATION_PROTOCOLS
) return _PATH_PROTOCOLS
;
264 else if (vtype
== VALIDATION_RPC
) return _PATH_RPCS
;
265 else if (vtype
== VALIDATION_FSTAB
) return _PATH_FSTAB
;
266 else if (vtype
== VALIDATION_ETHERS
) return _PATH_ETHERS
;
272 _fsi_get_validation(si_mod_t
*si
, int vtype
, const char *path
, FILE *f
, uint64_t *a
, uint64_t *b
)
275 file_si_private_t
*pp
;
279 if (a
!= NULL
) *a
= 0;
280 if (b
!= NULL
) *b
= 0;
282 if (si
== NULL
) return;
283 if (path
== NULL
) return;
284 if (gL1CacheEnabled
== 0) return;
286 pp
= (file_si_private_t
*)si
->private;
287 if (pp
== NULL
) return;
289 if (vtype
>= VALIDATION_COUNT
) return;
292 if (bit
& pp
->validation_notify_mask
)
294 /* use notify validation for this type */
295 if (pp
->notify_token
[vtype
] < 0)
298 asprintf(&str
, "com.apple.system.info:%s", path
);
299 if (str
== NULL
) return;
301 status
= notify_register_check(str
, &(pp
->notify_token
[vtype
]));
307 status
= notify_peek(pp
->notify_token
[vtype
], &peek
);
308 if (status
== NOTIFY_STATUS_OK
) *a
= ntohl(peek
);
311 if (b
!= NULL
) *b
= vtype
;
315 /* use stat() and last mod time for this type */
316 memset(&sb
, 0, sizeof(struct stat
));
319 if (fstat(fileno(f
), &sb
) == 0)
321 if (a
!= NULL
) *a
= sb
.st_mtimespec
.tv_sec
;
322 if (b
!= NULL
) *b
= sb
.st_mtimespec
.tv_nsec
;
327 path
= _fsi_validation_path(vtype
);
330 memset(&sb
, 0, sizeof(struct stat
));
331 if (stat(path
, &sb
) == 0)
333 if (a
!= NULL
) *a
= sb
.st_mtimespec
.tv_sec
;
334 if (b
!= NULL
) *b
= sb
.st_mtimespec
.tv_nsec
;
342 _fsi_validate(si_mod_t
*si
, int cat
, uint64_t va
, uint64_t vb
)
346 uint32_t item_val
, curr_val
, vtype
;
347 file_si_private_t
*pp
;
350 if (si
== NULL
) return 0;
352 #if TARGET_OS_EMBEDDED
353 /* /etc is on a read-only filesystem, so no validation is required */
357 pp
= (file_si_private_t
*)si
->private;
358 if (pp
== NULL
) return 0;
365 if (geteuid() == 0) vtype
= VALIDATION_MASTER_PASSWD
;
366 else vtype
= VALIDATION_PASSWD
;
371 vtype
= VALIDATION_GROUP
;
374 case CATEGORY_GROUPLIST
:
376 vtype
= VALIDATION_GROUP
;
379 case CATEGORY_NETGROUP
:
381 vtype
= VALIDATION_NETGROUP
;
386 vtype
= VALIDATION_ALIASES
;
389 case CATEGORY_HOST_IPV4
:
391 vtype
= VALIDATION_HOSTS
;
394 case CATEGORY_HOST_IPV6
:
396 vtype
= VALIDATION_HOSTS
;
399 case CATEGORY_NETWORK
:
401 vtype
= VALIDATION_NETWORKS
;
404 case CATEGORY_SERVICE
:
406 vtype
= VALIDATION_SERVICES
;
409 case CATEGORY_PROTOCOL
:
411 vtype
= VALIDATION_PROTOCOLS
;
416 vtype
= VALIDATION_RPC
;
421 vtype
= VALIDATION_FSTAB
;
426 vtype
= VALIDATION_ETHERS
;
432 if (pp
->notify_token
[vtype
] < 0)
434 path
= _fsi_validation_path(vtype
);
435 if (path
== NULL
) return 0;
437 memset(&sb
, 0, sizeof(struct stat
));
438 if (stat(path
, &sb
) != 0) return 0;
439 if (va
!= sb
.st_mtimespec
.tv_sec
) return 0;
440 if (vb
!= sb
.st_mtimespec
.tv_nsec
) return 0;
446 status
= notify_peek(pp
->notify_token
[vtype
], &curr_val
);
447 if (status
!= NOTIFY_STATUS_OK
) return 0;
449 curr_val
= ntohl(curr_val
);
450 if (item_val
!= curr_val
) return 0;
456 /* netgroup support */
458 _fsi_append_char_to_line(char c
, char *buf
, size_t *x
)
460 if (x
== NULL
) return NULL
;
462 if (buf
== NULL
) *x
= 0;
464 if ((*x
% CHUNK
) == 0)
466 buf
= reallocf(buf
, *x
+ CHUNK
);
467 if (buf
== NULL
) return NULL
;
469 memset(buf
+ *x
, 0, CHUNK
);
479 _fsi_read_netgroup_line(FILE *f
)
486 if (f
== NULL
) return NULL
;
493 if (out
== NULL
) return NULL
;
494 return _fsi_append_char_to_line('\0', out
, &x
);
497 if (c
== '\n') return _fsi_append_char_to_line('\0', out
, &x
);
498 if (c
== '(') paren
= 1;
499 else if (c
== ')') paren
= 0;
501 if ((c
== ' ') || (c
== '\t'))
503 if ((white
== 0) && (paren
== 0)) out
= _fsi_append_char_to_line(' ', out
, &x
);
511 if (c
== EOF
) return _fsi_append_char_to_line('\0', out
, &x
);
512 if (c
== '\n') break;
517 out
= _fsi_append_char_to_line(c
, out
, &x
);
523 static file_netgroup_t
*
524 _fsi_find_netgroup(file_netgroup_t
**list
, const char *name
, int create
)
528 if (list
== NULL
) return NULL
;
530 for (n
= *list
; n
!= NULL
; n
= n
->next
)
532 if (!strcmp(name
, n
->name
)) return n
;
535 if (create
== 0) return NULL
;
537 n
= (file_netgroup_t
*)calloc(1, sizeof(file_netgroup_t
));
538 if (n
== NULL
) return NULL
;
540 n
->name
= strdup(name
);
548 _fsi_free_file_netgroup(file_netgroup_t
*n
)
550 file_netgroup_member_t
*m
;
552 if (n
== NULL
) return;
558 file_netgroup_member_t
*x
= m
;
570 _fsi_add_netgroup_group(file_netgroup_t
*n
, char *grp
)
572 file_netgroup_member_t
*g
;
574 if (n
== NULL
) return;
576 g
= (file_netgroup_member_t
*)calloc(1, sizeof(file_netgroup_member_t
));
577 if (g
== NULL
) return;
580 g
->host
= strdup(grp
);
582 g
->next
= n
->members
;
589 _fsi_add_netgroup_member(file_netgroup_t
*n
, char *mem
)
592 file_netgroup_member_t
*m
;
595 if (n
== NULL
) return;
597 m
= (file_netgroup_member_t
*)calloc(1, sizeof(file_netgroup_member_t
));
598 if (m
== NULL
) return;
600 tokens
= _fsi_tokenize(mem
+ 1, ",)", 0, &ntokens
);
608 if ((ntokens
> 0) && (tokens
[0][0] != '\0')) m
->host
= strdup(tokens
[0]);
609 if ((ntokens
> 1) && (tokens
[1][0] != '\0')) m
->user
= strdup(tokens
[1]);
610 if ((ntokens
> 2) && (tokens
[2][0] != '\0')) m
->domain
= strdup(tokens
[2]);
615 m
->next
= n
->members
;
619 static file_netgroup_t
*
620 _fsi_process_netgroup_line(file_netgroup_t
**pass1
, char *line
)
626 tokens
= _fsi_tokenize(line
, " ", 0, &ntokens
);
627 if (tokens
== NULL
) return NULL
;
628 if (tokens
[0] == NULL
)
634 n
= _fsi_find_netgroup(pass1
, tokens
[0], 1);
636 for (i
= 1; tokens
[i
] != NULL
; i
++)
638 if (tokens
[i
][0] == '(') _fsi_add_netgroup_member(n
, tokens
[i
]);
639 else if (tokens
[i
][0] != '\0') _fsi_add_netgroup_group(n
, tokens
[i
]);
647 _fsi_flatten_netgroup(file_netgroup_t
*pass1
, file_netgroup_t
**top
, file_netgroup_t
*n
, file_netgroup_member_t
*m
)
649 if (n
== NULL
) return;
651 if (n
->flags
== 1) return;
656 *top
= (file_netgroup_t
*)calloc(1, sizeof(file_netgroup_t
));
657 if (*top
== NULL
) return;
658 (*top
)->name
= strdup(n
->name
);
659 if ((*top
)->name
== NULL
)
669 if (m
->flags
& FNG_MEM
)
671 file_netgroup_member_t
*x
= (file_netgroup_member_t
*)calloc(1, sizeof(file_netgroup_member_t
));
672 if (x
== NULL
) return;
675 if (m
->host
!= NULL
) x
->host
= strdup(m
->host
);
676 if (m
->user
!= NULL
) x
->user
= strdup(m
->user
);
677 if (m
->domain
!= NULL
) x
->domain
= strdup(m
->domain
);
679 x
->next
= (*top
)->members
;
684 file_netgroup_t
*g
= _fsi_find_netgroup(&pass1
, m
->host
, 0);
685 if (g
== NULL
) continue;
687 _fsi_flatten_netgroup(pass1
, top
, g
, g
->members
);
695 _fsi_check_netgroup_cache(si_mod_t
*si
)
697 file_netgroup_t
*p1
, *n
, *x
, *a
;
700 file_si_private_t
*pp
;
702 if (si
== NULL
) return;
704 pp
= (file_si_private_t
*)si
->private;
705 if (pp
== NULL
) return;
707 pthread_mutex_lock(&file_mutex
);
709 if (_fsi_validate(si
, CATEGORY_NETGROUP
, pp
->netgroup_validation_a
, pp
->netgroup_validation_b
))
711 pthread_mutex_unlock(&file_mutex
);
715 n
= pp
->file_netgroup_cache
;
720 _fsi_free_file_netgroup(x
);
723 pp
->file_netgroup_cache
= NULL
;
725 f
= fopen(_PATH_NETGROUP
, "r");
728 pthread_mutex_unlock(&file_mutex
);
732 _fsi_get_validation(si
, VALIDATION_NETGROUP
, _PATH_NETGROUP
, f
, &(pp
->netgroup_validation_a
), &(pp
->netgroup_validation_b
));
736 line
= _fsi_read_netgroup_line(f
);
739 n
= _fsi_process_netgroup_line(&p1
, line
);
742 line
= _fsi_read_netgroup_line(f
);
747 for (n
= p1
; n
!= NULL
; n
= n
->next
)
750 _fsi_flatten_netgroup(p1
, &a
, n
, n
->members
);
751 for (x
= p1
; x
!= NULL
; x
= x
->next
) x
->flags
= 0;
755 a
->next
= pp
->file_netgroup_cache
;
756 pp
->file_netgroup_cache
= a
;
765 _fsi_free_file_netgroup(x
);
768 pthread_mutex_unlock(&file_mutex
);
774 _fsi_parse_user(si_mod_t
*si
, const char *name
, uid_t uid
, int which
, char *data
, int format
, uint64_t va
, uint64_t vb
)
778 time_t change
, expire
;
782 if (data
== NULL
) return NULL
;
785 tokens
= _fsi_tokenize(data
, ":", 1, &ntokens
);
786 if (((format
== 0) && (ntokens
!= 10)) || ((format
== 1) && (ntokens
!= 7)))
792 xuid
= atoi(tokens
[2]);
795 /* XXX MATCH GECOS? XXX*/
796 if (which
== SEL_ALL
) match
= 1;
797 else if ((which
== SEL_NAME
) && (string_equal(name
, tokens
[0]))) match
= 1;
798 else if ((which
== SEL_NUMBER
) && (uid
== xuid
)) match
= 1;
808 /* master.passwd: name[0] passwd[1] uid[2] gid[3] class[4] change[5] expire[6] gecos[7] dir[8] shell[9] */
809 /* struct pwd: name[0] passwd[1] uid[2] gid[3] change[5] class[4] gecos[7] dir[8] shell[9] expire[6] */
810 change
= atoi(tokens
[5]);
811 expire
= atoi(tokens
[6]);
812 item
= (si_item_t
*)LI_ils_create("L4488ss44LssssL", (unsigned long)si
, CATEGORY_USER
, 1, va
, vb
, tokens
[0], tokens
[1], xuid
, atoi(tokens
[3]), change
, tokens
[4], tokens
[7], tokens
[8], tokens
[9], expire
);
816 /* passwd: name[0] passwd[1] uid[2] gid[3] gecos[4] dir[5] shell[6] */
817 /* struct pwd: name[0] passwd[1] uid[2] gid[3] change[-] class[-] gecos[4] dir[5] shell[6] expire[-] */
818 item
= (si_item_t
*)LI_ils_create("L4488ss44LssssL", (unsigned long)si
, CATEGORY_USER
, 1, va
, vb
, tokens
[0], tokens
[1], xuid
, atoi(tokens
[3]), 0, "", tokens
[4], tokens
[5], tokens
[6], 0);
826 _fsi_get_user(si_mod_t
*si
, const char *name
, uid_t uid
, int which
)
835 if ((which
== SEL_NAME
) && (name
== NULL
)) return NULL
;
845 f
= fopen(_PATH_MASTERPASSWD
, "r");
846 _fsi_get_validation(si
, VALIDATION_MASTER_PASSWD
, _PATH_MASTERPASSWD
, f
, &va
, &vb
);
850 f
= fopen(_PATH_PASSWD
, "r");
851 _fsi_get_validation(si
, VALIDATION_PASSWD
, _PATH_PASSWD
, f
, &va
, &vb
);
855 if (f
== NULL
) return NULL
;
860 line
= _fsi_get_line(f
);
861 if (line
== NULL
) break;
870 item
= _fsi_parse_user(si
, name
, uid
, which
, line
, fmt
, va
, vb
);
874 if (item
== NULL
) continue;
876 if (which
== SEL_ALL
)
878 all
= si_list_add(all
, item
);
879 si_item_release(item
);
893 _fsi_parse_group(si_mod_t
*si
, const char *name
, gid_t gid
, int which
, char *data
, uint64_t va
, uint64_t vb
)
895 char **tokens
, **members
;
900 if (data
== NULL
) return NULL
;
903 tokens
= _fsi_tokenize(data
, ":", 1, &ntokens
);
910 xgid
= atoi(tokens
[2]);
913 if (which
== SEL_ALL
) match
= 1;
914 else if ((which
== SEL_NAME
) && (string_equal(name
, tokens
[0]))) match
= 1;
915 else if ((which
== SEL_NUMBER
) && (gid
== xgid
)) match
= 1;
924 members
= _fsi_tokenize(tokens
[3], ",", 1, &ntokens
);
926 item
= (si_item_t
*)LI_ils_create("L4488ss4*", (unsigned long)si
, CATEGORY_GROUP
, 1, va
, vb
, tokens
[0], tokens
[1], xgid
, members
);
935 _fsi_get_group(si_mod_t
*si
, const char *name
, gid_t gid
, int which
)
943 if ((which
== SEL_NAME
) && (name
== NULL
)) return NULL
;
948 f
= fopen(_PATH_GROUP
, "r");
949 if (f
== NULL
) return NULL
;
951 _fsi_get_validation(si
, VALIDATION_GROUP
, _PATH_GROUP
, f
, &va
, &vb
);
955 line
= _fsi_get_line(f
);
956 if (line
== NULL
) break;
965 item
= _fsi_parse_group(si
, name
, gid
, which
, line
, va
, vb
);
969 if (item
== NULL
) continue;
971 if (which
== SEL_ALL
)
973 all
= si_list_add(all
, item
);
974 si_item_release(item
);
987 _fsi_get_grouplist(si_mod_t
*si
, const char *user
)
989 char **tokens
, **members
;
990 int ntokens
, i
, match
, gidcount
;
999 if (user
== NULL
) return NULL
;
1006 item
= si
->vtable
->sim_user_byname(si
, user
);
1009 pw
= (struct passwd
*)((uintptr_t)item
+ sizeof(si_item_t
));
1010 basegid
= pw
->pw_gid
;
1011 si_item_release(item
);
1015 f
= fopen(_PATH_GROUP
, "r");
1016 if (f
== NULL
) return NULL
;
1018 _fsi_get_validation(si
, VALIDATION_GROUP
, _PATH_GROUP
, f
, &va
, &vb
);
1022 line
= _fsi_get_line(f
);
1023 if (line
== NULL
) break;
1033 tokens
= _fsi_tokenize(line
, ":", 1, &ntokens
);
1041 members
= _fsi_tokenize(tokens
[3], ",", 1, &ntokens
);
1046 for (i
= 0; i
< ntokens
; i
++)
1048 if (string_equal(user
, members
[i
]))
1050 gid
= atoi(tokens
[2]);
1063 gidlist
= (gid_t
*) reallocf(gidlist
, (gidcount
+ 1) * sizeof(gid_t
));
1064 if (gidlist
== NULL
)
1070 gidlist
[gidcount
++] = gid
;
1076 if (gidcount
!= 0) {
1077 item
= (si_item_t
*)LI_ils_create("L4488s4@", (unsigned long)si
, CATEGORY_GROUPLIST
, 1, va
, vb
, user
, gidcount
,
1078 gidcount
* sizeof(gid_t
), gidlist
);
1089 _fsi_parse_alias(si_mod_t
*si
, const char *name
, int which
, char *data
, uint64_t va
, uint64_t vb
)
1091 char **tokens
, **members
;
1095 if (data
== NULL
) return NULL
;
1098 tokens
= _fsi_tokenize(data
, ":", 1, &ntokens
);
1107 if (which
== SEL_ALL
) match
= 1;
1108 else if (string_equal(name
, tokens
[0])) match
= 1;
1117 members
= _fsi_tokenize(tokens
[1], ",", 1, &ntokens
);
1119 item
= (si_item_t
*)LI_ils_create("L4488s4*4", (unsigned long)si
, CATEGORY_ALIAS
, 1, va
, vb
, tokens
[0], ntokens
, members
, 1);
1128 _fsi_get_alias(si_mod_t
*si
, const char *name
, int which
)
1136 if ((which
== SEL_NAME
) && (name
== NULL
)) return NULL
;
1141 f
= fopen(_PATH_ALIASES
, "r");
1142 if (f
== NULL
) return NULL
;
1144 _fsi_get_validation(si
, VALIDATION_ALIASES
, _PATH_ALIASES
, f
, &va
, &vb
);
1148 line
= _fsi_get_line(f
);
1149 if (line
== NULL
) break;
1158 item
= _fsi_parse_alias(si
, name
, which
, line
, va
, vb
);
1162 if (item
== NULL
) continue;
1164 if (which
== SEL_ALL
)
1166 all
= si_list_add(all
, item
);
1167 si_item_release(item
);
1182 _fsi_parse_ether(si_mod_t
*si
, const char *name
, int which
, char *data
, uint64_t va
, uint64_t vb
)
1189 if (data
== NULL
) return NULL
;
1192 tokens
= _fsi_tokenize(data
, " \t", 1, &ntokens
);
1199 cmac
= si_standardize_mac_address(tokens
[0]);
1207 if (which
== SEL_ALL
) match
= 1;
1208 else if ((which
== SEL_NAME
) && (string_equal(name
, tokens
[1]))) match
= 1;
1209 else if ((which
== SEL_NUMBER
) && (string_equal(name
, cmac
))) match
= 1;
1218 item
= (si_item_t
*)LI_ils_create("L4488ss", (unsigned long)si
, CATEGORY_MAC
, 1, va
, vb
, tokens
[1], cmac
);
1227 _fsi_get_ether(si_mod_t
*si
, const char *name
, int which
)
1235 if ((which
!= SEL_ALL
) && (name
== NULL
)) return NULL
;
1238 if (which
== SEL_NUMBER
)
1240 cmac
= si_standardize_mac_address(name
);
1241 if (cmac
== NULL
) return NULL
;
1247 f
= fopen(_PATH_ETHERS
, "r");
1248 if (f
== NULL
) return NULL
;
1250 _fsi_get_validation(si
, VALIDATION_ETHERS
, _PATH_ETHERS
, f
, &va
, &vb
);
1254 line
= _fsi_get_line(f
);
1255 if (line
== NULL
) break;
1265 if (which
== SEL_NUMBER
) item
= _fsi_parse_ether(si
, cmac
, which
, line
, va
, vb
);
1266 else item
= _fsi_parse_ether(si
, name
, which
, line
, va
, vb
);
1271 if (item
== NULL
) continue;
1273 if (which
== SEL_ALL
)
1275 all
= si_list_add(all
, item
);
1276 si_item_release(item
);
1291 _fsi_parse_host(si_mod_t
*si
, const char *name
, const void *addr
, int af
, int which
, char *data
, uint64_t va
, uint64_t vb
)
1293 char **tokens
, **h_aliases
, *null_alias
;
1294 int i
, ntokens
, match
, h_addrtype
, h_length
;
1298 char *h_addr_list
[2];
1299 char h_addr_4
[4], h_addr_6
[16];
1301 if (data
== NULL
) return NULL
;
1306 tokens
= _fsi_tokenize(data
, " ", 0, &ntokens
);
1313 h_addr_list
[1] = NULL
;
1315 h_addrtype
= AF_UNSPEC
;
1316 if (inet_pton(AF_INET
, tokens
[0], &a4
) == 1)
1318 h_addrtype
= AF_INET
;
1319 h_length
= sizeof(struct in_addr
);
1320 memcpy(h_addr_4
, &a4
, 4);
1321 h_addr_list
[0] = h_addr_4
;
1323 else if (inet_pton(AF_INET6
, tokens
[0], &a6
) == 1)
1325 h_addrtype
= AF_INET6
;
1326 h_length
= sizeof(struct in6_addr
);
1327 memcpy(h_addr_6
, &a6
, 16);
1328 h_addr_list
[0] = h_addr_6
;
1331 if (h_addrtype
== AF_UNSPEC
)
1338 if (ntokens
> 2) h_aliases
= &(tokens
[2]);
1342 if (which
== SEL_ALL
) match
= 1;
1345 if (h_addrtype
== af
)
1347 if (which
== SEL_NAME
)
1349 if (string_equal(name
, tokens
[1])) match
= 1;
1350 else if (h_aliases
!= NULL
)
1352 for (i
= 0; (h_aliases
[i
] != NULL
) && (match
== 0); i
++)
1353 if (string_equal(name
, h_aliases
[i
])) match
= 1;
1356 else if (which
== SEL_NUMBER
)
1358 if (memcmp(addr
, h_addr_list
[0], h_length
) == 0) match
= 1;
1371 if (h_aliases
== NULL
) h_aliases
= &null_alias
;
1373 if (h_addrtype
== AF_INET
)
1375 item
= (si_item_t
*)LI_ils_create("L4488s*44a", (unsigned long)si
, CATEGORY_HOST_IPV4
, 1, va
, vb
, tokens
[1], h_aliases
, h_addrtype
, h_length
, h_addr_list
);
1379 item
= (si_item_t
*)LI_ils_create("L4488s*44c", (unsigned long)si
, CATEGORY_HOST_IPV6
, 1, va
, vb
, tokens
[1], h_aliases
, h_addrtype
, h_length
, h_addr_list
);
1388 _fsi_get_host(si_mod_t
*si
, const char *name
, const void *addr
, int af
, int which
, uint32_t *err
)
1396 if ((which
== SEL_NAME
) && (name
== NULL
))
1398 if (err
!= NULL
) *err
= NO_RECOVERY
;
1402 if ((which
== SEL_NUMBER
) && (addr
== NULL
))
1404 if (err
!= NULL
) *err
= NO_RECOVERY
;
1408 f
= fopen(_PATH_HOSTS
, "r");
1411 if (err
!= NULL
) *err
= NO_RECOVERY
;
1415 _fsi_get_validation(si
, VALIDATION_HOSTS
, _PATH_HOSTS
, f
, &va
, &vb
);
1421 line
= _fsi_get_line(f
);
1422 if (line
== NULL
) break;
1431 item
= _fsi_parse_host(si
, name
, addr
, af
, which
, line
, va
, vb
);
1435 if (item
== NULL
) continue;
1437 if (which
== SEL_ALL
)
1439 all
= si_list_add(all
, item
);
1440 si_item_release(item
);
1455 _fsi_parse_service(si_mod_t
*si
, const char *name
, const char *proto
, int port
, int which
, char *data
, uint64_t va
, uint64_t vb
)
1457 char **tokens
, **s_aliases
, *xproto
;
1458 int i
, ntokens
, match
;
1462 if (data
== NULL
) return NULL
;
1467 tokens
= _fsi_tokenize(data
, " ", 0, &ntokens
);
1475 if (ntokens
> 2) s_aliases
= &(tokens
[2]);
1477 xport
= atoi(tokens
[1]);
1479 xproto
= strchr(tokens
[1], '/');
1488 if ((proto
!= NULL
) && (string_not_equal(proto
, xproto
)))
1495 if (which
== SEL_ALL
) match
= 1;
1496 else if (which
== SEL_NAME
)
1498 if (string_equal(name
, tokens
[0])) match
= 1;
1499 else if (s_aliases
!= NULL
)
1501 for (i
= 0; (s_aliases
[i
] != NULL
) && (match
== 0); i
++)
1502 if (string_equal(name
, s_aliases
[i
])) match
= 1;
1505 else if ((which
== SEL_NUMBER
) && (port
== xport
)) match
= 1;
1513 /* strange but correct */
1514 xport
= htons(xport
);
1516 item
= (si_item_t
*)LI_ils_create("L4488s*4s", (unsigned long)si
, CATEGORY_SERVICE
, 1, va
, vb
, tokens
[0], s_aliases
, xport
, xproto
);
1524 _fsi_get_service(si_mod_t
*si
, const char *name
, const char *proto
, int port
, int which
)
1532 if ((which
== SEL_NAME
) && (name
== NULL
)) return NULL
;
1533 if ((which
== SEL_NUMBER
) && (port
== 0)) return NULL
;
1535 f
= fopen(_PATH_SERVICES
, "r");
1536 if (f
== NULL
) return NULL
;
1538 _fsi_get_validation(si
, VALIDATION_SERVICES
, _PATH_SERVICES
, f
, &va
, &vb
);
1544 line
= _fsi_get_line(f
);
1545 if (line
== NULL
) break;
1554 p
= strchr(line
, '#');
1555 if (p
!= NULL
) *p
= '\0';
1557 item
= _fsi_parse_service(si
, name
, proto
, port
, which
, line
, va
, vb
);
1561 if (item
== NULL
) continue;
1563 if (which
== SEL_ALL
)
1565 all
= si_list_add(all
, item
);
1566 si_item_release(item
);
1579 * Generic name/number/aliases lookup
1580 * Works for protocols, networks, and rpcs
1584 _fsi_parse_name_num_aliases(si_mod_t
*si
, const char *name
, int num
, int which
, char *data
, uint64_t va
, uint64_t vb
, int cat
)
1586 char **tokens
, **aliases
;
1587 int i
, ntokens
, match
, xnum
;
1590 if (data
== NULL
) return NULL
;
1593 tokens
= _fsi_tokenize(data
, " ", 0, &ntokens
);
1600 xnum
= atoi(tokens
[1]);
1603 if (ntokens
> 2) aliases
= &(tokens
[2]);
1607 if (which
== SEL_ALL
) match
= 1;
1608 else if (which
== SEL_NAME
)
1610 if (string_equal(name
, tokens
[0])) match
= 1;
1611 else if (aliases
!= NULL
)
1613 for (i
= 0; (aliases
[i
] != NULL
) && (match
== 0); i
++)
1614 if (string_equal(name
, aliases
[i
])) match
= 1;
1617 else if ((which
== SEL_NUMBER
) && (num
== xnum
)) match
= 1;
1626 case CATEGORY_NETWORK
:
1628 item
= (si_item_t
*)LI_ils_create("L4488s*44", (unsigned long)si
, cat
, 1, va
, vb
, tokens
[0], aliases
, AF_INET
, xnum
);
1630 case CATEGORY_PROTOCOL
:
1634 item
= (si_item_t
*)LI_ils_create("L4488s*4", (unsigned long)si
, cat
, 1, va
, vb
, tokens
[0], aliases
, xnum
);
1646 _fsi_get_name_number_aliases(si_mod_t
*si
, const char *name
, int num
, int which
, int cat
)
1657 case CATEGORY_NETWORK
:
1658 vtype
= VALIDATION_NETWORKS
;
1659 path
= _PATH_NETWORKS
;
1661 case CATEGORY_PROTOCOL
:
1662 vtype
= VALIDATION_PROTOCOLS
;
1663 path
= _PATH_PROTOCOLS
;
1666 vtype
= VALIDATION_RPC
;
1673 f
= fopen(path
, "r");
1674 if (f
== NULL
) return NULL
;
1676 _fsi_get_validation(si
, vtype
, path
, f
, &va
, &vb
);
1682 line
= _fsi_get_line(f
);
1683 if (line
== NULL
) break;
1692 p
= strchr(line
, '#');
1693 if (p
!= NULL
) *p
= '\0';
1695 item
= _fsi_parse_name_num_aliases(si
, name
, num
, which
, line
, va
, vb
, cat
);
1699 if (item
== NULL
) continue;
1701 if (which
== SEL_ALL
)
1703 all
= si_list_add(all
, item
);
1704 si_item_release(item
);
1719 _fsi_parse_fs(si_mod_t
*si
, const char *name
, int which
, char *data
, uint64_t va
, uint64_t vb
)
1721 char **tokens
, *tmp
, **opts
, *fstype
;
1722 int ntokens
, match
, i
, freq
, passno
;
1725 if (data
== NULL
) return NULL
;
1732 tokens
= _fsi_tokenize(data
, " ", 0, &ntokens
);
1733 if ((ntokens
< 4) || (ntokens
> 6))
1739 if (ntokens
>= 5) freq
= atoi(tokens
[4]);
1740 if (ntokens
== 6) passno
= atoi(tokens
[5]);
1742 tmp
= strdup(tokens
[3]);
1750 opts
= _fsi_tokenize(tmp
, ",", 0, &ntokens
);
1759 for (i
= 0; i
< ntokens
; i
++)
1761 if ((string_equal(opts
[i
], "rw")) || (string_equal(opts
[i
], "ro")) || (string_equal(opts
[i
], "sw")) || (string_equal(opts
[i
], "xx")))
1770 if (which
== SEL_ALL
) match
= 1;
1771 else if ((which
== SEL_NAME
) && (string_equal(name
, tokens
[0]))) match
= 1;
1772 else if ((which
== SEL_NUMBER
) && (string_equal(name
, tokens
[1]))) match
= 1;
1780 item
= (si_item_t
*)LI_ils_create("L4488sssss44", (unsigned long)si
, CATEGORY_FS
, 1, va
, vb
, tokens
[0], tokens
[1], tokens
[2], tokens
[3], (fstype
== NULL
) ? "rw" : fstype
, freq
, passno
);
1790 _fsi_get_device_path(dev_t target_dev
)
1795 char namebuf
[PATH_MAX
];
1799 strlcpy(dev
, _PATH_DEV
, sizeof(dev
));
1801 /* The root device in fstab should always be a block special device */
1802 name
= devname_r(target_dev
, S_IFBLK
, namebuf
, sizeof(namebuf
));
1807 struct dirent
*ent
, entbuf
;
1809 /* No _PATH_DEVDB. We have to search for it the slow way */
1810 dirp
= opendir(_PATH_DEV
);
1811 if (dirp
== NULL
) return NULL
;
1813 while (readdir_r(dirp
, &entbuf
, &ent
) == 0 && ent
!= NULL
)
1815 /* Look for a block special device */
1816 if (ent
->d_type
== DT_BLK
)
1818 strlcat(dev
, ent
->d_name
, sizeof(dev
));
1819 if (stat(dev
, &devst
) == 0)
1821 if (devst
.st_rdev
== target_dev
) {
1822 result
= strdup(dev
);
1828 /* reset dev to _PATH_DEV and try again */
1829 dev
[sizeof(_PATH_DEV
) - 1] = '\0';
1832 if (dirp
) closedir(dirp
);
1836 /* We found the _PATH_DEVDB entry */
1837 strlcat(dev
, name
, sizeof(dev
));
1838 result
= strdup(dev
);
1845 _fsi_fs_root(si_mod_t
*si
)
1847 dispatch_once(&rootfs_once
, ^{
1848 struct stat rootstat
;
1849 struct statfs rootfsinfo
;
1851 const char *root_path
= "/";
1853 if (stat(root_path
, &rootstat
) < 0) return;
1854 if (statfs(root_path
, &rootfsinfo
) < 0) return;
1856 // Check to make sure we're not looking at a synthetic root:
1857 if (string_equal(rootfsinfo
.f_fstypename
, "synthfs")) {
1858 root_path
= "/root";
1859 if (stat(root_path
, &rootstat
) < 0) return;
1860 if (statfs(root_path
, &rootfsinfo
) < 0) return;
1863 root_spec
= _fsi_get_device_path(rootstat
.st_dev
);
1865 rootfs
= (si_item_t
*)LI_ils_create("L4488sssss44", (unsigned long)si
, CATEGORY_FS
, 1, 0LL, 0LL, root_spec
, root_path
, rootfsinfo
.f_fstypename
, FSTAB_RW
, FSTAB_RW
, 0, 1);
1868 return si_item_retain(rootfs
);
1872 _fsi_get_fs(si_mod_t
*si
, const char *name
, int which
)
1879 int synthesize_root
;
1882 if ((which
!= SEL_ALL
) && (name
== NULL
)) return NULL
;
1887 synthesize_root
= 1;
1889 synthesize_root
= 0;
1892 f
= fopen(_PATH_FSTAB
, "r");
1893 if ((f
== NULL
) || (synthesize_root
== 1))
1895 item
= _fsi_fs_root(si
);
1898 if (item
!= NULL
) rfs
= (struct fstab
*)((uintptr_t)item
+ sizeof(si_item_t
));
1904 if ((rfs
!= NULL
) && (string_equal(name
, rfs
->fs_spec
)))
1906 if (f
!= NULL
) fclose(f
);
1915 if ((rfs
!= NULL
) && (string_equal(name
, rfs
->fs_file
)))
1917 if (f
!= NULL
) fclose(f
);
1926 all
= si_list_add(all
, item
);
1927 si_item_release(item
);
1933 if (f
== NULL
) return all
;
1935 _fsi_get_validation(si
, VALIDATION_FSTAB
, _PATH_FSTAB
, f
, &va
, &vb
);
1939 line
= _fsi_get_line(f
);
1940 if (line
== NULL
) break;
1949 item
= _fsi_parse_fs(si
, name
, which
, line
, va
, vb
);
1953 if (item
== NULL
) continue;
1955 if (which
== SEL_ALL
)
1957 all
= si_list_add(all
, item
);
1958 si_item_release(item
);
1971 file_is_valid(si_mod_t
*si
, si_item_t
*item
)
1975 if (si
== NULL
) return 0;
1976 if (item
== NULL
) return 0;
1977 if (si
->name
== NULL
) return 0;
1978 if (item
->src
== NULL
) return 0;
1980 src
= (si_mod_t
*)item
->src
;
1982 if (src
->name
== NULL
) return 0;
1983 if (string_not_equal(si
->name
, src
->name
)) return 0;
1985 if (item
== rootfs
) return 1;
1987 return _fsi_validate(si
, item
->type
, item
->validation_a
, item
->validation_b
);
1991 file_user_byname(si_mod_t
*si
, const char *name
)
1993 return _fsi_get_user(si
, name
, 0, SEL_NAME
);
1997 file_user_byuid(si_mod_t
*si
, uid_t uid
)
1999 return _fsi_get_user(si
, NULL
, uid
, SEL_NUMBER
);
2003 file_user_all(si_mod_t
*si
)
2005 return _fsi_get_user(si
, NULL
, 0, SEL_ALL
);
2009 file_group_byname(si_mod_t
*si
, const char *name
)
2011 return _fsi_get_group(si
, name
, 0, SEL_NAME
);
2015 file_group_bygid(si_mod_t
*si
, gid_t gid
)
2017 return _fsi_get_group(si
, NULL
, gid
, SEL_NUMBER
);
2021 file_group_all(si_mod_t
*si
)
2023 return _fsi_get_group(si
, NULL
, 0, SEL_ALL
);
2027 file_grouplist(si_mod_t
*si
, const char *name
, __unused
uint32_t ignored
)
2029 return _fsi_get_grouplist(si
, name
);
2033 file_netgroup_byname(si_mod_t
*si
, const char *name
)
2035 si_list_t
*list
= NULL
;
2037 uint64_t va
=0, vb
=0;
2039 file_si_private_t
*pp
;
2041 if (name
== NULL
) return NULL
;
2043 pp
= (file_si_private_t
*)si
->private;
2044 if (pp
== NULL
) return NULL
;
2046 _fsi_check_netgroup_cache(si
);
2048 pthread_mutex_lock(&file_mutex
);
2050 n
= _fsi_find_netgroup(&(pp
->file_netgroup_cache
), name
, 0);
2053 file_netgroup_member_t
*m
= n
->members
;
2056 item
= (si_item_t
*)LI_ils_create("L4488sss", (unsigned long)si
, CATEGORY_NETGROUP
, 1, va
, vb
, m
->host
, m
->user
, m
->domain
);
2057 list
= si_list_add(list
, item
);
2062 pthread_mutex_unlock(&file_mutex
);
2068 file_in_netgroup(si_mod_t
*si
, const char *group
, const char *host
, const char *user
, const char *domain
)
2071 file_netgroup_member_t
*m
;
2072 file_si_private_t
*pp
;
2074 if (group
== NULL
) return 0;
2076 pp
= (file_si_private_t
*)si
->private;
2077 if (pp
== NULL
) return 0;
2079 _fsi_check_netgroup_cache(si
);
2081 pthread_mutex_lock(&file_mutex
);
2083 n
= _fsi_find_netgroup(&(pp
->file_netgroup_cache
), group
, 0);
2086 pthread_mutex_unlock(&file_mutex
);
2093 file_netgroup_member_t
*x
= m
;
2098 if (x
->host
== NULL
) continue;
2099 if (strcmp(host
, x
->host
)) continue;
2104 if (x
->user
== NULL
) continue;
2105 if (strcmp(user
, x
->user
)) continue;
2110 if (x
->domain
== NULL
) continue;
2111 if (strcmp(domain
, x
->domain
)) continue;
2114 pthread_mutex_unlock(&file_mutex
);
2118 pthread_mutex_unlock(&file_mutex
);
2123 file_host_byname(si_mod_t
*si
, const char *name
, int af
, const char *ignored
, uint32_t *err
)
2127 if (err
!= NULL
) *err
= SI_STATUS_NO_ERROR
;
2129 item
= _fsi_get_host(si
, name
, NULL
, af
, SEL_NAME
, err
);
2130 if ((item
== NULL
) && (err
!= NULL
) && (*err
== 0)) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
2136 file_host_byaddr(si_mod_t
*si
, const void *addr
, int af
, const char *ignored
, uint32_t *err
)
2140 if (err
!= NULL
) *err
= SI_STATUS_NO_ERROR
;
2142 item
= _fsi_get_host(si
, NULL
, addr
, af
, SEL_NUMBER
, err
);
2143 if ((item
== NULL
) && (err
!= NULL
) && (*err
== 0)) *err
= SI_STATUS_H_ERRNO_HOST_NOT_FOUND
;
2149 file_host_all(si_mod_t
*si
)
2151 return _fsi_get_host(si
, NULL
, NULL
, 0, SEL_ALL
, NULL
);
2155 file_network_byname(si_mod_t
*si
, const char *name
)
2157 if (name
== NULL
) return NULL
;
2158 return _fsi_get_name_number_aliases(si
, name
, 0, SEL_NAME
, CATEGORY_NETWORK
);
2162 file_network_byaddr(si_mod_t
*si
, uint32_t addr
)
2164 return _fsi_get_name_number_aliases(si
, NULL
, (int)addr
, SEL_NUMBER
, CATEGORY_NETWORK
);
2168 file_network_all(si_mod_t
*si
)
2170 return _fsi_get_name_number_aliases(si
, NULL
, 0, SEL_ALL
, CATEGORY_NETWORK
);
2174 file_service_byname(si_mod_t
*si
, const char *name
, const char *proto
)
2176 return _fsi_get_service(si
, name
, proto
, 0, SEL_NAME
);
2180 file_service_byport(si_mod_t
*si
, int port
, const char *proto
)
2182 return _fsi_get_service(si
, NULL
, proto
, port
, SEL_NUMBER
);
2186 file_service_all(si_mod_t
*si
)
2188 return _fsi_get_service(si
, NULL
, NULL
, 0, SEL_ALL
);
2192 file_protocol_byname(si_mod_t
*si
, const char *name
)
2194 if (name
== NULL
) return NULL
;
2195 return _fsi_get_name_number_aliases(si
, name
, 0, SEL_NAME
, CATEGORY_PROTOCOL
);
2199 file_protocol_bynumber(si_mod_t
*si
, int number
)
2201 return _fsi_get_name_number_aliases(si
, NULL
, number
, SEL_NUMBER
, CATEGORY_PROTOCOL
);
2205 file_protocol_all(si_mod_t
*si
)
2207 return _fsi_get_name_number_aliases(si
, NULL
, 0, SEL_ALL
, CATEGORY_PROTOCOL
);
2211 file_rpc_byname(si_mod_t
*si
, const char *name
)
2213 if (name
== NULL
) return NULL
;
2214 return _fsi_get_name_number_aliases(si
, name
, 0, SEL_NAME
, CATEGORY_RPC
);
2218 file_rpc_bynumber(si_mod_t
*si
, int number
)
2220 return _fsi_get_name_number_aliases(si
, NULL
, number
, SEL_NUMBER
, CATEGORY_RPC
);
2224 file_rpc_all(si_mod_t
*si
)
2226 return _fsi_get_name_number_aliases(si
, NULL
, 0, SEL_ALL
, CATEGORY_RPC
);
2230 file_fs_byspec(si_mod_t
*si
, const char *spec
)
2232 return _fsi_get_fs(si
, spec
, SEL_NAME
);
2236 file_fs_byfile(si_mod_t
*si
, const char *file
)
2238 return _fsi_get_fs(si
, file
, SEL_NUMBER
);
2242 file_fs_all(si_mod_t
*si
)
2244 return _fsi_get_fs(si
, NULL
, SEL_ALL
);
2248 file_alias_byname(si_mod_t
*si
, const char *name
)
2250 return _fsi_get_alias(si
, name
, SEL_NAME
);
2254 file_alias_all(si_mod_t
*si
)
2256 return _fsi_get_alias(si
, NULL
, SEL_ALL
);
2260 file_mac_byname(si_mod_t
*si
, const char *name
)
2262 return _fsi_get_ether(si
, name
, SEL_NAME
);
2266 file_mac_bymac(si_mod_t
*si
, const char *mac
)
2268 return _fsi_get_ether(si
, mac
, SEL_NUMBER
);
2272 file_mac_all(si_mod_t
*si
)
2274 return _fsi_get_ether(si
, NULL
, SEL_ALL
);
2278 file_addrinfo(si_mod_t
*si
, const void *node
, const void *serv
, uint32_t family
, uint32_t socktype
, uint32_t proto
, uint32_t flags
, const char *interface
, uint32_t *err
)
2280 if (err
!= NULL
) *err
= SI_STATUS_NO_ERROR
;
2281 return _gai_simple(si
, node
, serv
, family
, socktype
, proto
, flags
, interface
, err
);
2285 si_module_static_file(void)
2287 static const struct si_mod_vtable_s file_vtable
=
2289 .sim_is_valid
= &file_is_valid
,
2291 .sim_user_byname
= &file_user_byname
,
2292 .sim_user_byuid
= &file_user_byuid
,
2293 .sim_user_byuuid
= NULL
,
2294 .sim_user_all
= &file_user_all
,
2296 .sim_group_byname
= &file_group_byname
,
2297 .sim_group_bygid
= &file_group_bygid
,
2298 .sim_group_byuuid
= NULL
,
2299 .sim_group_all
= &file_group_all
,
2301 .sim_grouplist
= &file_grouplist
,
2303 .sim_netgroup_byname
= &file_netgroup_byname
,
2304 .sim_in_netgroup
= &file_in_netgroup
,
2306 .sim_alias_byname
= &file_alias_byname
,
2307 .sim_alias_all
= &file_alias_all
,
2309 .sim_host_byname
= &file_host_byname
,
2310 .sim_host_byaddr
= &file_host_byaddr
,
2311 .sim_host_all
= &file_host_all
,
2313 .sim_network_byname
= &file_network_byname
,
2314 .sim_network_byaddr
= &file_network_byaddr
,
2315 .sim_network_all
= &file_network_all
,
2317 .sim_service_byname
= &file_service_byname
,
2318 .sim_service_byport
= &file_service_byport
,
2319 .sim_service_all
= &file_service_all
,
2321 .sim_protocol_byname
= &file_protocol_byname
,
2322 .sim_protocol_bynumber
= &file_protocol_bynumber
,
2323 .sim_protocol_all
= &file_protocol_all
,
2325 .sim_rpc_byname
= &file_rpc_byname
,
2326 .sim_rpc_bynumber
= &file_rpc_bynumber
,
2327 .sim_rpc_all
= &file_rpc_all
,
2329 .sim_fs_byspec
= &file_fs_byspec
,
2330 .sim_fs_byfile
= &file_fs_byfile
,
2331 .sim_fs_all
= &file_fs_all
,
2333 .sim_mac_byname
= &file_mac_byname
,
2334 .sim_mac_bymac
= &file_mac_bymac
,
2335 .sim_mac_all
= &file_mac_all
,
2337 .sim_wants_addrinfo
= NULL
,
2338 .sim_addrinfo
= &file_addrinfo
,
2340 /* no nameinfo support */
2341 .sim_nameinfo
= NULL
,
2344 static si_mod_t si
=
2348 .flags
= SI_MOD_FLAG_STATIC
,
2351 .vtable
= &file_vtable
,
2354 static dispatch_once_t once
;
2356 dispatch_once(&once
, ^{
2357 si
.name
= strdup("file");
2358 file_si_private_t
*pp
= calloc(1, sizeof(file_si_private_t
));
2362 for (i
= 0; i
< VALIDATION_COUNT
; i
++) pp
->notify_token
[i
] = -1;
2364 /* hardwired for now, but we may want to make this configurable someday */
2365 pp
->validation_notify_mask
= VALIDATION_MASK_HOSTS
| VALIDATION_MASK_SERVICES
| VALIDATION_MASK_PROTOCOLS
;
2371 return (si_mod_t
*)&si
;