1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 1995 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29
30 #include "mail.h"
31 /*
32 If mail file does not exist create it
33 */
34 #ifdef OLD
createmf(uid,file)35 void createmf(uid, file)
36 uid_t uid;
37 char *file;
38 {
39 int fd;
40
41 void (*istat)(), (*qstat)(), (*hstat)();
42
43 if (access(file, A_EXIST) == CERROR) {
44 istat = signal(SIGINT, SIG_IGN);
45 qstat = signal(SIGQUIT, SIG_IGN);
46 hstat = signal(SIGHUP, SIG_IGN);
47 umask(0);
48 if ((fd = creat(file, MFMODE)) == -1)
49 sav_errno = errno;
50 else
51 close(fd);
52 umask(7);
53 (void) signal(SIGINT, istat);
54 (void) signal(SIGQUIT, qstat);
55 (void) signal(SIGHUP, hstat);
56 }
57 }
58 #else
59
60 #include <sys/stat.h>
61 #include <fcntl.h>
62 #include <stdio.h>
63
accessmf(path)64 int accessmf(path)
65 char *path;
66 {
67
68 struct stat fsb,sb;
69 int mbfd;
70 tryagain:
71 if (lstat(path, &sb)) {
72 /* file/symlink does not exist, so create one */
73 mbfd = open(path,
74 O_APPEND|O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
75 chmod(path, 0660);
76 /* if someone create a symlink/file just ahead */
77 /* of us, the create will failed with EEXIST */
78 /* This is what we want, because we do not */
79 /* want someone to re-direct our "create" */
80 /* request to a another location. */
81 if (mbfd == -1) {
82 if (errno == EEXIST)
83 goto tryagain;
84 }
85
86 /* file/symlink exist, make sure it is not linked */
87 } else if (sb.st_nlink != 1 || S_ISLNK(sb.st_mode)) {
88 fprintf(stderr,
89 "%s: security violation, '%s' should not be linked to other file\n", program, path);
90 sav_errno = errno;
91 return -1;
92 } else {
93 /* if we get here, there is a pre-existing file, */
94 /* and it is not a symlink... */
95 /* open it, and make sure it is the same file */
96 /* we lstat() before... */
97 /* this is to guard against someone deleting the */
98 /* old file and creat a new symlink in its place */
99 /* We are not createing a new file here, but we */
100 /* do not want append to the worng file either */
101 mbfd = open(path, O_APPEND|O_WRONLY, 0);
102 if (mbfd != -1 &&
103 (fstat(mbfd, &fsb) || fsb.st_nlink != 1 ||
104 S_ISLNK(fsb.st_mode) || sb.st_dev != fsb.st_dev ||
105 sb.st_ino != fsb.st_ino)) {
106 /* file changed after open */
107 fprintf(stderr, "%s: security violation, '%s' inode changed after open\n", program, path);
108 (void)close(mbfd);
109 return -1;
110 }
111 }
112
113 if (mbfd == -1) {
114 sav_errno = errno;
115 return -1;
116 }
117
118 return mbfd;
119 }
120 #endif
121