diff options
author | Ulrich Drepper <drepper@redhat.com> | 2006-04-29 01:14:07 +0000 |
---|---|---|
committer | Ulrich Drepper <drepper@redhat.com> | 2006-04-29 01:14:07 +0000 |
commit | e0d4e91d2b0e8069fc044899cf04a41db3612ffb (patch) | |
tree | 6a3f542af192521f75572d7f2cf239ed68478288 /nis/nss_nis/nis-rpc.c | |
parent | 4eaa9bb48b319803544909a085651fc6ff4a9ea1 (diff) | |
download | glibc-e0d4e91d2b0e8069fc044899cf04a41db3612ffb.tar.gz glibc-e0d4e91d2b0e8069fc044899cf04a41db3612ffb.tar.xz glibc-e0d4e91d2b0e8069fc044899cf04a41db3612ffb.zip |
* nis/nss_nis/nis-spwd.c (internal_nis_getspent_r): Remove data
variable. * nis/nss-nis.h: Define response_t and intern_t. Declare _nis_saveit. * nis/nss_nis/nis-pwd.c: Remove response_t and intern_t definition. (saveit): Renamed to _nis_saveit. Take parameter which is pointer to the intern_t object. Change all users. * nis/nss_nis/nis-grp.c: Remove response_t, intern_t, and saveit definition. Use _nis_saveit instead of saveit. * nis/nss_nis/nis-service.c: Likewise. * nis/nss_nis/nis-initgroups.c: Likewise. (internal_setgrent): Adjust for buffer handling. (internal_getgrent_r): Likewise. * nis/nss_nis/nis-rpc.c: Likewise.
Diffstat (limited to 'nis/nss_nis/nis-rpc.c')
-rw-r--r-- | nis/nss_nis/nis-rpc.c | 114 |
1 files changed, 55 insertions, 59 deletions
diff --git a/nis/nss_nis/nis-rpc.c b/nis/nss_nis/nis-rpc.c index 162f3572bf..e7049ffa9f 100644 --- a/nis/nss_nis/nis-rpc.c +++ b/nis/nss_nis/nis-rpc.c @@ -36,59 +36,22 @@ __libc_lock_define_initialized (static, lock) -struct response_t -{ - struct response_t *next; - char val[0]; -}; - -struct intern_t -{ - struct response_t *start; - struct response_t *next; -}; -typedef struct intern_t intern_t; - -static intern_t intern = {NULL, NULL}; +static intern_t intern; -static int -saveit (int instatus, char *inkey, int inkeylen, char *inval, - int invallen, char *indata) -{ - intern_t *intern = (intern_t *) indata; - - if (instatus != YP_TRUE) - return 1; - - if (inkey && inkeylen > 0 && inval && invallen > 0) - { - struct response_t *newp = malloc (sizeof (struct response_t) - + invallen + 1); - if (newp == NULL) - return 1; /* We have no error code for out of memory */ - - if (intern->start == NULL) - intern->start = newp; - else - intern->next->next = newp; - intern->next = newp; - - newp->next = NULL; - *((char *) mempcpy (newp->val, inval, invallen)) = '\0'; - } - - return 0; -} static void internal_nis_endrpcent (intern_t *intern) { - while (intern->start != NULL) + struct response_t *curr = intern->next; + + while (curr != NULL) { - intern->next = intern->start; - intern->start = intern->start->next; - free (intern->next); + struct response_t *last = curr; + curr = curr->next; + free (last); } + + intern->next = intern->start = NULL; } static enum nss_status @@ -103,10 +66,16 @@ internal_nis_setrpcent (intern_t *intern) internal_nis_endrpcent (intern); - ypcb.foreach = saveit; - ypcb.data = (char *)intern; - status = yperr2nss (yp_all(domainname, "rpc.bynumber", &ypcb)); + ypcb.foreach = _nis_saveit; + ypcb.data = (char *) intern; + status = yperr2nss (yp_all (domainname, "rpc.bynumber", &ypcb)); + + /* Mark the last buffer as full. */ + if (intern->next != NULL) + intern->next->size = intern->offset; + intern->next = intern->start; + intern->offset = 0; return status; } @@ -139,29 +108,56 @@ _nss_nis_endrpcent (void) static enum nss_status internal_nis_getrpcent_r (struct rpcent *rpc, char *buffer, size_t buflen, - int *errnop, intern_t *data) + int *errnop, intern_t *intern) { struct parser_data *pdata = (void *) buffer; int parse_res; char *p; - if (data->start == NULL) - internal_nis_setrpcent (data); + if (intern->start == NULL) + internal_nis_setrpcent (intern); /* Get the next entry until we found a correct one. */ do { - if (data->next == NULL) - return NSS_STATUS_NOTFOUND; + struct response_t *bucket = intern->next; + + if (__builtin_expect (intern->offset >= bucket->size, 0)) + { + if (bucket->next == NULL) + return NSS_STATUS_NOTFOUND; + + /* We look at all the content in the current bucket. Go on + to the next. */ + bucket = intern->next = bucket->next; + intern->offset = 0; + } + + for (p = &bucket->mem[intern->offset]; isspace (*p); ++p) + ++intern->offset; - p = strncpy (buffer, data->next->val, buflen); - while (isspace (*p)) - ++p; + size_t len = strlen (p) + 1; + if (__builtin_expect (len > buflen, 0)) + { + *errnop = ERANGE; + return NSS_STATUS_TRYAGAIN; + } + + /* We unfortunately have to copy the data in the user-provided + buffer because that buffer might be around for a very long + time and the servent structure must remain valid. If we would + rely on the BUCKET memory the next 'setservent' or 'endservent' + call would destroy it. + + The important thing is that it is a single NUL-terminated + string. This is what the parsing routine expects. */ + p = memcpy (buffer, &bucket->mem[intern->offset], len); parse_res = _nss_files_parse_rpcent (p, rpc, pdata, buflen, errnop); if (__builtin_expect (parse_res == -1, 0)) return NSS_STATUS_TRYAGAIN; - data->next = data->next->next; + + intern->offset += len; } while (!parse_res); @@ -193,7 +189,7 @@ _nss_nis_getrpcbyname_r (const char *name, struct rpcent *rpc, return NSS_STATUS_UNAVAIL; } - intern_t data = { NULL, NULL }; + intern_t data = { NULL, NULL, 0 }; enum nss_status status = internal_nis_setrpcent (&data); if (__builtin_expect (status != NSS_STATUS_SUCCESS, 0)) return status; |