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 #include "../libcli/smb/smbXcli_base.h"
24 
25 /*****************************************************************************
26  Convert a character pointer in a cli_call_api() response to a form we can use.
27  This function contains code to prevent core dumps if the server returns
28  invalid data.
29 *****************************************************************************/
fix_char_ptr(unsigned int datap,unsigned int converter,char * rdata,int rdrcnt)30 static const char *fix_char_ptr(unsigned int datap, unsigned int converter,
31 			  char *rdata, int rdrcnt)
32 {
33 	unsigned int offset;
34 
35 	if (datap == 0)	{
36 		/* turn NULL pointers into zero length strings */
37 		return "";
38 	}
39 
40 	offset = datap - converter;
41 
42 	if (offset >= rdrcnt) {
43 		DEBUG(1,("bad char ptr: datap=%u, converter=%u rdrcnt=%d>",
44 			 datap, converter, rdrcnt));
45 		return "<ERROR>";
46 	}
47 	return &rdata[offset];
48 }
49 
50 /****************************************************************************
51 call fn() on each entry in a print queue
52 ****************************************************************************/
53 
cli_print_queue(struct cli_state * cli,void (* fn)(struct print_job_info *))54 int cli_print_queue(struct cli_state *cli,
55 		    void (*fn)(struct print_job_info *))
56 {
57 	char *rparam = NULL;
58 	char *rdata = NULL;
59 	char *p;
60 	unsigned int rdrcnt, rprcnt;
61 	char param[1024];
62 	int result_code=0;
63 	int i = -1;
64 
65 	memset(param,'\0',sizeof(param));
66 
67 	p = param;
68 	SSVAL(p,0,76);         /* API function number 76 (DosPrintJobEnum) */
69 	p += 2;
70 	strlcpy_base(p,"zWrLeh", param, sizeof(param));   /* parameter description? */
71 	p = skip_string(param,sizeof(param),p);
72 	strlcpy_base(p,"WWzWWDDzz", param, sizeof(param));  /* returned data format */
73 	p = skip_string(param,sizeof(param),p);
74 	strlcpy_base(p,cli->share, param, sizeof(param));    /* name of queue */
75 	p = skip_string(param,sizeof(param),p);
76 	SSVAL(p,0,2);   /* API function level 2, PRJINFO_2 data structure */
77 	SSVAL(p,2,1000); /* size of bytes of returned data buffer */
78 	p += 4;
79 	strlcpy_base(p,"", param,sizeof(param));   /* subformat */
80 	p = skip_string(param,sizeof(param),p);
81 
82 	DEBUG(4,("doing cli_print_queue for %s\n", cli->share));
83 
84 	if (cli_api(cli,
85 		    param, PTR_DIFF(p,param), 1024,  /* Param, length, maxlen */
86 		    NULL, 0, CLI_BUFFER_SIZE,            /* data, length, maxlen */
87 		    &rparam, &rprcnt,                /* return params, length */
88 		    &rdata, &rdrcnt)) {               /* return data, length */
89 		int converter;
90 		result_code = SVAL(rparam,0);
91 		converter = SVAL(rparam,2);       /* conversion factor */
92 
93 		if (result_code == 0) {
94 			struct print_job_info job;
95 
96 			p = rdata;
97 
98 			for (i = 0; i < SVAL(rparam,4); ++i) {
99 				job.id = SVAL(p,0);
100 				job.priority = SVAL(p,2);
101 				fstrcpy(job.user,
102 					fix_char_ptr(SVAL(p,4), converter,
103 						     rdata, rdrcnt));
104 				job.t = make_unix_date3(
105 					p + 12, smb1cli_conn_server_time_zone(cli->conn));
106 				job.size = IVAL(p,16);
107 				fstrcpy(job.name,fix_char_ptr(SVAL(p,24),
108 							      converter,
109 							      rdata, rdrcnt));
110 				fn(&job);
111 				p += 28;
112 			}
113 		}
114 	}
115 
116 	/* If any parameters or data were returned, free the storage. */
117 	SAFE_FREE(rparam);
118 	SAFE_FREE(rdata);
119 
120 	return i;
121 }
122 
123 /****************************************************************************
124   cancel a print job
125   ****************************************************************************/
126 
cli_printjob_del(struct cli_state * cli,int job)127 int cli_printjob_del(struct cli_state *cli, int job)
128 {
129 	char *rparam = NULL;
130 	char *rdata = NULL;
131 	char *p;
132 	unsigned int rdrcnt,rprcnt;
133 	int ret = -1;
134 	char param[1024];
135 
136 	memset(param,'\0',sizeof(param));
137 
138 	p = param;
139 	SSVAL(p,0,81);		/* DosPrintJobDel() */
140 	p += 2;
141 	strlcpy_base(p,"W", param,sizeof(param));
142 	p = skip_string(param,sizeof(param),p);
143 	strlcpy_base(p,"", param,sizeof(param));
144 	p = skip_string(param,sizeof(param),p);
145 	SSVAL(p,0,job);
146 	p += 2;
147 
148 	if (cli_api(cli,
149 		    param, PTR_DIFF(p,param), 1024,  /* Param, length, maxlen */
150 		    NULL, 0, CLI_BUFFER_SIZE,            /* data, length, maxlen */
151 		    &rparam, &rprcnt,                /* return params, length */
152 		    &rdata, &rdrcnt)) {               /* return data, length */
153 		ret = SVAL(rparam,0);
154 	}
155 
156 	SAFE_FREE(rparam);
157 	SAFE_FREE(rdata);
158 
159 	return ret;
160 }
161