xf.li | bfc6e71 | 2025-02-07 01:54:34 -0800 | [diff] [blame^] | 1 | /* Netgroup file parser in nss_files modules. |
| 2 | Copyright (C) 1996-2016 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <ctype.h> |
| 21 | #include <errno.h> |
| 22 | #include <netdb.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdio_ext.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include "nsswitch.h" |
| 28 | #include "netgroup.h" |
| 29 | |
| 30 | #define DATAFILE "/etc/netgroup" |
| 31 | |
| 32 | libnss_files_hidden_proto (_nss_files_endnetgrent) |
| 33 | |
| 34 | #define EXPAND(needed) \ |
| 35 | do \ |
| 36 | { \ |
| 37 | size_t old_cursor = result->cursor - result->data; \ |
| 38 | void *old_data = result->data; \ |
| 39 | \ |
| 40 | result->data_size += 512 > 2 * needed ? 512 : 2 * needed; \ |
| 41 | result->data = realloc (result->data, result->data_size); \ |
| 42 | \ |
| 43 | if (result->data == NULL) \ |
| 44 | { \ |
| 45 | free (old_data); \ |
| 46 | status = NSS_STATUS_UNAVAIL; \ |
| 47 | goto the_end; \ |
| 48 | } \ |
| 49 | \ |
| 50 | result->cursor = result->data + old_cursor; \ |
| 51 | } \ |
| 52 | while (0) |
| 53 | |
| 54 | |
| 55 | enum nss_status |
| 56 | _nss_files_setnetgrent (const char *group, struct __netgrent *result) |
| 57 | { |
| 58 | FILE *fp; |
| 59 | enum nss_status status; |
| 60 | |
| 61 | if (group[0] == '\0') |
| 62 | return NSS_STATUS_UNAVAIL; |
| 63 | |
| 64 | /* Find the netgroups file and open it. */ |
| 65 | fp = fopen (DATAFILE, "rce"); |
| 66 | if (fp == NULL) |
| 67 | status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL; |
| 68 | else |
| 69 | { |
| 70 | /* Read the file line by line and try to find the description |
| 71 | GROUP. We must take care for long lines. */ |
| 72 | char *line = NULL; |
| 73 | size_t line_len = 0; |
| 74 | const ssize_t group_len = strlen (group); |
| 75 | |
| 76 | status = NSS_STATUS_NOTFOUND; |
| 77 | result->cursor = result->data; |
| 78 | |
| 79 | __fsetlocking (fp, FSETLOCKING_BYCALLER); |
| 80 | |
| 81 | while (!feof_unlocked (fp)) |
| 82 | { |
| 83 | ssize_t curlen = getline (&line, &line_len, fp); |
| 84 | int found; |
| 85 | |
| 86 | if (curlen < 0) |
| 87 | { |
| 88 | status = NSS_STATUS_NOTFOUND; |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | found = (curlen > group_len && strncmp (line, group, group_len) == 0 |
| 93 | && isspace (line[group_len])); |
| 94 | |
| 95 | /* Read the whole line (including continuation) and store it |
| 96 | if FOUND in nonzero. Otherwise we don't need it. */ |
| 97 | if (found) |
| 98 | { |
| 99 | /* Store the data from the first line. */ |
| 100 | EXPAND (curlen - group_len); |
| 101 | memcpy (result->cursor, &line[group_len + 1], |
| 102 | curlen - group_len); |
| 103 | result->cursor += (curlen - group_len) - 1; |
| 104 | } |
| 105 | |
| 106 | while (curlen > 1 && line[curlen - 1] == '\n' |
| 107 | && line[curlen - 2] == '\\') |
| 108 | { |
| 109 | /* Yes, we have a continuation line. */ |
| 110 | if (found) |
| 111 | /* Remove these characters from the stored line. */ |
| 112 | result->cursor -= 2; |
| 113 | |
| 114 | /* Get next line. */ |
| 115 | curlen = getline (&line, &line_len, fp); |
| 116 | if (curlen <= 0) |
| 117 | break; |
| 118 | |
| 119 | if (found) |
| 120 | { |
| 121 | /* Make sure we have enough room. */ |
| 122 | EXPAND (1 + curlen + 1); |
| 123 | |
| 124 | /* Add separator in case next line starts immediately. */ |
| 125 | *result->cursor++ = ' '; |
| 126 | |
| 127 | /* Copy new line. */ |
| 128 | memcpy (result->cursor, line, curlen + 1); |
| 129 | result->cursor += curlen; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if (found) |
| 134 | { |
| 135 | /* Now we have read the line. */ |
| 136 | status = NSS_STATUS_SUCCESS; |
| 137 | result->cursor = result->data; |
| 138 | result->first = 1; |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | the_end: |
| 144 | /* We don't need the file and the line buffer anymore. */ |
| 145 | free (line); |
| 146 | fclose (fp); |
| 147 | |
| 148 | if (status != NSS_STATUS_SUCCESS) |
| 149 | _nss_files_endnetgrent (result); |
| 150 | } |
| 151 | |
| 152 | return status; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | enum nss_status |
| 157 | _nss_files_endnetgrent (struct __netgrent *result) |
| 158 | { |
| 159 | /* Free allocated memory for data if some is present. */ |
| 160 | free (result->data); |
| 161 | result->data = NULL; |
| 162 | result->data_size = 0; |
| 163 | result->cursor = NULL; |
| 164 | return NSS_STATUS_SUCCESS; |
| 165 | } |
| 166 | libnss_files_hidden_def (_nss_files_endnetgrent) |
| 167 | |
| 168 | static char * |
| 169 | strip_whitespace (char *str) |
| 170 | { |
| 171 | char *cp = str; |
| 172 | |
| 173 | /* Skip leading spaces. */ |
| 174 | while (isspace (*cp)) |
| 175 | cp++; |
| 176 | |
| 177 | str = cp; |
| 178 | while (*cp != '\0' && ! isspace(*cp)) |
| 179 | cp++; |
| 180 | |
| 181 | /* Null-terminate, stripping off any trailing spaces. */ |
| 182 | *cp = '\0'; |
| 183 | |
| 184 | return *str == '\0' ? NULL : str; |
| 185 | } |
| 186 | |
| 187 | enum nss_status |
| 188 | _nss_netgroup_parseline (char **cursor, struct __netgrent *result, |
| 189 | char *buffer, size_t buflen, int *errnop) |
| 190 | { |
| 191 | enum nss_status status; |
| 192 | const char *host, *user, *domain; |
| 193 | char *cp = *cursor; |
| 194 | |
| 195 | /* Some sanity checks. */ |
| 196 | if (cp == NULL) |
| 197 | return NSS_STATUS_NOTFOUND; |
| 198 | |
| 199 | /* First skip leading spaces. */ |
| 200 | while (isspace (*cp)) |
| 201 | ++cp; |
| 202 | |
| 203 | if (*cp != '(') |
| 204 | { |
| 205 | /* We have a list of other netgroups. */ |
| 206 | char *name = cp; |
| 207 | |
| 208 | while (*cp != '\0' && ! isspace (*cp)) |
| 209 | ++cp; |
| 210 | |
| 211 | if (name != cp) |
| 212 | { |
| 213 | /* It is another netgroup name. */ |
| 214 | int last = *cp == '\0'; |
| 215 | |
| 216 | result->type = group_val; |
| 217 | result->val.group = name; |
| 218 | *cp = '\0'; |
| 219 | if (! last) |
| 220 | ++cp; |
| 221 | *cursor = cp; |
| 222 | result->first = 0; |
| 223 | |
| 224 | return NSS_STATUS_SUCCESS; |
| 225 | } |
| 226 | |
| 227 | return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN; |
| 228 | } |
| 229 | |
| 230 | /* Match host name. */ |
| 231 | host = ++cp; |
| 232 | while (*cp != ',') |
| 233 | if (*cp++ == '\0') |
| 234 | return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN; |
| 235 | |
| 236 | /* Match user name. */ |
| 237 | user = ++cp; |
| 238 | while (*cp != ',') |
| 239 | if (*cp++ == '\0') |
| 240 | return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN; |
| 241 | |
| 242 | /* Match domain name. */ |
| 243 | domain = ++cp; |
| 244 | while (*cp != ')') |
| 245 | if (*cp++ == '\0') |
| 246 | return result->first ? NSS_STATUS_NOTFOUND : NSS_STATUS_RETURN; |
| 247 | ++cp; |
| 248 | |
| 249 | |
| 250 | /* When we got here we have found an entry. Before we can copy it |
| 251 | to the private buffer we have to make sure it is big enough. */ |
| 252 | if (cp - host > buflen) |
| 253 | { |
| 254 | *errnop = ERANGE; |
| 255 | status = NSS_STATUS_TRYAGAIN; |
| 256 | } |
| 257 | else |
| 258 | { |
| 259 | memcpy (buffer, host, cp - host); |
| 260 | result->type = triple_val; |
| 261 | |
| 262 | buffer[(user - host) - 1] = '\0'; /* Replace ',' with '\0'. */ |
| 263 | result->val.triple.host = strip_whitespace (buffer); |
| 264 | |
| 265 | buffer[(domain - host) - 1] = '\0'; /* Replace ',' with '\0'. */ |
| 266 | result->val.triple.user = strip_whitespace (buffer + (user - host)); |
| 267 | |
| 268 | buffer[(cp - host) - 1] = '\0'; /* Replace ')' with '\0'. */ |
| 269 | result->val.triple.domain = strip_whitespace (buffer + (domain - host)); |
| 270 | |
| 271 | status = NSS_STATUS_SUCCESS; |
| 272 | |
| 273 | /* Remember where we stopped reading. */ |
| 274 | *cursor = cp; |
| 275 | |
| 276 | result->first = 0; |
| 277 | } |
| 278 | |
| 279 | return status; |
| 280 | } |
| 281 | libnss_files_hidden_def (_nss_netgroup_parseline) |
| 282 | |
| 283 | |
| 284 | enum nss_status |
| 285 | _nss_files_getnetgrent_r (struct __netgrent *result, char *buffer, |
| 286 | size_t buflen, int *errnop) |
| 287 | { |
| 288 | enum nss_status status; |
| 289 | |
| 290 | status = _nss_netgroup_parseline (&result->cursor, result, buffer, buflen, |
| 291 | errnop); |
| 292 | |
| 293 | return status; |
| 294 | } |