Imported Upstream version 1.10.2
[platform/upstream/krb5.git] / src / lib / krb5 / os / krbfileio.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/os/krbfileio.c */
3 /*
4  * Copyright (c) Hewlett-Packard Company 1991
5  * Released to the Massachusetts Institute of Technology for inclusion
6  * in the Kerberos source code distribution.
7  *
8  * Copyright 1991 by the Massachusetts Institute of Technology.
9  * All Rights Reserved.
10  *
11  * Export of this software from the United States of America may
12  *   require a specific license from the United States Government.
13  *   It is the responsibility of any person or organization contemplating
14  *   export to obtain such a license before exporting.
15  *
16  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
17  * distribute this software and its documentation for any purpose and
18  * without fee is hereby granted, provided that the above copyright
19  * notice appear in all copies and that both that copyright notice and
20  * this permission notice appear in supporting documentation, and that
21  * the name of M.I.T. not be used in advertising or publicity pertaining
22  * to distribution of the software without specific, written prior
23  * permission.  Furthermore if you modify this software you must label
24  * your software as modified software and not distribute it in such a
25  * fashion that it might be confused with the original M.I.T. software.
26  * M.I.T. makes no representations about the suitability of
27  * this software for any purpose.  It is provided "as is" without express
28  * or implied warranty.
29  *
30  *
31  * krb5_create_secure_file
32  * krb5_sync_disk_file
33  */
34
35 #ifdef MODULE_VERSION_ID
36 static char *VersionID = "@(#)krbfileio.c       2 - 08/22/91";
37 #endif
38
39
40 #include "k5-int.h"
41 #ifdef HAVE_SYS_FILE_H
42 #include <sys/file.h>
43 #endif
44 #include <fcntl.h>
45
46 #ifndef O_BINARY
47 #define O_BINARY 0
48 #endif
49
50 #ifdef apollo
51 #   define OPEN_MODE_NOT_TRUSTWORTHY
52 #endif
53
54 krb5_error_code
55 krb5_create_secure_file(krb5_context context, const char *pathname)
56 {
57     int fd;
58
59     /*
60      * Create the file with access restricted to the owner
61      */
62     fd = THREEPARAMOPEN(pathname, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
63
64 #ifdef OPEN_MODE_NOT_TRUSTWORTHY
65     /*
66      * Some systems that support default acl inheritance do not
67      * apply ownership information from the process - force the file
68      * to have the proper info.
69      */
70     if (fd > -1) {
71         uid_t   uid;
72         gid_t   gid;
73
74         uid = getuid();
75         gid = getgid();
76
77         fchown(fd, uid, gid);
78
79         fchmod(fd, 0600);
80     }
81 #endif /* OPEN_MODE_NOT_TRUSTWORTHY */
82
83     if (fd > -1) {
84         close(fd);
85         return 0;
86     } else {
87         return errno;
88     }
89 }
90
91 krb5_error_code
92 krb5_sync_disk_file(krb5_context context, FILE *fp)
93 {
94     fflush(fp);
95 #if !defined(MSDOS_FILESYSTEM)
96     if (fsync(fileno(fp))) {
97         return errno;
98     }
99 #endif
100
101     return 0;
102 }