|
1 /* |
|
2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Server implementation fo share files in application private gage |
|
15 * with controlled way |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 |
|
21 #include <e32std.h> |
|
22 #include <f32file.h> |
|
23 #include <e32math.h> |
|
24 #include "ftokenserver.h" |
|
25 #include "ftokenconsts.h" |
|
26 |
|
27 void CFsTokenServer::NewLC() |
|
28 { |
|
29 CFsTokenServer* pS = new (ELeave) CFsTokenServer; |
|
30 CleanupStack::PushL(pS); |
|
31 pS->ConstructL(); |
|
32 } |
|
33 |
|
34 void CFsTokenServer::ConstructL() |
|
35 { |
|
36 User::LeaveIfError( iFs.Connect() ); |
|
37 User::LeaveIfError( iFs.ShareProtected() ); |
|
38 StartL(FTOKEN_SERVER_NAME); |
|
39 } |
|
40 |
|
41 CSession2* CFsTokenServer::NewSessionL(const TVersion& /*aVersion*/,const RMessage2& /*aMessage*/) const |
|
42 { |
|
43 return new(ELeave) CFsTokenServerSession(); |
|
44 } |
|
45 |
|
46 void CFsTokenServer::HandleClientExit(const CSession2* aClient) |
|
47 { // Todo: We could invent somekind notify here to notify |
|
48 // possible user of a file to release its handle as well.. |
|
49 for(TInt index = iTokens.Find(*aClient, TToken::CompareOwner); |
|
50 index != KErrNotFound; |
|
51 index = iTokens.Find(*aClient, TToken::CompareOwner) ) |
|
52 { |
|
53 if (iTokens[index].iHasSubToken == 0) |
|
54 { |
|
55 delete iTokens[index].iFileName; |
|
56 } |
|
57 iTokens.Remove(index); |
|
58 } |
|
59 } |
|
60 |
|
61 CFsTokenServer::~CFsTokenServer() |
|
62 { |
|
63 for(TInt count = iTokens.Count(); count > 0; count-- ) |
|
64 { |
|
65 if (iTokens[0].iHasSubToken == 0) |
|
66 { |
|
67 delete iTokens[0].iFileName; |
|
68 } |
|
69 iTokens.Remove(0); |
|
70 } |
|
71 |
|
72 iFs.Close(); |
|
73 iTokens.Close(); |
|
74 } |
|
75 |
|
76 void CFsTokenServer::AddTokenL(const RMessage2& aMessage) |
|
77 { |
|
78 HBufC* fileName = 0; |
|
79 TKeyArrayFix tokenKey(_FOFF(TToken, iToken), ECmpTInt64); |
|
80 |
|
81 TInt function = aMessage.Function(); |
|
82 switch(aMessage.Function()) |
|
83 { |
|
84 case EGenerateTokenBasedOnName: |
|
85 //================================================== |
|
86 // 1) filename |
|
87 //=================================================== |
|
88 { |
|
89 User::Leave(KErrNotSupported); |
|
90 /* TInt msgLenght = aMessage.GetDesLength(1); |
|
91 if (length > 0) |
|
92 { |
|
93 fileName = HBufC::NewLC(length); |
|
94 TPtr ptr = fileName->Des(); |
|
95 aMessage.Read(1,ptr); |
|
96 if (iFs.IsValidName(ptr)) |
|
97 { |
|
98 |
|
99 } |
|
100 else |
|
101 { |
|
102 User::Leave(KErrArgument); |
|
103 } |
|
104 } |
|
105 else |
|
106 { |
|
107 User::Leave(KErrArgument); |
|
108 } |
|
109 */ |
|
110 break; |
|
111 } |
|
112 case EGenerateTokenBasedOnOpenHandle: |
|
113 //=================================================== |
|
114 // 2) handle |
|
115 //=================================================== |
|
116 { |
|
117 // 2.1) check that we are able to open the file |
|
118 RFile file; |
|
119 User::LeaveIfError(file.AdoptFromClient(aMessage,2,3)); |
|
120 // 2.2) resolve full filename |
|
121 TFileName fn; |
|
122 file.FullName(fn); |
|
123 file.Close(); |
|
124 |
|
125 fileName = fn.AllocLC(); |
|
126 break; |
|
127 } |
|
128 case EGenerateTokenBasedOnAnotherToken: |
|
129 // ================================================== |
|
130 // 3) token |
|
131 //=================================================== |
|
132 { |
|
133 TInt64 token = 0; |
|
134 TPckg<TInt64> buf(token); |
|
135 |
|
136 // 3.1) just check that client uid and token match |
|
137 aMessage.ReadL(1,buf); |
|
138 TInt index = iTokens.Find(token, TToken::CompareToken); |
|
139 User::LeaveIfError( index ); |
|
140 |
|
141 if (iTokens[index].iPolicy.CheckPolicy(aMessage)) |
|
142 { |
|
143 // set a pointer to existing buffer, when deleting |
|
144 // this must be handled slightly different way |
|
145 // but theres no need to spend lots of mem |
|
146 // for duplicate filenames |
|
147 fileName = iTokens[index].iFileName; |
|
148 |
|
149 // there shouldn't be more than one subtoken per token ever |
|
150 ASSERT(iTokens[index].iHasSubToken==EFalse); |
|
151 iTokens[index].iHasSubToken = ETrue; |
|
152 } |
|
153 else |
|
154 { |
|
155 User::Leave(KErrPermissionDenied); |
|
156 } |
|
157 |
|
158 break; |
|
159 } |
|
160 default: |
|
161 break; |
|
162 } |
|
163 // add token to array |
|
164 TInt64 newToken = User::NTickCount(); |
|
165 |
|
166 for(newToken = Math::FRand(newToken); |
|
167 iTokens.Find(newToken, TToken::CompareToken) != KErrNotFound; |
|
168 newToken = User::NTickCount()) |
|
169 { |
|
170 newToken = Math::FRand(newToken); |
|
171 } |
|
172 |
|
173 TToken entry; |
|
174 entry.iToken = newToken; |
|
175 entry.iFileName = fileName; |
|
176 entry.iOwner = aMessage.Session(); |
|
177 |
|
178 TPckg<TSecurityPolicy> policyBuf(entry.iPolicy); |
|
179 aMessage.ReadL(0, policyBuf); |
|
180 |
|
181 User::LeaveIfError(iTokens.Append(entry)); |
|
182 if (function == EGenerateTokenBasedOnOpenHandle) |
|
183 { |
|
184 CleanupStack::Pop(); // filename |
|
185 } |
|
186 } |
|
187 |
|
188 void CFsTokenServer::OpenFileL(const RMessage2& aMessage) |
|
189 { |
|
190 // find token based on client uid and given token |
|
191 TInt64 token = 0; |
|
192 TPckg<TInt64> buf(token); |
|
193 aMessage.ReadL(1,buf); |
|
194 |
|
195 TInt index = iTokens.Find(token, TToken::CompareToken); |
|
196 User::LeaveIfError(index); |
|
197 |
|
198 if (iTokens[index].iPolicy.CheckPolicy(aMessage)) |
|
199 { // authorized client requests file |
|
200 RFile file; |
|
201 // Todo: what should the mode be ?? |
|
202 User::LeaveIfError(file.Open(iFs, *(iTokens[index].iFileName), EFileShareReadersOnly)); |
|
203 CleanupClosePushL(file); |
|
204 User::LeaveIfError(file.TransferToClient(aMessage, 0)); |
|
205 CleanupStack::PopAndDestroy(); //file |
|
206 } |
|
207 else |
|
208 { |
|
209 User::Leave(KErrPermissionDenied); |
|
210 } |
|
211 } |
|
212 |
|
213 |
|
214 CFsTokenServerSession::CFsTokenServerSession() |
|
215 { |
|
216 } |
|
217 |
|
218 CFsTokenServerSession::~CFsTokenServerSession() |
|
219 { |
|
220 ((CFsTokenServer*)(Server()))->HandleClientExit(this); |
|
221 } |
|
222 |
|
223 void CFsTokenServerSession::ServiceL(const RMessage2& aMessage) |
|
224 { |
|
225 CFsTokenServer* server = (CFsTokenServer*)( Server() ); |
|
226 switch(aMessage.Function()) |
|
227 { |
|
228 case EGenerateTokenBasedOnName: |
|
229 case EGenerateTokenBasedOnOpenHandle: |
|
230 case EGenerateTokenBasedOnAnotherToken: |
|
231 { |
|
232 server->AddTokenL(aMessage); |
|
233 break; |
|
234 } |
|
235 case EOpenHandleForToken: |
|
236 { |
|
237 server->OpenFileL(aMessage); |
|
238 // Todo: We could set somekind penalty to client that continuously |
|
239 // passes wrong tokens (i.e. seems to be guessing), e.g. panic it... |
|
240 break; |
|
241 } |
|
242 |
|
243 default: |
|
244 aMessage.Complete(KErrNotSupported); |
|
245 break; |
|
246 } |
|
247 if (!aMessage.IsNull()) |
|
248 { |
|
249 aMessage.Complete(KErrNone); |
|
250 } |
|
251 } |
|
252 |
|
253 static void RunServerL() |
|
254 { |
|
255 // Create Scheduler, obsolete nowdays ? |
|
256 CActiveScheduler* s=new(ELeave) CActiveScheduler; |
|
257 CleanupStack::PushL(s); |
|
258 CActiveScheduler::Install(s); |
|
259 |
|
260 CFsTokenServer::NewLC(); |
|
261 |
|
262 RProcess::Rendezvous(KErrNone); |
|
263 |
|
264 CActiveScheduler::Start(); |
|
265 |
|
266 CleanupStack::PopAndDestroy(2); |
|
267 } |
|
268 |
|
269 |
|
270 TInt E32Main() |
|
271 { |
|
272 TInt err = User::RenameThread(FTOKEN_SERVER_NAME); |
|
273 if ( err != KErrNone ) |
|
274 { |
|
275 return err; |
|
276 } |
|
277 |
|
278 // create CCleanup |
|
279 CTrapCleanup * cleanup = CTrapCleanup::New(); |
|
280 if (cleanup) |
|
281 { |
|
282 TRAP(err, RunServerL()); |
|
283 delete cleanup; |
|
284 } |
|
285 |
|
286 return err; |
|
287 } |
|
288 |