17c478bd9Sstevel@tonic-gate /* 2*159d09a2SMark Phalan * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 3505d05c7Sgtb * Use is subject to license terms. 47c478bd9Sstevel@tonic-gate */ 57c478bd9Sstevel@tonic-gate 67c478bd9Sstevel@tonic-gate 77c478bd9Sstevel@tonic-gate 87c478bd9Sstevel@tonic-gate #include "k5-int.h" 97c478bd9Sstevel@tonic-gate #include <sys/file.h> 107c478bd9Sstevel@tonic-gate #include <fcntl.h> 117c478bd9Sstevel@tonic-gate 127c478bd9Sstevel@tonic-gate #ifndef O_BINARY 13*159d09a2SMark Phalan #define O_BINARY 0 147c478bd9Sstevel@tonic-gate #endif 157c478bd9Sstevel@tonic-gate 167c478bd9Sstevel@tonic-gate krb5_error_code krb5_create_secure_file(krb5_context context,const char * pathname)17505d05c7Sgtbkrb5_create_secure_file(krb5_context context, const char *pathname) 187c478bd9Sstevel@tonic-gate { 197c478bd9Sstevel@tonic-gate int fd; 207c478bd9Sstevel@tonic-gate int open_flag; 217c478bd9Sstevel@tonic-gate 227c478bd9Sstevel@tonic-gate open_flag = O_CREAT|O_EXCL|O_TRUNC|O_RDWR; 237c478bd9Sstevel@tonic-gate 247c478bd9Sstevel@tonic-gate /* 257c478bd9Sstevel@tonic-gate * Make sure file name is reserved. 267c478bd9Sstevel@tonic-gate * The O_BINARY flag is not a supported flag in the Solaris 277c478bd9Sstevel@tonic-gate * open(2) system call, but it is included here to be consistent 287c478bd9Sstevel@tonic-gate * with other open calls in the Kerberos library code. 297c478bd9Sstevel@tonic-gate */ 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate fd = open(pathname, open_flag | O_BINARY, 0600); 327c478bd9Sstevel@tonic-gate if (fd == -1) { 337c478bd9Sstevel@tonic-gate return (errno); 347c478bd9Sstevel@tonic-gate } else { 357c478bd9Sstevel@tonic-gate close(fd); 367c478bd9Sstevel@tonic-gate return (0); 377c478bd9Sstevel@tonic-gate } 387c478bd9Sstevel@tonic-gate } 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate krb5_error_code krb5_sync_disk_file(krb5_context context,FILE * fp)41505d05c7Sgtbkrb5_sync_disk_file(krb5_context context, FILE *fp) 427c478bd9Sstevel@tonic-gate { 437c478bd9Sstevel@tonic-gate if (fp == NULL) { 447c478bd9Sstevel@tonic-gate (void) fclose(fp); 457c478bd9Sstevel@tonic-gate return (errno); 467c478bd9Sstevel@tonic-gate } 477c478bd9Sstevel@tonic-gate if ((fflush(fp) == EOF) || ferror(fp) || (fsync(fileno(fp)) == -1)) { 487c478bd9Sstevel@tonic-gate return (errno); 497c478bd9Sstevel@tonic-gate } 507c478bd9Sstevel@tonic-gate return (0); 517c478bd9Sstevel@tonic-gate } 52