diff options
Diffstat (limited to 'grp')
-rw-r--r-- | grp/Makefile | 5 | ||||
-rw-r--r-- | grp/Versions | 3 | ||||
-rw-r--r-- | grp/getgrgid_r.c | 3 | ||||
-rw-r--r-- | grp/getgrnam_r.c | 4 | ||||
-rw-r--r-- | grp/grp-merge.c | 186 | ||||
-rw-r--r-- | grp/grp-merge.h | 37 |
6 files changed, 236 insertions, 2 deletions
diff --git a/grp/Makefile b/grp/Makefile index 4f1809cea5..b4d52e2928 100644 --- a/grp/Makefile +++ b/grp/Makefile @@ -22,11 +22,12 @@ subdir := grp include ../Makeconfig -headers := grp.h +headers := grp.h grp-merge.h routines := fgetgrent initgroups setgroups \ getgrent getgrgid getgrnam putgrent \ - getgrent_r getgrgid_r getgrnam_r fgetgrent_r + getgrent_r getgrgid_r getgrnam_r fgetgrent_r \ + grp-merge tests := testgrp tst-putgrent diff --git a/grp/Versions b/grp/Versions index e01360da42..096caa47c5 100644 --- a/grp/Versions +++ b/grp/Versions @@ -28,4 +28,7 @@ libc { # g* getgrouplist; } + GLIBC_PRIVATE { + __merge_grp; __copy_grp; + } } diff --git a/grp/getgrgid_r.c b/grp/getgrgid_r.c index 9da834ac98..01d3b99166 100644 --- a/grp/getgrgid_r.c +++ b/grp/getgrgid_r.c @@ -18,6 +18,7 @@ #include <grp.h> +#include <grp-merge.h> #define LOOKUP_TYPE struct group #define FUNCTION_NAME getgrgid @@ -25,5 +26,7 @@ #define ADD_PARAMS gid_t gid #define ADD_VARIABLES gid #define BUFLEN NSS_BUFLEN_GROUP +#define DEEPCOPY_FN __copy_grp +#define MERGE_FN __merge_grp #include <nss/getXXbyYY_r.c> diff --git a/grp/getgrnam_r.c b/grp/getgrnam_r.c index 80960825c0..3e8596222a 100644 --- a/grp/getgrnam_r.c +++ b/grp/getgrnam_r.c @@ -18,6 +18,7 @@ #include <grp.h> +#include <grp-merge.h> #define LOOKUP_TYPE struct group #define FUNCTION_NAME getgrnam @@ -25,4 +26,7 @@ #define ADD_PARAMS const char *name #define ADD_VARIABLES name +#define DEEPCOPY_FN __copy_grp +#define MERGE_FN __merge_grp + #include <nss/getXXbyYY_r.c> diff --git a/grp/grp-merge.c b/grp/grp-merge.c new file mode 100644 index 0000000000..0a1eb38d2c --- /dev/null +++ b/grp/grp-merge.c @@ -0,0 +1,186 @@ +/* Group merging implementation. + Copyright (C) 2016 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <http://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <stdlib.h> +#include <string.h> +#include <grp.h> +#include <grp-merge.h> + +#define BUFCHECK(size) \ + ({ \ + do \ + { \ + if (c + (size) > buflen) \ + { \ + free (members); \ + return ERANGE; \ + } \ + } \ + while (0); \ + }) + +int +internal_function +__copy_grp (const struct group srcgrp, const size_t buflen, + struct group *destgrp, char *destbuf, char **endptr) +{ + size_t i; + size_t c = 0; + size_t len; + size_t memcount; + char **members = NULL; + + /* Copy the GID. */ + destgrp->gr_gid = srcgrp.gr_gid; + + /* Copy the name. */ + len = strlen (srcgrp.gr_name) + 1; + BUFCHECK (len); + memcpy (&destbuf[c], srcgrp.gr_name, len); + destgrp->gr_name = &destbuf[c]; + c += len; + + /* Copy the password. */ + len = strlen (srcgrp.gr_passwd) + 1; + BUFCHECK (len); + memcpy (&destbuf[c], srcgrp.gr_passwd, len); + destgrp->gr_passwd = &destbuf[c]; + c += len; + + /* Count all of the members. */ + for (memcount = 0; srcgrp.gr_mem[memcount]; memcount++) + ; + + /* Allocate a temporary holding area for the pointers to the member + contents, including space for a NULL-terminator. */ + members = malloc (sizeof (char *) * (memcount + 1)); + if (members == NULL) + return ENOMEM; + + /* Copy all of the group members to destbuf and add a pointer to each of + them into the 'members' array. */ + for (i = 0; srcgrp.gr_mem[i]; i++) + { + len = strlen (srcgrp.gr_mem[i]) + 1; + BUFCHECK (len); + memcpy (&destbuf[c], srcgrp.gr_mem[i], len); + members[i] = &destbuf[c]; + c += len; + } + members[i] = NULL; + + /* Copy the pointers from the members array into the buffer and assign them + to the gr_mem member of destgrp. */ + destgrp->gr_mem = (char **) &destbuf[c]; + len = sizeof (char *) * (memcount + 1); + BUFCHECK (len); + memcpy (&destbuf[c], members, len); + c += len; + free (members); + members = NULL; + + /* Save the count of members at the end. */ + BUFCHECK (sizeof (size_t)); + memcpy (&destbuf[c], &memcount, sizeof (size_t)); + c += sizeof (size_t); + + if (endptr) + *endptr = destbuf + c; + return 0; +} +libc_hidden_def (__copy_grp) + +/* Check that the name, GID and passwd fields match, then + copy in the gr_mem array. */ +int +internal_function +__merge_grp (struct group *savedgrp, char *savedbuf, char *savedend, + size_t buflen, struct group *mergegrp, char *mergebuf) +{ + size_t c, i, len; + size_t savedmemcount; + size_t memcount; + size_t membersize; + char **members = NULL; + + /* We only support merging members of groups with identical names and + GID values. If we hit this case, we need to overwrite the current + buffer with the saved one (which is functionally equivalent to + treating the new lookup as NSS_STATUS_NOTFOUND). */ + if (mergegrp->gr_gid != savedgrp->gr_gid + || strcmp (mergegrp->gr_name, savedgrp->gr_name)) + return __copy_grp (*savedgrp, buflen, mergegrp, mergebuf, NULL); + + /* Get the count of group members from the last sizeof (size_t) bytes in the + mergegrp buffer. */ + savedmemcount = (size_t) *(savedend - sizeof (size_t)); + + /* Get the count of new members to add. */ + for (memcount = 0; mergegrp->gr_mem[memcount]; memcount++) + ; + + /* Create a temporary array to hold the pointers to the member values from + both the saved and merge groups. */ + membersize = savedmemcount + memcount + 1; + members = malloc (sizeof (char *) * membersize); + if (members == NULL) + return ENOMEM; + + /* Copy in the existing member pointers from the saved group + Note: this is not NULL-terminated yet. */ + memcpy (members, savedgrp->gr_mem, sizeof (char *) * savedmemcount); + + /* Back up into the savedbuf until we get back to the NULL-terminator of the + group member list. (This means walking back savedmemcount + 1 (char *) pointers + and the member count value. + The value of c is going to be the used length of the buffer backed up by + the member count and further backed up by the size of the pointers. */ + c = savedend - savedbuf + - sizeof (size_t) + - sizeof (char *) * (savedmemcount + 1); + + /* Add all the new group members, overwriting the old NULL-terminator while + adding the new pointers to the temporary array. */ + for (i = 0; mergegrp->gr_mem[i]; i++) + { + len = strlen (mergegrp->gr_mem[i]) + 1; + BUFCHECK (len); + memcpy (&savedbuf[c], mergegrp->gr_mem[i], len); + members[savedmemcount + i] = &savedbuf[c]; + c += len; + } + /* Add the NULL-terminator. */ + members[savedmemcount + memcount] = NULL; + + /* Copy the member array back into the buffer after the member list and free + the member array. */ + savedgrp->gr_mem = (char **) &savedbuf[c]; + len = sizeof (char *) * membersize; + BUFCHECK (len); + memcpy (&savedbuf[c], members, len); + c += len; + + free (members); + members = NULL; + + /* Finally, copy the results back into mergebuf, since that's the buffer + that we were provided by the caller. */ + return __copy_grp (*savedgrp, buflen, mergegrp, mergebuf, NULL); +} +libc_hidden_def (__merge_grp) diff --git a/grp/grp-merge.h b/grp/grp-merge.h new file mode 100644 index 0000000000..969612b34d --- /dev/null +++ b/grp/grp-merge.h @@ -0,0 +1,37 @@ +/* Group merging implementation. + Copyright (C) 2016 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + <http://www.gnu.org/licenses/>. */ + +#ifndef _GRP_MERGE_H +#define _GRP_MERGE_H 1 + +#include <grp.h> + +/* Duplicate a grp struct (and its members). When no longer needed, the + calling function must free(newbuf). */ +int +__copy_grp (const struct group srcgrp, const size_t buflen, + struct group *destgrp, char *destbuf, char **endptr) + internal_function; + +/* Merge the member lists of two grp structs together. */ +int +__merge_grp (struct group *savedgrp, char *savedbuf, char *savedend, + size_t buflen, struct group *mergegrp, char *mergebuf) + internal_function; + +#endif /* _GRP_MERGE_H */ |