AccessControl 7.6__cp315-cp315-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. AccessControl/AuthEncoding.py +48 -0
  2. AccessControl/ImplC.py +39 -0
  3. AccessControl/ImplPython.py +914 -0
  4. AccessControl/Implementation.py +111 -0
  5. AccessControl/Owned.py +30 -0
  6. AccessControl/Permission.py +179 -0
  7. AccessControl/PermissionMapping.py +165 -0
  8. AccessControl/PermissionRole.py +19 -0
  9. AccessControl/Permissions.py +65 -0
  10. AccessControl/Role.py +34 -0
  11. AccessControl/SecurityInfo.py +502 -0
  12. AccessControl/SecurityManagement.py +84 -0
  13. AccessControl/SecurityManager.py +30 -0
  14. AccessControl/SimpleObjectPolicies.py +151 -0
  15. AccessControl/SpecialUsers.py +25 -0
  16. AccessControl/User.py +54 -0
  17. AccessControl/ZopeGuards.py +712 -0
  18. AccessControl/ZopeSecurityPolicy.py +61 -0
  19. AccessControl/__init__.py +55 -0
  20. AccessControl/_compat.h +18 -0
  21. AccessControl/cAccessControl.c +2526 -0
  22. AccessControl/cAccessControl.cp315-win_amd64.pyd +0 -0
  23. AccessControl/class_init.py +94 -0
  24. AccessControl/configure.zcml +5 -0
  25. AccessControl/frozen.py +57 -0
  26. AccessControl/interfaces.py +390 -0
  27. AccessControl/logger_wrapper.py +8 -0
  28. AccessControl/meta.zcml +52 -0
  29. AccessControl/metaconfigure.py +61 -0
  30. AccessControl/owner.py +288 -0
  31. AccessControl/permissions.zcml +100 -0
  32. AccessControl/requestmethod.py +96 -0
  33. AccessControl/rolemanager.py +490 -0
  34. AccessControl/safe_formatter.py +86 -0
  35. AccessControl/security.py +203 -0
  36. AccessControl/tainted.py +195 -0
  37. AccessControl/tests/__init__.py +0 -0
  38. AccessControl/tests/actual_python.py +260 -0
  39. AccessControl/tests/mixed_module/__init__.py +9 -0
  40. AccessControl/tests/mixed_module/submodule/__init__.py +5 -0
  41. AccessControl/tests/private_module/__init__.py +5 -0
  42. AccessControl/tests/private_module/submodule/__init__.py +5 -0
  43. AccessControl/tests/public_module/__init__.py +11 -0
  44. AccessControl/tests/public_module/submodule/__init__.py +5 -0
  45. AccessControl/tests/testClassSecurityInfo.py +192 -0
  46. AccessControl/tests/testImplementation.py +65 -0
  47. AccessControl/tests/testModuleSecurity.py +335 -0
  48. AccessControl/tests/testPermissionMapping.py +12 -0
  49. AccessControl/tests/testPermissionRole.py +170 -0
  50. AccessControl/tests/testSecurityManager.py +264 -0
  51. AccessControl/tests/testZCML.py +489 -0
  52. AccessControl/tests/testZopeGuards.py +1086 -0
  53. AccessControl/tests/testZopeSecurityPolicy.py +799 -0
  54. AccessControl/tests/test_frozen.py +124 -0
  55. AccessControl/tests/test_owner.py +289 -0
  56. AccessControl/tests/test_requestmethod.py +138 -0
  57. AccessControl/tests/test_rolemanager.py +137 -0
  58. AccessControl/tests/test_safe_formatter.py +226 -0
  59. AccessControl/tests/test_safeiter.py +59 -0
  60. AccessControl/tests/test_tainted.py +320 -0
  61. AccessControl/tests/test_userfolder.py +198 -0
  62. AccessControl/tests/test_users.py +666 -0
  63. AccessControl/unauthorized.py +38 -0
  64. AccessControl/userfolder.py +432 -0
  65. AccessControl/users.py +502 -0
  66. accesscontrol-7.6.dist-info/METADATA +474 -0
  67. accesscontrol-7.6.dist-info/RECORD +71 -0
  68. accesscontrol-7.6.dist-info/WHEEL +5 -0
  69. accesscontrol-7.6.dist-info/entry_points.txt +2 -0
  70. accesscontrol-7.6.dist-info/licenses/LICENSE.txt +44 -0
  71. accesscontrol-7.6.dist-info/top_level.txt +1 -0
@@ -0,0 +1,2526 @@
1
+ /*
2
+ ** cAccessControl.c
3
+ **
4
+ ** Access control acceleration routines
5
+
6
+ Copyright (c) 2001, Zope Foundation and Contributors.
7
+ All rights reserved.
8
+
9
+ Redistribution and use in source and binary forms, with or without
10
+ modification, are permitted provided that the following conditions are
11
+ met:
12
+
13
+ o Redistributions of source code must retain the above copyright
14
+ notice, this list of conditions, and the disclaimer that follows.
15
+
16
+ o Redistributions in binary form must reproduce the above copyright
17
+ notice, this list of conditions, and the following disclaimer in
18
+ the documentation and/or other materials provided with the
19
+ distribution.
20
+
21
+ o Neither the name of Digital Creations nor the names of its
22
+ contributors may be used to endorse or promote products derived
23
+ from this software without specific prior written permission.
24
+
25
+
26
+ THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
27
+ IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28
+ TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29
+ PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
30
+ CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
33
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35
+ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36
+ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
37
+ DAMAGE.
38
+
39
+ If you have questions regarding this software,
40
+ contact:
41
+
42
+ Digital Creations L.C.
43
+ info@digicool.com
44
+
45
+ (540) 371-6909
46
+
47
+ */
48
+
49
+ #include "ExtensionClass/ExtensionClass.h"
50
+ #include "ExtensionClass/_compat.h"
51
+ #include "Acquisition/Acquisition.h"
52
+ #include "_compat.h"
53
+
54
+ #include <stdio.h>
55
+ #include <stdlib.h>
56
+
57
+ static void
58
+ PyVar_Assign(PyObject **v, PyObject *e)
59
+ {
60
+ Py_XDECREF(*v);
61
+ *v=e;
62
+ }
63
+
64
+ #define ASSIGN(V,E) PyVar_Assign(&(V),(E))
65
+ #define UNLESS(E) if (!(E))
66
+ #define OBJECT(o) ((PyObject *) (o))
67
+
68
+ static PyObject *
69
+ callfunction1(PyObject *function, PyObject *arg)
70
+ {
71
+ PyObject *t, *r;
72
+ t = PyTuple_New(1);
73
+ if (t == NULL)
74
+ return NULL;
75
+ Py_INCREF(arg);
76
+ PyTuple_SET_ITEM(t, 0, arg);
77
+ r = PyObject_CallObject(function, t);
78
+ Py_DECREF(t);
79
+ return r;
80
+ }
81
+
82
+ static PyObject *
83
+ callmethod1(PyObject *self, PyObject *name, PyObject *arg)
84
+ {
85
+ UNLESS(self = PyObject_GetAttr(self,name)) return NULL;
86
+ ASSIGN(self, callfunction1(self, arg));
87
+ return self;
88
+ }
89
+
90
+ static PyObject *
91
+ callfunction2(PyObject *function, PyObject *arg0, PyObject *arg1)
92
+ {
93
+ PyObject *t, *r;
94
+ t = PyTuple_New(2);
95
+ if (t == NULL)
96
+ return NULL;
97
+ Py_INCREF(arg0);
98
+ Py_INCREF(arg1);
99
+ PyTuple_SET_ITEM(t, 0, arg0);
100
+ PyTuple_SET_ITEM(t, 1, arg1);
101
+ r = PyObject_CallObject(function, t);
102
+ Py_DECREF(t);
103
+ return r;
104
+ }
105
+
106
+ static PyObject *
107
+ callfunction3(PyObject *function,
108
+ PyObject *arg0, PyObject *arg1,
109
+ PyObject *arg2
110
+ )
111
+ {
112
+ PyObject *t, *r;
113
+ t = PyTuple_New(3);
114
+ if (t == NULL)
115
+ return NULL;
116
+ Py_INCREF(arg0);
117
+ Py_INCREF(arg1);
118
+ Py_INCREF(arg2);
119
+ PyTuple_SET_ITEM(t, 0, arg0);
120
+ PyTuple_SET_ITEM(t, 1, arg1);
121
+ PyTuple_SET_ITEM(t, 2, arg2);
122
+ r = PyObject_CallObject(function, t);
123
+ Py_DECREF(t);
124
+ return r;
125
+ }
126
+
127
+ static PyObject *
128
+ callfunction4(PyObject *function,
129
+ PyObject *arg0, PyObject *arg1,
130
+ PyObject *arg2, PyObject *arg3
131
+ )
132
+ {
133
+ PyObject *t, *r;
134
+ t = PyTuple_New(4);
135
+ if (t == NULL)
136
+ return NULL;
137
+ Py_INCREF(arg0);
138
+ Py_INCREF(arg1);
139
+ Py_INCREF(arg2);
140
+ Py_INCREF(arg3);
141
+ PyTuple_SET_ITEM(t, 0, arg0);
142
+ PyTuple_SET_ITEM(t, 1, arg1);
143
+ PyTuple_SET_ITEM(t, 2, arg2);
144
+ PyTuple_SET_ITEM(t, 3, arg3);
145
+ r = PyObject_CallObject(function, t);
146
+ Py_DECREF(t);
147
+ return r;
148
+ }
149
+
150
+ static PyObject *
151
+ callfunction5(PyObject *function,
152
+ PyObject *arg0, PyObject *arg1,
153
+ PyObject *arg2, PyObject *arg3, PyObject *arg4
154
+ )
155
+ {
156
+ PyObject *t, *r;
157
+ t = PyTuple_New(5);
158
+ if (t == NULL)
159
+ return NULL;
160
+ Py_INCREF(arg0);
161
+ Py_INCREF(arg1);
162
+ Py_INCREF(arg2);
163
+ Py_INCREF(arg3);
164
+ Py_INCREF(arg4);
165
+ PyTuple_SET_ITEM(t, 0, arg0);
166
+ PyTuple_SET_ITEM(t, 1, arg1);
167
+ PyTuple_SET_ITEM(t, 2, arg2);
168
+ PyTuple_SET_ITEM(t, 3, arg3);
169
+ PyTuple_SET_ITEM(t, 4, arg4);
170
+ r = PyObject_CallObject(function, t);
171
+ Py_DECREF(t);
172
+ return r;
173
+ }
174
+
175
+ static PyObject *
176
+ callfunction6(PyObject *function,
177
+ PyObject *arg0, PyObject *arg1,
178
+ PyObject *arg2, PyObject *arg3,
179
+ PyObject *arg4, PyObject *arg5
180
+ )
181
+ {
182
+ PyObject *t, *r;
183
+ t = PyTuple_New(6);
184
+ if (t == NULL)
185
+ return NULL;
186
+ Py_INCREF(arg0);
187
+ Py_INCREF(arg1);
188
+ Py_INCREF(arg2);
189
+ Py_INCREF(arg3);
190
+ Py_INCREF(arg4);
191
+ Py_INCREF(arg5);
192
+ PyTuple_SET_ITEM(t, 0, arg0);
193
+ PyTuple_SET_ITEM(t, 1, arg1);
194
+ PyTuple_SET_ITEM(t, 2, arg2);
195
+ PyTuple_SET_ITEM(t, 3, arg3);
196
+ PyTuple_SET_ITEM(t, 4, arg4);
197
+ PyTuple_SET_ITEM(t, 5, arg5);
198
+ r = PyObject_CallObject(function, t);
199
+ Py_DECREF(t);
200
+ return r;
201
+ }
202
+
203
+ static int
204
+ unpacktuple2(PyObject *args, char *name, int min,
205
+ PyObject **a0, PyObject **a1)
206
+ {
207
+ int l;
208
+ l=PyTuple_Size(args);
209
+ if (l < 0) return -1;
210
+ if (l < min)
211
+ {
212
+ PyErr_Format(PyExc_TypeError, "expected %d arguments, got %d", min, l);
213
+ return -1;
214
+ }
215
+ if (l > 0) *a0=PyTuple_GET_ITEM(args, 0);
216
+ if (l > 1) *a1=PyTuple_GET_ITEM(args, 1);
217
+ return 0;
218
+ }
219
+
220
+ static int
221
+ unpacktuple3(PyObject *args, char *name, int min,
222
+ PyObject **a0, PyObject **a1, PyObject **a2)
223
+ {
224
+ int l;
225
+ l=PyTuple_Size(args);
226
+ if (l < 0) return -1;
227
+ if (l < min)
228
+ {
229
+ PyErr_Format(PyExc_TypeError, "expected %d arguments, got %d", min, l);
230
+ return -1;
231
+ }
232
+ if (l > 0) *a0=PyTuple_GET_ITEM(args, 0);
233
+ if (l > 1) *a1=PyTuple_GET_ITEM(args, 1);
234
+ if (l > 2) *a2=PyTuple_GET_ITEM(args, 2);
235
+ return 0;
236
+ }
237
+
238
+ static int
239
+ unpacktuple4(PyObject *args, char *name, int min,
240
+ PyObject **a0, PyObject **a1, PyObject **a2, PyObject **a3)
241
+ {
242
+ int l;
243
+ l=PyTuple_Size(args);
244
+ if (l < 0) return -1;
245
+ if (l < min)
246
+ {
247
+ PyErr_Format(PyExc_TypeError, "expected %d arguments, got %d", min, l);
248
+ return -1;
249
+ }
250
+ if (l > 0) *a0=PyTuple_GET_ITEM(args, 0);
251
+ if (l > 1) *a1=PyTuple_GET_ITEM(args, 1);
252
+ if (l > 2) *a2=PyTuple_GET_ITEM(args, 2);
253
+ if (l > 3) *a3=PyTuple_GET_ITEM(args, 3);
254
+ return 0;
255
+ }
256
+
257
+ static int
258
+ unpacktuple5(PyObject *args, char *name, int min,
259
+ PyObject **a0, PyObject **a1, PyObject **a2,
260
+ PyObject **a3, PyObject **a4)
261
+ {
262
+ int l;
263
+ l=PyTuple_Size(args);
264
+ if (l < 0) return -1;
265
+ if (l < min)
266
+ {
267
+ PyErr_Format(PyExc_TypeError, "expected %d arguments, got %d", min, l);
268
+ return -1;
269
+ }
270
+ if (l > 0) *a0=PyTuple_GET_ITEM(args, 0);
271
+ if (l > 1) *a1=PyTuple_GET_ITEM(args, 1);
272
+ if (l > 2) *a2=PyTuple_GET_ITEM(args, 2);
273
+ if (l > 3) *a3=PyTuple_GET_ITEM(args, 3);
274
+ if (l > 4) *a4=PyTuple_GET_ITEM(args, 4);
275
+ return 0;
276
+ }
277
+
278
+ static int
279
+ unpacktuple6(PyObject *args, char *name, int min,
280
+ PyObject **a0, PyObject **a1, PyObject **a2,
281
+ PyObject **a3, PyObject **a4, PyObject **a5)
282
+ {
283
+ int l;
284
+ l=PyTuple_Size(args);
285
+ if (l < 0) return -1;
286
+ if (l < min)
287
+ {
288
+ PyErr_Format(PyExc_TypeError, "expected %d arguments, got %d", min, l);
289
+ return -1;
290
+ }
291
+ if (l > 0) *a0=PyTuple_GET_ITEM(args, 0);
292
+ if (l > 1) *a1=PyTuple_GET_ITEM(args, 1);
293
+ if (l > 2) *a2=PyTuple_GET_ITEM(args, 2);
294
+ if (l > 3) *a3=PyTuple_GET_ITEM(args, 3);
295
+ if (l > 4) *a4=PyTuple_GET_ITEM(args, 4);
296
+ if (l > 5) *a5=PyTuple_GET_ITEM(args, 5);
297
+ return 0;
298
+ }
299
+
300
+
301
+
302
+ /*
303
+ ** Structures
304
+ */
305
+
306
+ typedef struct {
307
+ PyObject_HEAD
308
+ } ZopeSecurityPolicy;
309
+
310
+ typedef struct {
311
+ PyObject_HEAD
312
+ PyObject *thread_id;
313
+ PyObject *context;
314
+ PyObject *policy;
315
+ PyObject *validate;
316
+ PyObject *checkPermission;
317
+ } SecurityManager;
318
+
319
+ typedef struct {
320
+ PyObject_HEAD
321
+ PyObject *__name__;
322
+ PyObject *_p;
323
+ PyObject *__roles__;
324
+ } PermissionRole;
325
+
326
+ typedef struct {
327
+ PyObject_HEAD
328
+ PyObject *_p;
329
+ PyObject *_pa;
330
+ PyObject *__roles__;
331
+ PyObject *_v;
332
+ } imPermissionRole;
333
+
334
+ /*
335
+ ** Prototypes
336
+ */
337
+
338
+ static PyObject *ZopeSecurityPolicy_validate(PyObject *self, PyObject *args);
339
+ static PyObject *ZopeSecurityPolicy_getattro(ZopeSecurityPolicy *self,
340
+ PyObject *name);
341
+ static void ZopeSecurityPolicy_dealloc(ZopeSecurityPolicy *self);
342
+
343
+
344
+ static PyObject *PermissionRole_init(PermissionRole *self, PyObject *args);
345
+ static PyObject *PermissionRole_of(PermissionRole *self, PyObject *parent);
346
+ static PyObject *
347
+ PermissionRole_rolesForPermissionOn(PermissionRole *self, PyObject *value);
348
+ static void PermissionRole_dealloc(PermissionRole *self);
349
+
350
+ static PyObject *PermissionRole_getattro(PermissionRole *self, PyObject *name);
351
+
352
+ static PyObject *imPermissionRole_of(imPermissionRole *self, PyObject *parent);
353
+ static Py_ssize_t imPermissionRole_length(imPermissionRole *self);
354
+ static PyObject *imPermissionRole_get(imPermissionRole *self,
355
+ Py_ssize_t item);
356
+ static void imPermissionRole_dealloc(imPermissionRole *self);
357
+
358
+ static PyObject *rolesForPermissionOn(PyObject *self, PyObject *args);
359
+ static PyObject *module_guarded_getattr(PyObject *self, PyObject *args);
360
+ static PyObject *module_aq_validate(PyObject *self, PyObject *args);
361
+ static PyObject *module_setDefaultBehaviors(PyObject *self, PyObject *args);
362
+ static PyObject *c_rolesForPermissionOn(PyObject *self, PyObject *perm,
363
+ PyObject *object, PyObject *deflt);
364
+
365
+ static PyObject *permissionName(PyObject *name);
366
+
367
+ static PyObject *SecurityManager_validate(SecurityManager *self,
368
+ PyObject *args);
369
+ static PyObject *SecurityManager_DTMLValidate(SecurityManager *self,
370
+ PyObject *args);
371
+ static PyObject *SecurityManager_checkPermission(SecurityManager *self,
372
+ PyObject *args);
373
+ static void SecurityManager_dealloc(SecurityManager *self);
374
+ static PyObject *SecurityManager_getattro(SecurityManager *self,
375
+ PyObject *name);
376
+ static int SecurityManager_setattro(SecurityManager *self,
377
+ PyObject *name, PyObject *value);
378
+
379
+ static getattrofunc ExtensionClassGetattro;
380
+
381
+
382
+ /*
383
+ ** Constants
384
+ */
385
+
386
+ static PyMethodDef cAccessControl_methods[] = {
387
+ {"rolesForPermissionOn",
388
+ (PyCFunction)rolesForPermissionOn,
389
+ METH_VARARGS,
390
+ ""
391
+ },
392
+ {"guarded_getattr",
393
+ (PyCFunction)module_guarded_getattr,
394
+ METH_VARARGS,
395
+ ""
396
+ },
397
+ {"aq_validate",
398
+ (PyCFunction)module_aq_validate,
399
+ METH_VARARGS,
400
+ ""
401
+ },
402
+ {"setDefaultBehaviors",
403
+ (PyCFunction)module_setDefaultBehaviors,
404
+ METH_VARARGS,
405
+ ""
406
+ },
407
+ { NULL, NULL }
408
+ };
409
+
410
+ static char ZopeSecurityPolicy__doc__[] = "ZopeSecurityPolicy C implementation";
411
+
412
+ static PyMethodDef ZopeSecurityPolicy_methods[] = {
413
+ {"validate",
414
+ (PyCFunction)ZopeSecurityPolicy_validate,
415
+ METH_VARARGS,
416
+ ""
417
+ },
418
+ { NULL, NULL }
419
+ };
420
+
421
+ static PyExtensionClass ZopeSecurityPolicyType = {
422
+ PyVarObject_HEAD_INIT(NULL, 0)
423
+ "ZopeSecurityPolicy", /* tp_name */
424
+ sizeof(ZopeSecurityPolicy), /* tp_basicsize */
425
+ 0, /* tp_itemsize */
426
+ /* Standard methods */
427
+ (destructor) ZopeSecurityPolicy_dealloc,/* tp_dealloc */
428
+ 0, /* tp_vectorcall_offset */
429
+ NULL, /* tp_getattr */
430
+ NULL, /* tp_setattr */
431
+ NULL, /* tp_compare */
432
+ NULL, /* tp_repr */
433
+ /* Method suites */
434
+ NULL, /* tp_as_number */
435
+ NULL, /* tp_as_sequence*/
436
+ NULL, /* tp_as_mapping */
437
+ /* More standard ops */
438
+ NULL, /* tp_hash */
439
+ NULL, /* tp_call */
440
+ NULL, /* tp_str */
441
+ (getattrofunc)ZopeSecurityPolicy_getattro,/* tp_getattro*/
442
+ NULL, /* tp_setattro */
443
+ /* Reserved fields */
444
+ 0, /* tp_xxx3 */
445
+ 0, /* tp_xxx4 */
446
+ /* Docstring */
447
+ ZopeSecurityPolicy__doc__, /* tp_doc */
448
+ #ifdef COUNT_ALLOCS
449
+ 0, /* tp_alloc */
450
+ 0, /* tp_free */
451
+ 0, /* tp_maxalloc */
452
+ NULL, /* tp_next */
453
+ #endif
454
+ METHOD_CHAIN(ZopeSecurityPolicy_methods),/* methods */
455
+ (void *)(EXTENSIONCLASS_BINDABLE_FLAG), /* flags */
456
+ };
457
+
458
+
459
+ static char SecurityManager__doc__[] = "ZopeSecurityPolicy C implementation";
460
+
461
+ static PyMethodDef SecurityManager_methods[] = {
462
+ {"validate",
463
+ (PyCFunction)SecurityManager_validate,
464
+ METH_VARARGS,
465
+ ""
466
+ },
467
+ {"DTMLValidate",
468
+ (PyCFunction)SecurityManager_DTMLValidate,
469
+ METH_VARARGS,
470
+ ""
471
+ },
472
+ {"checkPermission",
473
+ (PyCFunction)SecurityManager_checkPermission,
474
+ METH_VARARGS,
475
+ ""
476
+ },
477
+ { NULL, NULL }
478
+ };
479
+
480
+ static PyExtensionClass SecurityManagerType = {
481
+ PyVarObject_HEAD_INIT(NULL, 0)
482
+ "SecurityManager", /* tp_name */
483
+ sizeof(SecurityManager), /* tp_basicsize */
484
+ 0, /* tp_itemsize */
485
+ /* Standard methods */
486
+ (destructor) SecurityManager_dealloc,/* tp_dealloc */
487
+ 0, /* tp_vectorcall_offset*/
488
+ NULL, /* tp_getattr */
489
+ NULL, /* tp_setattr */
490
+ NULL, /* tp_compare */
491
+ NULL, /* tp_repr */
492
+ /* Method suites */
493
+ NULL, /* tp_as_number */
494
+ NULL, /* tp_as_sequence*/
495
+ NULL, /* tp_as_mapping */
496
+ /* More standard ops */
497
+ NULL, /* tp_hash */
498
+ NULL, /* tp_call */
499
+ NULL, /* tp_str */
500
+ (getattrofunc)SecurityManager_getattro, /* tp_getattro */
501
+ (setattrofunc)SecurityManager_setattro, /* tp_setattro */
502
+ /* Reserved fields */
503
+ 0, /* tp_xxx3 */
504
+ 0, /* tp_xxx4 */
505
+ /* Docstring */
506
+ SecurityManager__doc__, /* tp_doc */
507
+ #ifdef COUNT_ALLOCS
508
+ 0, /* tp_alloc */
509
+ 0, /* tp_free */
510
+ 0, /* tp_maxalloc */
511
+ NULL, /* tp_next */
512
+ #endif
513
+ METHOD_CHAIN(SecurityManager_methods), /* methods */
514
+ 0, /* flags */
515
+ };
516
+
517
+
518
+ static char PermissionRole__doc__[] = "PermissionRole C implementation";
519
+
520
+ static PyMethodDef PermissionRole_methods[] = {
521
+ {"__init__",
522
+ (PyCFunction)PermissionRole_init,
523
+ METH_VARARGS,
524
+ ""
525
+ },
526
+ {"__of__",
527
+ (PyCFunction)PermissionRole_of,
528
+ METH_O,
529
+ ""
530
+ },
531
+ {"rolesForPermissionOn",
532
+ (PyCFunction)PermissionRole_rolesForPermissionOn,
533
+ METH_O,
534
+ ""
535
+ },
536
+ { NULL, NULL }
537
+ };
538
+
539
+ static PyExtensionClass PermissionRoleType = {
540
+ PyVarObject_HEAD_INIT(NULL, 0)
541
+ "PermissionRole", /* tp_name */
542
+ sizeof(PermissionRole), /* tp_basicsize */
543
+ 0, /* tp_itemsize */
544
+ /* Standard methods */
545
+ (destructor) PermissionRole_dealloc, /* tp_dealloc */
546
+ 0, /* tp_vectorcall_offset */
547
+ NULL, /* tp_getattr */
548
+ NULL, /* tp_setattr */
549
+ NULL, /* tp_compare */
550
+ NULL, /* tp_repr */
551
+ /* Method suites */
552
+ NULL, /* tp_as_number */
553
+ NULL, /* tp_as_sequence*/
554
+ NULL, /* tp_as_mapping */
555
+ /* More standard ops */
556
+ NULL, /* tp_hash */
557
+ NULL, /* tp_call */
558
+ NULL, /* tp_str */
559
+ (getattrofunc) PermissionRole_getattro, /* tp_getattro */
560
+ NULL, /* tp_setattro */
561
+ /* Reserved fields */
562
+ 0, /* tp_xxx3 */
563
+ 0, /* tp_xxx4 */
564
+ /* Docstring */
565
+ PermissionRole__doc__, /* tp_doc */
566
+ #ifdef COUNT_ALLOCS
567
+ 0, /* tp_alloc */
568
+ 0, /* tp_free */
569
+ 0, /* tp_maxalloc */
570
+ NULL, /* tp_next */
571
+ #endif
572
+ METHOD_CHAIN(PermissionRole_methods), /* methods */
573
+ (void *)(EXTENSIONCLASS_BINDABLE_FLAG)/*|
574
+ EXTENSIONCLASS_INSTDICT_FLAG*/, /* flags */
575
+ NULL, /* Class dict */
576
+ 0, /* bases */
577
+ NULL, /* reserved */
578
+ };
579
+
580
+ static char imPermissionRole__doc__[] = "imPermissionRole C implementation";
581
+
582
+ static PyMethodDef imPermissionRole_methods[] = {
583
+ {"__of__",
584
+ (PyCFunction)imPermissionRole_of,
585
+ METH_O,
586
+ ""
587
+ },
588
+ {"rolesForPermissionOn",
589
+ (PyCFunction)imPermissionRole_of,
590
+ METH_O,
591
+ ""
592
+ },
593
+ { NULL, NULL }
594
+ };
595
+
596
+ static PySequenceMethods imSequenceMethods = {
597
+ (lenfunc) imPermissionRole_length, /* sq_length */
598
+ (binaryfunc) NULL, /* sq_concat */
599
+ (ssizeargfunc) NULL, /* sq_repeat */
600
+ (ssizeargfunc) imPermissionRole_get, /* sq_item */
601
+ (ssizessizeargfunc) NULL, /* sq_slice */
602
+ (ssizeobjargproc) NULL, /* sq_ass_item */
603
+ (ssizessizeobjargproc) NULL, /* sq_ass_slice */
604
+ (objobjproc) NULL, /* sq_contains */
605
+ (binaryfunc) NULL, /* sq_inplace_concat */
606
+ (ssizeargfunc) NULL /* sq_inplace_repeat */
607
+ };
608
+
609
+ static PyExtensionClass imPermissionRoleType = {
610
+ PyVarObject_HEAD_INIT(NULL, 0)
611
+ "imPermissionRole", /* tp_name */
612
+ sizeof(imPermissionRole), /* tp_basicsize */
613
+ 0, /* tp_itemsize */
614
+ /* Standard methods */
615
+ (destructor) imPermissionRole_dealloc, /* tp_dealloc */
616
+ 0, /* tp_vectorcall_offset */
617
+ NULL, /* tp_getattr */
618
+ NULL, /* tp_setattr */
619
+ NULL, /* tp_compare */
620
+ NULL, /* tp_repr */
621
+ /* Method suites */
622
+ NULL, /* tp_as_number */
623
+ &imSequenceMethods, /* tp_as_sequence*/
624
+ NULL, /* tp_as_mapping */
625
+ /* More standard ops */
626
+ NULL, /* tp_hash */
627
+ NULL, /* tp_call */
628
+ NULL, /* tp_str */
629
+ NULL, /* tp_getattro */
630
+ NULL, /* tp_setattro */
631
+ /* Reserved fields */
632
+ 0, /* tp_xxx3 */
633
+ 0, /* tp_xxx4 */
634
+ /* Docstring */
635
+ imPermissionRole__doc__, /* tp_doc */
636
+ #ifdef COUNT_ALLOCS
637
+ 0, /* tp_alloc */
638
+ 0, /* tp_free */
639
+ 0, /* tp_maxalloc */
640
+ NULL, /* tp_next */
641
+ #endif
642
+ METHOD_CHAIN(imPermissionRole_methods), /* methods */
643
+ (void *)(EXTENSIONCLASS_BINDABLE_FLAG), /* flags */
644
+ };
645
+
646
+
647
+ /* --------------------------------------------------------------
648
+ ** STATIC OBJECTS
649
+ ** --------------------------------------------------------------
650
+ */
651
+
652
+ static PyObject *Containers = NULL;
653
+ static PyObject *ContainerAssertions = NULL;
654
+ static PyObject *Unauthorized = NULL;
655
+ static PyObject *zExceptions_Unauthorized = NULL;
656
+ static PyObject *warn= NULL;
657
+ static PyObject *NoSequenceFormat = NULL;
658
+ static PyObject *_what_not_even_god_should_do = NULL;
659
+ static PyObject *Anonymous = NULL;
660
+ static PyObject *AnonymousTuple = NULL;
661
+ static PyObject *ManagerTuple = NULL;
662
+ static PyObject *imPermissionRoleObj = NULL;
663
+ static PyObject *defaultPermission = NULL;
664
+ static PyObject *__roles__ = NULL;
665
+ static PyObject *__of__ = NULL;
666
+ static PyObject *__allow_access_to_unprotected_subobjects__ = NULL;
667
+ static PyObject *stack_str = NULL;
668
+ static PyObject *user_str = NULL;
669
+ static PyObject *validate_str = NULL;
670
+ static PyObject *_proxy_roles_str = NULL;
671
+ static PyObject *allowed_str = NULL;
672
+ static PyObject *getOwner_str = NULL;
673
+ static PyObject *getPhysicalRoot_str = NULL;
674
+ static PyObject *checkPermission_str = NULL;
675
+ static PyObject *getSecurityManager = NULL;
676
+ static PyObject *unrestrictedTraverse_str = NULL;
677
+ static PyObject *aq_validate = NULL;
678
+ static PyObject *aq_parent_str = NULL;
679
+ static PyObject *_check_context_str = NULL;
680
+ static PyObject *getRoles = NULL;
681
+ static PyObject *getWrappedOwner_str = NULL;
682
+
683
+ static int ownerous = 1;
684
+ static int authenticated = 1;
685
+
686
+ /* --------------------------------------------------------------
687
+ ** ZopeSecurityPolicy Methods
688
+ ** --------------------------------------------------------------
689
+ */
690
+
691
+ /* ZopeSecurityPolicy_setup
692
+ **
693
+ ** Setup for ZopeSecurityPolicy -- load all necessary objects from
694
+ ** elsewhere... (e.g. imports)
695
+ */
696
+
697
+ static int
698
+ ZopeSecurityPolicy_setup(void) {
699
+ UNLESS (NoSequenceFormat = NATIVE_FROM_STRING(
700
+ "'%s' passed as roles"
701
+ " during validation of '%s' is not a sequence."
702
+ )) return -1;
703
+
704
+ UNLESS (defaultPermission = Py_BuildValue("(s)", "Manager")) return -1;
705
+ UNLESS (_what_not_even_god_should_do = Py_BuildValue("[]")) return -1;
706
+ UNLESS (__roles__ = NATIVE_FROM_STRING("__roles__")) return -1;
707
+ UNLESS (__of__ = NATIVE_FROM_STRING("__of__")) return -1;
708
+ UNLESS (Anonymous = NATIVE_FROM_STRING("Anonymous")) return -1;
709
+ UNLESS (AnonymousTuple = Py_BuildValue("(O)", Anonymous)) return -1;
710
+ UNLESS (ManagerTuple = Py_BuildValue("(s)", "Manager")) return -1;
711
+ UNLESS (stack_str = NATIVE_FROM_STRING("stack")) return -1;
712
+ UNLESS (user_str = NATIVE_FROM_STRING("user")) return -1;
713
+ UNLESS (validate_str = NATIVE_FROM_STRING("validate")) return -1;
714
+ UNLESS (_proxy_roles_str = NATIVE_FROM_STRING("_proxy_roles"))
715
+ return -1;
716
+ UNLESS (allowed_str = NATIVE_FROM_STRING("allowed")) return -1;
717
+ UNLESS (getOwner_str = NATIVE_FROM_STRING("getOwner")) return -1;
718
+ UNLESS (getWrappedOwner_str = NATIVE_FROM_STRING("getWrappedOwner"))
719
+ return -1;
720
+ UNLESS (getPhysicalRoot_str = NATIVE_FROM_STRING("getPhysicalRoot"))
721
+ return -1;
722
+ UNLESS (aq_parent_str = NATIVE_FROM_STRING("aq_parent")) return -1;
723
+ UNLESS (_check_context_str = NATIVE_FROM_STRING("_check_context"))
724
+ return -1;
725
+ UNLESS (unrestrictedTraverse_str = NATIVE_FROM_STRING(
726
+ "unrestrictedTraverse"))
727
+ return -1;
728
+
729
+ UNLESS (checkPermission_str = NATIVE_FROM_STRING("checkPermission"))
730
+ return -1;
731
+ UNLESS (__allow_access_to_unprotected_subobjects__ =
732
+ NATIVE_FROM_STRING(
733
+ "__allow_access_to_unprotected_subobjects__"))
734
+ return -1;
735
+
736
+ return 0;
737
+ }
738
+
739
+ static PyObject*
740
+ convert_name(PyObject *name)
741
+ {
742
+ #ifdef Py_USING_UNICODE
743
+ if (PyUnicode_Check(name)) {
744
+ name = PyUnicode_AsEncodedString(name, NULL, NULL);
745
+ }
746
+ else
747
+ #endif
748
+ if (!PyBytes_Check(name)) {
749
+ PyErr_SetString(PyExc_TypeError, "name must be a string");
750
+ return NULL;
751
+ }
752
+ else {
753
+ Py_INCREF(name);
754
+ }
755
+ return name;
756
+ }
757
+
758
+ /*
759
+ ** unauthErr
760
+ **
761
+ ** Generate the unauthorized error
762
+ */
763
+
764
+ static void unauthErr(PyObject *name, PyObject *value) {
765
+
766
+ PyObject *v;
767
+
768
+ if ((v=Py_BuildValue("OO", name, value)))
769
+ {
770
+ PyErr_SetObject(Unauthorized, v);
771
+ Py_DECREF(v);
772
+ }
773
+ }
774
+
775
+ static int
776
+ is_string_format_name(PyObject *name)
777
+ {
778
+ PyObject *name_as_bytes;
779
+ char *name_s;
780
+ int result;
781
+
782
+ name_as_bytes = convert_name(name);
783
+ if (name_as_bytes == NULL) {
784
+ return -1;
785
+ }
786
+
787
+ name_s = PyBytes_AS_STRING(name_as_bytes);
788
+ result = strcmp(name_s, "format") == 0
789
+ || strcmp(name_s, "format_map") == 0;
790
+ Py_DECREF(name_as_bytes);
791
+ return result;
792
+ }
793
+
794
+ static PyObject *
795
+ guarded_string_format(PyObject *inst, PyObject *name)
796
+ {
797
+ PyObject *assertion;
798
+ PyObject *attrv;
799
+
800
+ assertion = PyDict_GetItem(ContainerAssertions, OBJECT(&PyUnicode_Type));
801
+ if (assertion == NULL || !PyDict_Check(assertion)) {
802
+ PyErr_SetObject(Unauthorized, name);
803
+ return NULL;
804
+ }
805
+
806
+ attrv = PyDict_GetItem(assertion, name);
807
+ if (attrv == NULL) {
808
+ PyErr_SetObject(Unauthorized, name);
809
+ return NULL;
810
+ }
811
+
812
+ return callfunction2(attrv, inst, name);
813
+ }
814
+
815
+ static PyObject *
816
+ ZopeSecurityPolicy_getattro(ZopeSecurityPolicy *self, PyObject *name)
817
+ {
818
+ PyObject *name_as_bytes = NULL;
819
+ char *name_s;
820
+
821
+ if (NATIVE_CHECK(name) || PyUnicode_Check(name)) {
822
+ if ((name_as_bytes = convert_name(name)) == NULL) {
823
+ return NULL;
824
+ }
825
+
826
+ name_s = PyBytes_AS_STRING(name_as_bytes);
827
+
828
+ if (name_s[0] == '_') {
829
+ if (! strcmp(name_s, "_ownerous")) {
830
+ Py_DECREF(name_as_bytes);
831
+ return INT_FROM_LONG(ownerous);
832
+ }
833
+ else if (! strcmp(name_s, "_authenticated")) {
834
+ Py_DECREF(name_as_bytes);
835
+ return INT_FROM_LONG(authenticated);
836
+ }
837
+ }
838
+ }
839
+
840
+ Py_XDECREF(name_as_bytes);
841
+ return Py_FindAttr(OBJECT(self), name);
842
+ }
843
+
844
+ /*
845
+ ** ZopeSecurityPolicy_validate
846
+ */
847
+
848
+ static PyObject *ZopeSecurityPolicy_validate(PyObject *self, PyObject *args) {
849
+ PyObject *accessed = NULL; /* Note: accessed is not used. */
850
+ PyObject *container = NULL;
851
+ PyObject *name = NULL;
852
+ PyObject *value = NULL;
853
+ PyObject *context = NULL;
854
+ PyObject *roles = NULL;
855
+ /* Import from SimpleObject Policy._noroles */
856
+ /* Note that _noroles means missing roles, spelled with a NULL in C.
857
+ Jim. */
858
+ PyObject *containerbase = NULL;
859
+ PyObject *accessedbase = NULL;
860
+ PyObject *p = NULL;
861
+ PyObject *rval = NULL;
862
+ PyObject *stack = NULL;
863
+ PyObject *user = NULL;
864
+
865
+ PyObject *method = NULL;
866
+ PyObject *tmp = NULL;
867
+
868
+
869
+ int i, l, contains;
870
+ PyObject *r;
871
+
872
+ /*| def validate(self, accessed, container, name, value, context
873
+ **| roles=_noroles ...
874
+ */
875
+
876
+ if (unpacktuple6(args, "validate", 5, &accessed, &container,
877
+ &name, &value, &context, &roles) < 0)
878
+ return NULL;
879
+
880
+ /*| # Provide special rules for acquisition attributes
881
+ **| if type(name) in (StringType, UnicodeType):
882
+ **| if name[:3] == 'aq_' and name not in valid_aq_:
883
+ **| raise Unauthorized(name, value)
884
+ */
885
+
886
+ if (NATIVE_CHECK(name) || PyUnicode_Check(name)) {
887
+ char *sname = NULL;
888
+ PyObject *name_as_bytes = convert_name(name);
889
+ if (name_as_bytes != NULL) {
890
+ sname = PyBytes_AS_STRING(name_as_bytes);
891
+ }
892
+
893
+ /* Conversion to string may have failed, e.g. if name is Unicode
894
+ * and can't be bashed into the default encoding. Unclear what
895
+ * to do then. It's arguably conservative to raise Unauthorized
896
+ * in this case.
897
+ */
898
+ if (sname == NULL ||
899
+ /* or starts with "aq_" */
900
+ (sname[0] == 'a' && sname[1] == 'q' && sname[2] == '_' &&
901
+ /* and isn't aq_{parent, inner, explicit} */
902
+ strcmp(sname + 3, "parent") &&
903
+ strcmp(sname + 3, "inner") &&
904
+ strcmp(sname + 3, "explicit")
905
+ )
906
+ )
907
+ {
908
+ Py_XDECREF(name_as_bytes);
909
+ /* Access control violation */
910
+ unauthErr(name, value);
911
+ return NULL; /* roles is not owned yet */
912
+ }
913
+
914
+ Py_XDECREF(name_as_bytes);
915
+ }
916
+ Py_XINCREF(roles); /* Convert the borrowed ref to a real one */
917
+
918
+ /*| containerbase = aq_base(container)
919
+ **| accessedbase = aq_base(accessed)
920
+ **| if accessedbase is accessed:
921
+ **| # accessed is not a wrapper, so assume that the
922
+ **| # value could not have been acquired.
923
+ **| accessedbase = container
924
+ */
925
+
926
+ containerbase = aq_base(container);
927
+ if (containerbase == NULL) goto err;
928
+
929
+ if (aq_isWrapper(accessed))
930
+ accessedbase = aq_base(accessed);
931
+ else {
932
+ Py_INCREF(container);
933
+ accessedbase = container;
934
+ }
935
+
936
+ /*| # If roles weren't passed in, we'll try to get them from
937
+ **| # the object
938
+ **|
939
+ **| if roles is _noroles:
940
+ **| roles = getRoles(container, name, value, _noroles)
941
+ */
942
+
943
+ if (roles == NULL) {
944
+ roles = callfunction4(getRoles, container, name, value, getRoles);
945
+ if (roles == getRoles) {
946
+ Py_DECREF(roles);
947
+ roles = NULL;
948
+ }
949
+ if (roles == NULL)
950
+ PyErr_Clear();
951
+ }
952
+
953
+ /*| # We still might not have any roles
954
+ **|
955
+ **| if roles is _noroles:
956
+ */
957
+
958
+ if (roles == NULL) {
959
+
960
+ /*| # We have an object without roles and we didn't get
961
+ **| # a list of roles passed in. Presumably, the value
962
+ **| # is some simple object like a string or a list.
963
+ **| # We'll try to get roles from its container
964
+ **|
965
+ **| if container is None: raise Unauthorized(name, value)
966
+ */
967
+
968
+ if (container == Py_None) {
969
+ unauthErr(name, value);
970
+ goto err;
971
+ }
972
+
973
+ /*| roles = getattr(container, "__roles__", _noroles)
974
+ **| if roles is _noroles:
975
+ **| if containerbase is container:
976
+ **| # Container is not wrapped.
977
+ **| if containerbase is not accessedbase:
978
+ **| raise Unauthorized(name, value)
979
+ **| else:
980
+ **| # Try to acquire roles
981
+ **| try: roles = container.aq_aquire('__roles__')
982
+ **| except AttributeError:
983
+ **| if containerbase is not accessedbase:
984
+ **| raise Unauthorized(name, value)
985
+ */
986
+
987
+ roles = PyObject_GetAttr(container, __roles__);
988
+ if (roles == NULL) {
989
+ PyErr_Clear();
990
+
991
+ if (!aq_isWrapper(container)) {
992
+ if (containerbase != accessedbase) {
993
+ unauthErr(name, value);
994
+ goto err;
995
+ }
996
+ }
997
+ else {
998
+ roles = aq_acquire(container, __roles__);
999
+ if (roles == NULL) {
1000
+ if (PyErr_ExceptionMatches(
1001
+ PyExc_AttributeError))
1002
+ {
1003
+ PyErr_Clear();
1004
+ if (containerbase != accessedbase) {
1005
+ unauthErr(name, value);
1006
+ goto err;
1007
+ }
1008
+ }
1009
+ else
1010
+ goto err;
1011
+ }
1012
+ }
1013
+
1014
+ }
1015
+
1016
+ /*| # We need to make sure that we are allowed to get
1017
+ **| # unprotected attributes from the container. We are
1018
+ **| # allowed for certain simple containers and if the
1019
+ **| # container says we can. Simple containers may also
1020
+ **| # impose name restrictions.
1021
+ **|
1022
+ **| p = Containers(type(container), None)
1023
+ **| if p is None:
1024
+ **| p = getattr(container,
1025
+ **| "__allow_access_to_unprotected_subobjects__", None)
1026
+ */
1027
+
1028
+ p = callfunction2(Containers, OBJECT(Py_TYPE(container)),
1029
+ Py_None);
1030
+ if (p == NULL)
1031
+ goto err;
1032
+
1033
+ if (p == Py_None) {
1034
+ ASSIGN(p, PyObject_GetAttr(container,
1035
+ __allow_access_to_unprotected_subobjects__));
1036
+ if (p == NULL)
1037
+ PyErr_Clear();
1038
+ }
1039
+
1040
+ /*| if p is not None:
1041
+ **| tp = type(p)
1042
+ **| if tp is not IntType:
1043
+ **| if tp is DictType:
1044
+ **| if (isinstance(name, StringType) or
1045
+ **| isinstance(name, UnicodeType)):
1046
+ **| p=p.get(name, None)
1047
+ **| else:
1048
+ **| p = 1
1049
+ **| else:
1050
+ **| p = p(name, value)
1051
+ */
1052
+
1053
+ if (p)
1054
+ {
1055
+ if (! INT_CHECK(p))
1056
+ {
1057
+ if (PyDict_Check(p))
1058
+ {
1059
+ if (NATIVE_CHECK(name) || PyUnicode_Check(name))
1060
+ {
1061
+ ASSIGN(p, PyObject_GetItem(p, name));
1062
+ if (p == NULL)
1063
+ PyErr_Clear();
1064
+ }
1065
+ else
1066
+ {
1067
+ ASSIGN(p, INT_FROM_LONG(1));
1068
+ if (p == NULL)
1069
+ goto err;
1070
+ }
1071
+ }
1072
+ else
1073
+ {
1074
+ ASSIGN(p, callfunction2(p, name, value));
1075
+ if (p == NULL)
1076
+ goto err;
1077
+ }
1078
+ }
1079
+ }
1080
+
1081
+ /*| if not p:
1082
+ **| raise Unauthorized, cleanupName(name, value)
1083
+ */
1084
+
1085
+ if (p == NULL || ! PyObject_IsTrue(p)) {
1086
+ Py_XDECREF(p);
1087
+ unauthErr(name, value);
1088
+ goto err;
1089
+ }
1090
+ else
1091
+ Py_DECREF(p);
1092
+
1093
+ /*| if roles is _noroles: return 1
1094
+ */
1095
+
1096
+ if (roles == NULL) {
1097
+ rval = INT_FROM_LONG(1);
1098
+ goto err;
1099
+ }
1100
+
1101
+ /*| # We are going to need a security-aware object to pass
1102
+ **| # to allowed(). We'll use the container
1103
+ **|
1104
+ **| value = container
1105
+ */
1106
+
1107
+ value = container; /* Both are borrowed references */
1108
+
1109
+ } /* if (roles == NULL) */
1110
+
1111
+ /*| # Short-circuit tests if we can
1112
+ **| try:
1113
+ **| if roles is None or 'Anonymous' in roles: return 1
1114
+ **| except TypeError:
1115
+ **| LOG.warning('"%s' passed as roles"
1116
+ **| " during validation of '%s' is not a sequence." %
1117
+ **| ('roles', name))
1118
+ **| raise
1119
+ */
1120
+
1121
+ if (roles == Py_None) {
1122
+ rval = INT_FROM_LONG(1);
1123
+ goto err;
1124
+ }
1125
+ else
1126
+ {
1127
+ int i;
1128
+ i = PySequence_Contains(roles, Anonymous);
1129
+ if (i > 0)
1130
+ {
1131
+ rval = INT_FROM_LONG(1);
1132
+ goto err;
1133
+ }
1134
+ else if (i < 0)
1135
+ { /* Error */
1136
+ PyObject *m, *t, *v, *tb;
1137
+
1138
+ if (!PyErr_ExceptionMatches(PyExc_TypeError))
1139
+ goto err;
1140
+ PyErr_Fetch(&t, &v, &tb);
1141
+
1142
+ m=PyObject_Repr(roles);
1143
+ if (m) ASSIGN(m, Py_BuildValue("OO", m, name));
1144
+ if (m) ASSIGN(m, NATIVE_FORMAT(NoSequenceFormat, m));
1145
+ if (m) ASSIGN(m, PyObject_CallFunction(warn, "O", m));
1146
+ Py_XDECREF(m);
1147
+ PyErr_Restore(t, v, tb);
1148
+ goto err;
1149
+ }
1150
+ }
1151
+
1152
+ /*| # Check executable security
1153
+ **| stack = context.stack
1154
+ **| if stack:
1155
+ */
1156
+
1157
+ stack = PyObject_GetAttr(context, stack_str);
1158
+ if (stack == NULL) goto err;
1159
+
1160
+ if (PyObject_IsTrue(stack)) {
1161
+ PyObject *eo;
1162
+ PyObject *owner;
1163
+ PyObject *proxy_roles;
1164
+
1165
+ /*| eo = stack[-1]
1166
+ **| # If the executable had an owner, can it execute?
1167
+ **| owner = eo.getOwner()
1168
+ **| if (owner is not None) and not owner.allowed(value, roles)
1169
+ **| # We don't want someone to acquire if they can't
1170
+ **| # get an unacquired!
1171
+ **| raise Unauthorized, ('You are not authorized to'
1172
+ **| 'access <em>%s</em>.' % cleanupName(name, value))
1173
+ */
1174
+
1175
+ eo = PySequence_GetItem(stack, -1);
1176
+ if (eo == NULL) goto err;
1177
+
1178
+ if (ownerous) { /* Tabbing not adjusted for diff reasons*/
1179
+
1180
+ owner = PyObject_GetAttr(eo, getOwner_str);
1181
+ if (owner) ASSIGN(owner, PyObject_CallObject(owner, NULL));
1182
+ if (owner ==NULL)
1183
+ {
1184
+ Py_DECREF(eo);
1185
+ goto err;
1186
+ }
1187
+
1188
+ if (owner != Py_None) {
1189
+ ASSIGN(owner,PyObject_GetAttr(owner, allowed_str));
1190
+ if (owner)
1191
+ ASSIGN(owner, callfunction2(owner, value, roles));
1192
+ if (owner == NULL)
1193
+ {
1194
+ Py_DECREF(eo);
1195
+ goto err;
1196
+ }
1197
+
1198
+ if (! PyObject_IsTrue(owner))
1199
+ {
1200
+ Py_DECREF(owner);
1201
+ Py_DECREF(eo);
1202
+ unauthErr(name, value);
1203
+ goto err;
1204
+ }
1205
+ }
1206
+ Py_DECREF(owner);
1207
+
1208
+ } /* End of if ownerous */
1209
+
1210
+ /*| # Proxy roles, which are a lot safer now
1211
+ **| proxy_roles = getattr(eo, "_proxy_roles", None)
1212
+ **| if proxy_roles:
1213
+ **| # Verify that the owner actually can state the proxy role
1214
+ **| # in the context of the accessed item; users in subfolders
1215
+ **| # should not be able to use proxy roles to access items
1216
+ **| # above their subfolder!
1217
+ **| owner = eo.getWrappedOwner()
1218
+ **|
1219
+ **| if owner is not None:
1220
+ **| if container is not containerbase:
1221
+ **| if not owner._check_context(container):
1222
+ **| # container is higher up than the owner,
1223
+ **| # deny access
1224
+ **| raise Unauthorized(name, value)
1225
+ **|
1226
+ **| for r in proxy_roles:
1227
+ **| if r in roles:
1228
+ **| return 1
1229
+ **|
1230
+ **| raise Unauthorized(name, value)
1231
+ */
1232
+ proxy_roles = PyObject_GetAttr(eo, _proxy_roles_str);
1233
+
1234
+ if (proxy_roles == NULL)
1235
+ {
1236
+ Py_DECREF(eo);
1237
+ PyErr_Clear();
1238
+ }
1239
+ else if (PyObject_IsTrue(proxy_roles))
1240
+ {
1241
+
1242
+ method = PyObject_GetAttr(eo, getWrappedOwner_str);
1243
+ if (method == NULL) {
1244
+ Py_DECREF(eo);
1245
+ Py_DECREF(proxy_roles);
1246
+ goto err;
1247
+ }
1248
+
1249
+ owner = PyObject_CallObject(method, NULL);
1250
+ Py_DECREF(method);
1251
+ if (owner == NULL) {
1252
+ Py_DECREF(eo);
1253
+ Py_DECREF(proxy_roles);
1254
+ goto err;
1255
+ }
1256
+
1257
+ Py_DECREF(eo);
1258
+
1259
+ if (owner != Py_None) {
1260
+
1261
+ if (containerbase != container) {
1262
+
1263
+ tmp = callmethod1(owner,_check_context_str,
1264
+ container
1265
+ );
1266
+ if (tmp == NULL) {
1267
+ Py_DECREF(proxy_roles);
1268
+ Py_DECREF(owner);
1269
+ goto err;
1270
+ }
1271
+
1272
+ if (!PyObject_IsTrue(tmp)) {
1273
+ Py_DECREF(proxy_roles);
1274
+ Py_DECREF(owner);
1275
+ Py_DECREF(tmp);
1276
+ unauthErr(name, value);
1277
+ goto err;
1278
+ }
1279
+ Py_DECREF(tmp);
1280
+ }
1281
+
1282
+ Py_DECREF(owner);
1283
+ }
1284
+
1285
+
1286
+ contains = 0;
1287
+ if (PyTuple_Check(proxy_roles))
1288
+ {
1289
+ l=PyTuple_GET_SIZE(proxy_roles);
1290
+ for (i=0; i < l; i++)
1291
+ {
1292
+ r=PyTuple_GET_ITEM(proxy_roles, i);
1293
+ if ((contains = PySequence_Contains(roles, r)))
1294
+ break;
1295
+ }
1296
+ }
1297
+ else
1298
+ {
1299
+ l=PySequence_Size(proxy_roles);
1300
+ if (l < 0) contains = -1;
1301
+ for (i=0; i < l; i++)
1302
+ {
1303
+ if ((r=PySequence_GetItem(proxy_roles, i)))
1304
+ {
1305
+ contains = PySequence_Contains(roles, r);
1306
+ Py_DECREF(r);
1307
+ }
1308
+ else
1309
+ contains = -1;
1310
+ if (contains < 0)
1311
+ break;
1312
+ }
1313
+ }
1314
+ Py_DECREF(proxy_roles);
1315
+
1316
+ if (contains > 0)
1317
+ rval = INT_FROM_LONG(contains);
1318
+ else if (contains == 0) {
1319
+ unauthErr(name, value);
1320
+ }
1321
+ goto err;
1322
+ }
1323
+ else {
1324
+ Py_DECREF(eo);
1325
+ Py_DECREF(proxy_roles);
1326
+ }
1327
+ } /* End of stack check */
1328
+
1329
+ /*| try:
1330
+ **| if context.user.allowed(value, roles): return 1
1331
+ **| except AttributeError: pass
1332
+ */
1333
+ if (authenticated) { /* Authentication skip for public only access */
1334
+ user = PyObject_GetAttr(context, user_str);
1335
+ if (user) ASSIGN(user, PyObject_GetAttr(user, allowed_str));
1336
+ if (user == NULL)
1337
+ {
1338
+ if (PyErr_ExceptionMatches(PyExc_AttributeError))
1339
+ PyErr_Clear();
1340
+ else
1341
+ goto err;
1342
+ }
1343
+ else
1344
+ {
1345
+ ASSIGN(user, callfunction2(user, value, roles));
1346
+ if (user == NULL) goto err;
1347
+ if (PyObject_IsTrue(user))
1348
+ {
1349
+ rval = INT_FROM_LONG(1);
1350
+ Py_DECREF(user);
1351
+ goto err;
1352
+ }
1353
+ Py_DECREF(user);
1354
+ }
1355
+ } /* End of authentiction skip for public only access */
1356
+
1357
+ /*| raise Unauthorized(name, value)
1358
+ */
1359
+
1360
+ unauthErr(name, value);
1361
+ err:
1362
+ Py_XDECREF(containerbase);
1363
+ Py_XDECREF(accessedbase);
1364
+ Py_XDECREF(stack);
1365
+ Py_XDECREF(roles);
1366
+
1367
+ return rval;
1368
+ }
1369
+
1370
+
1371
+ /*
1372
+ ** ZopeSecurityPolicy_dealloc
1373
+ **
1374
+ */
1375
+
1376
+ static void ZopeSecurityPolicy_dealloc(ZopeSecurityPolicy *self) {
1377
+
1378
+ Py_DECREF(Py_TYPE(self)); /* Extensionclass init incref'd */
1379
+ Py_TYPE(self)->tp_free((PyObject*) self);
1380
+ }
1381
+
1382
+
1383
+
1384
+
1385
+ /* SecurityManager */
1386
+
1387
+ #define CHECK_SECURITY_MANAGER_STATE(self, R) \
1388
+ UNLESS (self->policy) { \
1389
+ PyErr_SetString(PyExc_AttributeError, "_policy"); return R; } \
1390
+ UNLESS (self->context) { \
1391
+ PyErr_SetString(PyExc_AttributeError, "_policy"); return R; }
1392
+
1393
+ #define GET_SECURITY_MANAGER_VALIDATE(self, R) \
1394
+ if (self->validate == NULL && \
1395
+ ((self->validate = PyObject_GetAttr(self->policy, validate_str)) \
1396
+ == NULL)) return R;
1397
+
1398
+
1399
+ static PyObject *
1400
+ SecurityManager_validate(SecurityManager *self, PyObject *args)
1401
+ {
1402
+ PyObject *accessed=Py_None, *container=Py_None, *name=Py_None,
1403
+ *value=Py_None, *roles=NULL;
1404
+
1405
+ if (unpacktuple5(args, "validate", 0,
1406
+ &accessed, &container, &name, &value, &roles) < 0)
1407
+ return NULL;
1408
+
1409
+ CHECK_SECURITY_MANAGER_STATE(self, NULL);
1410
+ GET_SECURITY_MANAGER_VALIDATE(self, NULL);
1411
+
1412
+ if (roles== NULL)
1413
+ return callfunction5(self->validate,
1414
+ accessed, container, name, value, self->context);
1415
+ return callfunction6(self->validate,
1416
+ accessed, container, name, value, self->context, roles);
1417
+ }
1418
+
1419
+ static PyObject *
1420
+ SecurityManager_DTMLValidate(SecurityManager *self, PyObject *args)
1421
+ {
1422
+ PyObject *accessed=Py_None, *container=Py_None, *name=Py_None,
1423
+ *value=Py_None, *md=NULL;
1424
+
1425
+ if (unpacktuple5(args, "DTMLValidate", 0,
1426
+ &accessed, &container, &name, &value, &md) < 0)
1427
+ return NULL;
1428
+
1429
+ CHECK_SECURITY_MANAGER_STATE(self, NULL);
1430
+ GET_SECURITY_MANAGER_VALIDATE(self, NULL);
1431
+
1432
+ return callfunction5(self->validate,
1433
+ accessed, container, name, value, self->context);
1434
+ }
1435
+
1436
+ static PyObject *
1437
+ SecurityManager_checkPermission(SecurityManager *self, PyObject *args)
1438
+ {
1439
+ PyObject *permission, *object;
1440
+
1441
+ if (unpacktuple2(args, "checkPermission", 2, &permission, &object) < 0)
1442
+ return NULL;
1443
+
1444
+ CHECK_SECURITY_MANAGER_STATE(self, NULL);
1445
+ if (self->checkPermission == NULL &&
1446
+ ((self->checkPermission = PyObject_GetAttr(self->policy,
1447
+ checkPermission_str))
1448
+ == NULL)) return NULL;
1449
+
1450
+ return callfunction3(self->checkPermission,
1451
+ permission, object, self->context);
1452
+ }
1453
+
1454
+ static void
1455
+ SecurityManager_dealloc(SecurityManager *self)
1456
+ {
1457
+ Py_XDECREF(self->thread_id);
1458
+ Py_XDECREF(self->context);
1459
+ Py_XDECREF(self->policy);
1460
+ Py_XDECREF(self->validate);
1461
+ Py_XDECREF(self->checkPermission);
1462
+ Py_DECREF(Py_TYPE(self)); /* Extensionclass init incref'd */
1463
+ Py_TYPE(self)->tp_free((PyObject*) self);
1464
+ }
1465
+
1466
+ static PyObject *
1467
+ SecurityManager_getattro(SecurityManager *self, PyObject *name)
1468
+ {
1469
+ PyObject *name_as_bytes = NULL;
1470
+ char *name_s;
1471
+
1472
+ if (NATIVE_CHECK(name) || PyUnicode_Check(name)) {
1473
+ if ((name_as_bytes = convert_name(name)) == NULL) {
1474
+ return NULL;
1475
+ }
1476
+
1477
+ name_s = PyBytes_AS_STRING(name_as_bytes);
1478
+
1479
+ if (name_s[0] == '_') {
1480
+ if (! strcmp(name_s, "_thread_id") && self->thread_id) {
1481
+ Py_DECREF(name_as_bytes);
1482
+
1483
+ Py_INCREF(self->thread_id);
1484
+ return self->thread_id;
1485
+ }
1486
+ else if (! strcmp(name_s, "_context") && self->context) {
1487
+ Py_DECREF(name_as_bytes);
1488
+
1489
+ Py_INCREF(self->context);
1490
+ return self->context;
1491
+ }
1492
+ else if (! strcmp(name_s, "_policy") && self->policy) {
1493
+ Py_DECREF(name_as_bytes);
1494
+
1495
+ Py_INCREF(self->policy);
1496
+ return self->policy;
1497
+ }
1498
+ }
1499
+ }
1500
+
1501
+ Py_XDECREF(name_as_bytes);
1502
+ return Py_FindAttr(OBJECT(self), name);
1503
+ }
1504
+
1505
+ static int
1506
+ SecurityManager_setattro(SecurityManager *self, PyObject *name, PyObject *v)
1507
+ {
1508
+ PyObject *name_as_bytes = NULL;
1509
+ char *name_s;
1510
+
1511
+ if (NATIVE_CHECK(name) || PyUnicode_Check(name)) {
1512
+ if ((name_as_bytes = convert_name(name)) == NULL) {
1513
+ return -1;
1514
+ }
1515
+
1516
+ name_s = PyBytes_AS_STRING(name_as_bytes);
1517
+
1518
+ if (name_s[0] == '_')
1519
+ {
1520
+ if (! strcmp(name_s, "_thread_id"))
1521
+ {
1522
+ Py_DECREF(name_as_bytes);
1523
+
1524
+ Py_INCREF(v);
1525
+ ASSIGN(self->thread_id, v);
1526
+ return 0;
1527
+ }
1528
+ else if (! strcmp(name_s, "_context"))
1529
+ {
1530
+ Py_DECREF(name_as_bytes);
1531
+
1532
+ Py_INCREF(v);
1533
+ ASSIGN(self->context, v);
1534
+ return 0;
1535
+ }
1536
+ else if (! strcmp(name_s, "_policy"))
1537
+ {
1538
+ Py_DECREF(name_as_bytes);
1539
+
1540
+ Py_INCREF(v);
1541
+ ASSIGN(self->policy, v);
1542
+ if (self->validate)
1543
+ {
1544
+ Py_DECREF(self->validate);
1545
+ self->validate=0;
1546
+ }
1547
+ if (self->checkPermission)
1548
+ {
1549
+ Py_DECREF(self->checkPermission);
1550
+ self->checkPermission=0;
1551
+ }
1552
+ return 0;
1553
+ }
1554
+ }
1555
+ }
1556
+
1557
+ Py_XDECREF(name_as_bytes);
1558
+ PyErr_SetObject(PyExc_AttributeError, name);
1559
+ return -1;
1560
+ }
1561
+
1562
+
1563
+
1564
+
1565
+ /*
1566
+ ** PermissionRole_init
1567
+ **
1568
+ */
1569
+
1570
+ static PyObject *PermissionRole_init(PermissionRole *self, PyObject *args) {
1571
+
1572
+ PyObject *name = NULL;
1573
+ PyObject *deflt = NULL;
1574
+
1575
+ /*|def __init__(self, name, default=('Manager',)):
1576
+ **| self.__name__ = name
1577
+ **| self._p = "_" + string.translate(name, name_trans) + "_Permission"
1578
+ **| self._d = default
1579
+ */
1580
+
1581
+ if (unpacktuple2(args, "__init__", 1, &name, &deflt) < 0) return NULL;
1582
+
1583
+ if (deflt == NULL) deflt = defaultPermission;
1584
+
1585
+ UNLESS(self->_p = permissionName(name)) return NULL;
1586
+
1587
+ self->__name__ = name;
1588
+ Py_INCREF(name);
1589
+
1590
+ self->__roles__ = deflt;
1591
+ Py_INCREF(deflt);
1592
+
1593
+ Py_INCREF(Py_None);
1594
+ return Py_None;
1595
+ }
1596
+
1597
+ /*
1598
+ def __of__(self, parent):
1599
+ */
1600
+ static PyObject *
1601
+ PermissionRole_of(PermissionRole *self, PyObject *parent) {
1602
+
1603
+ imPermissionRole *r = NULL;
1604
+ PyObject *_p = NULL;
1605
+ PyObject *result = NULL;
1606
+
1607
+ /*| r = imPermissionRole()
1608
+ */
1609
+
1610
+ r = (imPermissionRole*)PyObject_CallObject(imPermissionRoleObj, NULL);
1611
+ if (r == NULL) return NULL;
1612
+
1613
+ /*| r._p = self._p
1614
+ */
1615
+
1616
+ r->_p = self->_p;
1617
+ Py_INCREF(r->_p);
1618
+
1619
+ /*| r._pa = parent
1620
+ */
1621
+
1622
+ r->_pa = parent;
1623
+ Py_INCREF(parent);
1624
+
1625
+ /*| r._d = self._d
1626
+ */
1627
+
1628
+ r->__roles__ = self->__roles__;
1629
+ Py_INCREF(r->__roles__);
1630
+
1631
+
1632
+ /*| p = getattr(parent, 'aq_inner', None)
1633
+ **| if p is not None:
1634
+ **| return r.__of__(p)
1635
+ **| else:
1636
+ **| return r
1637
+ */
1638
+
1639
+
1640
+ if (aq_isWrapper(parent)) {
1641
+ _p = aq_inner(parent);
1642
+ result = callmethod1(OBJECT(r), __of__, _p);
1643
+ Py_DECREF(_p);
1644
+ /* Dont need goto */
1645
+ } else {
1646
+ result = OBJECT(r);
1647
+ Py_INCREF(r);
1648
+ }
1649
+
1650
+ Py_DECREF(r);
1651
+
1652
+ return result;
1653
+ }
1654
+
1655
+ /*
1656
+ def rolesForPermissionOn(self, value):
1657
+ return rolesForPermissionOn(None, value, self._d, self._p)
1658
+ */
1659
+ static PyObject *
1660
+ PermissionRole_rolesForPermissionOn(PermissionRole *self, PyObject *value)
1661
+ {
1662
+ return c_rolesForPermissionOn(NULL, value, self->__roles__, self->_p);
1663
+ }
1664
+
1665
+
1666
+
1667
+ /*
1668
+ ** PermissionRole_dealloc
1669
+ **
1670
+ */
1671
+
1672
+ static void PermissionRole_dealloc(PermissionRole *self) {
1673
+
1674
+ Py_XDECREF(self->__name__);
1675
+
1676
+ Py_XDECREF(self->_p);
1677
+
1678
+ Py_XDECREF(self->__roles__);
1679
+
1680
+ Py_XDECREF(Py_TYPE(self)); /* Extensionclass init incref'd */
1681
+
1682
+ Py_TYPE(self)->tp_free((PyObject*) self);
1683
+ }
1684
+
1685
+
1686
+ /* for DocFinder */
1687
+ /*
1688
+ ** PermissionRole_getattro
1689
+ **
1690
+ */
1691
+
1692
+ static PyObject *
1693
+ PermissionRole_getattro(PermissionRole *self, PyObject *name) {
1694
+ PyObject *name_as_bytes = NULL;
1695
+ PyObject *result = NULL;
1696
+ char *name_s = NULL;
1697
+
1698
+ if ((name_as_bytes = convert_name(name)) != NULL) {
1699
+ name_s = PyBytes_AS_STRING(name_as_bytes);
1700
+ }
1701
+
1702
+ /* see whether we know the attribute */
1703
+ /* we support both the old "_d" (from the Python implementation)
1704
+ and the new "__roles__"
1705
+ */
1706
+
1707
+ if (name_s == NULL)
1708
+ PyErr_Clear(); /* defer to ExtensionClassGetattro */
1709
+ else if (name_s[0] == '_') {
1710
+ if (! strcmp(name_s, "__name__"))
1711
+ result = self->__name__;
1712
+ else if (! strcmp(name_s, "__roles__"))
1713
+ result = self->__roles__;
1714
+ else if (! strcmp(name_s, "_p"))
1715
+ result = self->_p;
1716
+ else if (! strcmp(name_s, "_d"))
1717
+ result = self->__roles__;
1718
+ }
1719
+
1720
+ Py_XDECREF(name_as_bytes);
1721
+
1722
+ if (result) {
1723
+ Py_INCREF(result);
1724
+ return result;
1725
+ }
1726
+ else
1727
+ return ExtensionClassGetattro((PyObject *)self, name);
1728
+ }
1729
+
1730
+
1731
+ /*
1732
+ def __of__(self, value):
1733
+ return rolesForPermissionOn(None, value, self._d, self._p)
1734
+ */
1735
+ static PyObject *
1736
+ imPermissionRole_of(imPermissionRole *self, PyObject *value) {
1737
+ return c_rolesForPermissionOn(NULL, value, self->__roles__, self->_p);
1738
+ }
1739
+
1740
+ /*
1741
+ ** imPermissionRole_length
1742
+ */
1743
+ static Py_ssize_t imPermissionRole_length(imPermissionRole *self) {
1744
+
1745
+ Py_ssize_t l;
1746
+ PyObject *v;
1747
+ PyObject *pa;
1748
+
1749
+ /*|
1750
+ **| try:
1751
+ **| v=self._v
1752
+ **| except:
1753
+ **| v = self._v = self.__of__(self._pa)
1754
+ **| del self._pa
1755
+ **|
1756
+ **| return len(v)
1757
+ */
1758
+
1759
+ v = self->_v;
1760
+ if (v == NULL) {
1761
+ pa = self->_pa;
1762
+ if (pa == NULL) {
1763
+ PyErr_SetString(PyExc_AttributeError, "_pa");
1764
+ return -1;
1765
+ }
1766
+ v = callmethod1(OBJECT(self), __of__, pa);
1767
+ if (v == NULL)
1768
+ return -1;
1769
+ self->_v = v;
1770
+ Py_DECREF(pa);
1771
+ self->_pa = NULL;
1772
+ }
1773
+
1774
+ l = PyObject_Length(v);
1775
+
1776
+ return l;
1777
+ }
1778
+
1779
+ /*
1780
+ ** imPermissionRole_get
1781
+ */
1782
+
1783
+ static PyObject *imPermissionRole_get(imPermissionRole *self,
1784
+ Py_ssize_t item) {
1785
+
1786
+ PyObject *v;
1787
+ PyObject *pa;
1788
+ PyObject *result;
1789
+
1790
+ /*| try:
1791
+ **| v = self._v
1792
+ **| except:
1793
+ **| v = self._v = self.__of__(self._pa)
1794
+ **| del self._pa
1795
+ **| return v[i]
1796
+ */
1797
+
1798
+ v = self->_v;
1799
+
1800
+ if (v == NULL) {
1801
+ pa = self->_pa;
1802
+ if (pa == NULL) {
1803
+ PyErr_SetString(PyExc_AttributeError, "_pa");
1804
+ return NULL;
1805
+ }
1806
+ v = callmethod1(OBJECT(self), __of__, pa);
1807
+ if (v == NULL)
1808
+ return NULL;
1809
+ self->_v = v;
1810
+ Py_DECREF(pa);
1811
+ self->_pa = NULL;
1812
+ }
1813
+
1814
+ result = PySequence_GetItem(v, item);
1815
+
1816
+ return result;
1817
+ }
1818
+
1819
+ /*
1820
+ ** imPermissionRole_dealloc
1821
+ **
1822
+ */
1823
+
1824
+ static void imPermissionRole_dealloc(imPermissionRole *self) {
1825
+ Py_XDECREF(self->_p);
1826
+
1827
+ Py_XDECREF(self->_pa);
1828
+
1829
+ Py_XDECREF(self->__roles__);
1830
+
1831
+ Py_XDECREF(self->_v);
1832
+
1833
+ Py_DECREF(Py_TYPE(self)); /* Extensionclass init incref'd */
1834
+
1835
+ Py_TYPE(self)->tp_free((PyObject*) self);
1836
+ }
1837
+
1838
+ /*
1839
+ ** rolesForPermissionOn
1840
+ */
1841
+
1842
+ static PyObject *rolesForPermissionOn(PyObject *self, PyObject *args) {
1843
+ PyObject *perm = NULL;
1844
+ PyObject *object = NULL;
1845
+ PyObject *deflt = NULL;
1846
+ PyObject *n = NULL;
1847
+
1848
+ /*|def rolesForPermissionOn(perm, object, default=('Manager',)):
1849
+ **|
1850
+ **| """Return the roles that have the permisson on the given object"""
1851
+ */
1852
+
1853
+ if (unpacktuple4(args, "rolesForPermissionOn", 2,
1854
+ &perm, &object, &deflt, &n) < 0)
1855
+ return NULL;
1856
+ return c_rolesForPermissionOn(perm, object, deflt, n);
1857
+ }
1858
+
1859
+ /*
1860
+ def rolesForPermissionOn(perm, object, default=_default_roles, n=None):
1861
+ """Return the roles that have the given permission on the given object
1862
+ """
1863
+ */
1864
+ static PyObject *
1865
+ c_rolesForPermissionOn(PyObject *perm, PyObject *object,
1866
+ PyObject *_default_roles, PyObject *n)
1867
+ {
1868
+ PyObject *r, *result = NULL;
1869
+
1870
+ /*
1871
+ n = n or '_' + string.translate(perm, name_trans) + "_Permission"
1872
+ */
1873
+ if (n)
1874
+ Py_INCREF(n);
1875
+ else
1876
+ {
1877
+ n = permissionName(perm);
1878
+ if (n == NULL)
1879
+ return NULL;
1880
+ }
1881
+
1882
+ Py_INCREF(object);
1883
+
1884
+ /*
1885
+ r = None
1886
+ */
1887
+ r = Py_None;
1888
+ Py_INCREF(r);
1889
+
1890
+ /*
1891
+ while True:
1892
+ */
1893
+ while (1)
1894
+ {
1895
+ /*
1896
+ try:
1897
+ roles = getattr(object, n)
1898
+ except (AttributeError, zExceptions_Unauthorized):
1899
+ pass
1900
+ else:
1901
+ */
1902
+ PyObject *roles = PyObject_GetAttr(object, n);
1903
+ if (roles == NULL)
1904
+ {
1905
+ if (! (PyErr_ExceptionMatches(PyExc_AttributeError)
1906
+ || PyErr_ExceptionMatches(zExceptions_Unauthorized)))
1907
+ {
1908
+ /* re-raise */
1909
+ return NULL;
1910
+ }
1911
+ }
1912
+ if (roles != NULL)
1913
+ {
1914
+
1915
+ /*
1916
+ if roles is None:
1917
+ return 'Anonymous',
1918
+ */
1919
+ if (roles == Py_None)
1920
+ {
1921
+ Py_DECREF(roles);
1922
+ result = AnonymousTuple;
1923
+ Py_INCREF(result);
1924
+ goto end;
1925
+ }
1926
+
1927
+ /*
1928
+ t = type(roles)
1929
+ */
1930
+ /*
1931
+ if t is tuple:
1932
+ # If we get a tuple, then we don't acquire
1933
+ if r is None:
1934
+ return roles
1935
+ return r+list(roles)
1936
+ */
1937
+ if (PyTuple_Check(roles))
1938
+ {
1939
+ if (r == Py_None)
1940
+ result = roles;
1941
+ else
1942
+ {
1943
+ PyObject *list_roles = PySequence_List(roles);
1944
+ Py_DECREF(roles);
1945
+ if (list_roles == NULL)
1946
+ goto end;
1947
+ result = PySequence_Concat(r, list_roles);
1948
+ Py_DECREF(list_roles);
1949
+ }
1950
+ goto end;
1951
+ }
1952
+
1953
+
1954
+ /*
1955
+ if t is str:
1956
+ # We found roles set to a name. Start over
1957
+ # with the new permission name. If the permission
1958
+ # name is '', then treat as private!
1959
+ if roles:
1960
+ n = roles
1961
+ else:
1962
+ return _what_not_even_god_should_do
1963
+ */
1964
+ if (NATIVE_CHECK(roles))
1965
+ {
1966
+ if (NATIVE_GET_SIZE(roles))
1967
+ {
1968
+ Py_DECREF(n);
1969
+ n = roles;
1970
+ }
1971
+ else
1972
+ {
1973
+ Py_DECREF(roles);
1974
+ result = _what_not_even_god_should_do;
1975
+ Py_INCREF(result);
1976
+ goto end;
1977
+ }
1978
+ }
1979
+
1980
+ /*
1981
+ elif roles:
1982
+ if r is None:
1983
+ r = list(roles)
1984
+ else: r = r + list(roles)
1985
+ */
1986
+ else
1987
+ {
1988
+ int roles_is_true = PyObject_IsTrue(roles);
1989
+ if (roles_is_true < 0)
1990
+ goto end;
1991
+ if (roles_is_true)
1992
+ {
1993
+ PyObject *list_roles = PySequence_List(roles);
1994
+ Py_DECREF(roles);
1995
+ if (list_roles == NULL)
1996
+ goto end;
1997
+ if (r == Py_None)
1998
+ {
1999
+ Py_DECREF(r);
2000
+ r = list_roles;
2001
+ }
2002
+ else
2003
+ {
2004
+ PyObject *tmp = PySequence_Concat(r, list_roles);
2005
+ Py_DECREF(list_roles);
2006
+ if (tmp == NULL)
2007
+ goto end;
2008
+ Py_DECREF(r);
2009
+ r = tmp;
2010
+ }
2011
+ }
2012
+ }
2013
+ }
2014
+
2015
+ else /* roles == NULL */
2016
+ PyErr_Clear();
2017
+
2018
+
2019
+ /*
2020
+ object = aq_inner(object)
2021
+ if object is None:
2022
+ break
2023
+ object = aq_parent(object)
2024
+ */
2025
+ {
2026
+ PyObject *tobj = aq_inner(object);
2027
+ if (tobj == NULL)
2028
+ goto end;
2029
+ Py_DECREF(object);
2030
+ object = tobj;
2031
+
2032
+ if (object == Py_None)
2033
+ break;
2034
+
2035
+ tobj = aq_parent(object);
2036
+ if (tobj == NULL)
2037
+ goto end;
2038
+ Py_DECREF(object);
2039
+ object = tobj;
2040
+ }
2041
+ }
2042
+
2043
+ /*
2044
+ if r is None:
2045
+ return default
2046
+
2047
+ return r
2048
+ */
2049
+
2050
+ if (r == Py_None)
2051
+ {
2052
+ result = _default_roles;
2053
+ if (result == NULL)
2054
+ result = ManagerTuple;
2055
+ Py_INCREF(result);
2056
+ goto end;
2057
+ }
2058
+
2059
+ Py_INCREF(r);
2060
+ result = r;
2061
+
2062
+ end:
2063
+ Py_DECREF(n);
2064
+ Py_DECREF(object);
2065
+ Py_DECREF(r);
2066
+ return result;
2067
+ }
2068
+
2069
+
2070
+ /*
2071
+ ** permissionName
2072
+ **
2073
+ ** Can silently truncate permission names if they are really long
2074
+ */
2075
+
2076
+ static PyObject *permissionName(PyObject *name) {
2077
+ PyObject *name_as_bytes;
2078
+ char namebuff[512];
2079
+ register int len = sizeof(namebuff) - 1;
2080
+ char *c = namebuff;
2081
+ char *in;
2082
+ char r;
2083
+
2084
+ *c = '_';
2085
+
2086
+ c++;
2087
+ len--;
2088
+
2089
+ if ((name_as_bytes = convert_name(name)) == NULL) {
2090
+ return NULL;
2091
+ }
2092
+
2093
+ in = PyBytes_AS_STRING(name_as_bytes);
2094
+
2095
+ while (len && *in) {
2096
+ r = *(in++);
2097
+ if (!isalnum(r)) r='_';
2098
+ *(c++) = r;
2099
+ len--;
2100
+ }
2101
+
2102
+ if (len) {
2103
+ in = "_Permission";
2104
+ while (len && *in) {
2105
+ *(c++) = *(in++);
2106
+ len--;
2107
+ }
2108
+ }
2109
+
2110
+ *c = '\0'; /* Saved room in len */
2111
+
2112
+ Py_DECREF(name_as_bytes);
2113
+ return NATIVE_FROM_STRING(namebuff);
2114
+ }
2115
+
2116
+ /* def guarded_getattr(inst, name, default=_marker): */
2117
+ static PyObject *
2118
+ guarded_getattr(PyObject *inst, PyObject *name, PyObject *default_,
2119
+ PyObject *validate)
2120
+ {
2121
+ PyObject *v=0, *t=0;
2122
+ int i;
2123
+ int starts_with_underscore;
2124
+
2125
+ /* if name[:1] != '_': */
2126
+ if (NATIVE_CHECK(name) || PyUnicode_Check(name)) {
2127
+ PyObject *name_as_bytes = convert_name(name);
2128
+ if (name_as_bytes == NULL) {
2129
+ return NULL;
2130
+ }
2131
+
2132
+ starts_with_underscore = PyBytes_AS_STRING(name_as_bytes)[0] == '_';
2133
+ Py_DECREF(name_as_bytes);
2134
+
2135
+ if (!starts_with_underscore)
2136
+ {
2137
+ /*
2138
+ Special-case str.format / str.format_map so a str subclass cannot
2139
+ escape the SafeFormatter guard (GHSA-pq59-9fq7-m886).
2140
+
2141
+ Gate on the receiver type FIRST: is_string_format_name() converts
2142
+ `name` to bytes (an allocation) and must not run on every attribute
2143
+ access -- only when inst is a str (or subclass), or a type that
2144
+ might be a str subclass. PyUnicode_Check / PyType_Check are just
2145
+ tp_flags bit tests, so the common case stays free.
2146
+ */
2147
+ if (PyUnicode_Check(inst) || PyType_Check(inst))
2148
+ {
2149
+ int string_format_name;
2150
+
2151
+ string_format_name = is_string_format_name(name);
2152
+ if (string_format_name < 0) {
2153
+ return NULL;
2154
+ }
2155
+ if (string_format_name) {
2156
+ if (PyUnicode_Check(inst)) {
2157
+ /* str instance or subclass -> guarded formatter */
2158
+ return guarded_string_format(inst, name);
2159
+ }
2160
+ else {
2161
+ /* inst is a type: forbid format access on str and subclasses */
2162
+ int string_subclass;
2163
+
2164
+ string_subclass = PyObject_IsSubclass(
2165
+ inst, OBJECT(&PyUnicode_Type));
2166
+ if (string_subclass < 0) {
2167
+ return NULL;
2168
+ }
2169
+ if (string_subclass) {
2170
+ PyErr_SetObject(Unauthorized, name);
2171
+ return NULL;
2172
+ }
2173
+ }
2174
+ }
2175
+ }
2176
+
2177
+ /*
2178
+ # Try to get the attribute normally so that unusual
2179
+ # exceptions are caught early.
2180
+ try: v = getattr(inst, name)
2181
+ except AttributeError:
2182
+ if default is not _marker:
2183
+ return default
2184
+ raise
2185
+ */
2186
+
2187
+ v=PyObject_GetAttr(inst, name);
2188
+ if (v==NULL)
2189
+ {
2190
+ if (default_ && PyErr_Occurred() == PyExc_AttributeError)
2191
+ {
2192
+ PyErr_Clear();
2193
+ Py_INCREF(default_);
2194
+ return default_;
2195
+ }
2196
+ return NULL;
2197
+ }
2198
+
2199
+ /*
2200
+
2201
+ assertion = Containers(type(inst))
2202
+ */
2203
+ t = PyDict_GetItem(ContainerAssertions, OBJECT(Py_TYPE(inst)));
2204
+ if (t != NULL)
2205
+ {
2206
+
2207
+ /*
2208
+ if isinstance(assertion, dict):
2209
+ # We got a table that lets us reason about individual
2210
+ # attrs
2211
+ assertion = assertion.get(name)
2212
+ if assertion:
2213
+ # There's an entry, but it may be a function.
2214
+ if callable(assertion):
2215
+ return assertion(inst, name)
2216
+
2217
+ # Nope, it's boolean
2218
+ return v
2219
+ raise Unauthorized, name
2220
+ */
2221
+ if (PyDict_Check(t))
2222
+ {
2223
+ PyObject *attrv;
2224
+
2225
+ attrv = PyDict_GetItem(t, name);
2226
+ if (attrv != NULL)
2227
+ {
2228
+ i=PyObject_IsTrue(attrv);
2229
+ if (i < 0) goto err;
2230
+ if (i)
2231
+ {
2232
+ if (Py_TYPE(attrv)->tp_call)
2233
+ {
2234
+ Py_DECREF(v);
2235
+ v = callfunction2(attrv, inst, name);
2236
+ return v;
2237
+ }
2238
+ return v;
2239
+ }
2240
+ }
2241
+ Py_DECREF(v);
2242
+ goto unauth;
2243
+ }
2244
+
2245
+ /*
2246
+ if assertion:
2247
+ if callable(assertion):
2248
+ factory = assertion(name, v)
2249
+ if callable(factory):
2250
+ return factory(inst, name)
2251
+ assert factory == 1
2252
+ assert callable == 1
2253
+ return v
2254
+
2255
+ */
2256
+ if (PyCallable_Check(t))
2257
+ {
2258
+ PyObject *factory;
2259
+
2260
+ factory = callfunction2(t, name, v);
2261
+ if (factory == NULL)
2262
+ goto err;
2263
+
2264
+ if (PyCallable_Check(factory))
2265
+ {
2266
+ Py_DECREF(v);
2267
+ v = callfunction2(factory, inst, name);
2268
+ }
2269
+ Py_DECREF(factory);
2270
+ }
2271
+ return v;
2272
+ }
2273
+
2274
+ /*
2275
+ # See if we can get the value doing a filtered acquire.
2276
+ # aq_acquire will either return the same value as held by
2277
+ # v or it will return an Unauthorized raised by validate.
2278
+ validate = SecurityManagement.getSecurityManager().validate
2279
+ aq_acquire(inst, name, aq_validate, validate)
2280
+
2281
+ return v
2282
+ */
2283
+
2284
+ t = aq_Acquire(inst, name, aq_validate, validate, 1, NULL, 0);
2285
+ if (t == NULL)
2286
+ {
2287
+ Py_DECREF(v);
2288
+ return NULL;
2289
+ }
2290
+ Py_DECREF(t);
2291
+
2292
+ return v;
2293
+
2294
+ unauthErr(name, v);
2295
+ err:
2296
+ Py_DECREF(v);
2297
+ return NULL;
2298
+ }
2299
+ } else {
2300
+ /* raise TypeError, name */
2301
+ PyErr_SetObject(PyExc_TypeError, name);
2302
+ return NULL;
2303
+ }
2304
+
2305
+ unauth:
2306
+ /* raise Unauthorized, name */
2307
+ PyErr_SetObject(Unauthorized, name);
2308
+ return NULL;
2309
+ }
2310
+
2311
+ static PyObject *
2312
+ module_guarded_getattr(PyObject *ignored, PyObject *args)
2313
+ {
2314
+ PyObject *inst, *name, *default_=0, *validate;
2315
+
2316
+ if (unpacktuple3(args, "guarded_getattr", 2, &inst, &name, &default_) < 0)
2317
+ return NULL;
2318
+
2319
+ /*
2320
+ validate = getSecurityManager().validate
2321
+ */
2322
+ validate=PyObject_CallObject(getSecurityManager, NULL);
2323
+ if (! validate) return NULL;
2324
+ ASSIGN(validate, PyObject_GetAttr(validate, validate_str));
2325
+ if (! validate) return NULL;
2326
+ ASSIGN(validate, guarded_getattr(inst, name, default_, validate));
2327
+
2328
+ return validate;
2329
+ }
2330
+
2331
+ /*
2332
+ def aq_validate(inst, obj, name, v, validate):
2333
+ return validate(inst, obj, name, v)
2334
+ */
2335
+ static PyObject *
2336
+ module_aq_validate(PyObject *ignored, PyObject *args)
2337
+ {
2338
+ PyObject *inst=NULL, *obj=NULL, *name=NULL, *v=NULL, *validate=NULL;
2339
+
2340
+ if (unpacktuple5(args, "validate", 0,
2341
+ &inst, &obj, &name, &v, &validate) < 0) return NULL;
2342
+ return callfunction4(validate, inst, obj, name, v);
2343
+ }
2344
+
2345
+ static PyObject *
2346
+ module_setDefaultBehaviors(PyObject *ignored, PyObject *args)
2347
+ {
2348
+ PyObject *result = NULL;
2349
+ int own, auth, verbose;
2350
+
2351
+ if (PyArg_ParseTuple(args, "iii:setDefaultBehaviors", &own, &auth,
2352
+ &verbose)) {
2353
+ if (verbose) {
2354
+ PyErr_SetString(PyExc_NotImplementedError,
2355
+ "This security policy implementation does not implement "
2356
+ "the verbose option. To enable verbose security "
2357
+ "exceptions, add 'security-policy-implementation "
2358
+ "python' to etc/zope.conf.");
2359
+ return NULL;
2360
+ }
2361
+ ownerous = own;
2362
+ authenticated = auth;
2363
+ result = Py_None;
2364
+ Py_INCREF(result);
2365
+ }
2366
+ return result;
2367
+ }
2368
+
2369
+ static PyObject *
2370
+ dtml_guarded_getattr(PyObject *self, PyObject *args)
2371
+ {
2372
+ PyObject *ob, *name, *default_=0, *validate;
2373
+
2374
+ if (unpacktuple3(args, "guarded_getattr", 2, &ob, &name, &default_) < 0)
2375
+ return NULL;
2376
+
2377
+
2378
+ UNLESS (validate = PyObject_GetAttr(self, validate_str))
2379
+ {
2380
+ /* This section is pure paranoia at this point. It was necessary
2381
+ while debugging. */
2382
+ PyErr_Clear();
2383
+ validate=PyObject_CallObject(getSecurityManager, NULL);
2384
+ if (! validate) return NULL;
2385
+ ASSIGN(validate, PyObject_GetAttr(validate, validate_str));
2386
+ if (! validate) return NULL;
2387
+ }
2388
+
2389
+ ASSIGN(validate, guarded_getattr(ob, name, default_, validate));
2390
+
2391
+ return validate;
2392
+ }
2393
+
2394
+
2395
+ static struct PyMethodDef dtml_methods[] = {
2396
+ {"guarded_getattr", (PyCFunction)dtml_guarded_getattr,
2397
+ METH_VARARGS|METH_KEYWORDS, "" },
2398
+ {NULL, NULL}
2399
+ };
2400
+
2401
+
2402
+ /* ----------------------------------------------------------------
2403
+ ** Module initialization
2404
+ ** ----------------------------------------------------------------
2405
+ */
2406
+ #define IMPORT(module, name) if ((module = PyImport_ImportModule(name)) == NULL) return NULL;
2407
+ #define GETATTR(module, name) if ((name = PyObject_GetAttrString(module, #name)) == NULL) return NULL;
2408
+ #define GETATTR_AS(module, name, as_name) if ((as_name = PyObject_GetAttrString(module, name)) == NULL) return NULL;
2409
+
2410
+ static struct PyModuleDef moduledef =
2411
+ {
2412
+ PyModuleDef_HEAD_INIT,
2413
+ "cAccessControl", /* m_name */
2414
+ "cAccessControl.c\n", /* m_doc */
2415
+ -1, /* m_size */
2416
+ cAccessControl_methods, /* m_methods */
2417
+ NULL, /* m_reload */
2418
+ NULL, /* m_traverse */
2419
+ NULL, /* m_clear */
2420
+ NULL, /* m_free */
2421
+ };
2422
+
2423
+ static PyObject*
2424
+ module_init(void) {
2425
+ PyObject *tmp;
2426
+ PyObject *module;
2427
+ PyObject *dict;
2428
+ PURE_MIXIN_CLASS(RestrictedDTMLMixin,
2429
+ "A mix-in for derivatives of DT_String.String "
2430
+ "that adds Zope security."
2431
+ , dtml_methods);
2432
+
2433
+ if (!ExtensionClassImported) return NULL;
2434
+
2435
+ if (ZopeSecurityPolicy_setup() < 0) return NULL;
2436
+
2437
+ ExtensionClassGetattro= Py_FindAttr;
2438
+
2439
+ module = PyModule_Create(&moduledef);
2440
+
2441
+ aq_init(); /* For Python <= 2.1.1, aq_init() should be after
2442
+ Py_InitModule(). */
2443
+
2444
+ dict = PyModule_GetDict(module);
2445
+
2446
+ PyDict_SetItemString(dict, "_what_not_even_god_should_do",
2447
+ _what_not_even_god_should_do);
2448
+
2449
+ PyExtensionClass_Export(dict, "RestrictedDTMLMixin",
2450
+ RestrictedDTMLMixinType);
2451
+
2452
+ PyExtensionClass_Export(dict, "ZopeSecurityPolicy",
2453
+ ZopeSecurityPolicyType);
2454
+
2455
+ PyExtensionClass_Export(dict,"SecurityManager",
2456
+ SecurityManagerType);
2457
+
2458
+ PyExtensionClass_Export(dict, "PermissionRole",
2459
+ PermissionRoleType);
2460
+
2461
+ PyExtensionClass_Export(dict, "imPermissionRole",
2462
+ imPermissionRoleType);
2463
+
2464
+ imPermissionRoleObj = PyMapping_GetItemString(dict,
2465
+ "imPermissionRole");
2466
+
2467
+ aq_validate = PyMapping_GetItemString(dict, "aq_validate");
2468
+
2469
+ /*| from SimpleObjectPolicies import Containers
2470
+ */
2471
+
2472
+ IMPORT(tmp, "AccessControl.SimpleObjectPolicies");
2473
+ GETATTR(tmp, Containers);
2474
+ GETATTR(tmp, ContainerAssertions);
2475
+ Py_DECREF(tmp);
2476
+ tmp = NULL;
2477
+
2478
+
2479
+ /*| from ZopeSecurityPolicy import getRoles
2480
+ */
2481
+
2482
+ IMPORT(tmp, "AccessControl.ZopeSecurityPolicy");
2483
+ GETATTR(tmp, getRoles);
2484
+ Py_DECREF(tmp);
2485
+ tmp = NULL;
2486
+
2487
+
2488
+ /*| from unauthorized import Unauthorized
2489
+ */
2490
+
2491
+ IMPORT(tmp, "AccessControl.unauthorized");
2492
+ GETATTR(tmp, Unauthorized);
2493
+ Py_DECREF(tmp);
2494
+ tmp = NULL;
2495
+
2496
+ /*| from zExceptions import Unauthorized as zExceptions_Unauthorized
2497
+ */
2498
+
2499
+ IMPORT(tmp, "zExceptions");
2500
+ GETATTR_AS(tmp, "Unauthorized", zExceptions_Unauthorized);
2501
+ Py_DECREF(tmp);
2502
+ tmp = NULL;
2503
+
2504
+ /*| from AccessControl.SecurityManagement import getSecurityManager
2505
+ */
2506
+
2507
+ IMPORT(tmp, "AccessControl.SecurityManagement");
2508
+ GETATTR(tmp, getSecurityManager);
2509
+ Py_DECREF(tmp);
2510
+ tmp = NULL;
2511
+
2512
+ /*| from logger_wrapper import warn
2513
+ */
2514
+
2515
+ IMPORT(tmp, "AccessControl.logger_wrapper");
2516
+ GETATTR(tmp, warn);
2517
+ Py_DECREF(tmp);
2518
+ tmp = NULL;
2519
+
2520
+ return module;
2521
+ }
2522
+
2523
+ PyMODINIT_FUNC PyInit_cAccessControl(void)
2524
+ {
2525
+ return module_init();
2526
+ }