Include errors.i verbatim in security.i, as it's the only file still using it.
[jra/samba/.git] / source4 / libcli / security / security.i
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
4    
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9    
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14    
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 %module(docstring="Security-related classes.",package="samba.security") security
20
21 %{
22 #include "includes.h"
23 #include "libcli/security/security.h"
24
25 typedef struct dom_sid dom_sid;
26 typedef struct security_token security_token;
27 typedef struct security_descriptor security_descriptor;
28 %}
29
30 %import "../lib/talloc/talloc.i"
31 %{
32 #include "libcli/util/pyerrors.h"
33 %}
34
35 %typemap(out,noblock=1) WERROR {
36     if (!W_ERROR_IS_OK($1)) {
37         PyErr_SetWERROR($1);
38         SWIG_fail;
39     } else if ($result == NULL) {
40         $result = Py_None;
41     }
42 };
43
44 %typemap(out,noblock=1) NTSTATUS {
45     if (NT_STATUS_IS_ERR($1)) {
46         PyErr_SetNTSTATUS($1);
47         SWIG_fail;
48     } else if ($result == NULL) {
49         $result = Py_None;
50     }
51 };
52
53 %typemap(in,noblock=1) NTSTATUS {
54         if (PyLong_Check($input))
55                 $1 = NT_STATUS(PyLong_AsUnsignedLong($input));
56         else if (PyInt_Check($input))
57                 $1 = NT_STATUS(PyInt_AsLong($input));
58         else {
59                 PyErr_SetString(PyExc_TypeError, "Expected a long or an int");
60                 return NULL;
61         }
62 }
63
64 %import "stdint.i"
65
66 enum sec_privilege {
67         SEC_PRIV_SECURITY=1,
68         SEC_PRIV_BACKUP=2,
69         SEC_PRIV_RESTORE=3,
70         SEC_PRIV_SYSTEMTIME=4,
71         SEC_PRIV_SHUTDOWN=5,
72         SEC_PRIV_REMOTE_SHUTDOWN=6,
73         SEC_PRIV_TAKE_OWNERSHIP=7,
74         SEC_PRIV_DEBUG=8,
75         SEC_PRIV_SYSTEM_ENVIRONMENT=9,
76         SEC_PRIV_SYSTEM_PROFILE=10,
77         SEC_PRIV_PROFILE_SINGLE_PROCESS=11,
78         SEC_PRIV_INCREASE_BASE_PRIORITY=12,
79         SEC_PRIV_LOAD_DRIVER=13,
80         SEC_PRIV_CREATE_PAGEFILE=14,
81         SEC_PRIV_INCREASE_QUOTA=15,
82         SEC_PRIV_CHANGE_NOTIFY=16,
83         SEC_PRIV_UNDOCK=17,
84         SEC_PRIV_MANAGE_VOLUME=18,
85         SEC_PRIV_IMPERSONATE=19,
86         SEC_PRIV_CREATE_GLOBAL=20,
87         SEC_PRIV_ENABLE_DELEGATION=21,
88         SEC_PRIV_INTERACTIVE_LOGON=22,
89         SEC_PRIV_NETWORK_LOGON=23,
90         SEC_PRIV_REMOTE_INTERACTIVE_LOGON=24
91 };
92
93 %rename(SecurityToken) security_token;
94
95 %talloctype(security_token);
96
97 typedef struct security_token { 
98     %extend {
99         security_token(TALLOC_CTX *mem_ctx) { return security_token_initialise(mem_ctx); }
100         %feature("docstring") is_sid "S.is_sid(sid) -> bool\n" \
101             "Check whether this token is of the specified SID.";
102         bool is_sid(const struct dom_sid *sid);
103         %feature("docstring") is_system "S.is_system() -> bool\n" \
104                           "Check whether this is a system token.";
105         bool is_system();
106         %feature("docstring") is_anonymous "S.is_anonymus() -> bool\n" \
107                           "Check whether this is an anonymous token.";
108         bool is_anonymous();
109         bool has_sid(const struct dom_sid *sid);
110         bool has_builtin_administrators();
111         bool has_nt_authenticated_users();
112         bool has_privilege(enum sec_privilege privilege);
113         void set_privilege(enum sec_privilege privilege);
114     }
115 } security_token;
116
117 %talloctype(security_descriptor);
118
119 typedef struct security_descriptor {
120     %extend {
121         security_descriptor(TALLOC_CTX *mem_ctx) { return security_descriptor_initialise(mem_ctx); }
122         %feature("docstring") sacl_add "S.sacl_add(ace) -> None\n" \
123                               "Add a security ace to this security descriptor";
124         NTSTATUS sacl_add(const struct security_ace *ace);
125         NTSTATUS dacl_add(const struct security_ace *ace);
126         NTSTATUS dacl_del(const struct dom_sid *trustee);
127         NTSTATUS sacl_del(const struct dom_sid *trustee);
128 #ifdef SWIGPYTHON
129         %rename(__eq__) equal;
130 #endif
131         bool equal(const struct security_descriptor *other);
132     }
133 } security_descriptor;
134
135 %rename(Sid) dom_sid;
136
137 %talloctype(dom_sid);
138
139 typedef struct dom_sid {
140     %immutable;
141     uint8_t sid_rev_num;
142     int8_t num_auths;/* [range(0,15)] */
143     uint8_t id_auth[6];
144     uint32_t *sub_auths;
145     %mutable;
146     %extend {
147         dom_sid(TALLOC_CTX *mem_ctx, const char *text) {
148             return dom_sid_parse_talloc(mem_ctx, text);
149         }
150 #ifdef SWIGPYTHON
151         const char *__str__(TALLOC_CTX *mem_ctx) {
152             return dom_sid_string(mem_ctx, $self);
153         }
154         %rename(__eq__) equal;
155 #endif
156         bool equal(const struct dom_sid *other);
157     }
158 %pythoncode {
159     def __repr__(self):
160         return "Sid(%r)" % str(self)
161 }
162 } dom_sid;
163
164 %feature("docstring") random_sid "random_sid() -> sid\n" \
165          "Generate a random SID";
166
167 %inline %{
168 static struct dom_sid *random_sid(TALLOC_CTX *mem_ctx)
169 {
170     char *str = talloc_asprintf(mem_ctx, "S-1-5-21-%u-%u-%u", 
171                                 (unsigned)generate_random(), 
172                                 (unsigned)generate_random(), 
173                                 (unsigned)generate_random());
174
175         return dom_sid_parse_talloc(mem_ctx, str);
176 }
177 %}
178
179 %rename(privilege_name) sec_privilege_name;
180 const char *sec_privilege_name(enum sec_privilege privilege);
181 %rename(privilege_id) sec_privilege_id;
182 enum sec_privilege sec_privilege_id(const char *name);