third_party: Update nss_wrapper to version 1.1.15
[samba4-gss.git] / third_party / nss_wrapper / nss_utils.c
blob1d0a3d049e9fa2dcee866a840b5ea6145d633c76
1 /*
2 * BSD 3-Clause License
4 * Copyright (c) 2007, Stefan Metzmacher <metze@samba.org>
5 * Copyright (c) 2009, Guenther Deschner <gd@samba.org>
6 * Copyright (c) 2014-2015, Michael Adam <obnox@samba.org>
7 * Copyright (c) 2015, Robin Hack <hack.robin@gmail.com>
8 * Copyright (c) 2013-2018, Andreas Schneider <asn@samba.org>
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the author nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
39 #include <errno.h>
40 #include <grp.h>
41 #include <string.h>
42 #include <stdint.h>
44 #include "nss_utils.h"
46 int nwrap_gr_copy_r(const struct group *src, struct group *dst,
47 char *buf, size_t buflen, struct group **dstp)
49 char *p = NULL;
50 uintptr_t align = 0;
51 unsigned int gr_mem_cnt = 0;
52 unsigned i;
53 size_t total_len;
54 size_t gr_name_len = strlen(src->gr_name) + 1;
55 size_t gr_passwd_len = strlen(src->gr_passwd) + 1;
56 union {
57 char *ptr;
58 char **data;
59 } g_mem;
61 for (i = 0; src->gr_mem[i] != NULL; i++) {
62 gr_mem_cnt++;
65 /* Align the memory for storing pointers */
66 align = __alignof__(char *) - ((p - (char *)0) % __alignof__(char *));
67 total_len = align +
68 (1 + gr_mem_cnt) * sizeof(char *) +
69 gr_name_len + gr_passwd_len;
71 if (total_len > buflen) {
72 errno = ERANGE;
73 return -1;
75 buflen -= total_len;
77 /* gr_mem */
78 p = buf + align;
79 g_mem.ptr = p;
80 dst->gr_mem = g_mem.data;
82 /* gr_name */
83 p += (1 + gr_mem_cnt) * sizeof(char *);
84 dst->gr_name = p;
86 /* gr_passwd */
87 p += gr_name_len;
88 dst->gr_passwd = p;
90 /* gr_mem[x] */
91 p += gr_passwd_len;
93 /* gr_gid */
94 dst->gr_gid = src->gr_gid;
96 memcpy(dst->gr_name, src->gr_name, gr_name_len);
98 memcpy(dst->gr_passwd, src->gr_passwd, gr_passwd_len);
100 /* Set the terminating entry */
101 dst->gr_mem[gr_mem_cnt] = NULL;
103 /* Now add the group members content */
104 total_len = 0;
105 for (i = 0; i < gr_mem_cnt; i++) {
106 size_t len = strlen(src->gr_mem[i]) + 1;
108 dst->gr_mem[i] = p;
109 total_len += len;
110 p += len;
113 if (total_len > buflen) {
114 errno = ERANGE;
115 return -1;
118 for (i = 0; i < gr_mem_cnt; i++) {
119 size_t len = strlen(src->gr_mem[i]) + 1;
121 memcpy(dst->gr_mem[i],
122 src->gr_mem[i],
123 len);
126 if (dstp != NULL) {
127 *dstp = dst;
130 return 0;