39655519f99966e8d0d583a9ba7da79c88e6cf0e
[kai/samba.git] / source3 / rpc_client / cli_spoolss.c
1 /*
2  *  Unix SMB/Netbios implementation.
3  *  Version 1.9.
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,
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #include "includes.h"
26 #include "rpc_parse.h"
27 #include "nterr.h"
28
29 /****************************************************************************
30 do a SPOOLSS Enum Printer Drivers
31 ****************************************************************************/
32 uint32 spoolss_enum_printerdrivers(const char *srv_name, const char *environment,
33                                    uint32 level, NEW_BUFFER *buffer, uint32 offered,
34                                    uint32 *needed, uint32 *returned)
35 {
36         prs_struct rbuf;
37         prs_struct buf;
38         SPOOL_Q_ENUMPRINTERDRIVERS q_o;
39         SPOOL_R_ENUMPRINTERDRIVERS r_o;
40         TALLOC_CTX *ctx = prs_get_mem_context(&buffer->prs);
41
42         struct cli_connection *con = NULL;
43
44         if (!cli_connection_init(srv_name, PIPE_SPOOLSS, &con))
45                 return False;
46
47         prs_init(&buf, MAX_PDU_FRAG_LEN, ctx, MARSHALL);
48         prs_init(&rbuf, 0, ctx, UNMARSHALL);
49
50         /* create and send a MSRPC command with api SPOOLSS_ENUM_PRINTERS */
51
52         DEBUG(5,("SPOOLSS Enum Printer Drivers (Server: %s Environment: %s level: %d)\n",
53                                 srv_name, environment, level));
54
55         make_spoolss_q_enumprinterdrivers(&q_o, srv_name, environment,
56                                 level, buffer, offered);
57
58         /* turn parameters into data stream */
59         if (spoolss_io_q_enumprinterdrivers("", &q_o, &buf, 0) &&
60              rpc_con_pipe_req(con, SPOOLSS_ENUMPRINTERDRIVERS, &buf, &rbuf)) 
61         {
62                 prs_mem_free(&buf);
63                 ZERO_STRUCT(r_o);
64
65                 prs_switch_type(&buffer->prs, UNMARSHALL);
66                 prs_set_offset(&buffer->prs, 0);
67                 r_o.buffer=buffer;
68
69                 if(spoolss_io_r_enumprinterdrivers("", &r_o, &rbuf, 0)) 
70                 {
71                         if (r_o.status != NT_STATUS_OK)
72                         {
73                                 DEBUG(3,("SPOOLSS_ENUMPRINTERDRIVERS:  %s\n", get_nt_error_msg(r_o.status)));
74                         }
75                         *needed=r_o.needed;
76                         *returned=r_o.returned;
77                 }
78         }
79
80         prs_mem_free(&rbuf);
81         prs_mem_free(&buf );
82
83         cli_connection_unlink(con);
84
85         return r_o.status;
86 }
87
88 /****************************************************************************
89 do a SPOOLSS Enum Printers
90 ****************************************************************************/
91 uint32 spoolss_enum_printers(uint32 flags, fstring srv_name, uint32 level,
92                              NEW_BUFFER *buffer, uint32 offered,
93                              uint32 *needed, uint32 *returned)
94 {
95         prs_struct rbuf;
96         prs_struct buf;
97         SPOOL_Q_ENUMPRINTERS q_o;
98         SPOOL_R_ENUMPRINTERS r_o;
99         TALLOC_CTX *ctx = prs_get_mem_context(&buffer->prs);
100
101         struct cli_connection *con = NULL;
102
103         if (!cli_connection_init(srv_name, PIPE_SPOOLSS, &con))
104                 return False;
105
106         prs_init(&buf, MAX_PDU_FRAG_LEN, ctx, MARSHALL);
107         prs_init(&rbuf, 0, ctx, UNMARSHALL);
108
109         /* create and send a MSRPC command with api SPOOLSS_ENUM_PRINTERS */
110
111         DEBUG(5,("SPOOLSS Enum Printers (Server: %s level: %d)\n", srv_name, level));
112
113         make_spoolss_q_enumprinters(&q_o, flags, "", level, buffer, offered);
114
115         /* turn parameters into data stream */
116         if (spoolss_io_q_enumprinters("", &q_o, &buf, 0) &&
117            rpc_con_pipe_req(con, SPOOLSS_ENUMPRINTERS, &buf, &rbuf)) 
118         {
119                 ZERO_STRUCT(r_o);
120
121                 prs_switch_type(&buffer->prs, UNMARSHALL);
122                 prs_set_offset(&buffer->prs, 0);
123                 r_o.buffer=buffer;
124
125                 if(new_spoolss_io_r_enumprinters("", &r_o, &rbuf, 0)) 
126                 {
127                         if (r_o.status != NT_STATUS_OK)
128                         {
129                                 /* report error code */
130                                 DEBUG(3,("SPOOLSS_ENUMPRINTERS: %s\n", get_nt_error_msg(r_o.status)));
131                         }
132
133                         *needed=r_o.needed;
134                         *returned=r_o.returned;
135                 }
136
137         }
138
139         prs_mem_free(&rbuf);
140         prs_mem_free(&buf );
141
142         cli_connection_unlink(con);
143
144         return r_o.status;
145 }
146
147 /****************************************************************************
148 do a SPOOLSS Enum Ports
149 ****************************************************************************/
150 uint32 spoolss_enum_ports(fstring srv_name, uint32 level,
151                              NEW_BUFFER *buffer, uint32 offered,
152                              uint32 *needed, uint32 *returned)
153 {
154         prs_struct rbuf;
155         prs_struct buf;
156         SPOOL_Q_ENUMPORTS q_o;
157         SPOOL_R_ENUMPORTS r_o;
158         TALLOC_CTX *ctx = prs_get_mem_context(&buffer->prs);
159
160         struct cli_connection *con = NULL;
161
162         if (!cli_connection_init(srv_name, PIPE_SPOOLSS, &con))
163                 return False;
164
165         prs_init(&buf, MAX_PDU_FRAG_LEN, ctx, MARSHALL);
166         prs_init(&rbuf, 0, ctx, UNMARSHALL);
167
168         /* create and send a MSRPC command with api SPOOLSS_ENUMPORTS */
169
170         DEBUG(5,("SPOOLSS Enum Ports (Server: %s level: %d)\n", srv_name, level));
171
172         make_spoolss_q_enumports(&q_o, "", level, buffer, offered);
173
174         /* turn parameters into data stream */
175         if (spoolss_io_q_enumports("", &q_o, &buf, 0) &&
176              rpc_con_pipe_req(con, SPOOLSS_ENUMPORTS, &buf, &rbuf))
177         {
178                 prs_mem_free(&buf );
179                 ZERO_STRUCT(r_o);
180
181                 prs_switch_type(&buffer->prs, UNMARSHALL);
182                 prs_set_offset(&buffer->prs, 0);
183                 r_o.buffer=buffer;
184
185                 if(new_spoolss_io_r_enumports("", &r_o, &rbuf, 0)) 
186                 {
187                         if (r_o.status != NT_STATUS_OK)
188                         {
189                                 DEBUG(3,("SPOOLSS_ENUMPORTS: %s\n", get_nt_error_msg(r_o.status)));
190                         }
191                         
192                         *needed=r_o.needed;
193                         *returned=r_o.returned;
194                 }
195         }
196
197         prs_mem_free(&rbuf);
198         prs_mem_free(&buf );
199
200         cli_connection_unlink(con);
201
202         return r_o.status;
203 }
204
205 /****************************************************************************
206 do a SPOOLSS Enum Jobs
207 ****************************************************************************/
208 uint32 spoolss_enum_jobs(const POLICY_HND *hnd, uint32 firstjob, uint32 numofjobs,
209                          uint32 level, NEW_BUFFER *buffer, uint32 offered,
210                          uint32 *needed, uint32 *returned)
211 {
212         prs_struct rbuf;
213         prs_struct buf;
214         SPOOL_Q_ENUMJOBS q_o;
215         SPOOL_R_ENUMJOBS r_o;
216         TALLOC_CTX *ctx = prs_get_mem_context(&buffer->prs);
217
218         if (hnd == NULL)
219                 return NT_STATUS_INVALID_PARAMETER;
220
221         prs_init(&buf, MAX_PDU_FRAG_LEN, ctx, MARSHALL);
222         prs_init(&rbuf, 0, ctx, UNMARSHALL);
223
224         /* create and send a MSRPC command with api SPOOLSS_ENUMJOBS */
225
226         DEBUG(5,("SPOOLSS Enum Jobs level: %d)\n", level));
227
228         make_spoolss_q_enumjobs(&q_o, hnd, firstjob, numofjobs, level, buffer, offered);
229
230         /* turn parameters into data stream */
231         if (spoolss_io_q_enumjobs("", &q_o, &buf, 0) &&
232              rpc_hnd_pipe_req(hnd, SPOOLSS_ENUMJOBS, &buf, &rbuf))
233         {
234                 ZERO_STRUCT(r_o);
235                 prs_mem_free(&buf );
236
237                 r_o.buffer=buffer;
238
239                 if(spoolss_io_r_enumjobs("", &r_o, &rbuf, 0)) 
240                 {
241                         if (r_o.status != NT_STATUS_OK)
242                         {
243                                 DEBUG(3,("SPOOLSS_ENUMJOBS: %s\n", get_nt_error_msg(r_o.status)));
244                         }
245                         *needed=r_o.needed;
246                         *returned=r_o.returned;
247                 }
248         }
249
250         prs_mem_free(&rbuf);
251         prs_mem_free(&buf );
252
253         return r_o.status;
254 }
255
256 /***************************************************************************
257 do a SPOOLSS Enum printer datas
258 ****************************************************************************/
259 uint32 spoolss_enum_printerdata(const POLICY_HND *hnd, uint32 idx,
260                                 uint32 *valuelen, uint16 *value, uint32 *rvaluelen,
261                                 uint32 *type, uint32 *datalen, uint8 *data, 
262                                 uint32 *rdatalen)
263 {
264         prs_struct rbuf;
265         prs_struct buf;
266         SPOOL_Q_ENUMPRINTERDATA q_o;
267         SPOOL_R_ENUMPRINTERDATA r_o;
268         TALLOC_CTX *mem_ctx = NULL;
269         
270         if (hnd == NULL)
271                 return NT_STATUS_INVALID_PARAMETER;
272
273         if ((mem_ctx=talloc_init()) == NULL)
274         {
275                 DEBUG(0,("msrpc_spoolss_enum_jobs: talloc_init failed!\n"));
276                 return False;
277         }
278         prs_init(&buf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
279         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
280
281         /* create and send a MSRPC command with api  SPOOLSS_ENUMPRINTERDATA*/
282
283         DEBUG(4,("SPOOLSS Enum Printer data\n"));
284
285         make_spoolss_q_enumprinterdata(&q_o, hnd, idx, *valuelen, *datalen);
286
287         /* turn parameters into data stream */
288         if (spoolss_io_q_enumprinterdata("", &q_o, &buf, 0) &&
289              rpc_hnd_pipe_req(hnd, SPOOLSS_ENUMPRINTERDATA, &buf, &rbuf)) 
290         {
291                 ZERO_STRUCT(r_o);
292                 prs_mem_free(&buf );
293
294                 r_o.data=data;
295                 r_o.value=value;
296
297                 if(spoolss_io_r_enumprinterdata("", &r_o, &rbuf, 0))
298                 {
299                         if (r_o.status != NT_STATUS_OK)
300                         {
301                                 DEBUG(3,("SPOOLSS_ENUMPRINTERDATA: %s\n", get_nt_error_msg(r_o.status)));
302                         }
303                         
304                         *valuelen=r_o.valuesize;
305                         *rvaluelen=r_o.realvaluesize;
306                         *type=r_o.type;
307                         *datalen=r_o.datasize;
308                         *rdatalen=r_o.realdatasize;
309
310                 }
311         }
312
313         prs_mem_free(&rbuf);
314         prs_mem_free(&buf );
315         if (mem_ctx)
316                 talloc_destroy(mem_ctx);
317
318         return r_o.status;
319 }
320
321 /****************************************************************************
322 do a SPOOLSS Enum printer datas
323 ****************************************************************************/
324 uint32 spoolss_getprinter(const POLICY_HND *hnd, uint32 level,
325                              NEW_BUFFER *buffer, uint32 offered,
326                              uint32 *needed)
327 {
328         prs_struct rbuf;
329         prs_struct buf;
330         SPOOL_Q_GETPRINTER q_o;
331         SPOOL_R_GETPRINTER r_o;
332         TALLOC_CTX *ctx = prs_get_mem_context(&buffer->prs);
333
334         if (hnd == NULL)
335                 return NT_STATUS_INVALID_PARAMETER;
336
337         prs_init(&buf, MAX_PDU_FRAG_LEN, ctx, MARSHALL);
338         prs_init(&rbuf, 0, ctx, UNMARSHALL);
339
340         /* create and send a MSRPC command with api SPOOLSS_ENUMJOBS */
341
342         DEBUG(5,("SPOOLSS Enum Printer data)\n"));
343
344         make_spoolss_q_getprinter(&q_o, hnd, level, buffer, offered);
345
346         /* turn parameters into data stream */
347         if (spoolss_io_q_getprinter("", &q_o, &buf, 0) &&
348              rpc_hnd_pipe_req(hnd, SPOOLSS_GETPRINTER, &buf, &rbuf)) 
349         {
350                 ZERO_STRUCT(r_o);
351                 prs_mem_free(&buf );
352
353                 prs_switch_type(&buffer->prs, UNMARSHALL);
354                 prs_set_offset(&buffer->prs, 0);
355                 r_o.buffer=buffer;
356
357                 if(!spoolss_io_r_getprinter("", &r_o, &rbuf, 0)) 
358                 {
359                         if (r_o.status != NT_STATUS_OK)
360                         {
361                                 DEBUG(3,("SPOOLSS_GETPRINTER: %s\n", get_nt_error_msg(r_o.status)));
362                         }
363                         *needed=r_o.needed;
364                 }
365         }
366
367         prs_mem_free(&rbuf);
368         prs_mem_free(&buf );
369
370         return r_o.status;
371 }
372
373 /****************************************************************************
374 do a SPOOLSS Enum printer driver
375 ****************************************************************************/
376 uint32 spoolss_getprinterdriver(const POLICY_HND *hnd,
377                                 const char *environment, uint32 level,
378                                 NEW_BUFFER *buffer, uint32 offered,
379                                 uint32 *needed)
380 {
381         prs_struct rbuf;
382         prs_struct buf;
383         SPOOL_Q_GETPRINTERDRIVER2 q_o;
384         SPOOL_R_GETPRINTERDRIVER2 r_o;
385         TALLOC_CTX *ctx = prs_get_mem_context(&buffer->prs);
386
387         if (hnd == NULL)
388                 return NT_STATUS_INVALID_PARAMETER;
389
390         prs_init(&buf, MAX_PDU_FRAG_LEN, ctx, MARSHALL);
391         prs_init(&rbuf, 0, ctx, UNMARSHALL);
392
393         /* create and send a MSRPC command with api SPOOLSS_ENUMJOBS */
394
395         DEBUG(5,("SPOOLSS Enum Printer driver)\n"));
396
397         make_spoolss_q_getprinterdriver2(&q_o, hnd, environment, level, 2, 0, buffer, offered);
398
399         /* turn parameters into data stream */
400         if (spoolss_io_q_getprinterdriver2("", &q_o, &buf, 0) &&
401              rpc_hnd_pipe_req(hnd, SPOOLSS_GETPRINTERDRIVER2, &buf, &rbuf)) 
402         {
403                 ZERO_STRUCT(r_o);
404                 prs_mem_free(&buf );
405
406                 prs_switch_type(&buffer->prs, UNMARSHALL);
407                 prs_set_offset(&buffer->prs, 0);
408                 r_o.buffer=buffer;
409
410                 if(spoolss_io_r_getprinterdriver2("", &r_o, &rbuf, 0)) 
411                 {
412                         if (r_o.status != NT_STATUS_OK)
413                         {
414                                 DEBUG(3,("SPOOLSS_GETPRINTERDRIVER2: %s\n", get_nt_error_msg(r_o.status)));
415                         }
416
417                         *needed=r_o.needed;
418                 }
419         }
420
421         prs_mem_free(&rbuf);
422         prs_mem_free(&buf );
423
424         return r_o.status;
425 }
426
427
428
429 /****************************************************************************
430 do a SPOOLSS Open Printer Ex
431 ****************************************************************************/
432 BOOL spoolss_open_printer_ex(  const char *printername,
433                          const char *datatype, uint32 access_required,
434                          const char *station,  const char *username,
435                         POLICY_HND *hnd)
436 {
437         prs_struct rbuf;
438         prs_struct buf;
439         SPOOL_Q_OPEN_PRINTER_EX q_o;
440         BOOL valid_pol = False;
441         fstring srv_name;
442         char *s = NULL;
443         struct cli_connection *con = NULL;
444         TALLOC_CTX *mem_ctx = NULL;
445         
446         memset(srv_name, 0, sizeof(srv_name));
447         fstrcpy(srv_name, printername);
448
449         s = strchr_m(&srv_name[2], '\\');
450         if (s != NULL)
451                 *s = '\0';
452
453         if (!cli_connection_init(srv_name, PIPE_SPOOLSS, &con))
454                 return False;
455
456         if (hnd == NULL) 
457                 return False;
458
459         if ((mem_ctx=talloc_init()) == NULL)
460         {
461                 DEBUG(0,("msrpc_spoolss_enum_jobs: talloc_init failed!\n"));
462                 return False;
463         }
464         prs_init(&buf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
465         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
466
467         /* create and send a MSRPC command with api SPOOLSS_OPENPRINTEREX */
468
469         DEBUG(5,("SPOOLSS Open Printer Ex\n"));
470
471         make_spoolss_q_open_printer_ex(&q_o, printername, datatype,
472                                        access_required, station, username);
473
474         /* turn parameters into data stream */
475         if (spoolss_io_q_open_printer_ex("", &q_o, &buf, 0) &&
476             rpc_con_pipe_req(con, SPOOLSS_OPENPRINTEREX, &buf, &rbuf))
477         {
478                 SPOOL_R_OPEN_PRINTER_EX r_o;
479                 BOOL p = True;
480
481                 spoolss_io_r_open_printer_ex("", &r_o, &rbuf, 0);
482
483                 if (prs_offset(&rbuf)!= 0 && r_o.status != 0)
484                 {
485                         /* report error code */
486                         DEBUG(3,("SPOOLSS_OPENPRINTEREX: %s\n", get_nt_error_msg(r_o.status)));
487                         p = False;
488                 }
489
490                 if (p)
491                 {
492                         /* ok, at last: we're happy. return the policy handle */
493                         *hnd = r_o.handle;
494
495                         /* associate the handle returned with the current 
496                            state of the clienjt connection */
497                         valid_pol = RpcHndList_set_connection(hnd, con);
498
499                 }
500         }
501
502         prs_mem_free(&rbuf);
503         prs_mem_free(&buf );
504         if (mem_ctx)
505                 talloc_destroy(mem_ctx);
506
507         return valid_pol;
508 }
509
510 /****************************************************************************
511  do a SPOOLSS AddPrinterEx()
512  **ALWAYS** uses as PRINTER_INFO level 2 struct
513 ****************************************************************************/
514 BOOL spoolss_addprinterex(POLICY_HND *hnd, const char* srv_name, PRINTER_INFO_2 *info2)
515 {
516         prs_struct rbuf;
517         prs_struct buf;
518         SPOOL_Q_ADDPRINTEREX q_o;
519         SPOOL_R_ADDPRINTEREX r_o;
520         struct cli_connection *con = NULL;
521         TALLOC_CTX *mem_ctx = NULL;
522         fstring the_client_name;
523         BOOL valid_pol = True;
524
525
526
527         if (!cli_connection_init(srv_name, PIPE_SPOOLSS, &con))
528                 return NT_STATUS_ACCESS_DENIED;
529
530         if (hnd == NULL)
531                 return NT_STATUS_INVALID_PARAMETER;
532
533         if ((mem_ctx=talloc_init()) == NULL)
534         {
535                 DEBUG(0,("spoolss_addprinterex: talloc_init() failed!\n"));
536                 return NT_STATUS_ACCESS_DENIED;
537         }
538         prs_init(&buf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
539         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
540
541         /* create and send a MSRPC command with api SPOOLSS_ENUMPORTS */
542         DEBUG(5,("SPOOLSS Add Printer Ex (Server: %s)\n", srv_name));
543         
544         fstrcpy(the_client_name, "\\\\");
545         fstrcat(the_client_name, con->pCli_state->desthost);
546         strupper(the_client_name);
547         
548
549         make_spoolss_q_addprinterex(mem_ctx, &q_o, srv_name, the_client_name, 
550                                     /* "Administrator", */
551                                     con->pCli_state->user_name,
552                                     2, info2);
553
554         /* turn parameters into data stream and send the request */
555         if (spoolss_io_q_addprinterex("", &q_o, &buf, 0) &&
556             rpc_con_pipe_req(con, SPOOLSS_ADDPRINTEREX, &buf, &rbuf)) 
557         {
558                 ZERO_STRUCT(r_o);
559
560                 if(spoolss_io_r_addprinterex("", &r_o, &rbuf, 0)) 
561                 {
562                         if (r_o.status != NT_STATUS_OK)
563                         {
564                                 /* report error code */
565                                 DEBUG(3,("SPOOLSS_ADDPRINTEREX: %s\n", get_nt_error_msg(r_o.status)));
566                                 valid_pol = False;
567                         }
568                 }
569                 
570                 if (valid_pol)
571                 {
572                         /* ok, at last: we're happy. return the policy handle */
573                         copy_policy_hnd( hnd, &r_o.handle);
574
575                         /* associate the handle returned with the current 
576                            state of the clienjt connection */
577                         RpcHndList_set_connection(hnd, con);
578                 }
579         }
580
581
582         prs_mem_free(&rbuf);
583         prs_mem_free(&buf );
584
585         if (mem_ctx)
586                 talloc_destroy(mem_ctx);
587
588         return valid_pol;
589 }
590
591 /****************************************************************************
592 do a SPOOL Close
593 ****************************************************************************/
594 BOOL spoolss_closeprinter(POLICY_HND *hnd)
595 {
596         prs_struct rbuf;
597         prs_struct buf;
598         SPOOL_Q_CLOSEPRINTER q_c;
599         BOOL valid_close = False;
600         TALLOC_CTX *mem_ctx = NULL;
601         
602         if (hnd == NULL) 
603                 return False;
604
605         /* create and send a MSRPC command with api SPOOLSS_CLOSEPRINTER */
606         if ((mem_ctx=talloc_init()) == NULL)
607         {
608                 DEBUG(0,("msrpc_spoolss_enum_jobs: talloc_init failed!\n"));
609                 return False;
610         }
611         prs_init(&buf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
612         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
613
614         DEBUG(4,("SPOOL Close Printer\n"));
615
616         /* store the parameters */
617         make_spoolss_q_closeprinter(&q_c, hnd);
618
619         /* turn parameters into data stream */
620         if (spoolss_io_q_closeprinter("", &q_c, &buf, 0) &&
621             rpc_hnd_pipe_req(hnd, SPOOLSS_CLOSEPRINTER, &buf, &rbuf))
622         {
623                 SPOOL_R_CLOSEPRINTER r_c;
624
625                 spoolss_io_r_closeprinter("", &r_c, &rbuf, 0);
626
627                 if (prs_offset(&rbuf)!=0 && r_c.status != 0)
628                 {
629                         /* report error code */
630                         DEBUG(3,("SPOOL_CLOSEPRINTER: %s\n", get_nt_error_msg(r_c.status)));
631                 }
632                 else
633                         valid_close = True;
634         }
635
636         prs_mem_free(&rbuf);
637         prs_mem_free(&buf );
638         if (mem_ctx)
639                 talloc_destroy(mem_ctx);
640
641         /* disassociate with the cli_connection */
642         RpcHndList_del_connection(hnd);
643
644         return valid_close;
645 }
646
647 /****************************************************************************
648 do a SPOOLSS Get printer datas
649 ****************************************************************************/
650 uint32 spoolss_getprinterdata(const POLICY_HND *hnd, const UNISTR2 *valuename,
651                         uint32 in_size,
652                         uint32 *type,
653                         uint32 *out_size,
654                         uint8 *data,
655                         uint32 *needed)
656 {
657         prs_struct rbuf;
658         prs_struct buf;
659         SPOOL_Q_GETPRINTERDATA q_o;
660         SPOOL_R_GETPRINTERDATA r_o;
661         TALLOC_CTX *mem_ctx = NULL;
662
663         if (hnd == NULL)
664                 return NT_STATUS_INVALID_PARAMETER;
665
666         if ((mem_ctx=talloc_init()) == NULL)
667         {
668                 DEBUG(0,("msrpc_spoolss_enum_jobs: talloc_init failed!\n"));
669                 return False;
670         }
671         prs_init(&buf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
672         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
673
674         /* create and send a MSRPC command with api SPOOLSS_GETPRINTERDATA */
675
676         DEBUG(5,("SPOOLSS Get Printer data)\n"));
677
678         make_spoolss_q_getprinterdata(&q_o, hnd,(UNISTR2 *)valuename, in_size);
679
680         /* turn parameters into data stream */
681         if (spoolss_io_q_getprinterdata("", &q_o, &buf, 0) &&
682              rpc_hnd_pipe_req(hnd, SPOOLSS_GETPRINTERDATA, &buf, &rbuf)) 
683         {
684                 ZERO_STRUCT(r_o);
685                 prs_mem_free(&buf );
686
687                 r_o.data=data;
688
689                 if(spoolss_io_r_getprinterdata("", &r_o, &rbuf, 0)) 
690                 {
691                         if (r_o.status != NT_STATUS_OK)
692                         {
693                                 DEBUG(3,("SPOOLSS_GETPRINTERDATA: %s\n", get_nt_error_msg(r_o.status)));
694                         }
695
696                         *type=r_o.type;
697                         *out_size=r_o.size;
698                         *needed=r_o.needed;
699                 }
700         }
701
702         prs_mem_free(&rbuf);
703         prs_mem_free(&buf );
704
705         return r_o.status;
706 }
707
708 /****************************************************************************
709 do a SPOOLSS Get Printer Driver Direcotry
710 ****************************************************************************/
711 uint32 spoolss_getprinterdriverdir(fstring srv_name, fstring env_name, uint32 level,
712                              NEW_BUFFER *buffer, uint32 offered,
713                              uint32 *needed)
714 {
715         prs_struct rbuf;
716         prs_struct buf;
717         SPOOL_Q_GETPRINTERDRIVERDIR q_o;
718         SPOOL_R_GETPRINTERDRIVERDIR r_o;
719         TALLOC_CTX *ctx = prs_get_mem_context(&buffer->prs);
720
721         struct cli_connection *con = NULL;
722
723         if (!cli_connection_init(srv_name, PIPE_SPOOLSS, &con))
724                 return False;
725
726         prs_init(&buf, MAX_PDU_FRAG_LEN, ctx, MARSHALL);
727         prs_init(&rbuf, 0, ctx, UNMARSHALL);
728
729         /* create and send a MSRPC command with api SPOOLSS_ENUM_PRINTERS */
730
731         DEBUG(5,("SPOOLSS GetPrinterDriverDir (Server: %s Env: %s level: %d)\n", 
732                   srv_name, env_name, level));
733
734         make_spoolss_q_getprinterdriverdir(&q_o, srv_name, env_name, level, 
735                                            buffer, offered);
736
737         /* turn parameters into data stream */
738         if (spoolss_io_q_getprinterdriverdir("", &q_o, &buf, 0) &&
739              rpc_con_pipe_req(con, SPOOLSS_GETPRINTERDRIVERDIRECTORY, &buf, &rbuf)) 
740         {
741                 prs_mem_free(&buf );
742                 ZERO_STRUCT(r_o);
743
744                 prs_switch_type(&buffer->prs, UNMARSHALL);
745                 prs_set_offset(&buffer->prs, 0);
746                 r_o.buffer=buffer;
747
748                 if(spoolss_io_r_getprinterdriverdir("", &r_o, &rbuf, 0)) 
749                 {
750                         if (r_o.status != NT_STATUS_OK)
751                         {
752                                 DEBUG(3,("SPOOLSS_GETPRINTERDRIVERDIRECTORY: %s\n", get_nt_error_msg(r_o.status)));
753                         }
754
755                         *needed=r_o.needed;
756                 }
757         }
758
759         prs_mem_free(&rbuf);
760         prs_mem_free(&buf );
761
762         cli_connection_unlink(con);
763
764         return r_o.status;
765 }
766
767 /******************************************************************************
768  AddPrinterDriver()
769  *****************************************************************************/
770 uint32 spoolss_addprinterdriver(const char *srv_name, uint32 level, PRINTER_DRIVER_CTR *info)
771 {
772         prs_struct                      rbuf;
773         prs_struct                      buf;
774         SPOOL_Q_ADDPRINTERDRIVER        q_o;
775         SPOOL_R_ADDPRINTERDRIVER        r_o;
776         TALLOC_CTX                      *mem_ctx = NULL;
777         struct cli_connection           *con = NULL;
778         
779         if (!cli_connection_init(srv_name, PIPE_SPOOLSS, &con))
780                 return False;
781
782         if ((mem_ctx=talloc_init()) == NULL)
783         {
784                 DEBUG(0,("msrpc_spoolss_enum_jobs: talloc_init failed!\n"));
785                 return False;
786         }
787         prs_init(&buf, MAX_PDU_FRAG_LEN, mem_ctx, MARSHALL);
788         prs_init(&rbuf, 0, mem_ctx, UNMARSHALL);
789         
790         /* make the ADDPRINTERDRIVER PDU */
791         make_spoolss_q_addprinterdriver(mem_ctx, &q_o, srv_name, level, info);
792
793         /* turn the data into an io stream */
794         if (spoolss_io_q_addprinterdriver("", &q_o, &buf, 0) &&
795             rpc_con_pipe_req(con, SPOOLSS_ADDPRINTERDRIVER, &buf, &rbuf)) 
796         {
797                 ZERO_STRUCT(r_o);
798
799                 if(spoolss_io_r_addprinterdriver("", &r_o, &rbuf, 0)) 
800                 {
801                         if (r_o.status != NT_STATUS_OK)
802                         {
803                                 /* report error code */
804                                 DEBUG(3,("SPOOLSS_ADDPRINTERDRIVER: %s\n", get_nt_error_msg(r_o.status)));
805                         }
806                 }
807         }
808
809
810         prs_mem_free(&rbuf);
811         prs_mem_free(&buf );
812
813         if (mem_ctx)
814                 talloc_destroy(mem_ctx);
815                 
816         return r_o.status;
817
818 }