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>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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
)
51 unsigned int gr_mem_cnt
= 0;
54 size_t gr_name_len
= strlen(src
->gr_name
) + 1;
55 size_t gr_passwd_len
= strlen(src
->gr_passwd
) + 1;
61 for (i
= 0; src
->gr_mem
[i
] != NULL
; i
++) {
65 /* Align the memory for storing pointers */
66 align
= __alignof__(char *) - ((p
- (char *)0) % __alignof__(char *));
68 (1 + gr_mem_cnt
) * sizeof(char *) +
69 gr_name_len
+ gr_passwd_len
;
71 if (total_len
> buflen
) {
80 dst
->gr_mem
= g_mem
.data
;
83 p
+= (1 + gr_mem_cnt
) * sizeof(char *);
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 */
105 for (i
= 0; i
< gr_mem_cnt
; i
++) {
106 size_t len
= strlen(src
->gr_mem
[i
]) + 1;
113 if (total_len
> buflen
) {
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
],