xref: /openbsd-src/lib/libc/gen/pw_dup.c (revision 47911bd667ac77dc523b8a13ef40b012dbffa741)
1 /*	$OpenBSD: pw_dup.c,v 1.2 2002/04/29 18:43:08 millert Exp $	*/
2 
3 /*
4  * Copyright (c) 2000,2002 Todd C. Miller <Todd.Miller@courtesan.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
21  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char rcsid[] = "$OpenBSD: pw_dup.c,v 1.2 2002/04/29 18:43:08 millert Exp $";
32 #endif /* LIBC_SCCS and not lint */
33 
34 #include <sys/types.h>
35 
36 #include <pwd.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 
41 struct passwd *
42 pw_dup(const struct passwd *pw)
43 {
44 	char		*cp;
45 	size_t		 nsize, psize, csize, gsize, dsize, ssize, total;
46 	struct passwd	*newpw;
47 
48 	/* Allocate in one big chunk for easy freeing */
49 	total = sizeof(struct passwd);
50 	if (pw->pw_name) {
51 		nsize = strlen(pw->pw_name) + 1;
52 		total += nsize;
53 	}
54 	if (pw->pw_passwd) {
55 		psize = strlen(pw->pw_passwd) + 1;
56 		total += psize;
57 	}
58 	if (pw->pw_class) {
59 		csize = strlen(pw->pw_class) + 1;
60 		total += csize;
61 	}
62 	if (pw->pw_gecos) {
63 		gsize = strlen(pw->pw_gecos) + 1;
64 		total += gsize;
65 	}
66 	if (pw->pw_dir) {
67 		dsize = strlen(pw->pw_dir) + 1;
68 		total += dsize;
69 	}
70 	if (pw->pw_shell) {
71 		ssize = strlen(pw->pw_shell) + 1;
72 		total += ssize;
73 	}
74 	if ((cp = malloc(total)) == NULL)
75 		return(NULL);
76 	newpw = (struct passwd *)cp;
77 
78 	/*
79 	 * Copy in passwd contents and make strings relative to space
80 	 * at the end of the buffer.
81 	 */
82 	(void)memcpy(newpw, pw, sizeof(struct passwd));
83 	cp += sizeof(struct passwd);
84 	if (pw->pw_name) {
85 		(void)memcpy(cp, pw->pw_name, nsize);
86 		newpw->pw_name = cp;
87 		cp += nsize;
88 	}
89 	if (pw->pw_passwd) {
90 		(void)memcpy(cp, pw->pw_passwd, psize);
91 		newpw->pw_passwd = cp;
92 		cp += psize;
93 	}
94 	if (pw->pw_class) {
95 		(void)memcpy(cp, pw->pw_class, csize);
96 		newpw->pw_class = cp;
97 		cp += csize;
98 	}
99 	if (pw->pw_gecos) {
100 		(void)memcpy(cp, pw->pw_gecos, gsize);
101 		newpw->pw_gecos = cp;
102 		cp += gsize;
103 	}
104 	if (pw->pw_dir) {
105 		(void)memcpy(cp, pw->pw_dir, dsize);
106 		newpw->pw_dir = cp;
107 		cp += dsize;
108 	}
109 	if (pw->pw_shell) {
110 		(void)memcpy(cp, pw->pw_shell, ssize);
111 		newpw->pw_shell = cp;
112 		cp += ssize;
113 	}
114 
115 	return(newpw);
116 }
117