| 1 | /* Mail alias file parser in nss_files module. |
| 2 | Copyright (C) 1996-2017 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 <aliases.h> |
| 21 | #include <ctype.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <libc-lock.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #include <kernel-features.h> |
| 30 | |
| 31 | #include "nsswitch.h" |
| 32 | |
| 33 | /* Locks the static variables in this file. */ |
| 34 | __libc_lock_define_initialized (static, lock) |
| 35 | |
| 36 | /* Maintenance of the stream open on the database file. For getXXent |
| 37 | operations the stream needs to be held open across calls, the other |
| 38 | getXXbyYY operations all use their own stream. */ |
| 39 | |
| 40 | static FILE *stream; |
| 41 | |
| 42 | |
| 43 | static enum nss_status |
| 44 | internal_setent (FILE **stream) |
| 45 | { |
| 46 | enum nss_status status = NSS_STATUS_SUCCESS; |
| 47 | |
| 48 | if (*stream == NULL) |
| 49 | { |
| 50 | *stream = fopen ("/etc/aliases" , "rce" ); |
| 51 | |
| 52 | if (*stream == NULL) |
| 53 | status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL; |
| 54 | } |
| 55 | else |
| 56 | rewind (*stream); |
| 57 | |
| 58 | return status; |
| 59 | } |
| 60 | |
| 61 | |
| 62 | /* Thread-safe, exported version of that. */ |
| 63 | enum nss_status |
| 64 | _nss_files_setaliasent (void) |
| 65 | { |
| 66 | enum nss_status status; |
| 67 | |
| 68 | __libc_lock_lock (lock); |
| 69 | |
| 70 | status = internal_setent (&stream); |
| 71 | |
| 72 | __libc_lock_unlock (lock); |
| 73 | |
| 74 | return status; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /* Close the database file. */ |
| 79 | static void |
| 80 | internal_endent (FILE **stream) |
| 81 | { |
| 82 | if (*stream != NULL) |
| 83 | { |
| 84 | fclose (*stream); |
| 85 | *stream = NULL; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /* Thread-safe, exported version of that. */ |
| 91 | enum nss_status |
| 92 | _nss_files_endaliasent (void) |
| 93 | { |
| 94 | __libc_lock_lock (lock); |
| 95 | |
| 96 | internal_endent (&stream); |
| 97 | |
| 98 | __libc_lock_unlock (lock); |
| 99 | |
| 100 | return NSS_STATUS_SUCCESS; |
| 101 | } |
| 102 | |
| 103 | /* Parsing the database file into `struct aliasent' data structures. */ |
| 104 | static enum nss_status |
| 105 | get_next_alias (FILE *stream, const char *match, struct aliasent *result, |
| 106 | char *buffer, size_t buflen, int *errnop) |
| 107 | { |
| 108 | enum nss_status status = NSS_STATUS_NOTFOUND; |
| 109 | int ignore = 0; |
| 110 | |
| 111 | result->alias_members_len = 0; |
| 112 | |
| 113 | while (1) |
| 114 | { |
| 115 | /* Now we are ready to process the input. We have to read a |
| 116 | line and all its continuations and construct the array of |
| 117 | string pointers. This pointers and the names itself have to |
| 118 | be placed in BUFFER. */ |
| 119 | char *first_unused = buffer; |
| 120 | size_t room_left = buflen - (buflen % __alignof__ (char *)); |
| 121 | char *line; |
| 122 | |
| 123 | /* Check whether the buffer is large enough for even trying to |
| 124 | read something. */ |
| 125 | if (room_left < 2) |
| 126 | goto no_more_room; |
| 127 | |
| 128 | /* Read the first line. It must contain the alias name and |
| 129 | possibly some alias names. */ |
| 130 | first_unused[room_left - 1] = '\xff'; |
| 131 | line = fgets_unlocked (first_unused, room_left, stream); |
| 132 | if (line == NULL) |
| 133 | /* Nothing to read. */ |
| 134 | break; |
| 135 | else if (first_unused[room_left - 1] != '\xff') |
| 136 | { |
| 137 | /* The line is too long for our buffer. */ |
| 138 | no_more_room: |
| 139 | *errnop = ERANGE; |
| 140 | status = NSS_STATUS_TRYAGAIN; |
| 141 | break; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | char *cp; |
| 146 | |
| 147 | /* If we are in IGNORE mode and the first character in the |
| 148 | line is a white space we ignore the line and start |
| 149 | reading the next. */ |
| 150 | if (ignore && isspace (*first_unused)) |
| 151 | continue; |
| 152 | |
| 153 | /* Terminate the line for any case. */ |
| 154 | cp = strpbrk (first_unused, "#\n" ); |
| 155 | if (cp != NULL) |
| 156 | *cp = '\0'; |
| 157 | |
| 158 | /* Skip leading blanks. */ |
| 159 | while (isspace (*line)) |
| 160 | ++line; |
| 161 | |
| 162 | result->alias_name = first_unused; |
| 163 | while (*line != '\0' && *line != ':') |
| 164 | *first_unused++ = *line++; |
| 165 | if (*line == '\0' || result->alias_name == first_unused) |
| 166 | /* No valid name. Ignore the line. */ |
| 167 | continue; |
| 168 | |
| 169 | *first_unused++ = '\0'; |
| 170 | if (room_left < (size_t) (first_unused - result->alias_name)) |
| 171 | goto no_more_room; |
| 172 | room_left -= first_unused - result->alias_name; |
| 173 | ++line; |
| 174 | |
| 175 | /* When we search for a specific alias we can avoid all the |
| 176 | difficult parts and compare now with the name we are |
| 177 | looking for. If it does not match we simply ignore all |
| 178 | lines until the next line containing the start of a new |
| 179 | alias is found. */ |
| 180 | ignore = (match != NULL |
| 181 | && __strcasecmp (result->alias_name, match) != 0); |
| 182 | |
| 183 | while (! ignore) |
| 184 | { |
| 185 | while (isspace (*line)) |
| 186 | ++line; |
| 187 | |
| 188 | cp = first_unused; |
| 189 | while (*line != '\0' && *line != ',') |
| 190 | *first_unused++ = *line++; |
| 191 | |
| 192 | if (first_unused != cp) |
| 193 | { |
| 194 | /* OK, we can have a regular entry or an include |
| 195 | request. */ |
| 196 | if (*line != '\0') |
| 197 | ++line; |
| 198 | *first_unused++ = '\0'; |
| 199 | |
| 200 | if (strncmp (cp, ":include:" , 9) != 0) |
| 201 | { |
| 202 | if (room_left < (first_unused - cp) + sizeof (char *)) |
| 203 | goto no_more_room; |
| 204 | room_left -= (first_unused - cp) + sizeof (char *); |
| 205 | |
| 206 | ++result->alias_members_len; |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | /* Oh well, we have to read the addressed file. */ |
| 211 | FILE *listfile; |
| 212 | char *old_line = NULL; |
| 213 | |
| 214 | first_unused = cp; |
| 215 | |
| 216 | listfile = fopen (&cp[9], "rce" ); |
| 217 | /* If the file does not exist we simply ignore |
| 218 | the statement. */ |
| 219 | if (listfile != NULL |
| 220 | && (old_line = strdup (line)) != NULL) |
| 221 | { |
| 222 | while (! feof_unlocked (listfile)) |
| 223 | { |
| 224 | first_unused[room_left - 1] = '\xff'; |
| 225 | line = fgets_unlocked (first_unused, room_left, |
| 226 | listfile); |
| 227 | if (line == NULL) |
| 228 | break; |
| 229 | if (first_unused[room_left - 1] != '\xff') |
| 230 | { |
| 231 | free (old_line); |
| 232 | goto no_more_room; |
| 233 | } |
| 234 | |
| 235 | /* Parse the line. */ |
| 236 | cp = strpbrk (line, "#\n" ); |
| 237 | if (cp != NULL) |
| 238 | *cp = '\0'; |
| 239 | |
| 240 | do |
| 241 | { |
| 242 | while (isspace (*line)) |
| 243 | ++line; |
| 244 | |
| 245 | cp = first_unused; |
| 246 | while (*line != '\0' && *line != ',') |
| 247 | *first_unused++ = *line++; |
| 248 | |
| 249 | if (*line != '\0') |
| 250 | ++line; |
| 251 | |
| 252 | if (first_unused != cp) |
| 253 | { |
| 254 | *first_unused++ = '\0'; |
| 255 | if (room_left < ((first_unused - cp) |
| 256 | + __alignof__ (char *))) |
| 257 | { |
| 258 | free (old_line); |
| 259 | goto no_more_room; |
| 260 | } |
| 261 | room_left -= ((first_unused - cp) |
| 262 | + __alignof__ (char *)); |
| 263 | ++result->alias_members_len; |
| 264 | } |
| 265 | } |
| 266 | while (*line != '\0'); |
| 267 | } |
| 268 | fclose (listfile); |
| 269 | |
| 270 | first_unused[room_left - 1] = '\0'; |
| 271 | strncpy (first_unused, old_line, room_left); |
| 272 | |
| 273 | free (old_line); |
| 274 | line = first_unused; |
| 275 | |
| 276 | if (first_unused[room_left - 1] != '\0') |
| 277 | goto no_more_room; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | if (*line == '\0') |
| 283 | { |
| 284 | /* Get the next line. But we must be careful. We |
| 285 | must not read the whole line at once since it |
| 286 | might belong to the current alias. Simply read |
| 287 | the first character. If it is a white space we |
| 288 | have a continuation line. Otherwise it is the |
| 289 | beginning of a new alias and we can push back the |
| 290 | just read character. */ |
| 291 | int ch; |
| 292 | |
| 293 | ch = fgetc_unlocked (stream); |
| 294 | if (ch == EOF || ch == '\n' || !isspace (ch)) |
| 295 | { |
| 296 | size_t cnt; |
| 297 | |
| 298 | /* Now prepare the return. Provide string |
| 299 | pointers for the currently selected aliases. */ |
| 300 | if (ch != EOF) |
| 301 | ungetc (ch, stream); |
| 302 | |
| 303 | /* Adjust the pointer so it is aligned for |
| 304 | storing pointers. */ |
| 305 | first_unused += __alignof__ (char *) - 1; |
| 306 | first_unused -= ((first_unused - (char *) 0) |
| 307 | % __alignof__ (char *)); |
| 308 | result->alias_members = (char **) first_unused; |
| 309 | |
| 310 | /* Compute addresses of alias entry strings. */ |
| 311 | cp = result->alias_name; |
| 312 | for (cnt = 0; cnt < result->alias_members_len; ++cnt) |
| 313 | { |
| 314 | cp = strchr (cp, '\0') + 1; |
| 315 | result->alias_members[cnt] = cp; |
| 316 | } |
| 317 | |
| 318 | status = (result->alias_members_len == 0 |
| 319 | ? NSS_STATUS_RETURN : NSS_STATUS_SUCCESS); |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | /* The just read character is a white space and so |
| 324 | can be ignored. */ |
| 325 | first_unused[room_left - 1] = '\xff'; |
| 326 | line = fgets_unlocked (first_unused, room_left, stream); |
| 327 | if (first_unused[room_left - 1] != '\xff') |
| 328 | goto no_more_room; |
| 329 | cp = strpbrk (line, "#\n" ); |
| 330 | if (cp != NULL) |
| 331 | *cp = '\0'; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | if (status != NSS_STATUS_NOTFOUND) |
| 337 | /* We read something. In any case break here. */ |
| 338 | break; |
| 339 | } |
| 340 | |
| 341 | return status; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | enum nss_status |
| 346 | _nss_files_getaliasent_r (struct aliasent *result, char *buffer, size_t buflen, |
| 347 | int *errnop) |
| 348 | { |
| 349 | /* Return next entry in host file. */ |
| 350 | enum nss_status status = NSS_STATUS_SUCCESS; |
| 351 | |
| 352 | __libc_lock_lock (lock); |
| 353 | |
| 354 | /* Be prepared that the set*ent function was not called before. */ |
| 355 | if (stream == NULL) |
| 356 | status = internal_setent (&stream); |
| 357 | |
| 358 | if (status == NSS_STATUS_SUCCESS) |
| 359 | { |
| 360 | result->alias_local = 1; |
| 361 | |
| 362 | /* Read lines until we get a definite result. */ |
| 363 | do |
| 364 | status = get_next_alias (stream, NULL, result, buffer, buflen, errnop); |
| 365 | while (status == NSS_STATUS_RETURN); |
| 366 | } |
| 367 | |
| 368 | __libc_lock_unlock (lock); |
| 369 | |
| 370 | return status; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | enum nss_status |
| 375 | _nss_files_getaliasbyname_r (const char *name, struct aliasent *result, |
| 376 | char *buffer, size_t buflen, int *errnop) |
| 377 | { |
| 378 | /* Return next entry in host file. */ |
| 379 | enum nss_status status = NSS_STATUS_SUCCESS; |
| 380 | FILE *stream = NULL; |
| 381 | |
| 382 | if (name == NULL) |
| 383 | { |
| 384 | __set_errno (EINVAL); |
| 385 | return NSS_STATUS_UNAVAIL; |
| 386 | } |
| 387 | |
| 388 | /* Open the stream. */ |
| 389 | status = internal_setent (&stream); |
| 390 | |
| 391 | if (status == NSS_STATUS_SUCCESS) |
| 392 | { |
| 393 | result->alias_local = 1; |
| 394 | |
| 395 | /* Read lines until we get a definite result. */ |
| 396 | do |
| 397 | status = get_next_alias (stream, name, result, buffer, buflen, errnop); |
| 398 | while (status == NSS_STATUS_RETURN); |
| 399 | } |
| 400 | |
| 401 | internal_endent (&stream); |
| 402 | |
| 403 | return status; |
| 404 | } |
| 405 | |