1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
Unix SMB/CIFS implementation.
printing command routines
Copyright (C) Andrew Tridgell 1992-2000
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "printing.h"
extern struct current_user current_user;
extern userdom_struct current_user_info;
/****************************************************************************
Run a given print command
a null terminated list of value/substitute pairs is provided
for local substitution strings
****************************************************************************/
static int print_run_command(int snum, const char* printername, bool do_sub,
const char *command, int *outfd, ...)
{
char *syscmd;
char *arg;
int ret;
TALLOC_CTX *ctx = talloc_tos();
va_list ap;
va_start(ap, outfd);
/* check for a valid system printername and valid command to run */
if ( !printername || !*printername ) {
va_end(ap);
return -1;
}
if (!command || !*command) {
va_end(ap);
return -1;
}
syscmd = talloc_strdup(ctx, command);
if (!syscmd) {
va_end(ap);
return -1;
}
while ((arg = va_arg(ap, char *))) {
char *value = va_arg(ap,char *);
syscmd = talloc_string_sub(ctx, syscmd, arg, value);
if (!syscmd) {
va_end(ap);
return -1;
}
}
va_end(ap);
syscmd = talloc_string_sub(ctx, syscmd, "%p", printername);
if (!syscmd) {
return -1;
}
if (do_sub && snum != -1) {
syscmd = talloc_sub_advanced(ctx,
lp_servicename(snum),
current_user_info.unix_name,
"",
current_user.ut.gid,
get_current_username(),
current_user_info.domain,
syscmd);
if (!syscmd) {
return -1;
}
}
ret = smbrun_no_sanitize(syscmd,outfd);
DEBUG(3,("Running the command `%s' gave %d\n",syscmd,ret));
return ret;
}
/****************************************************************************
delete a print job
****************************************************************************/
static int generic_job_delete( const char *sharename, const char *lprm_command, struct printjob *pjob)
{
fstring jobstr;
/* need to delete the spooled entry */
slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
return print_run_command( -1, sharename, False, lprm_command, NULL,
"%j", jobstr,
"%T", http_timestring(talloc_tos(), pjob->starttime),
NULL);
}
/****************************************************************************
pause a job
****************************************************************************/
static int generic_job_pause(int snum, struct printjob *pjob)
{
fstring jobstr;
/* need to pause the spooled entry */
slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
return print_run_command(snum, PRINTERNAME(snum), True,
lp_lppausecommand(snum), NULL,
"%j", jobstr,
NULL);
}
/****************************************************************************
resume a job
****************************************************************************/
static int generic_job_resume(int snum, struct printjob *pjob)
{
fstring jobstr;
/* need to pause the spooled entry */
slprintf(jobstr, sizeof(jobstr)-1, "%d", pjob->sysjob);
return print_run_command(snum, PRINTERNAME(snum), True,
lp_lpresumecommand(snum), NULL,
"%j", jobstr,
NULL);
}
/****************************************************************************
Submit a file for printing - called from print_job_end()
****************************************************************************/
static int generic_job_submit(int snum, struct printjob *pjob)
{
int ret = -1;
char *current_directory = NULL;
char *print_directory = NULL;
char *wd = NULL;
char *p = NULL;
char *jobname = NULL;
TALLOC_CTX *ctx = talloc_tos();
fstring job_page_count, job_size;
/* we print from the directory path to give the best chance of
parsing the lpq output */
current_directory = TALLOC_ARRAY(ctx,
char,
PATH_MAX+1);
if (!current_directory) {
return -1;
}
wd = sys_getwd(current_directory);
if (!wd) {
return -1;
}
print_directory = talloc_strdup(ctx, pjob->filename);
if (!print_directory) {
return -1;
}
p = strrchr_m(print_directory,'/');
if (!p) {
return -1;
}
*p++ = 0;
if (chdir(print_directory) != 0) {
return -1;
}
jobname = talloc_strdup(ctx, pjob->jobname);
if (!jobname) {
ret = -1;
goto out;
}
jobname = talloc_string_sub(ctx, jobname, "'", "_");
if (!jobname) {
ret = -1;
goto out;
}
slprintf(job_page_count, sizeof(job_page_count)-1, "%d", pjob->page_count);
slprintf(job_size, sizeof(job_size)-1, "%lu", (unsigned long)pjob->size);
/* send it to the system spooler */
ret = print_run_command(snum, PRINTERNAME(snum), True,
lp_printcommand(snum), NULL,
"%s", p,
"%J", jobname,
"%f", p,
"%z", job_size,
"%c", job_page_count,
NULL);
out:
chdir(wd);
TALLOC_FREE(current_directory);
return ret;
}
/****************************************************************************
get the current list of queued jobs
****************************************************************************/
static int generic_queue_get(const char *printer_name,
enum printing_types printing_type,
char *lpq_command,
print_queue_struct **q,
print_status_struct *status)
{
char **qlines;
int fd;
int numlines, i, qcount;
print_queue_struct *queue = NULL;
/* never do substitution when running the 'lpq command' since we can't
get it rigt when using the background update daemon. Make the caller
do it before passing off the command string to us here. */
print_run_command(-1, printer_name, False, lpq_command, &fd, NULL);
if (fd == -1) {
DEBUG(5,("generic_queue_get: Can't read print queue status for printer %s\n",
printer_name ));
return 0;
}
numlines = 0;
qlines = fd_lines_load(fd, &numlines,0);
close(fd);
/* turn the lpq output into a series of job structures */
qcount = 0;
ZERO_STRUCTP(status);
if (numlines && qlines) {
queue = SMB_MALLOC_ARRAY(print_queue_struct, numlines+1);
if (!queue) {
file_lines_free(qlines);
*q = NULL;
return 0;
}
memset(queue, '\0', sizeof(print_queue_struct)*(numlines+1));
for (i=0; i<numlines; i++) {
/* parse the line */
if (parse_lpq_entry(printing_type,qlines[i],
&queue[qcount],status,qcount==0)) {
qcount++;
}
}
}
file_lines_free(qlines);
*q = queue;
return qcount;
}
/****************************************************************************
pause a queue
****************************************************************************/
static int generic_queue_pause(int snum)
{
return print_run_command(snum, PRINTERNAME(snum), True, lp_queuepausecommand(snum), NULL, NULL);
}
/****************************************************************************
resume a queue
****************************************************************************/
static int generic_queue_resume(int snum)
{
return print_run_command(snum, PRINTERNAME(snum), True, lp_queueresumecommand(snum), NULL, NULL);
}
/****************************************************************************
* Generic printing interface definitions...
***************************************************************************/
struct printif generic_printif =
{
DEFAULT_PRINTING,
generic_queue_get,
generic_queue_pause,
generic_queue_resume,
generic_job_delete,
generic_job_pause,
generic_job_resume,
generic_job_submit,
};
|