1 | /* Copyright (C) 1996-2021 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <ctype.h> |
20 | #include <dlfcn.h> |
21 | #include <errno.h> |
22 | #include <netdb.h> |
23 | #include <libc-lock.h> |
24 | #include <search.h> |
25 | #include <stdio.h> |
26 | #include <stdio_ext.h> |
27 | #include <stdlib.h> |
28 | #include <string.h> |
29 | |
30 | #include <aliases.h> |
31 | #include <grp.h> |
32 | #include <netinet/ether.h> |
33 | #include <pwd.h> |
34 | #include <shadow.h> |
35 | #include <unistd.h> |
36 | |
37 | #if !defined DO_STATIC_NSS || defined SHARED |
38 | # include <gnu/lib-names.h> |
39 | #endif |
40 | |
41 | #include "nsswitch.h" |
42 | #include "../nscd/nscd_proto.h" |
43 | #include <sysdep.h> |
44 | #include <config.h> |
45 | |
46 | /* Declare external database variables. */ |
47 | #define DEFINE_DATABASE(name) \ |
48 | nss_action_list __nss_##name##_database attribute_hidden; \ |
49 | weak_extern (__nss_##name##_database) |
50 | #include "databases.def" |
51 | #undef DEFINE_DATABASE |
52 | |
53 | |
54 | #ifdef USE_NSCD |
55 | /* Flags whether custom rules for database is set. */ |
56 | bool __nss_database_custom[NSS_DBSIDX_max]; |
57 | #endif |
58 | |
59 | /*__libc_lock_define_initialized (static, lock)*/ |
60 | |
61 | /* -1 == not found |
62 | 0 == function found |
63 | 1 == finished */ |
64 | int |
65 | __nss_lookup (nss_action_list *ni, const char *fct_name, const char *fct2_name, |
66 | void **fctp) |
67 | { |
68 | *fctp = __nss_lookup_function (*ni, fct_name); |
69 | if (*fctp == NULL && fct2_name != NULL) |
70 | *fctp = __nss_lookup_function (*ni, fct2_name); |
71 | |
72 | while (*fctp == NULL |
73 | && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE |
74 | && (*ni)[1].module != NULL) |
75 | { |
76 | ++(*ni); |
77 | |
78 | *fctp = __nss_lookup_function (*ni, fct_name); |
79 | if (*fctp == NULL && fct2_name != NULL) |
80 | *fctp = __nss_lookup_function (*ni, fct2_name); |
81 | } |
82 | |
83 | return *fctp != NULL ? 0 : (*ni)[1].module == NULL ? 1 : -1; |
84 | } |
85 | libc_hidden_def (__nss_lookup) |
86 | |
87 | |
88 | /* -1 == not found |
89 | 0 == adjusted for next function |
90 | 1 == finished */ |
91 | int |
92 | __nss_next2 (nss_action_list *ni, const char *fct_name, const char *fct2_name, |
93 | void **fctp, int status, int all_values) |
94 | { |
95 | if (all_values) |
96 | { |
97 | if (nss_next_action (*ni, NSS_STATUS_TRYAGAIN) == NSS_ACTION_RETURN |
98 | && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_RETURN |
99 | && nss_next_action (*ni, NSS_STATUS_NOTFOUND) == NSS_ACTION_RETURN |
100 | && nss_next_action (*ni, NSS_STATUS_SUCCESS) == NSS_ACTION_RETURN) |
101 | return 1; |
102 | } |
103 | else |
104 | { |
105 | /* This is really only for debugging. */ |
106 | if (__builtin_expect (NSS_STATUS_TRYAGAIN > status |
107 | || status > NSS_STATUS_RETURN, 0)) |
108 | __libc_fatal ("Illegal status in __nss_next.\n" ); |
109 | |
110 | if (nss_next_action (*ni, status) == NSS_ACTION_RETURN) |
111 | return 1; |
112 | } |
113 | |
114 | if ((*ni)[1].module == NULL) |
115 | return -1; |
116 | |
117 | do |
118 | { |
119 | ++(*ni); |
120 | |
121 | *fctp = __nss_lookup_function (*ni, fct_name); |
122 | if (*fctp == NULL && fct2_name != NULL) |
123 | *fctp = __nss_lookup_function (*ni, fct2_name); |
124 | } |
125 | while (*fctp == NULL |
126 | && nss_next_action (*ni, NSS_STATUS_UNAVAIL) == NSS_ACTION_CONTINUE |
127 | && (*ni)[1].module != NULL); |
128 | |
129 | return *fctp != NULL ? 0 : -1; |
130 | } |
131 | libc_hidden_def (__nss_next2) |
132 | |
133 | void * |
134 | __nss_lookup_function (nss_action_list ni, const char *fct_name) |
135 | { |
136 | if (ni->module == NULL) |
137 | return NULL; |
138 | return __nss_module_get_function (ni->module, fct_name); |
139 | } |
140 | libc_hidden_def (__nss_lookup_function) |
141 | |