57e2c049d8f2f35c1a27496dfc31785ff1c249bb
[ira/wip.git] / source3 / libsmb / cliprint.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    client print routines
5    Copyright (C) Andrew Tridgell 1994-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #define NO_SYSLOG
23
24 #include "includes.h"
25
26 /*****************************************************************************
27  Convert a character pointer in a cli_call_api() response to a form we can use.
28  This function contains code to prevent core dumps if the server returns 
29  invalid data.
30 *****************************************************************************/
31 static char *fix_char_ptr(unsigned int datap, unsigned int converter, 
32                           char *rdata, int rdrcnt)
33 {
34         if (datap == 0) {       /* turn NULL pointers into zero length strings */
35                 return "";
36         } else {
37                 unsigned int offset = datap - converter;
38
39                 if (offset >= rdrcnt) {
40                         DEBUG(1,("bad char ptr: datap=%u, converter=%u rdrcnt=%d>",
41                                  datap, converter, rdrcnt));
42                         return "<ERROR>";
43                 } else {
44                         return &rdata[offset];
45                 }
46         }
47 }
48
49
50 /****************************************************************************
51 call fn() on each entry in a print queue
52 ****************************************************************************/
53 int cli_print_queue(struct cli_state *cli, 
54                     void (*fn)(struct print_job_info *))
55 {
56         char *rparam = NULL;
57         char *rdata = NULL;
58         char *p;
59         int rdrcnt, rprcnt;
60         pstring param;
61         int result_code=0;
62         int i = -1;
63         
64         memset(param,'\0',sizeof(param));
65
66         p = param;
67         SSVAL(p,0,76);         /* API function number 76 (DosPrintJobEnum) */
68         p += 2;
69         pstrcpy(p,"zWrLeh");   /* parameter description? */
70         p = skip_string(p,1);
71         pstrcpy(p,"WWzWWDDzz");  /* returned data format */
72         p = skip_string(p,1);
73         pstrcpy(p,cli->share);    /* name of queue */
74         p = skip_string(p,1);
75         SSVAL(p,0,2);   /* API function level 2, PRJINFO_2 data structure */
76         SSVAL(p,2,1000); /* size of bytes of returned data buffer */
77         p += 4;
78         pstrcpy(p,"");   /* subformat */
79         p = skip_string(p,1);
80
81         DEBUG(4,("doing cli_print_queue for %s\n", cli->share));
82
83         if (cli_api(cli, 
84                     param, PTR_DIFF(p,param), 1024,  /* Param, length, maxlen */
85                     NULL, 0, CLI_BUFFER_SIZE,            /* data, length, maxlen */
86                     &rparam, &rprcnt,                /* return params, length */
87                     &rdata, &rdrcnt)) {               /* return data, length */
88                 int converter;
89                 result_code = SVAL(rparam,0);
90                 converter = SVAL(rparam,2);       /* conversion factor */
91
92                 if (result_code == 0) {
93                         struct print_job_info job;
94                         
95                         p = rdata; 
96
97                         for (i = 0; i < SVAL(rparam,4); ++i) {
98                                 job.id = SVAL(p,0);
99                                 job.priority = SVAL(p,2);
100                                 fstrcpy(job.user,
101                                         fix_char_ptr(SVAL(p,4), converter, 
102                                                      rdata, rdrcnt));
103                                 job.t = make_unix_date3(p + 12);
104                                 job.size = IVAL(p,16);
105                                 fstrcpy(job.name,fix_char_ptr(SVAL(p,24), 
106                                                               converter, 
107                                                               rdata, rdrcnt));
108                                 fn(&job);                               
109                                 p += 28;
110                         }
111                 }
112         }
113
114         /* If any parameters or data were returned, free the storage. */
115         SAFE_FREE(rparam);
116         SAFE_FREE(rdata);
117
118         return i;
119 }
120
121 /****************************************************************************
122   cancel a print job
123   ****************************************************************************/
124 int cli_printjob_del(struct cli_state *cli, int job)
125 {
126         char *rparam = NULL;
127         char *rdata = NULL;
128         char *p;
129         int rdrcnt,rprcnt, ret = -1;
130         pstring param;
131
132         memset(param,'\0',sizeof(param));
133
134         p = param;
135         SSVAL(p,0,81);          /* DosPrintJobDel() */
136         p += 2;
137         pstrcpy(p,"W");
138         p = skip_string(p,1);
139         pstrcpy(p,"");
140         p = skip_string(p,1);
141         SSVAL(p,0,job);     
142         p += 2;
143         
144         if (cli_api(cli, 
145                     param, PTR_DIFF(p,param), 1024,  /* Param, length, maxlen */
146                     NULL, 0, CLI_BUFFER_SIZE,            /* data, length, maxlen */
147                     &rparam, &rprcnt,                /* return params, length */
148                     &rdata, &rdrcnt)) {               /* return data, length */
149                 ret = SVAL(rparam,0);
150         }
151
152         SAFE_FREE(rparam);
153         SAFE_FREE(rdata);
154
155         return ret;
156 }
157
158