2 * Unix SMB/Netbios implementation.
4 * RPC Pipe client / server routines
5 * Copyright (C) Andrew Tridgell 1992-2000,
6 * Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
7 * Copyright (C) Paul Ashton 1997-2000,
8 * Copyright (C) Jean Francois Micouleau 1998-2000,
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "rpc_parse.h"
27 #include "rpc_client.h"
30 extern int DEBUGLEVEL;
32 /****************************************************************************
33 do a SPOOLSS Enum Printer Drivers
34 ****************************************************************************/
35 uint32 spoolss_enum_printerdrivers(const char *srv_name, const char *environment,
36 uint32 level, NEW_BUFFER *buffer, uint32 offered,
37 uint32 *needed, uint32 *returned)
41 SPOOL_Q_ENUMPRINTERDRIVERS q_o;
42 SPOOL_R_ENUMPRINTERDRIVERS r_o;
43 TALLOC_CTX *ctx = prs_get_mem_context(&buffer->prs);
45 struct cli_connection *con = NULL;
47 if (!cli_connection_init(srv_name, PIPE_SPOOLSS, &con))
50 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, ctx, MARSHALL);
51 prs_init(&rbuf, 0, 4, ctx, UNMARSHALL);
53 /* create and send a MSRPC command with api SPOOLSS_ENUM_PRINTERS */
55 DEBUG(5,("SPOOLSS Enum Printer Drivers (Server: %s Environment: %s level: %d)\n",
56 srv_name, environment, level));
58 make_spoolss_q_enumprinterdrivers(&q_o, srv_name, environment,
59 level, buffer, offered);
61 /* turn parameters into data stream */
62 if (!spoolss_io_q_enumprinterdrivers("", &q_o, &buf, 0) ) {
66 cli_connection_unlink(con);
69 if(!rpc_con_pipe_req(con, SPOOLSS_ENUMPRINTERDRIVERS, &buf, &rbuf)) {
73 cli_connection_unlink(con);
79 prs_switch_type(&buffer->prs, UNMARSHALL);
80 prs_set_offset(&buffer->prs, 0);
83 if(!new_spoolss_io_r_enumprinterdrivers("", &r_o, &rbuf, 0)) {
85 cli_connection_unlink(con);
89 *returned=r_o.returned;
94 cli_connection_unlink(con);
99 /****************************************************************************
100 do a SPOOLSS Enum Printers
101 ****************************************************************************/
102 uint32 spoolss_enum_printers(uint32 flags, fstring srv_name, uint32 level,
103 NEW_BUFFER *buffer, uint32 offered,
104 uint32 *needed, uint32 *returned)
108 SPOOL_Q_ENUMPRINTERS q_o;
109 SPOOL_R_ENUMPRINTERS r_o;
110 TALLOC_CTX *ctx = prs_get_mem_context(&buffer->prs);
112 struct cli_connection *con = NULL;
114 if (!cli_connection_init(srv_name, PIPE_SPOOLSS, &con))
117 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, ctx, MARSHALL);
118 prs_init(&rbuf, 0, 4, ctx, UNMARSHALL);
120 /* create and send a MSRPC command with api SPOOLSS_ENUM_PRINTERS */
122 DEBUG(5,("SPOOLSS Enum Printers (Server: %s level: %d)\n", srv_name, level));
124 make_spoolss_q_enumprinters(&q_o, flags, "", level, buffer, offered);
126 /* turn parameters into data stream */
127 if (!spoolss_io_q_enumprinters("", &q_o, &buf, 0) ) {
131 cli_connection_unlink(con);
134 if(!rpc_con_pipe_req(con, SPOOLSS_ENUMPRINTERS, &buf, &rbuf)) {
138 cli_connection_unlink(con);
144 prs_switch_type(&buffer->prs, UNMARSHALL);
145 prs_set_offset(&buffer->prs, 0);
148 if(!new_spoolss_io_r_enumprinters("", &r_o, &rbuf, 0)) {
150 cli_connection_unlink(con);
154 *returned=r_o.returned;
159 cli_connection_unlink(con);
164 /****************************************************************************
165 do a SPOOLSS Enum Ports
166 ****************************************************************************/
167 uint32 spoolss_enum_ports(fstring srv_name, uint32 level,
168 NEW_BUFFER *buffer, uint32 offered,
169 uint32 *needed, uint32 *returned)
173 SPOOL_Q_ENUMPORTS q_o;
174 SPOOL_R_ENUMPORTS r_o;
175 TALLOC_CTX *ctx = prs_get_mem_context(&buffer->prs);
177 struct cli_connection *con = NULL;
179 if (!cli_connection_init(srv_name, PIPE_SPOOLSS, &con))
182 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, ctx, MARSHALL);
183 prs_init(&rbuf, 0, 4, ctx, UNMARSHALL);
185 /* create and send a MSRPC command with api SPOOLSS_ENUMPORTS */
187 DEBUG(5,("SPOOLSS Enum Ports (Server: %s level: %d)\n", srv_name, level));
189 make_spoolss_q_enumports(&q_o, "", level, buffer, offered);
191 /* turn parameters into data stream */
192 if (!spoolss_io_q_enumports("", &q_o, &buf, 0) ) {
196 cli_connection_unlink(con);
199 if(!rpc_con_pipe_req(con, SPOOLSS_ENUMPORTS, &buf, &rbuf)) {
203 cli_connection_unlink(con);
209 prs_switch_type(&buffer->prs, UNMARSHALL);
210 prs_set_offset(&buffer->prs, 0);
213 if(!new_spoolss_io_r_enumports("", &r_o, &rbuf, 0)) {
215 cli_connection_unlink(con);
219 *returned=r_o.returned;
224 cli_connection_unlink(con);
229 /****************************************************************************
230 do a SPOOLSS Enum Jobs
231 ****************************************************************************/
232 uint32 spoolss_enum_jobs(const POLICY_HND *hnd, uint32 firstjob, uint32 numofjobs,
233 uint32 level, NEW_BUFFER *buffer, uint32 offered,
234 uint32 *needed, uint32 *returned)
238 SPOOL_Q_ENUMJOBS q_o;
239 SPOOL_R_ENUMJOBS r_o;
240 TALLOC_CTX *ctx = prs_get_mem_context(&buffer->prs);
243 return NT_STATUS_INVALID_PARAMETER;
245 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, ctx, MARSHALL);
246 prs_init(&rbuf, 0, 4, ctx, UNMARSHALL);
248 /* create and send a MSRPC command with api SPOOLSS_ENUMJOBS */
250 DEBUG(5,("SPOOLSS Enum Jobs level: %d)\n", level));
252 make_spoolss_q_enumjobs(&q_o, hnd, firstjob, numofjobs, level, buffer, offered);
254 /* turn parameters into data stream */
255 if (!spoolss_io_q_enumjobs("", &q_o, &buf, 0)) {
260 if(!rpc_hnd_pipe_req(hnd, SPOOLSS_ENUMJOBS, &buf, &rbuf))
271 if(!spoolss_io_r_enumjobs("", &r_o, &rbuf, 0)) {
276 *returned=r_o.returned;
284 /***************************************************************************
285 do a SPOOLSS Enum printer datas
286 ****************************************************************************/
287 uint32 spoolss_enum_printerdata(const POLICY_HND *hnd, uint32 idx,
288 uint32 *valuelen, uint16 *value, uint32 *rvaluelen,
289 uint32 *type, uint32 *datalen, uint8 *data,
294 SPOOL_Q_ENUMPRINTERDATA q_o;
295 SPOOL_R_ENUMPRINTERDATA r_o;
296 TALLOC_CTX *mem_ctx = NULL;
299 return NT_STATUS_INVALID_PARAMETER;
301 if ((mem_ctx=talloc_init()) == NULL)
303 DEBUG(0,("msrpc_spoolss_enum_jobs: talloc_init failed!\n"));
306 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, mem_ctx, MARSHALL);
307 prs_init(&rbuf, 0, 4, mem_ctx, UNMARSHALL);
309 /* create and send a MSRPC command with api SPOOLSS_ENUMPRINTERDATA*/
311 DEBUG(4,("SPOOLSS Enum Printer data\n"));
313 make_spoolss_q_enumprinterdata(&q_o, hnd, idx, *valuelen, *datalen);
315 /* turn parameters into data stream */
316 if (!spoolss_io_q_enumprinterdata("", &q_o, &buf, 0)) {
321 if(!rpc_hnd_pipe_req(hnd, SPOOLSS_ENUMPRINTERDATA, &buf, &rbuf)) {
332 if(!spoolss_io_r_enumprinterdata("", &r_o, &rbuf, 0)) {
336 *valuelen=r_o.valuesize;
337 *rvaluelen=r_o.realvaluesize;
339 *datalen=r_o.datasize;
340 *rdatalen=r_o.realdatasize;
345 talloc_destroy(mem_ctx);
350 /****************************************************************************
351 do a SPOOLSS Enum printer datas
352 ****************************************************************************/
353 uint32 spoolss_getprinter(const POLICY_HND *hnd, uint32 level,
354 NEW_BUFFER *buffer, uint32 offered,
359 SPOOL_Q_GETPRINTER q_o;
360 SPOOL_R_GETPRINTER r_o;
361 TALLOC_CTX *ctx = prs_get_mem_context(&buffer->prs);
364 return NT_STATUS_INVALID_PARAMETER;
366 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, ctx, MARSHALL);
367 prs_init(&rbuf, 0, 4, ctx, UNMARSHALL);
369 /* create and send a MSRPC command with api SPOOLSS_ENUMJOBS */
371 DEBUG(5,("SPOOLSS Enum Printer data)\n"));
373 make_spoolss_q_getprinter(&q_o, hnd, level, buffer, offered);
375 /* turn parameters into data stream */
376 if (!spoolss_io_q_getprinter("", &q_o, &buf, 0)) {
381 if(!rpc_hnd_pipe_req(hnd, SPOOLSS_GETPRINTER, &buf, &rbuf)) {
389 prs_switch_type(&buffer->prs, UNMARSHALL);
390 prs_set_offset(&buffer->prs, 0);
393 if(!spoolss_io_r_getprinter("", &r_o, &rbuf, 0)) {
405 /****************************************************************************
406 do a SPOOLSS Enum printer driver
407 ****************************************************************************/
408 uint32 spoolss_getprinterdriver(const POLICY_HND *hnd,
409 const char *environment, uint32 level,
410 NEW_BUFFER *buffer, uint32 offered,
415 SPOOL_Q_GETPRINTERDRIVER2 q_o;
416 SPOOL_R_GETPRINTERDRIVER2 r_o;
417 TALLOC_CTX *ctx = prs_get_mem_context(&buffer->prs);
420 return NT_STATUS_INVALID_PARAMETER;
422 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, ctx, MARSHALL);
423 prs_init(&rbuf, 0, 4, ctx, UNMARSHALL);
425 /* create and send a MSRPC command with api SPOOLSS_ENUMJOBS */
427 DEBUG(5,("SPOOLSS Enum Printer driver)\n"));
429 make_spoolss_q_getprinterdriver2(&q_o, hnd, environment, level, 2, 0, buffer, offered);
431 /* turn parameters into data stream */
432 if (!spoolss_io_q_getprinterdriver2("", &q_o, &buf, 0)) {
437 if(!rpc_hnd_pipe_req(hnd, SPOOLSS_GETPRINTERDRIVER2, &buf, &rbuf)) {
445 prs_switch_type(&buffer->prs, UNMARSHALL);
446 prs_set_offset(&buffer->prs, 0);
449 if(!spoolss_io_r_getprinterdriver2("", &r_o, &rbuf, 0)) {
463 /****************************************************************************
464 do a SPOOLSS Open Printer Ex
465 ****************************************************************************/
466 BOOL spoolss_open_printer_ex( const char *printername,
467 const char *datatype, uint32 access_required,
468 const char *station, const char *username,
473 SPOOL_Q_OPEN_PRINTER_EX q_o;
474 BOOL valid_pol = False;
477 struct cli_connection *con = NULL;
478 TALLOC_CTX *mem_ctx = NULL;
480 memset(srv_name, 0, sizeof(srv_name));
481 fstrcpy(srv_name, printername);
483 s = strchr(&srv_name[2], '\\');
487 if (!cli_connection_init(srv_name, PIPE_SPOOLSS, &con))
493 if ((mem_ctx=talloc_init()) == NULL)
495 DEBUG(0,("msrpc_spoolss_enum_jobs: talloc_init failed!\n"));
498 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, mem_ctx, MARSHALL);
499 prs_init(&rbuf, 0, 4, mem_ctx, UNMARSHALL);
501 /* create and send a MSRPC command with api SPOOLSS_OPENPRINTEREX */
503 DEBUG(5,("SPOOLSS Open Printer Ex\n"));
505 make_spoolss_q_open_printer_ex(&q_o, printername, datatype,
506 access_required, station, username);
508 /* turn parameters into data stream */
509 if (spoolss_io_q_open_printer_ex("", &q_o, &buf, 0) &&
510 rpc_con_pipe_req(con, SPOOLSS_OPENPRINTEREX, &buf, &rbuf))
512 SPOOL_R_OPEN_PRINTER_EX r_o;
515 spoolss_io_r_open_printer_ex("", &r_o, &rbuf, 0);
517 if (prs_offset(&rbuf)!= 0 && r_o.status != 0)
519 /* report error code */
520 DEBUG(5,("SPOOLSS_OPENPRINTEREX: %s\n", get_nt_error_msg(r_o.status)));
526 /* ok, at last: we're happy. return the policy handle */
529 /* associate the handle returned with the current
530 state of the clienjt connection */
531 valid_pol = RpcHndList_set_connection(hnd, con);
539 talloc_destroy(mem_ctx);
544 /****************************************************************************
545 do a SPOOLSS AddPrinterEx()
546 **ALWAYS** uses as PRINTER_INFO level 2 struct
547 ****************************************************************************/
548 BOOL spoolss_addprinterex(POLICY_HND *hnd, PRINTER_INFO_2 *info2)
553 SPOOL_Q_ADDPRINTEREX q_o;
554 BOOL valid_pol = False;
557 struct cli_connection *con = NULL;
560 memset(srv_name, 0, sizeof(srv_name));
561 unistr_to_ascii(srv_name, info2->servername.buffer, sizeof(srv_name));
563 if (!cli_connection_init(srv_name, PIPE_SPOOLSS, &con))
566 if (hnd == NULL) return False;
568 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, MARSHALL);
569 prs_init(&rbuf, 0, 4, UNMARSHALL);
575 /****************************************************************************
577 ****************************************************************************/
578 BOOL spoolss_closeprinter(POLICY_HND *hnd)
582 SPOOL_Q_CLOSEPRINTER q_c;
583 BOOL valid_close = False;
584 TALLOC_CTX *mem_ctx = NULL;
589 /* create and send a MSRPC command with api SPOOLSS_CLOSEPRINTER */
590 if ((mem_ctx=talloc_init()) == NULL)
592 DEBUG(0,("msrpc_spoolss_enum_jobs: talloc_init failed!\n"));
595 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, mem_ctx, MARSHALL);
596 prs_init(&rbuf, 0, 4, mem_ctx, UNMARSHALL);
598 DEBUG(4,("SPOOL Close Printer\n"));
600 /* store the parameters */
601 make_spoolss_q_closeprinter(&q_c, hnd);
603 /* turn parameters into data stream */
604 if (spoolss_io_q_closeprinter("", &q_c, &buf, 0) &&
605 rpc_hnd_pipe_req(hnd, SPOOLSS_CLOSEPRINTER, &buf, &rbuf))
607 SPOOL_R_CLOSEPRINTER r_c;
609 spoolss_io_r_closeprinter("", &r_c, &rbuf, 0);
611 if (prs_offset(&rbuf)!=0 && r_c.status != 0)
613 /* report error code */
614 DEBUG(0,("SPOOL_CLOSEPRINTER: %s\n", get_nt_error_msg(r_c.status)));
623 talloc_destroy(mem_ctx);
625 /* disassociate with the cli_connection */
626 RpcHndList_del_connection(hnd);
631 /****************************************************************************
632 do a SPOOLSS Get printer datas
633 ****************************************************************************/
634 uint32 spoolss_getprinterdata(const POLICY_HND *hnd, const UNISTR2 *valuename,
643 SPOOL_Q_GETPRINTERDATA q_o;
644 SPOOL_R_GETPRINTERDATA r_o;
645 TALLOC_CTX *mem_ctx = NULL;
648 return NT_STATUS_INVALID_PARAMETER;
650 if ((mem_ctx=talloc_init()) == NULL)
652 DEBUG(0,("msrpc_spoolss_enum_jobs: talloc_init failed!\n"));
655 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, mem_ctx, MARSHALL);
656 prs_init(&rbuf, 0, 4, mem_ctx, UNMARSHALL);
658 /* create and send a MSRPC command with api SPOOLSS_GETPRINTERDATA */
660 DEBUG(5,("SPOOLSS Get Printer data)\n"));
662 make_spoolss_q_getprinterdata(&q_o, hnd, valuename, in_size);
664 /* turn parameters into data stream */
665 if (!spoolss_io_q_getprinterdata("", &q_o, &buf, 0)) {
670 if (!rpc_hnd_pipe_req(hnd, SPOOLSS_GETPRINTERDATA, &buf, &rbuf)) {
680 if(!spoolss_io_r_getprinterdata("", &r_o, &rbuf, 0)) {
694 /****************************************************************************
695 do a SPOOLSS Get Printer Driver Direcotry
696 ****************************************************************************/
697 uint32 spoolss_getprinterdriverdir(fstring srv_name, fstring env_name, uint32 level,
698 NEW_BUFFER *buffer, uint32 offered,
703 SPOOL_Q_GETPRINTERDRIVERDIR q_o;
704 SPOOL_R_GETPRINTERDRIVERDIR r_o;
705 TALLOC_CTX *ctx = prs_get_mem_context(&buffer->prs);
707 struct cli_connection *con = NULL;
709 if (!cli_connection_init(srv_name, PIPE_SPOOLSS, &con))
712 prs_init(&buf , MAX_PDU_FRAG_LEN, 4, ctx, MARSHALL);
713 prs_init(&rbuf, 0, 4, ctx, UNMARSHALL);
715 /* create and send a MSRPC command with api SPOOLSS_ENUM_PRINTERS */
717 DEBUG(5,("SPOOLSS GetPrinterDriverDir (Server: %s Env: %s level: %d)\n",
718 srv_name, env_name, level));
720 make_spoolss_q_getprinterdriverdir(&q_o, srv_name, env_name, level,
723 /* turn parameters into data stream */
724 if (!spoolss_io_q_getprinterdriverdir("", &q_o, &buf, 0) ) {
728 cli_connection_unlink(con);
731 if(!rpc_con_pipe_req(con, SPOOLSS_GETPRINTERDRIVERDIRECTORY, &buf, &rbuf)) {
735 cli_connection_unlink(con);
741 prs_switch_type(&buffer->prs, UNMARSHALL);
742 prs_set_offset(&buffer->prs, 0);
745 if(!spoolss_io_r_getprinterdriverdir("", &r_o, &rbuf, 0)) {
747 cli_connection_unlink(con);
754 cli_connection_unlink(con);