1 /* Parse a service line from nsswitch.conf.
2 Copyright (c) 1996-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
25 /* Staging area during parsing. */
26 #define DYNARRAY_STRUCT action_list
27 #define DYNARRAY_ELEMENT struct nss_action
28 #define DYNARRAY_PREFIX action_list_
29 #include <malloc/dynarray-skeleton.c>
31 /* Skip whitespace in line[]. */
33 while (line[0] != '\0' && isspace (line[0])) \
36 /* Read the source names:
37 `( <source> ( "[" "!"? (<status> "=" <action> )+ "]" )? )*'
40 nss_action_parse (const char *line
, struct action_list
*result
)
46 /* No more sources specified. */
49 /* Read <source> identifier. */
50 const char *name
= line
;
51 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '[')
56 struct nss_action new_service
57 = { .module
= __nss_module_allocate (name
, line
- name
), };
58 if (new_service
.module
== NULL
)
60 /* Memory allocation error. */
61 action_list_mark_failed (result
);
64 nss_action_set_all (&new_service
, NSS_ACTION_CONTINUE
);
65 nss_action_set (&new_service
, NSS_STATUS_SUCCESS
, NSS_ACTION_RETURN
);
66 nss_action_set (&new_service
, NSS_STATUS_RETURN
, NSS_ACTION_RETURN
);
72 /* Read criterions. */
81 enum nss_status status
;
82 lookup_actions action
;
84 /* Grok ! before name to mean all statuses but that one. */
89 /* Read status name. */
91 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
95 /* Compare with known statuses. */
98 if (__strncasecmp (name
, "SUCCESS", 7) == 0)
99 status
= NSS_STATUS_SUCCESS
;
100 else if (__strncasecmp (name
, "UNAVAIL", 7) == 0)
101 status
= NSS_STATUS_UNAVAIL
;
105 else if (line
- name
== 8)
107 if (__strncasecmp (name
, "NOTFOUND", 8) == 0)
108 status
= NSS_STATUS_NOTFOUND
;
109 else if (__strncasecmp (name
, "TRYAGAIN", 8) == 0)
110 status
= NSS_STATUS_TRYAGAIN
;
125 while (line
[0] != '\0' && !isspace (line
[0]) && line
[0] != '='
129 if (line
- name
== 6 && __strncasecmp (name
, "RETURN", 6) == 0)
130 action
= NSS_ACTION_RETURN
;
131 else if (line
- name
== 8
132 && __strncasecmp (name
, "CONTINUE", 8) == 0)
133 action
= NSS_ACTION_CONTINUE
;
134 else if (line
- name
== 5
135 && __strncasecmp (name
, "MERGE", 5) == 0)
136 action
= NSS_ACTION_MERGE
;
142 /* Save the current action setting for this status,
143 set them all to the given action, and reset this one. */
144 const lookup_actions save
145 = nss_action_get (&new_service
, status
);
146 nss_action_set_all (&new_service
, action
);
147 nss_action_set (&new_service
, status
, save
);
150 nss_action_set (&new_service
, status
, action
);
154 while (line
[0] != ']');
160 action_list_add (result
, new_service
);
165 __nss_action_parse (const char *line
)
167 struct action_list list
;
168 action_list_init (&list
);
169 if (nss_action_parse (line
, &list
))
172 struct nss_action null_service
173 = { .module
= NULL
, };
175 action_list_add (&list
, null_service
);
176 size
= action_list_size (&list
);
177 return __nss_action_allocate (action_list_begin (&list
), size
);
179 else if (action_list_has_failed (&list
))
181 /* Memory allocation error. */
182 __set_errno (ENOMEM
);
188 __set_errno (EINVAL
);