1 /*
2 * Copyright (c) 2000,2002 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 TODD C. MILLER DISCLAIMS ALL
9 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
11 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 /*
18 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
19 *
20 * Permission to use, copy, modify, and distribute this software for any
21 * purpose with or without fee is hereby granted, provided that the above
22 * copyright notice and this permission notice appear in all copies.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
25 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
27 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
28 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
29 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
30 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 */
32
33 #include "config.h"
34
35 #include <sys/param.h>
36
37 #if !defined(OpenBSD) || OpenBSD < 200105
38
39 #include <pwd.h>
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
43
44 #include "funcs.h"
45 #include "globals.h"
46
47 struct passwd *
48 pw_dup(const struct passwd *pw) {
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
49 char *cp;
50 size_t nsize=0, psize=0, gsize=0, dsize=0, ssize=0, total;
51 struct passwd *newpw;
52
53 /* Allocate in one big chunk for easy freeing */
54 total = sizeof(struct passwd);
55 if (pw->pw_name) {
56 nsize = strlen(pw->pw_name) + 1;
57 total += nsize;
58 }
59 if (pw->pw_passwd) {
60 psize = strlen(pw->pw_passwd) + 1;
61 total += psize;
62 }
63 #ifdef LOGIN_CAP
64 if (pw->pw_class) {
65 csize = strlen(pw->pw_class) + 1;
66 total += csize;
67 }
68 #endif /* LOGIN_CAP */
69 if (pw->pw_gecos) {
70 gsize = strlen(pw->pw_gecos) + 1;
71 total += gsize;
72 }
73 if (pw->pw_dir) {
74 dsize = strlen(pw->pw_dir) + 1;
75 total += dsize;
76 }
77 if (pw->pw_shell) {
78 ssize = strlen(pw->pw_shell) + 1;
79 total += ssize;
80 }
81 if ((cp = malloc(total)) == NULL)
82 return (NULL);
83 newpw = (struct passwd *)cp;
84
85 /*
86 * Copy in passwd contents and make strings relative to space
87 * at the end of the buffer.
88 */
89 (void)memcpy(newpw, pw, sizeof(struct passwd));
90 cp += sizeof(struct passwd);
91 if (pw->pw_name) {
92 (void)memcpy(cp, pw->pw_name, nsize);
93 newpw->pw_name = cp;
94 cp += nsize;
95 }
96 if (pw->pw_passwd) {
97 (void)memcpy(cp, pw->pw_passwd, psize);
98 newpw->pw_passwd = cp;
99 cp += psize;
100 }
101 #ifdef LOGIN_CAP
102 if (pw->pw_class) {
103 (void)memcpy(cp, pw->pw_class, csize);
104 newpw->pw_class = cp;
105 cp += csize;
106 }
107 #endif /* LOGIN_CAP */
108 if (pw->pw_gecos) {
109 (void)memcpy(cp, pw->pw_gecos, gsize);
110 newpw->pw_gecos = cp;
111 cp += gsize;
112 }
113 if (pw->pw_dir) {
114 (void)memcpy(cp, pw->pw_dir, dsize);
115 newpw->pw_dir = cp;
116 cp += dsize;
117 }
118 if (pw->pw_shell) {
119 (void)memcpy(cp, pw->pw_shell, ssize);
120 newpw->pw_shell = cp;
121 cp += ssize;
122 }
123
124 return (newpw);
125 }
126
127 #endif /* !OpenBSD || OpenBSD < 200105 */