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