]> git.saurik.com Git - apple/system_cmds.git/blame - chpass.tproj/edit.c
system_cmds-175.tar.gz
[apple/system_cmds.git] / chpass.tproj / edit.c
CommitLineData
1815bff5
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*-
25 * Copyright (c) 1990, 1993, 1994
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 */
56
57#include <sys/param.h>
58#include <sys/stat.h>
59
60#include <ctype.h>
61#include <err.h>
62#include <errno.h>
63#include <paths.h>
64#include <pwd.h>
65#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>
68#include <unistd.h>
69
70#include <pw_scan.h>
71#include <pw_util.h>
72
73#include "chpass.h"
74
75extern char *tempname;
76
77void
78edit(pw)
79 struct passwd *pw;
80{
81 struct stat begin, end;
82
83 for (;;) {
84 if (stat(tempname, &begin))
85 pw_error(tempname, 1, 1);
86 pw_edit(1);
87 if (stat(tempname, &end))
88 pw_error(tempname, 1, 1);
89 if (begin.st_mtime == end.st_mtime) {
90 warnx("no changes made");
91 pw_error(NULL, 0, 0);
92 }
93 if (verify(pw))
94 break;
95 pw_prompt();
96 }
97}
98
99/*
100 * display --
101 * print out the file for the user to edit; strange side-effect:
102 * set conditional flag if the user gets to edit the shell.
103 */
104void
105display(fd, pw)
106 int fd;
107 struct passwd *pw;
108{
109 FILE *fp;
110 char *bp, *p, *ttoa();
111
112 if (!(fp = fdopen(fd, "w")))
113 pw_error(tempname, 1, 1);
114
115 (void)fprintf(fp,
116 "#Changing user database information for %s.\n", pw->pw_name);
117 if (!uid) {
118 (void)fprintf(fp, "Login: %s\n", pw->pw_name);
119 (void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
120 (void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
121 (void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
122 (void)fprintf(fp, "Change [month day year]: %s\n",
123 ttoa(pw->pw_change));
124 (void)fprintf(fp, "Expire [month day year]: %s\n",
125 ttoa(pw->pw_expire));
126 (void)fprintf(fp, "Class: %s\n", pw->pw_class);
127 (void)fprintf(fp, "Home directory: %s\n", pw->pw_dir);
128 (void)fprintf(fp, "Shell: %s\n",
129 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
130 }
131 /* Only admin can change "restricted" shells. */
132 else if (ok_shell(pw->pw_shell))
133 /*
134 * Make shell a restricted field. Ugly with a
135 * necklace, but there's not much else to do.
136 */
137 (void)fprintf(fp, "Shell: %s\n",
138 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
139 else
140 list[E_SHELL].restricted = 1;
141 bp = pw->pw_gecos;
142 p = strsep(&bp, ",");
143 (void)fprintf(fp, "Full Name: %s\n", p ? p : "");
144 p = strsep(&bp, ",");
145 (void)fprintf(fp, "Location: %s\n", p ? p : "");
146 p = strsep(&bp, ",");
147 (void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
148 p = strsep(&bp, ",");
149 (void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
150
151 (void)fchown(fd, getuid(), getgid());
152 (void)fclose(fp);
153}
154
155int
156verify(pw)
157 struct passwd *pw;
158{
159 ENTRY *ep;
160 char *p;
161 struct stat sb;
162 FILE *fp;
163 int len;
164 char buf[LINE_MAX];
165
166 if (!(fp = fopen(tempname, "r")))
167 pw_error(tempname, 1, 1);
168 if (fstat(fileno(fp), &sb))
169 pw_error(tempname, 1, 1);
170 if (sb.st_size == 0) {
171 warnx("corrupted temporary file");
172 goto bad;
173 }
174 while (fgets(buf, sizeof(buf), fp)) {
175 if (!buf[0] || buf[0] == '#')
176 continue;
177 if (!(p = strchr(buf, '\n'))) {
178 warnx("line too long");
179 goto bad;
180 }
181 *p = '\0';
182 for (ep = list;; ++ep) {
183 if (!ep->prompt) {
184 warnx("unrecognized field");
185 goto bad;
186 }
187 if (!strncasecmp(buf, ep->prompt, ep->len)) {
188 if (ep->restricted && uid) {
189 warnx(
190 "you may not change the %s field",
191 ep->prompt);
192 goto bad;
193 }
194 if (!(p = strchr(buf, ':'))) {
195 warnx("line corrupted");
196 goto bad;
197 }
198 while (isspace(*++p));
199 if (ep->except && strpbrk(p, ep->except)) {
200 warnx(
201 "illegal character in the \"%s\" field",
202 ep->prompt);
203 goto bad;
204 }
205 if ((ep->func)(p, pw, ep)) {
206bad: (void)fclose(fp);
207 return (0);
208 }
209 break;
210 }
211 }
212 }
213 (void)fclose(fp);
214
215 /* Build the gecos field. */
216 len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
217 strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
218 if (!(p = malloc(len)))
219 err(1, NULL);
220 (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save,
221 list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
222
223 if (snprintf(buf, sizeof(buf),
224 "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
225 pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
226 pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir,
227 pw->pw_shell) >= sizeof(buf)) {
228 warnx("entries too long");
229 return (0);
230 }
231 return (pw_scan(buf, pw));
232}