Windows global stuff:
[krb5.git] / src / lib / krb5 / os / krbfileio.c
1 /*
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.  M.I.T. makes no representations about the suitability of
24  * this software for any purpose.  It is provided "as is" without express
25  * or implied warranty.
26  * 
27  *
28  * krb5_create_secure_file
29  * krb5_sync_disk_file
30  */
31
32 #ifdef MODULE_VERSION_ID
33 static char *VersionID = "@(#)krbfileio.c       2 - 08/22/91";
34 #endif
35
36 #define NEED_LOWLEVEL_IO                        /* Need open(), etc. */
37
38 #include "k5-int.h"
39 #ifdef HAVE_SYS_FILE_H
40 #include <sys/file.h>
41 #endif
42 #ifdef NEED_SYS_FCNTL_H
43 #include <sys/fcntl.h>
44 #endif
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(context, pathname)
56     krb5_context context;
57     const char * pathname;
58 {
59     int fd;
60
61     /*
62      * Create the file with access restricted to the owner
63      */
64     fd = open(pathname, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0600);
65
66 #ifdef OPEN_MODE_NOT_TRUSTWORTHY
67     /*
68      * Some systems that support default acl inheritance do not 
69      * apply ownership information from the process - force the file
70      * to have the proper info.
71      */
72     if (fd > -1) {
73         uid_t   uid;
74         gid_t   gid;
75
76         uid = getuid();
77         gid = getgid();
78
79         fchown(fd, uid, gid);
80
81         fchmod(fd, 0600);
82     }
83 #endif /* OPEN_MODE_NOT_TRUSTWORTHY */
84
85     if (fd > -1) {
86         close(fd);
87         return 0;
88     } else {
89         return errno;
90     }
91 }
92
93 krb5_error_code
94 krb5_sync_disk_file(context, fp)
95     krb5_context context;
96     FILE *fp;
97 {
98     fflush(fp);
99 #ifndef MSDOS_FILESYSTEM
100     if (fsync(fileno(fp))) {
101         return errno;
102     }
103 #endif
104
105     return 0;
106 }
107