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