1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate * All rights reserved.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate #ident "%Z%%M% %I% %E% SMI" /* from SVR4 bnu:cpmv.c 2.13 */
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate #include "uucp.h"
33*7c478bd9Sstevel@tonic-gate
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate * copy f1 to f2 locally
36*7c478bd9Sstevel@tonic-gate * f1 -> source file name
37*7c478bd9Sstevel@tonic-gate * f2 -> destination file name
38*7c478bd9Sstevel@tonic-gate * return:
39*7c478bd9Sstevel@tonic-gate * 0 -> ok
40*7c478bd9Sstevel@tonic-gate * FAIL -> failed
41*7c478bd9Sstevel@tonic-gate */
42*7c478bd9Sstevel@tonic-gate
43*7c478bd9Sstevel@tonic-gate static int
xcp(f1,f2)44*7c478bd9Sstevel@tonic-gate xcp(f1, f2)
45*7c478bd9Sstevel@tonic-gate char *f1, *f2;
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate register int fd1, fd2;
48*7c478bd9Sstevel@tonic-gate register int nr, nw;
49*7c478bd9Sstevel@tonic-gate char buf[BUFSIZ];
50*7c478bd9Sstevel@tonic-gate char *temp_p, temp[MAXFULLNAME];
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate if ((fd1 = open(f1, O_RDONLY)) == -1)
53*7c478bd9Sstevel@tonic-gate return (FAIL);
54*7c478bd9Sstevel@tonic-gate
55*7c478bd9Sstevel@tonic-gate if (DIRECTORY(f2)) {
56*7c478bd9Sstevel@tonic-gate (void) strcat(f2, "/");
57*7c478bd9Sstevel@tonic-gate (void) strcat(f2, BASENAME(f1, '/'));
58*7c478bd9Sstevel@tonic-gate }
59*7c478bd9Sstevel@tonic-gate DEBUG(4, "file name is %s\n", f2);
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate (void) strcpy(temp, f2);
62*7c478bd9Sstevel@tonic-gate if ((temp_p = strrchr(temp, '/')) == NULL)
63*7c478bd9Sstevel@tonic-gate temp_p = temp;
64*7c478bd9Sstevel@tonic-gate else
65*7c478bd9Sstevel@tonic-gate temp_p++;
66*7c478bd9Sstevel@tonic-gate (void) strcpy(temp_p, ".TM.XXXXXX");
67*7c478bd9Sstevel@tonic-gate temp_p = temp;
68*7c478bd9Sstevel@tonic-gate DEBUG(4, "temp name is %s\n", temp_p);
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gate if ((fd2 = mkstemp(temp_p)) == -1) {
71*7c478bd9Sstevel@tonic-gate /* open of temp may fail if called from uidxcp() */
72*7c478bd9Sstevel@tonic-gate /* in this case, try f2 since it is pre-created */
73*7c478bd9Sstevel@tonic-gate temp_p = f2;
74*7c478bd9Sstevel@tonic-gate if ((fd2 = open(temp_p, O_CREAT | O_TRUNC | O_WRONLY,
75*7c478bd9Sstevel@tonic-gate PUB_FILEMODE)) == -1) {
76*7c478bd9Sstevel@tonic-gate DEBUG(5, "open of file returned errno %d\n", errno);
77*7c478bd9Sstevel@tonic-gate (void) close(fd1);
78*7c478bd9Sstevel@tonic-gate return (FAIL);
79*7c478bd9Sstevel@tonic-gate }
80*7c478bd9Sstevel@tonic-gate DEBUG(4, "using file name directly.%s\n", "");
81*7c478bd9Sstevel@tonic-gate }
82*7c478bd9Sstevel@tonic-gate (void) chmod(temp_p, PUB_FILEMODE);
83*7c478bd9Sstevel@tonic-gate
84*7c478bd9Sstevel@tonic-gate /* copy, looking for read or write failures */
85*7c478bd9Sstevel@tonic-gate while ((nr = read(fd1, buf, sizeof (buf))) > 0 &&
86*7c478bd9Sstevel@tonic-gate (nw = write(fd2, buf, nr)) == nr)
87*7c478bd9Sstevel@tonic-gate ;
88*7c478bd9Sstevel@tonic-gate
89*7c478bd9Sstevel@tonic-gate close(fd1);
90*7c478bd9Sstevel@tonic-gate close(fd2);
91*7c478bd9Sstevel@tonic-gate
92*7c478bd9Sstevel@tonic-gate if (nr != 0 || nw == -1) {
93*7c478bd9Sstevel@tonic-gate (void) unlink(temp_p);
94*7c478bd9Sstevel@tonic-gate return (FAIL);
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate if (temp_p != f2) {
97*7c478bd9Sstevel@tonic-gate if (rename(temp_p, f2) != 0) {
98*7c478bd9Sstevel@tonic-gate DEBUG(5, "rename failed: errno %d\n", errno);
99*7c478bd9Sstevel@tonic-gate (void) unlink(temp_p);
100*7c478bd9Sstevel@tonic-gate return (FAIL);
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate return (0);
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate
106*7c478bd9Sstevel@tonic-gate
107*7c478bd9Sstevel@tonic-gate /*
108*7c478bd9Sstevel@tonic-gate * move f1 to f2 locally
109*7c478bd9Sstevel@tonic-gate * returns:
110*7c478bd9Sstevel@tonic-gate * 0 -> ok
111*7c478bd9Sstevel@tonic-gate * FAIL -> failed
112*7c478bd9Sstevel@tonic-gate */
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate int
xmv(f1,f2)115*7c478bd9Sstevel@tonic-gate xmv(f1, f2)
116*7c478bd9Sstevel@tonic-gate register char *f1, *f2;
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate register int do_unlink, ret;
119*7c478bd9Sstevel@tonic-gate struct stat sbuf;
120*7c478bd9Sstevel@tonic-gate
121*7c478bd9Sstevel@tonic-gate if (stat(f2, &sbuf) == 0)
122*7c478bd9Sstevel@tonic-gate do_unlink = ((sbuf.st_mode & S_IFMT) == S_IFREG);
123*7c478bd9Sstevel@tonic-gate else
124*7c478bd9Sstevel@tonic-gate do_unlink = 1;
125*7c478bd9Sstevel@tonic-gate
126*7c478bd9Sstevel@tonic-gate if (do_unlink)
127*7c478bd9Sstevel@tonic-gate (void) unlink(f2); /* i'm convinced this is the right */
128*7c478bd9Sstevel@tonic-gate /* thing to do */
129*7c478bd9Sstevel@tonic-gate if ((ret = link(f1, f2)) < 0) {
130*7c478bd9Sstevel@tonic-gate /* copy file */
131*7c478bd9Sstevel@tonic-gate ret = xcp(f1, f2);
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate
134*7c478bd9Sstevel@tonic-gate if (ret == 0)
135*7c478bd9Sstevel@tonic-gate (void) unlink(f1);
136*7c478bd9Sstevel@tonic-gate return (ret);
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate
139*7c478bd9Sstevel@tonic-gate /*
140*7c478bd9Sstevel@tonic-gate * toCorrupt - move file to CORRUPTDIR
141*7c478bd9Sstevel@tonic-gate * return - none
142*7c478bd9Sstevel@tonic-gate */
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate void
toCorrupt(file)145*7c478bd9Sstevel@tonic-gate toCorrupt(file)
146*7c478bd9Sstevel@tonic-gate char *file;
147*7c478bd9Sstevel@tonic-gate {
148*7c478bd9Sstevel@tonic-gate char corrupt[MAXFULLNAME];
149*7c478bd9Sstevel@tonic-gate
150*7c478bd9Sstevel@tonic-gate (void) sprintf(corrupt, "%s/%s", CORRUPTDIR, BASENAME(file, '/'));
151*7c478bd9Sstevel@tonic-gate (void) link(file, corrupt);
152*7c478bd9Sstevel@tonic-gate ASSERT(unlink(file) == 0, Ct_UNLINK, file, errno);
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate
155*7c478bd9Sstevel@tonic-gate /*
156*7c478bd9Sstevel@tonic-gate * append f1 to f2
157*7c478bd9Sstevel@tonic-gate * f1 -> source FILE pointer
158*7c478bd9Sstevel@tonic-gate * f2 -> destination FILE pointer
159*7c478bd9Sstevel@tonic-gate * return:
160*7c478bd9Sstevel@tonic-gate * SUCCESS -> ok
161*7c478bd9Sstevel@tonic-gate * FAIL -> failed
162*7c478bd9Sstevel@tonic-gate *
163*7c478bd9Sstevel@tonic-gate * to avoid confusing mail, turn lines with just "." into "..".
164*7c478bd9Sstevel@tonic-gate */
165*7c478bd9Sstevel@tonic-gate int
xfappend(fp1,fp2)166*7c478bd9Sstevel@tonic-gate xfappend(fp1, fp2)
167*7c478bd9Sstevel@tonic-gate register FILE *fp1, *fp2;
168*7c478bd9Sstevel@tonic-gate {
169*7c478bd9Sstevel@tonic-gate char buf[BUFSIZ];
170*7c478bd9Sstevel@tonic-gate
171*7c478bd9Sstevel@tonic-gate while (fgets(buf, sizeof (buf), fp1) != NULL) {
172*7c478bd9Sstevel@tonic-gate if (buf[0] == '.' && buf[1] == '\n')
173*7c478bd9Sstevel@tonic-gate strcpy(buf, "..\n");
174*7c478bd9Sstevel@tonic-gate fputs(buf, fp2);
175*7c478bd9Sstevel@tonic-gate }
176*7c478bd9Sstevel@tonic-gate
177*7c478bd9Sstevel@tonic-gate return (ferror(fp1) || ferror(fp2) ? FAIL : SUCCESS);
178*7c478bd9Sstevel@tonic-gate }
179*7c478bd9Sstevel@tonic-gate
180*7c478bd9Sstevel@tonic-gate
181*7c478bd9Sstevel@tonic-gate /*
182*7c478bd9Sstevel@tonic-gate * copy f1 to f2 locally under uid of uid argument
183*7c478bd9Sstevel@tonic-gate * f1 -> source file name
184*7c478bd9Sstevel@tonic-gate * f2 -> destination file name
185*7c478bd9Sstevel@tonic-gate * Uid and Euid are global
186*7c478bd9Sstevel@tonic-gate * return:
187*7c478bd9Sstevel@tonic-gate * 0 -> ok
188*7c478bd9Sstevel@tonic-gate * FAIL -> failed
189*7c478bd9Sstevel@tonic-gate * NOTES:
190*7c478bd9Sstevel@tonic-gate * for V7 systems, flip-flop between real and effective uid is
191*7c478bd9Sstevel@tonic-gate * not allowed, so fork must be done. This code will not
192*7c478bd9Sstevel@tonic-gate * work correctly when realuid is root on System 5 because of
193*7c478bd9Sstevel@tonic-gate * a bug in setuid.
194*7c478bd9Sstevel@tonic-gate */
195*7c478bd9Sstevel@tonic-gate
196*7c478bd9Sstevel@tonic-gate int
uidxcp(f1,f2)197*7c478bd9Sstevel@tonic-gate uidxcp(f1, f2)
198*7c478bd9Sstevel@tonic-gate char *f1, *f2;
199*7c478bd9Sstevel@tonic-gate {
200*7c478bd9Sstevel@tonic-gate int status;
201*7c478bd9Sstevel@tonic-gate char full[MAXFULLNAME];
202*7c478bd9Sstevel@tonic-gate
203*7c478bd9Sstevel@tonic-gate (void) strcpy(full, f2);
204*7c478bd9Sstevel@tonic-gate if (DIRECTORY(f2)) {
205*7c478bd9Sstevel@tonic-gate (void) strcat(full, "/");
206*7c478bd9Sstevel@tonic-gate (void) strcat(full, BASENAME(f1, '/'));
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate
209*7c478bd9Sstevel@tonic-gate /* create full owned by uucp */
210*7c478bd9Sstevel@tonic-gate (void) close(creat(full, PUB_FILEMODE));
211*7c478bd9Sstevel@tonic-gate (void) chmod(full, PUB_FILEMODE);
212*7c478bd9Sstevel@tonic-gate
213*7c478bd9Sstevel@tonic-gate /* do file copy as read uid */
214*7c478bd9Sstevel@tonic-gate #ifndef V7
215*7c478bd9Sstevel@tonic-gate (void) setuid(Uid);
216*7c478bd9Sstevel@tonic-gate status = xcp(f1, full);
217*7c478bd9Sstevel@tonic-gate (void) setuid(Euid);
218*7c478bd9Sstevel@tonic-gate return (status);
219*7c478bd9Sstevel@tonic-gate
220*7c478bd9Sstevel@tonic-gate #else /* V7 */
221*7c478bd9Sstevel@tonic-gate
222*7c478bd9Sstevel@tonic-gate if (vfork() == 0) {
223*7c478bd9Sstevel@tonic-gate setuid(Uid);
224*7c478bd9Sstevel@tonic-gate _exit(xcp(f1, full));
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate wait(&status);
227*7c478bd9Sstevel@tonic-gate return (status);
228*7c478bd9Sstevel@tonic-gate #endif
229*7c478bd9Sstevel@tonic-gate }
230*7c478bd9Sstevel@tonic-gate
231*7c478bd9Sstevel@tonic-gate /*
232*7c478bd9Sstevel@tonic-gate * put file in public place
233*7c478bd9Sstevel@tonic-gate * if successful, filename is modified
234*7c478bd9Sstevel@tonic-gate * returns:
235*7c478bd9Sstevel@tonic-gate * 0 -> success
236*7c478bd9Sstevel@tonic-gate * FAIL -> failure
237*7c478bd9Sstevel@tonic-gate */
238*7c478bd9Sstevel@tonic-gate int
putinpub(file,tmp,user)239*7c478bd9Sstevel@tonic-gate putinpub(file, tmp, user)
240*7c478bd9Sstevel@tonic-gate char *file, *user, *tmp;
241*7c478bd9Sstevel@tonic-gate {
242*7c478bd9Sstevel@tonic-gate int status;
243*7c478bd9Sstevel@tonic-gate char fullname[MAXFULLNAME];
244*7c478bd9Sstevel@tonic-gate
245*7c478bd9Sstevel@tonic-gate (void) sprintf(fullname, "%s/%s/", Pubdir, user);
246*7c478bd9Sstevel@tonic-gate if (mkdirs(fullname, PUBMASK) != 0) {
247*7c478bd9Sstevel@tonic-gate /* cannot make directories */
248*7c478bd9Sstevel@tonic-gate return (FAIL);
249*7c478bd9Sstevel@tonic-gate }
250*7c478bd9Sstevel@tonic-gate (void) strcat(fullname, BASENAME(file, '/'));
251*7c478bd9Sstevel@tonic-gate status = xmv(tmp, fullname);
252*7c478bd9Sstevel@tonic-gate if (status == 0) {
253*7c478bd9Sstevel@tonic-gate (void) strcpy(file, fullname);
254*7c478bd9Sstevel@tonic-gate (void) chmod(fullname, PUB_FILEMODE);
255*7c478bd9Sstevel@tonic-gate }
256*7c478bd9Sstevel@tonic-gate return (status);
257*7c478bd9Sstevel@tonic-gate }
258