From d08a1d404d8b1fb21494fef2a3aff0022ae6de8b Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Sat, 29 Apr 2006 20:20:05 +0000 Subject: [PATCH] * nis/nisplus-parser.h (_nss_nisplus_parse_pwent): Add entry parameter. (_nss_nisplus_parse_pwent_chk): New prototype. * nis/nss_nisplus/nisplus-parser.c (_nss_nisplus_parse_pwent): Add entry parameter. Use it for column value in all accesses. Move checks for well-formed reply to... (_nss_nisplus_parse_pwent_chk): ...here. New function. * nis/nss_nisplus/nisplus-pwd.c: Support SETENT_BATCH_READ option. --- ChangeLog | 9 ++ nis/nisplus-parser.h | 8 +- nis/nss_nisplus/nisplus-parser.c | 99 ++++++++-------- nis/nss_nisplus/nisplus-pwd.c | 186 ++++++++++++++++++++++++------- 4 files changed, 215 insertions(+), 87 deletions(-) diff --git a/ChangeLog b/ChangeLog index 37feca2897..0b49c6e325 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2006-04-29 Ulrich Drepper + * nis/nisplus-parser.h (_nss_nisplus_parse_pwent): Add entry + parameter. + (_nss_nisplus_parse_pwent_chk): New prototype. + * nis/nss_nisplus/nisplus-parser.c (_nss_nisplus_parse_pwent): + Add entry parameter. Use it for column value in all accesses. + Move checks for well-formed reply to... + (_nss_nisplus_parse_pwent_chk): ...here. New function. + * nis/nss_nisplus/nisplus-pwd.c: Support SETENT_BATCH_READ option. + * nis/nss_nisplus/nisplus-parser.c: Some cleanups. Remove hidden_def definitions. * nis/nisplus-parser.h: Add parameter names. Remove hidden_proto diff --git a/nis/nisplus-parser.h b/nis/nisplus-parser.h index e629119d6f..97580d224a 100644 --- a/nis/nisplus-parser.h +++ b/nis/nisplus-parser.h @@ -24,8 +24,12 @@ #include #include -extern int _nss_nisplus_parse_pwent (nis_result *result, struct passwd *pw, - char *buffer, size_t buflen, int *errnop); +extern int _nss_nisplus_parse_pwent (nis_result *result, size_t entry, + struct passwd *pw, char *buffer, + size_t buflen, int *errnop); +extern int _nss_nisplus_parse_pwent_chk (nis_result *result, struct passwd *pw, + char *buffer, size_t buflen, + int *errnop); extern int _nss_nisplus_parse_grent (nis_result *result, u_long entry, struct group *gr, char *buffer, size_t buflen, int *errnop); diff --git a/nis/nss_nisplus/nisplus-parser.c b/nis/nss_nisplus/nisplus-parser.c index 4cd0738aa2..6fd8f2ef33 100644 --- a/nis/nss_nisplus/nisplus-parser.c +++ b/nis/nss_nisplus/nisplus-parser.c @@ -33,16 +33,9 @@ int -_nss_nisplus_parse_pwent (nis_result *result, struct passwd *pw, - char *buffer, size_t buflen, int *errnop) +_nss_nisplus_parse_pwent_chk (nis_result *result, struct passwd *pw, + char *buffer, size_t buflen, int *errnop) { - char *first_unused = buffer; - size_t room_left = buflen; - size_t len; - - if (result == NULL) - return 0; - if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) || NIS_RES_NUMOBJ (result) != 1 || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ @@ -50,7 +43,19 @@ _nss_nisplus_parse_pwent (nis_result *result, struct passwd *pw, || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 7) return 0; - if (NISENTRYLEN (0, 0, result) >= room_left) + return _nss_nisplus_parse_pwent (result, 0, pw, buffer, buflen, errnop); +} + + +int +_nss_nisplus_parse_pwent (nis_result *result, size_t entry, struct passwd *pw, + char *buffer, size_t buflen, int *errnop) +{ + char *first_unused = buffer; + size_t room_left = buflen; + size_t len; + + if (NISENTRYLEN (entry, 0, result) >= room_left) { /* The line is too long for our buffer. */ no_more_room: @@ -58,81 +63,81 @@ _nss_nisplus_parse_pwent (nis_result *result, struct passwd *pw, return -1; } - strncpy (first_unused, NISENTRYVAL (0, 0, result), - NISENTRYLEN (0, 0, result)); - first_unused[NISENTRYLEN (0, 0, result)] = '\0'; + strncpy (first_unused, NISENTRYVAL (entry, 0, result), + NISENTRYLEN (entry, 0, result)); + first_unused[NISENTRYLEN (entry, 0, result)] = '\0'; len = strlen (first_unused); if (len == 0) /* No name ? Should never happen, database is corrupt */ return 0; pw->pw_name = first_unused; - room_left -= (len + 1); - first_unused += (len + 1); + room_left -= len + 1; + first_unused += len + 1; - if (NISENTRYLEN (0, 1, result) >= room_left) + if (NISENTRYLEN (entry, 1, result) >= room_left) goto no_more_room; - strncpy (first_unused, NISENTRYVAL (0, 1, result), - NISENTRYLEN (0, 1, result)); - first_unused[NISENTRYLEN (0, 1, result)] = '\0'; + strncpy (first_unused, NISENTRYVAL (entry, 1, result), + NISENTRYLEN (entry, 1, result)); + first_unused[NISENTRYLEN (entry, 1, result)] = '\0'; pw->pw_passwd = first_unused; len = strlen (first_unused); - room_left -= (len + 1); - first_unused += (len + 1); + room_left -= len + 1; + first_unused += len + 1; - if (NISENTRYLEN(0, 2, result) >= room_left) + if (NISENTRYLEN (entry, 2, result) >= room_left) goto no_more_room; - strncpy (first_unused, NISENTRYVAL (0, 2, result), - NISENTRYLEN (0, 2, result)); - first_unused[NISENTRYLEN (0, 2, result)] = '\0'; + strncpy (first_unused, NISENTRYVAL (entry, 2, result), + NISENTRYLEN (entry, 2, result)); + first_unused[NISENTRYLEN (entry, 2, result)] = '\0'; len = strlen (first_unused); if (len == 0) /* If we don't have a uid, it's an invalid shadow entry */ return 0; pw->pw_uid = strtoul (first_unused, NULL, 10); - if (NISENTRYLEN (0, 3, result) >= room_left) + if (NISENTRYLEN (entry, 3, result) >= room_left) goto no_more_room; - strncpy (first_unused, NISENTRYVAL (0, 3, result), - NISENTRYLEN (0, 3, result)); - first_unused[NISENTRYLEN (0, 3, result)] = '\0'; + strncpy (first_unused, NISENTRYVAL (entry, 3, result), + NISENTRYLEN (entry, 3, result)); + first_unused[NISENTRYLEN (entry, 3, result)] = '\0'; len = strlen (first_unused); if (len == 0) /* If we don't have a gid, it's an invalid shadow entry */ return 0; pw->pw_gid = strtoul (first_unused, NULL, 10); - if (NISENTRYLEN(0, 4, result) >= room_left) + if (NISENTRYLEN(entry, 4, result) >= room_left) goto no_more_room; - strncpy (first_unused, NISENTRYVAL (0, 4, result), - NISENTRYLEN (0, 4, result)); - first_unused[NISENTRYLEN (0, 4, result)] = '\0'; + strncpy (first_unused, NISENTRYVAL (entry, 4, result), + NISENTRYLEN (entry, 4, result)); + first_unused[NISENTRYLEN (entry, 4, result)] = '\0'; pw->pw_gecos = first_unused; len = strlen (first_unused); - room_left -= (len + 1); - first_unused += (len + 1); + room_left -= len + 1; + first_unused += len + 1; - if (NISENTRYLEN (0, 5, result) >= room_left) + if (NISENTRYLEN (entry, 5, result) >= room_left) goto no_more_room; - strncpy (first_unused, NISENTRYVAL (0, 5, result), - NISENTRYLEN (0, 5, result)); - first_unused[NISENTRYLEN (0, 5, result)] = '\0'; + strncpy (first_unused, NISENTRYVAL (entry, 5, result), + NISENTRYLEN (entry, 5, result)); + first_unused[NISENTRYLEN (entry, 5, result)] = '\0'; pw->pw_dir = first_unused; len = strlen (first_unused); - room_left -= (len + 1); - first_unused += (len + 1); + room_left -= len + 1; + first_unused += len + 1; - if (NISENTRYLEN (0, 6, result) >= room_left) + if (NISENTRYLEN (entry, 6, result) >= room_left) goto no_more_room; - strncpy (first_unused, NISENTRYVAL (0, 6, result), - NISENTRYLEN (0, 6, result)); - first_unused[NISENTRYLEN (0, 6, result)] = '\0'; + strncpy (first_unused, NISENTRYVAL (entry, 6, result), + NISENTRYLEN (entry, 6, result)); + first_unused[NISENTRYLEN (entry, 6, result)] = '\0'; pw->pw_shell = first_unused; len = strlen (first_unused); - room_left -= (len + 1); - first_unused += (len + 1); + room_left -= len + 1; + first_unused += len + 1; return 1; } diff --git a/nis/nss_nisplus/nisplus-pwd.c b/nis/nss_nisplus/nisplus-pwd.c index 6c222ede02..7957e6a27d 100644 --- a/nis/nss_nisplus/nisplus-pwd.c +++ b/nis/nss_nisplus/nisplus-pwd.c @@ -28,10 +28,18 @@ #include "nss-nisplus.h" #include "nisplus-parser.h" +#include + __libc_lock_define_initialized (static, lock) +/* Previous result of iteration. */ static nis_result *result; + +/* All results of batch table load. */ +static nis_result *cached_results; +static size_t cached_results_iter; + nis_name pwd_tablename_val attribute_hidden; size_t pwd_tablename_len attribute_hidden; @@ -69,6 +77,59 @@ _nss_pwd_create_tablename (int *errnop) } +static void +internal_nisplus_endpwent (void) +{ + if (cached_results != NULL) + { + nis_freeresult (cached_results); + cached_results = NULL; + cached_results_iter = 0; + } + + if (result != NULL) + { + nis_freeresult (result); + result = NULL; + } +} + + +static enum nss_status +internal_nisplus_setpwent (int *errnop) +{ + enum nss_status status; + + cached_results = nis_list (pwd_tablename_val, FOLLOW_PATH | FOLLOW_LINKS, + NULL, NULL); + + if (cached_results == NULL) + { + *errnop = errno; + status = NSS_STATUS_TRYAGAIN; + } + else if (__builtin_expect ((status = niserr2nss (cached_results->status)) + != NSS_STATUS_SUCCESS, 0)) + { + nis_freeresult (cached_results); + cached_results = NULL; + } + else if (__builtin_expect (__type_of (NIS_RES_OBJECT (cached_results)) + != NIS_ENTRY_OBJ + || strcmp (NIS_RES_OBJECT (cached_results)->EN_data.en_type, + "passwd_tbl") != 0 + || NIS_RES_OBJECT (cached_results)->EN_data.en_cols.en_cols_len < 7, + 0)) + { + nis_freeresult (cached_results); + cached_results = NULL; + status = NSS_STATUS_NOTFOUND; + } + + return status; +} + + enum nss_status _nss_nisplus_setpwent (int stayopen) { @@ -76,88 +137,135 @@ _nss_nisplus_setpwent (int stayopen) __libc_lock_lock (lock); - if (result != NULL) - { - nis_freeresult (result); - result = NULL; - } + internal_nisplus_endpwent (); if (pwd_tablename_val == NULL) { + // XXX We need to be able to set errno. Pass in new parameter. int err; status = _nss_pwd_create_tablename (&err); } + if (status == NSS_STATUS_SUCCESS + && (_nsl_default_nss () & NSS_FLAG_SETENT_BATCH_READ)) + { + // XXX We need to be able to set errno. Pass in new parameter. + int err; + status = internal_nisplus_setpwent (&err); + } + __libc_lock_unlock (lock); return status; } + enum nss_status _nss_nisplus_endpwent (void) { __libc_lock_lock (lock); - if (result != NULL) - { - nis_freeresult (result); - result = NULL; - } + internal_nisplus_endpwent (); __libc_lock_unlock (lock); return NSS_STATUS_SUCCESS; } + static enum nss_status internal_nisplus_getpwent_r (struct passwd *pw, char *buffer, size_t buflen, int *errnop) { - int parse_res; + int parse_res = -1; + nis_result *saved_res = NULL; /* Get the next entry until we found a correct one. */ do { - nis_result *saved_res; - - if (result == NULL) + if (cached_results != NULL) { - saved_res = NULL; - if (pwd_tablename_val == NULL) - { - enum nss_status status = _nss_pwd_create_tablename (errnop); + handle_batch_read: + /* See whether we reported the last problem. */ + if (cached_results_iter >= NIS_RES_NUMOBJ (cached_results)) + return NSS_STATUS_NOTFOUND; - if (status != NSS_STATUS_SUCCESS) - return status; - } - - result = nis_first_entry (pwd_tablename_val); - if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) - return niserr2nss (result->status); + parse_res = _nss_nisplus_parse_pwent (cached_results, + cached_results_iter, pw, + buffer, buflen, errnop); } else { - saved_res = result; - result = nis_next_entry (pwd_tablename_val, &result->cookie); - if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + if (result == NULL) { - nis_freeresult (saved_res); - return niserr2nss (result->status); + if (pwd_tablename_val == NULL) + { + enum nss_status status = _nss_pwd_create_tablename (errnop); + + if (status != NSS_STATUS_SUCCESS) + return status; + } + + /* Determine whether we should instead read all entries at + once. */ + if (_nsl_default_nss () & NSS_FLAG_SETENT_BATCH_READ) + { + enum nss_status status = internal_nisplus_setpwent (errnop); + + if (status == NSS_STATUS_SUCCESS && cached_results != NULL) + goto handle_batch_read; + } + + saved_res = NULL; + + result = nis_first_entry (pwd_tablename_val); + if (result == NULL) + { + *errnop = errno; + return NSS_STATUS_TRYAGAIN; + } + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + return niserr2nss (result->status); } + else + { + saved_res = result; + result = nis_next_entry (pwd_tablename_val, &result->cookie); + if (result == NULL) + { + *errnop = errno; + return NSS_STATUS_TRYAGAIN; + } + if (niserr2nss (result->status) != NSS_STATUS_SUCCESS) + { + nis_freeresult (saved_res); + return niserr2nss (result->status); + } + } + + parse_res = _nss_nisplus_parse_pwent_chk (result, pw, buffer, + buflen, errnop); } - parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, - buflen, errnop); if (__builtin_expect (parse_res == -1, 0)) { - nis_freeresult (result); - result = saved_res; + if (cached_results == NULL) + { + nis_freeresult (result); + result = saved_res; + } *errnop = ERANGE; return NSS_STATUS_TRYAGAIN; } - if (saved_res) - nis_freeresult (saved_res); + if (cached_results != NULL) + ++cached_results_iter; + else + if (saved_res) + { + nis_freeresult (saved_res); + saved_res = NULL; + } } while (!parse_res); @@ -223,7 +331,8 @@ _nss_nisplus_getpwnam_r (const char *name, struct passwd *pw, return status; } - parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen, errnop); + parse_res = _nss_nisplus_parse_pwent_chk (result, pw, buffer, buflen, + errnop); nis_freeresult (result); @@ -282,7 +391,8 @@ _nss_nisplus_getpwuid_r (const uid_t uid, struct passwd *pw, return status; } - parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen, errnop); + parse_res = _nss_nisplus_parse_pwent_chk (result, pw, buffer, buflen, + errnop); nis_freeresult (result);