PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/release/src/router/samba3/source/python/py_spoolss_printers.c

https://gitlab.com/envieidoc/tomato
C | 464 lines | 310 code | 111 blank | 43 comment | 56 complexity | e5166cb57c279acd4323c4bbd28b023e MD5 | raw file
  1. /*
  2. Python wrappers for DCERPC/SMB client routines.
  3. Copyright (C) Tim Potter, 2002
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include "python/py_spoolss.h"
  17. /* Open a printer */
  18. PyObject *spoolss_openprinter(PyObject *self, PyObject *args, PyObject *kw)
  19. {
  20. char *unc_name, *server, *errstr;
  21. TALLOC_CTX *mem_ctx = NULL;
  22. POLICY_HND hnd;
  23. WERROR werror;
  24. PyObject *result = NULL, *creds = NULL;
  25. static char *kwlist[] = { "printername", "creds", "access", NULL };
  26. uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
  27. struct cli_state *cli;
  28. if (!PyArg_ParseTupleAndKeywords(
  29. args, kw, "s|Oi", kwlist, &unc_name, &creds,
  30. &desired_access))
  31. return NULL;
  32. if (unc_name[0] != '\\' || unc_name[1] != '\\') {
  33. PyErr_SetString(PyExc_ValueError, "UNC name required");
  34. return NULL;
  35. }
  36. server = SMB_STRDUP(unc_name + 2);
  37. if (strchr(server, '\\')) {
  38. char *c = strchr(server, '\\');
  39. *c = 0;
  40. }
  41. if (creds && creds != Py_None && !PyDict_Check(creds)) {
  42. PyErr_SetString(PyExc_TypeError,
  43. "credentials must be dictionary or None");
  44. return NULL;
  45. }
  46. if (!(cli = open_pipe_creds(server, creds, PI_SPOOLSS, &errstr))) {
  47. PyErr_SetString(spoolss_error, errstr);
  48. free(errstr);
  49. goto done;
  50. }
  51. if (!(mem_ctx = talloc_init("spoolss_openprinter"))) {
  52. PyErr_SetString(spoolss_error,
  53. "unable to init talloc context\n");
  54. goto done;
  55. }
  56. werror = rpccli_spoolss_open_printer_ex(
  57. cli->pipe_list, mem_ctx, unc_name, "", desired_access, server,
  58. "", &hnd);
  59. if (!W_ERROR_IS_OK(werror)) {
  60. PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
  61. goto done;
  62. }
  63. result = new_spoolss_policy_hnd_object(cli, mem_ctx, &hnd);
  64. done:
  65. if (!result) {
  66. if (cli)
  67. cli_shutdown(cli);
  68. if (mem_ctx)
  69. talloc_destroy(mem_ctx);
  70. }
  71. SAFE_FREE(server);
  72. return result;
  73. }
  74. /* Close a printer */
  75. PyObject *spoolss_closeprinter(PyObject *self, PyObject *args)
  76. {
  77. PyObject *po;
  78. spoolss_policy_hnd_object *hnd;
  79. WERROR result;
  80. /* Parse parameters */
  81. if (!PyArg_ParseTuple(args, "O!", &spoolss_policy_hnd_type, &po))
  82. return NULL;
  83. hnd = (spoolss_policy_hnd_object *)po;
  84. /* Call rpc function */
  85. result = rpccli_spoolss_close_printer(
  86. hnd->cli, hnd->mem_ctx, &hnd->pol);
  87. /* Return value */
  88. Py_INCREF(Py_None);
  89. return Py_None;
  90. }
  91. /* Fetch printer information */
  92. PyObject *spoolss_hnd_getprinter(PyObject *self, PyObject *args, PyObject *kw)
  93. {
  94. spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
  95. WERROR werror;
  96. PyObject *result = NULL;
  97. PRINTER_INFO_CTR ctr;
  98. int level = 1;
  99. static char *kwlist[] = {"level", NULL};
  100. /* Parse parameters */
  101. if (!PyArg_ParseTupleAndKeywords(args, kw, "|i", kwlist, &level))
  102. return NULL;
  103. ZERO_STRUCT(ctr);
  104. /* Call rpc function */
  105. werror = rpccli_spoolss_getprinter(
  106. hnd->cli, hnd->mem_ctx, &hnd->pol, level, &ctr);
  107. /* Return value */
  108. if (!W_ERROR_IS_OK(werror)) {
  109. PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
  110. return NULL;
  111. }
  112. result = Py_None;
  113. switch (level) {
  114. case 0:
  115. py_from_PRINTER_INFO_0(&result, ctr.printers_0);
  116. break;
  117. case 1:
  118. py_from_PRINTER_INFO_1(&result, ctr.printers_1);
  119. break;
  120. case 2:
  121. py_from_PRINTER_INFO_2(&result, ctr.printers_2);
  122. break;
  123. case 3:
  124. py_from_PRINTER_INFO_3(&result, ctr.printers_3);
  125. break;
  126. }
  127. Py_INCREF(result);
  128. return result;
  129. }
  130. /* Set printer information */
  131. PyObject *spoolss_hnd_setprinter(PyObject *self, PyObject *args, PyObject *kw)
  132. {
  133. spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
  134. WERROR werror;
  135. PyObject *info;
  136. PRINTER_INFO_CTR ctr;
  137. uint32 level;
  138. static char *kwlist[] = {"dict", NULL};
  139. union {
  140. PRINTER_INFO_1 printers_1;
  141. PRINTER_INFO_2 printers_2;
  142. PRINTER_INFO_3 printers_3;
  143. } pinfo;
  144. /* Parse parameters */
  145. if (!PyArg_ParseTupleAndKeywords(
  146. args, kw, "O!", kwlist, &PyDict_Type, &info))
  147. return NULL;
  148. if (!get_level_value(info, &level)) {
  149. PyErr_SetString(spoolss_error, "invalid info level");
  150. return NULL;
  151. }
  152. if (level < 1 && level > 3) {
  153. PyErr_SetString(spoolss_error, "unsupported info level");
  154. return NULL;
  155. }
  156. /* Fill in printer info */
  157. ZERO_STRUCT(ctr);
  158. switch (level) {
  159. case 1:
  160. ctr.printers_1 = &pinfo.printers_1;
  161. if (!py_to_PRINTER_INFO_1(ctr.printers_1, info)){
  162. PyErr_SetString(spoolss_error,
  163. "error converting printer to info 1");
  164. return NULL;
  165. }
  166. break;
  167. case 2:
  168. ctr.printers_2 = &pinfo.printers_2;
  169. if (!py_to_PRINTER_INFO_2(ctr.printers_2, info,
  170. hnd->mem_ctx)){
  171. PyErr_SetString(spoolss_error,
  172. "error converting printer to info 2");
  173. return NULL;
  174. }
  175. break;
  176. case 3:
  177. ctr.printers_3 = &pinfo.printers_3;
  178. if (!py_to_PRINTER_INFO_3(ctr.printers_3, info,
  179. hnd->mem_ctx)) {
  180. PyErr_SetString(spoolss_error,
  181. "error converting to printer info 3");
  182. return NULL;
  183. }
  184. break;
  185. default:
  186. PyErr_SetString(spoolss_error, "unsupported info level");
  187. return NULL;
  188. }
  189. /* Call rpc function */
  190. werror = rpccli_spoolss_setprinter(
  191. hnd->cli, hnd->mem_ctx, &hnd->pol, level, &ctr, 0);
  192. /* Return value */
  193. if (!W_ERROR_IS_OK(werror)) {
  194. PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
  195. return NULL;
  196. }
  197. Py_INCREF(Py_None);
  198. return Py_None;
  199. }
  200. /* Enumerate printers */
  201. PyObject *spoolss_enumprinters(PyObject *self, PyObject *args, PyObject *kw)
  202. {
  203. WERROR werror;
  204. PyObject *result = NULL, *creds = NULL;
  205. PRINTER_INFO_CTR ctr;
  206. int level = 1, flags = PRINTER_ENUM_LOCAL, i;
  207. uint32 num_printers;
  208. static char *kwlist[] = {"server", "name", "level", "flags",
  209. "creds", NULL};
  210. TALLOC_CTX *mem_ctx = NULL;
  211. struct cli_state *cli = NULL;
  212. char *server, *errstr, *name = NULL;
  213. /* Parse parameters */
  214. if (!PyArg_ParseTupleAndKeywords(
  215. args, kw, "s|siiO", kwlist, &server, &name, &level,
  216. &flags, &creds))
  217. return NULL;
  218. if (server[0] != '\\' || server[1] != '\\') {
  219. PyErr_SetString(PyExc_ValueError, "UNC name required");
  220. return NULL;
  221. }
  222. server += 2;
  223. if (creds && creds != Py_None && !PyDict_Check(creds)) {
  224. PyErr_SetString(PyExc_TypeError,
  225. "credentials must be dictionary or None");
  226. return NULL;
  227. }
  228. if (!(cli = open_pipe_creds(server, creds, PI_SPOOLSS, &errstr))) {
  229. PyErr_SetString(spoolss_error, errstr);
  230. free(errstr);
  231. goto done;
  232. }
  233. if (!(mem_ctx = talloc_init("spoolss_enumprinters"))) {
  234. PyErr_SetString(
  235. spoolss_error, "unable to init talloc context\n");
  236. goto done;
  237. }
  238. /* This RPC is weird. By setting the server name to different
  239. values we can get different behaviour. If however the server
  240. name is not specified, we default it to being the full server
  241. name as this is probably what the caller intended. To pass a
  242. NULL name, pass a value of "" */
  243. if (!name)
  244. name = server;
  245. else {
  246. if (!name[0])
  247. name = NULL;
  248. }
  249. /* Call rpc function */
  250. werror = rpccli_spoolss_enum_printers(
  251. cli->pipe_list, mem_ctx, name, flags, level, &num_printers, &ctr);
  252. if (!W_ERROR_IS_OK(werror)) {
  253. PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
  254. goto done;
  255. }
  256. /* Return value */
  257. switch (level) {
  258. case 0:
  259. result = PyDict_New();
  260. for (i = 0; i < num_printers; i++) {
  261. PyObject *value;
  262. fstring s;
  263. rpcstr_pull(s, ctr.printers_0[i].printername.buffer,
  264. sizeof(fstring), -1, STR_TERMINATE);
  265. py_from_PRINTER_INFO_0(&value, &ctr.printers_0[i]);
  266. PyDict_SetItemString(
  267. value, "level", PyInt_FromLong(0));
  268. PyDict_SetItemString(result, s, value);
  269. }
  270. break;
  271. case 1:
  272. result = PyDict_New();
  273. for(i = 0; i < num_printers; i++) {
  274. PyObject *value;
  275. fstring s;
  276. rpcstr_pull(s, ctr.printers_1[i].name.buffer,
  277. sizeof(fstring), -1, STR_TERMINATE);
  278. py_from_PRINTER_INFO_1(&value, &ctr.printers_1[i]);
  279. PyDict_SetItemString(
  280. value, "level", PyInt_FromLong(1));
  281. PyDict_SetItemString(result, s, value);
  282. }
  283. break;
  284. case 2:
  285. result = PyDict_New();
  286. for(i = 0; i < num_printers; i++) {
  287. PyObject *value;
  288. fstring s;
  289. rpcstr_pull(s, ctr.printers_2[i].printername.buffer,
  290. sizeof(fstring), -1, STR_TERMINATE);
  291. py_from_PRINTER_INFO_2(&value, &ctr.printers_2[i]);
  292. PyDict_SetItemString(
  293. value, "level", PyInt_FromLong(2));
  294. PyDict_SetItemString(result, s, value);
  295. }
  296. break;
  297. default:
  298. PyErr_SetString(spoolss_error, "unknown info level");
  299. goto done;
  300. }
  301. done:
  302. if (cli)
  303. cli_shutdown(cli);
  304. if (mem_ctx)
  305. talloc_destroy(mem_ctx);
  306. return result;
  307. }
  308. /* Add a printer */
  309. PyObject *spoolss_addprinterex(PyObject *self, PyObject *args, PyObject *kw)
  310. {
  311. static char *kwlist[] = { "server", "printername", "info", "creds",
  312. NULL};
  313. char *printername, *server, *errstr;
  314. PyObject *info, *result = NULL, *creds = NULL;
  315. struct cli_state *cli = NULL;
  316. TALLOC_CTX *mem_ctx = NULL;
  317. PRINTER_INFO_CTR ctr;
  318. PRINTER_INFO_2 info2;
  319. WERROR werror;
  320. if (!PyArg_ParseTupleAndKeywords(
  321. args, kw, "ssO!|O!", kwlist, &server, &printername,
  322. &PyDict_Type, &info, &PyDict_Type, &creds))
  323. return NULL;
  324. if (!(cli = open_pipe_creds(server, creds, PI_SPOOLSS, &errstr))) {
  325. PyErr_SetString(spoolss_error, errstr);
  326. free(errstr);
  327. goto done;
  328. }
  329. if (!(mem_ctx = talloc_init("spoolss_addprinterex"))) {
  330. PyErr_SetString(
  331. spoolss_error, "unable to init talloc context\n");
  332. goto done;
  333. }
  334. if (!py_to_PRINTER_INFO_2(&info2, info, mem_ctx)) {
  335. PyErr_SetString(spoolss_error,
  336. "error converting to printer info 2");
  337. goto done;
  338. }
  339. ctr.printers_2 = &info2;
  340. werror = rpccli_spoolss_addprinterex(cli->pipe_list, mem_ctx, 2, &ctr);
  341. Py_INCREF(Py_None);
  342. result = Py_None;
  343. done:
  344. if (cli)
  345. cli_shutdown(cli);
  346. if (mem_ctx)
  347. talloc_destroy(mem_ctx);
  348. return result;
  349. }