python/py_spoolss_printers.c

説明を見る。
00001 /* 
00002    Python wrappers for DCERPC/SMB client routines.
00003 
00004    Copyright (C) Tim Potter, 2002
00005    
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015    
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 */
00020 
00021 #include "python/py_spoolss.h"
00022 
00023 /* Open a printer */
00024 
00025 PyObject *spoolss_openprinter(PyObject *self, PyObject *args, PyObject *kw)
00026 {
00027         char *unc_name, *server, *errstr;
00028         TALLOC_CTX *mem_ctx = NULL;
00029         POLICY_HND hnd;
00030         WERROR werror;
00031         PyObject *result = NULL, *creds = NULL;
00032         static char *kwlist[] = { "printername", "creds", "access", NULL };
00033         uint32 desired_access = MAXIMUM_ALLOWED_ACCESS;
00034         struct cli_state *cli;
00035 
00036         if (!PyArg_ParseTupleAndKeywords(
00037                     args, kw, "s|Oi", kwlist, &unc_name, &creds,
00038                     &desired_access))
00039                 return NULL;
00040 
00041         if (unc_name[0] != '\\' || unc_name[1] != '\\') {
00042                 PyErr_SetString(PyExc_ValueError, "UNC name required");
00043                 return NULL;
00044         }
00045 
00046         server = SMB_STRDUP(unc_name + 2);
00047 
00048         if (strchr(server, '\\')) {
00049                 char *c = strchr(server, '\\');
00050                 *c = 0;
00051         }
00052 
00053         if (creds && creds != Py_None && !PyDict_Check(creds)) {
00054                 PyErr_SetString(PyExc_TypeError, 
00055                                 "credentials must be dictionary or None");
00056                 return NULL;
00057         }
00058 
00059         if (!(cli = open_pipe_creds(server, creds, PI_SPOOLSS, &errstr))) {
00060                 PyErr_SetString(spoolss_error, errstr);
00061                 free(errstr);
00062                 goto done;
00063         }
00064 
00065         if (!(mem_ctx = talloc_init("spoolss_openprinter"))) {
00066                 PyErr_SetString(spoolss_error, 
00067                                 "unable to init talloc context\n");
00068                 goto done;
00069         }
00070 
00071         werror = rpccli_spoolss_open_printer_ex(
00072                 cli->pipe_list, mem_ctx, unc_name, "", desired_access, server, 
00073                 "", &hnd);
00074 
00075         if (!W_ERROR_IS_OK(werror)) {
00076                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
00077                 goto done;
00078         }
00079 
00080         result = new_spoolss_policy_hnd_object(cli, mem_ctx, &hnd);
00081 
00082  done:
00083         if (!result) {
00084                 if (cli)
00085                         cli_shutdown(cli);
00086 
00087                 if (mem_ctx)
00088                         talloc_destroy(mem_ctx);
00089         }
00090 
00091         SAFE_FREE(server);
00092 
00093         return result;
00094 }
00095 
00096 /* Close a printer */
00097 
00098 PyObject *spoolss_closeprinter(PyObject *self, PyObject *args)
00099 {
00100         PyObject *po;
00101         spoolss_policy_hnd_object *hnd;
00102         WERROR result;
00103 
00104         /* Parse parameters */
00105 
00106         if (!PyArg_ParseTuple(args, "O!", &spoolss_policy_hnd_type, &po))
00107                 return NULL;
00108 
00109         hnd = (spoolss_policy_hnd_object *)po;
00110 
00111         /* Call rpc function */
00112 
00113         result = rpccli_spoolss_close_printer(
00114                 hnd->cli, hnd->mem_ctx, &hnd->pol);
00115 
00116         /* Return value */
00117 
00118         Py_INCREF(Py_None);
00119         return Py_None; 
00120 }
00121 
00122 /* Fetch printer information */
00123 
00124 PyObject *spoolss_hnd_getprinter(PyObject *self, PyObject *args, PyObject *kw)
00125 {
00126         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
00127         WERROR werror;
00128         PyObject *result = NULL;
00129         PRINTER_INFO_CTR ctr;
00130         int level = 1;
00131         static char *kwlist[] = {"level", NULL};
00132         
00133         /* Parse parameters */
00134 
00135         if (!PyArg_ParseTupleAndKeywords(args, kw, "|i", kwlist, &level))
00136                 return NULL;
00137         
00138         ZERO_STRUCT(ctr);
00139 
00140         /* Call rpc function */
00141         
00142         werror = rpccli_spoolss_getprinter(
00143                 hnd->cli, hnd->mem_ctx, &hnd->pol, level, &ctr);
00144 
00145         /* Return value */
00146 
00147         if (!W_ERROR_IS_OK(werror)) {
00148                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
00149                 return NULL;
00150         }
00151 
00152         result = Py_None;
00153 
00154         switch (level) {
00155                 
00156         case 0:
00157                 py_from_PRINTER_INFO_0(&result, ctr.printers_0);
00158                 break;
00159 
00160         case 1:
00161                 py_from_PRINTER_INFO_1(&result, ctr.printers_1);
00162                 break;
00163 
00164         case 2:
00165                 py_from_PRINTER_INFO_2(&result, ctr.printers_2);
00166                 break;
00167 
00168         case 3:
00169                 py_from_PRINTER_INFO_3(&result, ctr.printers_3);
00170                 break;
00171         }
00172 
00173         Py_INCREF(result);
00174         return result;
00175 }
00176 
00177 /* Set printer information */
00178 
00179 PyObject *spoolss_hnd_setprinter(PyObject *self, PyObject *args, PyObject *kw)
00180 {
00181         spoolss_policy_hnd_object *hnd = (spoolss_policy_hnd_object *)self;
00182         WERROR werror;
00183         PyObject *info;
00184         PRINTER_INFO_CTR ctr;
00185         uint32 level;
00186         static char *kwlist[] = {"dict", NULL};
00187         union {
00188                 PRINTER_INFO_1 printers_1;
00189                 PRINTER_INFO_2 printers_2;
00190                 PRINTER_INFO_3 printers_3;
00191         } pinfo;
00192 
00193         /* Parse parameters */
00194 
00195         if (!PyArg_ParseTupleAndKeywords(
00196                     args, kw, "O!", kwlist, &PyDict_Type, &info))
00197                 return NULL;
00198         
00199         if (!get_level_value(info, &level)) {
00200                 PyErr_SetString(spoolss_error, "invalid info level");
00201                 return NULL;
00202         }
00203 
00204         if (level < 1 && level > 3) {
00205                 PyErr_SetString(spoolss_error, "unsupported info level");
00206                 return NULL;
00207         }
00208 
00209         /* Fill in printer info */
00210 
00211         ZERO_STRUCT(ctr);
00212 
00213         switch (level) {
00214         case 1:
00215                 ctr.printers_1 = &pinfo.printers_1;
00216 
00217                 if (!py_to_PRINTER_INFO_1(ctr.printers_1, info)){
00218                         PyErr_SetString(spoolss_error, 
00219                                         "error converting printer to info 1");
00220                         return NULL;
00221                 }
00222 
00223                 break;
00224         case 2:
00225                 ctr.printers_2 = &pinfo.printers_2;
00226 
00227                 if (!py_to_PRINTER_INFO_2(ctr.printers_2, info,
00228                                           hnd->mem_ctx)){
00229                         PyErr_SetString(spoolss_error, 
00230                                         "error converting printer to info 2");
00231                         return NULL;
00232                 }
00233 
00234                 break;
00235         case 3:
00236                 ctr.printers_3 = &pinfo.printers_3;
00237 
00238                 if (!py_to_PRINTER_INFO_3(ctr.printers_3, info,
00239                                           hnd->mem_ctx)) {
00240                         PyErr_SetString(spoolss_error,
00241                                         "error converting to printer info 3");
00242                         return NULL;
00243                 }
00244 
00245                 break;
00246         default:
00247                 PyErr_SetString(spoolss_error, "unsupported info level");
00248                 return NULL;
00249         }
00250 
00251         /* Call rpc function */
00252         
00253         werror = rpccli_spoolss_setprinter(
00254                 hnd->cli, hnd->mem_ctx, &hnd->pol, level, &ctr, 0);
00255 
00256         /* Return value */
00257 
00258         if (!W_ERROR_IS_OK(werror)) {
00259                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
00260                 return NULL;
00261         }
00262 
00263         Py_INCREF(Py_None);
00264         return Py_None;
00265 }
00266 
00267 /* Enumerate printers */
00268 
00269 PyObject *spoolss_enumprinters(PyObject *self, PyObject *args, PyObject *kw)
00270 {
00271         WERROR werror;
00272         PyObject *result = NULL, *creds = NULL;
00273         PRINTER_INFO_CTR ctr;
00274         int level = 1, flags = PRINTER_ENUM_LOCAL, i;
00275         uint32 num_printers;
00276         static char *kwlist[] = {"server", "name", "level", "flags", 
00277                                  "creds", NULL};
00278         TALLOC_CTX *mem_ctx = NULL;
00279         struct cli_state *cli = NULL;
00280         char *server, *errstr, *name = NULL;
00281 
00282         /* Parse parameters */
00283 
00284         if (!PyArg_ParseTupleAndKeywords(
00285                     args, kw, "s|siiO", kwlist, &server, &name, &level, 
00286                     &flags, &creds))
00287                 return NULL;
00288         
00289         if (server[0] != '\\' || server[1] != '\\') {
00290                 PyErr_SetString(PyExc_ValueError, "UNC name required");
00291                 return NULL;
00292         }
00293 
00294         server += 2;
00295 
00296         if (creds && creds != Py_None && !PyDict_Check(creds)) {
00297                 PyErr_SetString(PyExc_TypeError, 
00298                                 "credentials must be dictionary or None");
00299                 return NULL;
00300         }
00301 
00302         if (!(cli = open_pipe_creds(server, creds, PI_SPOOLSS, &errstr))) {
00303                 PyErr_SetString(spoolss_error, errstr);
00304                 free(errstr);
00305                 goto done;
00306         }
00307 
00308         if (!(mem_ctx = talloc_init("spoolss_enumprinters"))) {
00309                 PyErr_SetString(
00310                         spoolss_error, "unable to init talloc context\n");
00311                 goto done;
00312         }
00313 
00314         /* This RPC is weird.  By setting the server name to different
00315            values we can get different behaviour.  If however the server
00316            name is not specified, we default it to being the full server
00317            name as this is probably what the caller intended.  To pass a
00318            NULL name, pass a value of "" */
00319 
00320         if (!name)
00321                 name = server;
00322         else {
00323                 if (!name[0])
00324                         name = NULL;
00325         }
00326 
00327         /* Call rpc function */
00328         
00329         werror = rpccli_spoolss_enum_printers(
00330                 cli->pipe_list, mem_ctx, name, flags, level, &num_printers, &ctr);
00331 
00332         if (!W_ERROR_IS_OK(werror)) {
00333                 PyErr_SetObject(spoolss_werror, py_werror_tuple(werror));
00334                 goto done;
00335         }
00336 
00337         /* Return value */
00338         
00339         switch (level) {
00340         case 0: 
00341                 result = PyDict_New();
00342 
00343                 for (i = 0; i < num_printers; i++) {
00344                         PyObject *value;
00345                         fstring s;
00346 
00347                         rpcstr_pull(s, ctr.printers_0[i].printername.buffer,
00348                                     sizeof(fstring), -1, STR_TERMINATE);
00349 
00350                         py_from_PRINTER_INFO_0(&value, &ctr.printers_0[i]);
00351 
00352                         PyDict_SetItemString(
00353                                 value, "level", PyInt_FromLong(0));
00354 
00355                         PyDict_SetItemString(result, s, value);
00356                 }
00357 
00358                 break;
00359         case 1:
00360                 result = PyDict_New();
00361 
00362                 for(i = 0; i < num_printers; i++) {
00363                         PyObject *value;
00364                         fstring s;
00365 
00366                         rpcstr_pull(s, ctr.printers_1[i].name.buffer,
00367                                     sizeof(fstring), -1, STR_TERMINATE);
00368 
00369                         py_from_PRINTER_INFO_1(&value, &ctr.printers_1[i]);
00370 
00371                         PyDict_SetItemString(
00372                                 value, "level", PyInt_FromLong(1));
00373 
00374                         PyDict_SetItemString(result, s, value);
00375                 }
00376                 
00377                 break;
00378         case 2:
00379                 result = PyDict_New();
00380 
00381                 for(i = 0; i < num_printers; i++) {
00382                         PyObject *value;
00383                         fstring s;
00384 
00385                         rpcstr_pull(s, ctr.printers_2[i].printername.buffer,
00386                                     sizeof(fstring), -1, STR_TERMINATE);
00387 
00388                         py_from_PRINTER_INFO_2(&value, &ctr.printers_2[i]);
00389 
00390                         PyDict_SetItemString(
00391                                 value, "level", PyInt_FromLong(2));
00392 
00393                         PyDict_SetItemString(result, s, value);
00394                 }
00395                 
00396                 break;
00397         default:
00398                 PyErr_SetString(spoolss_error, "unknown info level");
00399                 goto done;
00400         }
00401 
00402 done:
00403         if (cli)
00404                 cli_shutdown(cli);
00405 
00406         if (mem_ctx)
00407                 talloc_destroy(mem_ctx);
00408 
00409         return result;
00410 }
00411 
00412 /* Add a printer */
00413 
00414 PyObject *spoolss_addprinterex(PyObject *self, PyObject *args, PyObject *kw)
00415 {
00416         static char *kwlist[] = { "server", "printername", "info", "creds", 
00417                                   NULL};
00418         char *printername, *server, *errstr;
00419         PyObject *info, *result = NULL, *creds = NULL;
00420         struct cli_state *cli = NULL;
00421         TALLOC_CTX *mem_ctx = NULL;
00422         PRINTER_INFO_CTR ctr;
00423         PRINTER_INFO_2 info2;
00424         WERROR werror;
00425 
00426         if (!PyArg_ParseTupleAndKeywords(
00427                     args, kw, "ssO!|O!", kwlist, &server, &printername,
00428                     &PyDict_Type, &info, &PyDict_Type, &creds))
00429                 return NULL;
00430 
00431         if (!(cli = open_pipe_creds(server, creds, PI_SPOOLSS, &errstr))) {
00432                 PyErr_SetString(spoolss_error, errstr);
00433                 free(errstr);
00434                 goto done;
00435         }
00436 
00437         if (!(mem_ctx = talloc_init("spoolss_addprinterex"))) {
00438                 PyErr_SetString(
00439                         spoolss_error, "unable to init talloc context\n");
00440                 goto done;
00441         }
00442 
00443         if (!py_to_PRINTER_INFO_2(&info2, info, mem_ctx)) {
00444                 PyErr_SetString(spoolss_error,
00445                                 "error converting to printer info 2");
00446                 goto done;
00447         }
00448 
00449         ctr.printers_2 = &info2;
00450 
00451         werror = rpccli_spoolss_addprinterex(cli->pipe_list, mem_ctx, 2, &ctr);
00452 
00453         Py_INCREF(Py_None);
00454         result = Py_None;
00455 
00456 done:
00457         if (cli)
00458                 cli_shutdown(cli);
00459 
00460         if (mem_ctx)
00461                 talloc_destroy(mem_ctx);
00462 
00463         return result;
00464 }

Sambaに対してSat Aug 29 21:23:14 2009に生成されました。  doxygen 1.4.7