s3:libsmb: make use of cli_state_server_time_zone()
[samba.git] / source3 / libsmb / cliprint.c
1 /*
2    Unix SMB/CIFS implementation.
3    client print routines
4    Copyright (C) Andrew Tridgell 1994-1998
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "libsmb/libsmb.h"
22 #include "libsmb/clirap.h"
23
24 /*****************************************************************************
25  Convert a character pointer in a cli_call_api() response to a form we can use.
26  This function contains code to prevent core dumps if the server returns
27  invalid data.
28 *****************************************************************************/
29 static const char *fix_char_ptr(unsigned int datap, unsigned int converter,
30                           char *rdata, int rdrcnt)
31 {
32         unsigned int offset;
33
34         if (datap == 0) {
35                 /* turn NULL pointers into zero length strings */
36                 return "";
37         }
38
39         offset = datap - converter;
40
41         if (offset >= rdrcnt) {
42                 DEBUG(1,("bad char ptr: datap=%u, converter=%u rdrcnt=%d>",
43                          datap, converter, rdrcnt));
44                 return "<ERROR>";
45         }
46         return &rdata[offset];
47 }
48
49 /****************************************************************************
50 call fn() on each entry in a print queue
51 ****************************************************************************/
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         unsigned int rdrcnt, rprcnt;
60         char param[1024];
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         strlcpy_base(p,"zWrLeh", param, sizeof(param));   /* parameter description? */
70         p = skip_string(param,sizeof(param),p);
71         strlcpy_base(p,"WWzWWDDzz", param, sizeof(param));  /* returned data format */
72         p = skip_string(param,sizeof(param),p);
73         strlcpy_base(p,cli->share, param, sizeof(param));    /* name of queue */
74         p = skip_string(param,sizeof(param),p);
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         strlcpy_base(p,"", param,sizeof(param));   /* subformat */
79         p = skip_string(param,sizeof(param),p);
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(
104                                         p + 12, cli_state_server_time_zone(cli));
105                                 job.size = IVAL(p,16);
106                                 fstrcpy(job.name,fix_char_ptr(SVAL(p,24),
107                                                               converter,
108                                                               rdata, rdrcnt));
109                                 fn(&job);
110                                 p += 28;
111                         }
112                 }
113         }
114
115         /* If any parameters or data were returned, free the storage. */
116         SAFE_FREE(rparam);
117         SAFE_FREE(rdata);
118
119         return i;
120 }
121
122 /****************************************************************************
123   cancel a print job
124   ****************************************************************************/
125
126 int cli_printjob_del(struct cli_state *cli, int job)
127 {
128         char *rparam = NULL;
129         char *rdata = NULL;
130         char *p;
131         unsigned int rdrcnt,rprcnt;
132         int ret = -1;
133         char param[1024];
134
135         memset(param,'\0',sizeof(param));
136
137         p = param;
138         SSVAL(p,0,81);          /* DosPrintJobDel() */
139         p += 2;
140         strlcpy_base(p,"W", param,sizeof(param));
141         p = skip_string(param,sizeof(param),p);
142         strlcpy_base(p,"", param,sizeof(param));
143         p = skip_string(param,sizeof(param),p);
144         SSVAL(p,0,job);
145         p += 2;
146
147         if (cli_api(cli,
148                     param, PTR_DIFF(p,param), 1024,  /* Param, length, maxlen */
149                     NULL, 0, CLI_BUFFER_SIZE,            /* data, length, maxlen */
150                     &rparam, &rprcnt,                /* return params, length */
151                     &rdata, &rdrcnt)) {               /* return data, length */
152                 ret = SVAL(rparam,0);
153         }
154
155         SAFE_FREE(rparam);
156         SAFE_FREE(rdata);
157
158         return ret;
159 }