]> git.saurik.com Git - apple/security.git/blob - SecurityServer/MacYarrow/YarrowServer/entropyFileOS9.c
Security-29.tar.gz
[apple/security.git] / SecurityServer / MacYarrow / YarrowServer / entropyFileOS9.c
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /*
20 File: entropyFile.c
21
22 Contains: Module to maintain MacYarrow's entropy file.
23
24 Written by: Doug Mitchell
25
26 Copyright: (c) 2000 by Apple Computer, Inc., all rights reserved.
27
28 Change History (most recent first):
29
30 02/29/00 dpm Created.
31
32 */
33
34 #include "entropyFile.h"
35 #include "debug.h"
36 #include <Files.h>
37 #include <Folders.h>
38 #include <Errors.h>
39 #include <Script.h> // for smSystemScript
40
41 /*
42 * FIXME - for debugging, we put the entropy file the current user's
43 * preferences folder. For the real thing, we should either put it in
44 * System preferences or use UNIX I/O to specify some other path.
45 */
46 #ifdef DEBUG
47 #define ENTROPY_FOLDER kPreferencesFolderType
48 #else
49 #define ENTROPY_FOLDER kSystemPreferencesFolderType
50 #endif
51 #define ENTROPY_FILE_NAME "\pSystem Entropy"
52 #define ENTROPY_FILE_CREATOR 'yarw'
53 #define ENTROPY_FILE_TYPE 'ENTR'
54
55 /*
56 * Open/create entropy file. fnfErr returned if doCreate is false and
57 * the file doesn't exist.
58 */
59 static OSErr openEntropyFile(
60 Boolean doCreate,
61 Boolean writeAccess, // required if doCreate true
62 short *refNum) // RETURNED
63 {
64 FSSpec fsp;
65 OSErr ortn;
66 short vRefNum;
67 long dirID;
68 SInt8 perm;
69
70 if(doCreate && !writeAccess) {
71 return paramErr;
72 }
73 *refNum = 0;
74 ortn = FindFolder(kOnSystemDisk,
75 ENTROPY_FOLDER,
76 kDontCreateFolder,
77 &vRefNum,
78 &dirID);
79 if(ortn) {
80 errorLog1("openEntropyFile: FindFolder returned %d\n", (int)ortn);
81 return ioErr;
82 }
83 ortn = FSMakeFSSpec(vRefNum, dirID, ENTROPY_FILE_NAME, &fsp);
84 switch(ortn) {
85 case noErr:
86 break;
87 case fnfErr:
88 if(!doCreate) {
89 return fnfErr;
90 }
91 else {
92 break;
93 }
94 default:
95 errorLog1("openEntropyFile: FSMakeFSSpec returned %d\n", (int)ortn);
96 return ioErr;
97 }
98
99 if(doCreate && (ortn == fnfErr)) {
100 /* create it */
101 ortn = FSpCreate(&fsp,
102 ENTROPY_FILE_CREATOR,
103 ENTROPY_FILE_TYPE,
104 smSystemScript);
105 if(ortn) {
106 errorLog1("openEntropyFile: FSpCreate returned %d\n", (int)ortn);
107 return ortn;
108 }
109
110 /* fixme - set FInfo.fdFlags.kIsInvisible? */
111 }
112
113 /* open it in any case */
114 perm = (writeAccess ? fsRdWrPerm : fsRdPerm);
115 ortn = FSpOpenDF(&fsp, perm, refNum);
116 if(ortn) {
117 errorLog1("openEntropyFile: FSpOpenDF returned %d\n", (int)ortn);
118 }
119 return ortn;
120 }
121
122 /*
123 * Write specified data to entropy file. A new file will be created
124 * if none exists. Data will be appended to possible existing data
125 * if append is true, otherwise the file's data is replaced with
126 * caller's data.
127 */
128 OSErr writeEntropyFile(
129 UInt8 *bytes,
130 UInt32 numBytes,
131 Boolean append)
132 {
133 OSErr ortn;
134 short refNum;
135 long eof;
136 long actLength = numBytes;
137
138 ortn = openEntropyFile(true, true, &refNum);
139 if(ortn) {
140 return ortn;
141 }
142 if(append) {
143 ortn = GetEOF(refNum, &eof);
144 if(ortn) {
145 goto done;
146 }
147 }
148 else {
149 /* truncate to 0 */
150 ortn = SetEOF(refNum, 0);
151 if(ortn) {
152 goto done;
153 }
154 eof = 0;
155 }
156 ortn = SetFPos(refNum, fsFromStart, eof);
157 if(ortn) {
158 goto done;
159 }
160 ortn = FSWrite(refNum, &actLength, bytes);
161 if((ortn == noErr) && (actLength != numBytes)) {
162 errorLog0("writeEntropyFile: short write\n");
163 }
164 done:
165 FSClose(refNum);
166 return ortn;
167 }
168
169 /*
170 * Read data from entropy file.
171 */
172 OSErr readEntropyFile(
173 UInt8 *bytes,
174 UInt32 numBytes, // max # of bytes to read
175 UInt32 *actualBytes) // RETURNED - number of bytes actually read
176 {
177 OSErr ortn;
178 short refNum;
179 long actLength = numBytes;
180
181 ortn = openEntropyFile(false, false, &refNum);
182 if(ortn) {
183 return ortn;
184 }
185 ortn = FSRead(refNum, &actLength, bytes);
186 *actualBytes = actLength;
187 FSClose(refNum);
188 return ortn;
189 }