summaryrefslogtreecommitdiff
path: root/source3/python/py_lsa.c
blob: 5e805a91ad72a096673fbe2f5f870569a15ee00d (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
#include "includes.h"
#include "Python.h"

#include "python/py_lsa.h"
static void py_policy_hnd_dealloc(PyObject* self)
{
	PyObject_Del(self);
}

PyTypeObject lsa_policy_hnd_type = {
	PyObject_HEAD_INIT(NULL)
	0,
	"LSA Policy Handle",
	sizeof(lsa_policy_hnd_object),
	0,
	py_policy_hnd_dealloc, /*tp_dealloc*/
	0,          /*tp_print*/
	0,          /*tp_getattr*/
	0,          /*tp_setattr*/
	0,          /*tp_compare*/
	0,          /*tp_repr*/
	0,          /*tp_as_number*/
	0,          /*tp_as_sequence*/
	0,          /*tp_as_mapping*/
	0,          /*tp_hash */
};

PyObject *new_lsa_policy_hnd_object(struct cli_state *cli, TALLOC_CTX *mem_ctx,
				    POLICY_HND *pol)
{
	lsa_policy_hnd_object *o;

	o = PyObject_New(lsa_policy_hnd_object, &lsa_policy_hnd_type);

	o->cli = cli;
	o->mem_ctx = mem_ctx;
	memcpy(&o->pol, pol, sizeof(POLICY_HND));

	return (PyObject*)o;
}

/* 
 * Exceptions raised by this module 
 */

PyObject *lsa_error;		/* This indicates a non-RPC related error
				   such as name lookup failure */

PyObject *lsa_ntstatus;		/* This exception is raised when a RPC call
				   returns a status code other than
				   NT_STATUS_OK */

/*
 * Open/close lsa handles
 */

static PyObject *lsa_openpolicy(PyObject *self, PyObject *args, 
				PyObject *kw) 
{
	static char *kwlist[] = { "servername", "creds", "access", NULL };
	char *server_name;
	PyObject *creds = NULL, *result;
	uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
	struct cli_state *cli;
	NTSTATUS ntstatus;
	TALLOC_CTX *mem_ctx;
	POLICY_HND hnd;

	if (!PyArg_ParseTupleAndKeywords(
		args, kw, "s|O!i", kwlist, &server_name, &PyDict_Type,
		&creds, &desired_access))
		return NULL;

	if (!(cli = open_pipe_creds(server_name, creds, cli_lsa_initialise,
				    NULL))) {
		fprintf(stderr, "could not initialise cli state\n");
		return NULL;
	}

	if (!(mem_ctx = talloc_init())) {
		fprintf(stderr, "unable to initialise talloc context\n");
		return NULL;
	}

	ntstatus = cli_lsa_open_policy(cli, mem_ctx, True,
				       SEC_RIGHTS_MAXIMUM_ALLOWED, &hnd);

	if (!NT_STATUS_IS_OK(ntstatus)) {
		cli_shutdown(cli);
		SAFE_FREE(cli);
		PyErr_SetObject(lsa_ntstatus, py_ntstatus_tuple(ntstatus));
		return NULL;
	}

	result = new_lsa_policy_hnd_object(cli, mem_ctx, &hnd);

	return result;
}

static PyObject *lsa_close(PyObject *self, PyObject *args, PyObject *kw) 
{
	PyObject *po;
	lsa_policy_hnd_object *hnd;
	NTSTATUS result;

	/* Parse parameters */

	if (!PyArg_ParseTuple(args, "O!", &lsa_policy_hnd_type, &po))
		return NULL;

	hnd = (lsa_policy_hnd_object *)po;

	/* Call rpc function */

	result = cli_lsa_close(hnd->cli, hnd->mem_ctx, &hnd->pol);

	/* Cleanup samba stuf */

	cli_shutdown(hnd->cli);
	talloc_destroy(hnd->mem_ctx);

	/* Return value */

	Py_INCREF(Py_None);
	return Py_None;	
}

static PyObject *lsa_lookupnames(PyObject *self, PyObject *args, 
				 PyObject *kw) 
{
	return NULL;
}

static PyObject *lsa_lookupsids(PyObject *self, PyObject *args, 
				PyObject *kw) 
{
	return NULL;
}

/*
 * Method dispatch table
 */

static PyMethodDef lsa_methods[] = {

	/* Open/close lsa handles */
	
	{ "openpolicy", lsa_openpolicy, METH_VARARGS | METH_KEYWORDS, 
	  "Open a policy handle" },
	
	{ "close", lsa_close, METH_VARARGS, 
	  "Close a policy handle" },

	/* Name <-> SID resolution */

	{ "lookupnames", lsa_lookupnames, METH_VARARGS | METH_KEYWORDS,
	  "Look up SIDS from a list of names" },

	{ "lookupsids", lsa_lookupsids, METH_VARARGS | METH_KEYWORDS,
	  "Look up names from a list of SIDS" },

	{ NULL }
};

/*
 * Module initialisation 
*/

void initlsa(void)
{
	PyObject *module, *dict;

	/* Initialise module */

	module = Py_InitModule("lsa", lsa_methods);
	dict = PyModule_GetDict(module);

	lsa_error = PyErr_NewException("lsa.error", NULL, NULL);
	PyDict_SetItemString(dict, "error", lsa_error);

	lsa_ntstatus = PyErr_NewException("lsa.ntstatus", NULL, NULL);
	PyDict_SetItemString(dict, "ntstatus", lsa_ntstatus);

	/* Initialise policy handle object */

	lsa_policy_hnd_type.ob_type = &PyType_Type;

	/* Initialise constants */

//	const_init(dict);

	/* Do samba initialisation */

	py_samba_init();
}