Annotation of embedaddon/sudo/compat/pw_dup.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (c) 2000, 2002, 2012 Todd C. Miller <Todd.Miller@courtesan.com>
                      3:  *
                      4:  * Permission to use, copy, modify, and distribute this software for any
                      5:  * purpose with or without fee is hereby granted, provided that the above
                      6:  * copyright notice and this permission notice appear in all copies.
                      7:  *
                      8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                      9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     15:  *
                     16:  * Sponsored in part by the Defense Advanced Research Projects
                     17:  * Agency (DARPA) and Air Force Research Laboratory, Air Force
                     18:  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
                     19:  */
                     20: 
                     21: #include <config.h>
                     22: 
                     23: #include <sys/types.h>
                     24: 
                     25: #include <stdio.h>
                     26: #ifdef STDC_HEADERS
                     27: # include <stdlib.h>
                     28: # include <stddef.h>
                     29: #else
                     30: # ifdef HAVE_STDLIB_H
                     31: #  include <stdlib.h>
                     32: # endif
                     33: #endif /* STDC_HEADERS */
                     34: #ifdef HAVE_STRING_H
                     35: # include <string.h>
                     36: #endif /* HAVE_STRING_H */
                     37: #ifdef HAVE_STRINGS_H
                     38: # include <strings.h>
                     39: #endif /* HAVE_STRINGS_H */
                     40: #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
                     41: # include <malloc.h>
                     42: #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
                     43: #include <pwd.h>
                     44: 
                     45: #define PW_SIZE(name, size)                            \
                     46: do {                                                   \
                     47:        if (pw->name) {                                 \
                     48:                size = strlen(pw->name) + 1;            \
                     49:                total += size;                          \
                     50:        }                                               \
                     51: } while (0)
                     52: 
                     53: #define PW_COPY(name, size)                            \
                     54: do {                                                   \
                     55:        if (pw->name) {                                 \
                     56:                (void)memcpy(cp, pw->name, size);       \
                     57:                newpw->name = cp;                       \
                     58:                cp += size;                             \
                     59:        }                                               \
                     60: } while (0)
                     61: 
                     62: struct passwd *
                     63: pw_dup(const struct passwd *pw)
                     64: {
                     65:        size_t nsize = 0, psize = 0, gsize = 0, dsize = 0, ssize = 0, total;
                     66: #ifdef HAVE_LOGIN_CAP_H
                     67:        size_t csize;
                     68: #endif
                     69:        struct passwd *newpw;
                     70:        char *cp;
                     71: 
                     72:        /* Allocate in one big chunk for easy freeing */
                     73:        total = sizeof(struct passwd);
                     74:        PW_SIZE(pw_name, nsize);
                     75:        PW_SIZE(pw_passwd, psize);
                     76: #ifdef HAVE_LOGIN_CAP_H
                     77:        PW_SIZE(pw_class, csize);
                     78: #endif
                     79:        PW_SIZE(pw_gecos, gsize);
                     80:        PW_SIZE(pw_dir, dsize);
                     81:        PW_SIZE(pw_shell, ssize);
                     82: 
                     83:        if ((cp = malloc(total)) == NULL)
                     84:                return NULL;
                     85:        newpw = (struct passwd *)cp;
                     86: 
                     87:        /*
                     88:         * Copy in passwd contents and make strings relative to space
                     89:         * at the end of the buffer.
                     90:         */
                     91:        (void)memcpy(newpw, pw, sizeof(struct passwd));
                     92:        cp += sizeof(struct passwd);
                     93: 
                     94:        PW_COPY(pw_name, nsize);
                     95:        PW_COPY(pw_passwd, psize);
                     96: #ifdef HAVE_LOGIN_CAP_H
                     97:        PW_COPY(pw_class, csize);
                     98: #endif
                     99:        PW_COPY(pw_gecos, gsize);
                    100:        PW_COPY(pw_dir, dsize);
                    101:        PW_COPY(pw_shell, ssize);
                    102: 
                    103:        return newpw;
                    104: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>