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

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

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