summaryrefslogtreecommitdiff
path: root/src/OSD/OSD_Protection.cxx
blob: a411aa00a7bb937ef8e456c33df09741cc37da97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519

#ifndef WNT

#include <OSD_WhoAmI.hxx>
#include <OSD_Protection.ixx>

#include <sys/stat.h>
//
// Ci-joint le tableau de gestion des protection (Ajout et Retrait). Les 
// tableaux sont des tableaux a deux dimensions, indices par l'enumeration
// OSD_SingleProtection. Il y a en tout 16 possibilites dans l enumeration.
// Voir JPT pour tous renseignements....
//
static OSD_SingleProtection TabProtAdd [16][16] =
{
{OSD_None,OSD_R,OSD_W,OSD_RW,OSD_X,OSD_RX,OSD_WX,OSD_RWX,OSD_D,OSD_RD,OSD_WD,OSD_RWD,OSD_XD,OSD_RXD,OSD_WXD,OSD_RWXD},

{OSD_R,OSD_R,OSD_RW,OSD_RW,OSD_RX,OSD_RX,OSD_RWX,OSD_RWX,OSD_RD,OSD_RD,OSD_RWD,OSD_RWD,OSD_RXD,OSD_RXD,OSD_RWXD,OSD_RWXD},

{OSD_W,OSD_RW,OSD_W,OSD_RW,OSD_WX,OSD_RWX,OSD_WX,OSD_RWX,OSD_WD,OSD_RWD,OSD_WD,OSD_RWD,OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD},

{OSD_RW,OSD_RW,OSD_RW,OSD_RW,OSD_RWX,OSD_RWX,OSD_RWX,OSD_RWX,OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD},

{OSD_X,OSD_RX,OSD_WX,OSD_RWX,OSD_X,OSD_RX,OSD_WX,OSD_RWX,OSD_XD,OSD_RXD,OSD_WXD,OSD_RWXD,OSD_XD,OSD_RXD,OSD_WXD,OSD_RWXD},

{OSD_RX,OSD_RX,OSD_RWX,OSD_RWX,OSD_RX,OSD_RX,OSD_RWX,OSD_RWX,OSD_RXD,OSD_RXD,OSD_RWXD,OSD_RWXD,OSD_RXD,OSD_RXD,OSD_RWXD,OSD_R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D,OSD_RXD,OSD_RXD,OSD_RWXD,OSD_RWXD},

{OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD},

{OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD}

};

// ----------------------- 

static OSD_SingleProtection TabProtSub [16][16] =
{
{OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None},

{OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None},

{OSD_W,OSD_W,OSD_None,OSD_None,OSD_W,OSD_W,OSD_None,OSD_None,OSD_W,OSD_W,OSD_None,OSD_None,OSD_W,OSD_W,OSD_None,OSD_None},

{OSD_RW,OSD_W,OSD_R,OSD_None,OSD_RW,OSD_W,OSD_R,OSD_None,OSD_RW,OSD_W,OSD_R,OSD_None,OSD_RW,OSD_W,OSD_R,OSD_None},

{OSD_X,OSD_X,OSD_X,OSD_None,OSD_None,OSD_None,OSD_None,OSD_X,OSD_X,OSD_X,OSD_X,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None},

{OSD_RX,OSD_X,OSD_RX,OSD_X,OSD_R,OSD_None,OSD_R,OSD_None,OSD_RX,OSD_X,OSD_RX,OSD_X,OSD_R,OSD_None,OSD_R,OSD_None},

{OSD_WX,OSD_WX,OSD_X,OSD_X,OSD_W,OSD_W,OSD_None,OSD_None,OSD_WX,OSD_WX,OSD_X,OSD_X,OSD_W,OSD_W,OSD_None,OSD_None},

{OSD_RWX,OSD_WX,OSD_RX,OSD_X,OSD_RW,OSD_W,OSD_R,OSD_None,OSD_RWX,OSD_WX,OSD_RX,OSD_X,OSD_RW,OSD_W,OSD_R,OSD_None},

{OSD_D,OSD_D,OSD_D,OSD_D,OSD_D,OSD_D,OSD_D,OSD_D,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None},

{OSD_RD,OSD_D,OSD_RD,OSD_D,OSD_RD,OSD_D,OSD_RD,OSD_D,OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None},

{OSD_WD,OSD_WD,OSD_D,OSD_D,OSD_WD,OSD_WD,OSD_D,OSD_D,OSD_W,OSD_W,OSD_None,OSD_None,OSD_W,OSD_W,OSD_None,OSD_None},

{OSD_RWD,OSD_WD,OSD_RD,OSD_D,OSD_RWD,OSD_WD,OSD_RD,OSD_D,OSD_RW,OSD_W,OSD_R,OSD_None,OSD_RW,OSD_W,OSD_R,OSD_None},

{OSD_XD,OSD_XD,OSD_XD,OSD_XD,OSD_D,OSD_D,OSD_D,OSD_D,OSD_X,OSD_X,OSD_X,OSD_X,OSD_None,OSD_None,OSD_None,OSD_None},

{OSD_RXD,OSD_XD,OSD_RXD,OSD_XD,OSD_RD,OSD_D,OSD_RD,OSD_D,OSD_RX,OSD_X,OSD_RX,OSD_X,OSD_R,OSD_None,OSD_R,OSD_None},

{OSD_WXD,OSD_WXD,OSD_XD,OSD_XD,OSD_WD,OSD_WD,OSD_D,OSD_D,OSD_WX,OSD_WX,OSD_X,OSD_X,OSD_W,OSD_W,OSD_None,OSD_None},

{OSD_RWXD,OSD_WXD,OSD_RXD,OSD_XD,OSD_RWD,OSD_WD,OSD_RD,OSD_D,OSD_RWX,OSD_WX,OSD_RX,OSD_X,OSD_RW,OSD_W,OSD_R,OSD_None}

};
//const OSD_WhoAmI Iam = OSD_WProtection;


// Initialize System, Group, World for read only and User for read & write

OSD_Protection::OSD_Protection(){
 s = OSD_R;
 u = OSD_RWD;
 g = OSD_R;
 w = OSD_R;
}

OSD_Protection::OSD_Protection(const OSD_SingleProtection System, 
                                     const OSD_SingleProtection User,
                                     const OSD_SingleProtection Group,
                                     const OSD_SingleProtection World){

 s = System;
 u = User;
 g = Group;
 w = World;
}

void  OSD_Protection::Values(OSD_SingleProtection& System, 
                             OSD_SingleProtection& User,
                             OSD_SingleProtection& Group,
                             OSD_SingleProtection& World){
 System = s;
 User = u;
 Group = g;
 World = w;
}


void  OSD_Protection::SetValues(const OSD_SingleProtection System, 
                                const OSD_SingleProtection User,
                                const OSD_SingleProtection Group,
                                const OSD_SingleProtection World){
 
 s = System;
 u = User;
 g = Group;
 w = World;
}


void OSD_Protection::SetSystem (const OSD_SingleProtection priv){
 s = priv;
}

void OSD_Protection::SetUser (const OSD_SingleProtection priv){
 u = priv;
}

void OSD_Protection::SetGroup (const OSD_SingleProtection priv){
 g = priv;
}

void OSD_Protection::SetWorld (const OSD_SingleProtection priv){
 w = priv;
}


OSD_SingleProtection OSD_Protection::System()const{
 return(s);
}

OSD_SingleProtection OSD_Protection::User()const{
 return(u);
}

OSD_SingleProtection OSD_Protection::Group()const{
 return(g);
}

OSD_SingleProtection OSD_Protection::World()const{
 return(w);
}


void OSD_Protection::Add(OSD_SingleProtection& aProtection,
                         const OSD_SingleProtection aRight){
 aProtection = TabProtAdd[aProtection][aRight];
}


void OSD_Protection::Sub(OSD_SingleProtection& aProtection,
                         const OSD_SingleProtection aRight){
 aProtection = TabProtSub[aProtection][aRight];
}


/* Get internal UNIX's access rights for user, group and other */

Standard_Integer  OSD_Protection::Internal()const{

Standard_Integer internal_prot = 0;

 if (u & OSD_R) internal_prot |= S_IRUSR;
 if (u & OSD_W) internal_prot |= S_IWUSR;
 if (u & OSD_D) internal_prot |= S_IWUSR;
 if (u & OSD_X) internal_prot |= S_IXUSR;

 if (g & OSD_R) internal_prot |= S_IRGRP;
 if (g & OSD_W) internal_prot |= S_IWGRP;
 if (g & OSD_D) internal_prot |= S_IWGRP;
 if (g & OSD_X) internal_prot |= S_IXGRP;

 if (w & OSD_R) internal_prot |= S_IROTH;
 if (w & OSD_W) internal_prot |= S_IWOTH;
 if (w & OSD_D) internal_prot |= S_IWOTH;
 if (w & OSD_X) internal_prot |= S_IXOTH;

 if (s & OSD_R) internal_prot |= S_IROTH;
 if (s & OSD_W) internal_prot |= S_IWOTH;
 if (s & OSD_D) internal_prot |= S_IWOTH;
 if (s & OSD_X) internal_prot |= S_IXOTH;                          

 return ( internal_prot );
}


#else

//------------------------------------------------------------------------
//-------------------  WNT Sources of OSD_Protection ---------------------
//------------------------------------------------------------------------

#include <OSD_Protection.hxx>

#define FLAG_READ    0x00000001
#define FLAG_WRITE   0x00000002
#define FLAG_EXECUTE 0x00000004
#define FLAG_DELETE  0x00000008

static Standard_Integer     __fastcall _get_mask ( OSD_SingleProtection );
static OSD_SingleProtection __fastcall _get_prot ( Standard_Integer     );

OSD_Protection :: OSD_Protection () {

 s = OSD_RWXD;
 u = OSD_RWXD;
 g = OSD_RX;
 w = OSD_RX;

}  // end constructor ( 1 )

OSD_Protection :: OSD_Protection (
                   const OSD_SingleProtection System,
                   const OSD_SingleProtection User,
                   const OSD_SingleProtection Group,
                   const OSD_SingleProtection World
                  ) {

 SetValues ( System, User, Group, World );

}  // end constructor ( 2 )

void OSD_Protection :: Values (
                        OSD_SingleProtection& System,
                        OSD_SingleProtection& User,
                        OSD_SingleProtection& Group,
                        OSD_SingleProtection& World
                       ) {
 System = s;
 User   = u;
 Group  = g;
 World  = w;

}  // end OSD_Protection :: Values

void OSD_Protection :: SetValues (
                        const OSD_SingleProtection System,
                        const OSD_SingleProtection User,
                        const OSD_SingleProtection Group,
                        const OSD_SingleProtection World
                       ) {

 s = System;
 u = User;
 g = Group;
 w = World;

}  // end OSD_Protection :: SetValues

void OSD_Protection :: SetSystem ( const OSD_SingleProtection priv ) {

 s = priv;

}  // end OSD_Protection :: SetSystem

void OSD_Protection :: SetUser ( const OSD_SingleProtection priv ) {

 u = priv;

}  // end OSD_Protection :: SetUser

void OSD_Protection :: SetGroup ( const OSD_SingleProtection priv ) {

 g = priv;

}  // end OSD_Protection :: SetGroup

void OSD_Protection :: SetWorld ( const OSD_SingleProtection priv ) {

 w = priv;

}  // end OSD_Protection :: SetWorld

OSD_SingleProtection OSD_Protection :: System () const {

 return s;

}  // end OSD_Protection :: System

OSD_SingleProtection OSD_Protection :: User () const {

 return u;

}  // end OSD_Protection :: User

OSD_SingleProtection OSD_Protection :: Group () const {

 return g;

}  // end OSD_Protection :: Group

OSD_SingleProtection OSD_Protection :: World () const {

 return w;

}  // end OSD_Protection :: World

void OSD_Protection :: Add (
                        OSD_SingleProtection& aProt,
                        const OSD_SingleProtection aRight
                       ) {

 Standard_Integer pMask = 0;
 Standard_Integer rMask = 0;
 Standard_Integer sMask = 0;

 pMask = _get_mask ( aProt  );
 rMask = _get_mask ( aRight );

 if (   (  rMask & FLAG_READ && !( pMask & FLAG_READ )  ) || pMask & FLAG_READ   )

  sMask |= FLAG_READ;

 if (   (  rMask & FLAG_WRITE && !( pMask & FLAG_WRITE )  ) || pMask & FLAG_WRITE   )

  sMask |= FLAG_WRITE;

 if (   (  rMask & FLAG_EXECUTE && !( pMask & FLAG_EXECUTE )  ) || pMask & FLAG_EXECUTE   )

  sMask |= FLAG_EXECUTE;

 if (   (  rMask & FLAG_DELETE && !( pMask & FLAG_DELETE )  ) || pMask & FLAG_DELETE   )

  sMask |= FLAG_DELETE;

 aProt = _get_prot ( sMask );

}  // end OSD_Protection :: Add

void OSD_Protection :: Sub (
                        OSD_SingleProtection& aProt,
                        const OSD_SingleProtection aRight
                       ) {

 Standard_Integer pMask = 0;
 Standard_Integer rMask = 0;

 pMask = _get_mask ( aProt  );
 rMask = _get_mask ( aRight );

 if ( rMask & FLAG_READ )

  pMask &= ~FLAG_READ;

 if ( rMask & FLAG_WRITE )

  pMask &= ~FLAG_WRITE;

 if ( rMask & FLAG_EXECUTE )

  pMask &= ~FLAG_EXECUTE;

 if ( rMask & FLAG_DELETE )

  pMask &= ~FLAG_DELETE;

 aProt = _get_prot ( pMask );

}  // end OSD_Protection :: Sub

Standard_Integer OSD_Protection :: Internal () const {

 return 0;

}  // end OSD_Protection :: Internal

static Standard_Integer __fastcall _get_mask ( OSD_SingleProtection p ) {

 Standard_Integer retVal = 0;

 if ( p == OSD_R   || p == OSD_RW   || p == OSD_RX || p == OSD_RWX ||
      p == OSD_RXD || p == OSD_RWXD || p == OSD_RD || p == OSD_RWD
 ) retVal |= FLAG_READ;

 if ( p == OSD_W   || p == OSD_RW   || p == OSD_WX || p == OSD_RWX ||
      p == OSD_WXD || p == OSD_RWXD || p == OSD_WD || p == OSD_RWD
 ) retVal |= FLAG_WRITE;

 if ( p == OSD_X  || p == OSD_RX  || p == OSD_WX  || p == OSD_RWX  ||
      p == OSD_XD || p == OSD_RXD || p == OSD_WXD || p == OSD_RWXD
 ) retVal |= FLAG_EXECUTE;

 if ( p == OSD_D  || p == OSD_RD  || p == OSD_WD  || p == OSD_RWD  ||
      p == OSD_XD || p == OSD_RXD || p == OSD_WXD || p == OSD_RWXD
 ) retVal |= FLAG_DELETE;

 return retVal;

}  // end _get_mask

static OSD_SingleProtection __fastcall _get_prot ( Standard_Integer m ) {

 OSD_SingleProtection retVal;

 switch ( m ) {
 
  case FLAG_READ:

   retVal = OSD_R;

  break;

  case FLAG_WRITE:

   retVal = OSD_W;

  break;

  case FLAG_READ | FLAG_WRITE:

   retVal = OSD_RW;

  break;

  case FLAG_EXECUTE:

   retVal = OSD_X;

  break;

  case FLAG_READ | FLAG_EXECUTE:

   retVal = OSD_RX;

  break;

  case FLAG_WRITE | FLAG_EXECUTE:

   retVal = OSD_WX;

  break;

  case FLAG_READ | FLAG_WRITE | FLAG_EXECUTE:

   retVal = OSD_RWX;

  break;

  case FLAG_DELETE:

   retVal = OSD_D;

  break;

  case FLAG_READ | FLAG_DELETE:

   retVal = OSD_RD;

  break;

  case FLAG_WRITE | FLAG_DELETE:

   retVal = OSD_WD;

  break;

  case FLAG_READ | FLAG_WRITE | FLAG_DELETE:

   retVal = OSD_RWD;

  break;

  case FLAG_EXECUTE | FLAG_DELETE:

   retVal = OSD_XD;

  break;

  case FLAG_READ | FLAG_EXECUTE | FLAG_DELETE:

   retVal = OSD_RXD;

  break;

  case FLAG_WRITE | FLAG_EXECUTE | FLAG_DELETE:

   retVal = OSD_WXD;

  break;

  case FLAG_READ | FLAG_WRITE | FLAG_EXECUTE | FLAG_DELETE:

   retVal = OSD_RWXD;

  break;

  default:

   retVal = OSD_None;
 
 }  // end switch

 return retVal;

}  // end _get_prot

#endif