Merge illumos-gate
[unleashed.git] / usr / src / lib / passwdutil / utils.c
blob1d70e82d2909150b7598d0143478ef02ff49ca7c
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/types.h>
27 #include <sys/time.h>
28 #include <string.h>
29 #include <thread.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <crypt.h>
33 #include <pwd.h>
34 #include <shadow.h>
36 #include <deflt.h>
38 #include "passwdutil.h"
40 #define PWADMIN "/etc/default/passwd"
42 #define MINWEEKS -1
43 #define MAXWEEKS -1
44 #define WARNWEEKS -1
46 extern repops_t files_repops, nis_repops, ldap_repops, nss_repops;
48 repops_t *rops[REP_LAST+1] = {
49 NULL,
50 &files_repops,
51 &nis_repops,
52 NULL,
53 &ldap_repops,
54 NULL,
55 NULL,
56 NULL,
57 &nss_repops,
60 void
61 free_pwd(struct passwd *pw)
63 free(pw->pw_name);
64 free(pw->pw_passwd);
65 free(pw->pw_gecos);
66 free(pw->pw_dir);
67 free(pw->pw_shell);
68 free(pw);
71 void
72 free_spwd(struct spwd *spw)
74 free(spw->sp_namp);
75 free(spw->sp_pwdp);
76 free(spw);
79 int
80 dup_pw(struct passwd **d, struct passwd *s)
82 if (s == NULL) {
83 *d = NULL;
84 return (PWU_NOT_FOUND);
86 if ((*d = calloc(1, sizeof (**d))) == NULL)
87 return (PWU_NOMEM);
89 if (s->pw_name) {
90 if (((*d)->pw_name = strdup(s->pw_name)) == NULL)
91 goto no_mem;
93 if (s->pw_passwd) {
94 if (((*d)->pw_passwd = strdup(s->pw_passwd)) == NULL)
95 goto no_mem;
97 (*d)->pw_uid = s->pw_uid;
98 (*d)->pw_gid = s->pw_gid;
100 if (s->pw_gecos) {
101 if (((*d)->pw_gecos = strdup(s->pw_gecos)) == NULL)
102 goto no_mem;
104 if (s->pw_dir) {
105 if (((*d)->pw_dir = strdup(s->pw_dir)) == NULL)
106 goto no_mem;
108 if (s->pw_shell) {
109 if (((*d)->pw_shell = strdup(s->pw_shell)) == NULL)
110 goto no_mem;
113 return (PWU_SUCCESS);
115 no_mem:
116 free_pwd(*d);
117 *d = NULL;
118 return (PWU_NOMEM);
122 dup_spw(struct spwd **d, struct spwd *s)
124 if (s == NULL) {
125 *d = NULL;
126 return (PWU_NOT_FOUND);
128 if ((*d = calloc(1, sizeof (**d))) == NULL)
129 return (PWU_NOMEM);
131 **d = *s;
133 if (s->sp_namp)
134 if (((*d)->sp_namp = strdup(s->sp_namp)) == NULL)
135 goto no_mem;
136 if (s->sp_pwdp)
137 if (((*d)->sp_pwdp = strdup(s->sp_pwdp)) == NULL)
138 goto no_mem;
139 return (PWU_SUCCESS);
141 no_mem:
142 free_spwd(*d);
143 return (PWU_NOMEM);
147 * read a value from the defaults file, and return it if it is
148 * a positive integer. If the value is not defined, or negative,
149 * return the supplied default value
152 def_getuint(char *name, int defvalue, void *defp)
154 char *p;
155 int val = -1; /* -1 is a guard to catch undefined values */
157 if ((p = defread_r(name, defp)) != NULL)
158 val = atoi(p);
160 return (val >= 0 ? val : defvalue);
163 void
164 turn_on_default_aging(struct spwd *spw)
166 int minweeks;
167 int maxweeks;
168 int warnweeks;
169 void *defp;
171 if ((defp = defopen_r(PWADMIN)) == NULL) {
172 minweeks = MINWEEKS;
173 maxweeks = MAXWEEKS;
174 warnweeks = WARNWEEKS;
175 } else {
176 minweeks = def_getuint("MINWEEKS=", MINWEEKS, defp);
177 maxweeks = def_getuint("MAXWEEKS=", MAXWEEKS, defp);
178 warnweeks = def_getuint("WARNWEEKS=", WARNWEEKS, defp);
179 defclose_r(defp);
183 * The values specified in /etc/default/passwd are interpreted
184 * in a specific way. Special cases are
185 * MINWEEKS==0 (results in sp_min = -1)
186 * MAXWEEKS==0 (results in sp_max = default)
188 spw->sp_min = 7 * minweeks;
189 if (spw->sp_min <= 0)
190 spw->sp_min = -1;
192 spw->sp_max = 7 * maxweeks;
193 if (spw->sp_max == 0)
194 spw->sp_max = 7 * MAXWEEKS;
195 if (spw->sp_max < 0)
196 spw->sp_max = -1;
198 spw->sp_warn = 7 * warnweeks;
199 if (spw->sp_warn <= 0)
200 spw->sp_warn = -1;
204 * open and read a value from the defaults file,
205 * return value found or default value if not found.
208 def_getint(char *name, int defvalue)
210 int val;
211 void *defp;
213 if ((defp = defopen_r(PWADMIN)) == NULL) {
214 val = defvalue;
215 } else {
216 val = def_getuint(name, defvalue, defp);
217 defclose_r(defp);
220 return (val);