summaryrefslogtreecommitdiffstats
path: root/source3/printing/tests/vlp.c
blob: 4bff290e28a6cfede47e6fddf3cf6d181901e5c4 (plain)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* 
   Unix SMB/Netbios implementation.

   Virtual lp system for printer testing

   Copyright (C) Tim Potter 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 "system/passwd.h"
#include "system/filesys.h"
#include "printing.h"

#ifdef malloc
#undef malloc
#endif

#define PRINT_FIRSTJOB "100"

static TDB_CONTEXT *tdb;

struct vlp_job {
	fstring owner;
	int jobid;
	fstring jobname;
	int size;
	int status;
	time_t submit_time;
	int deleted;
};

/* Print usage */

static void usage(void)
{
	printf("Usage: vlp tdbfile=/tmp/vlp.tdb lpq|lprm|print|queuepause|queueresume|"
	       "lppause|lpresume [args]\n");
}

/* Return an array of vlp jobs that is the printer queue */

static void get_job_list(char *printer, struct vlp_job **job_list, 
			 int *num_jobs)
{
	fstring keystr;
	TDB_DATA data;

	slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);
	data = tdb_fetch_bystring(tdb, keystr);

	*job_list = (struct vlp_job *)data.dptr;
	*num_jobs = data.dsize / sizeof(struct vlp_job);
}

/* Store an array of vl jobs for the queue */

static void set_job_list(char *printer, struct vlp_job *job_list, 
			 int num_jobs)
{
	fstring keystr;
	TDB_DATA data;

	slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);

	data.dptr = (unsigned char *)job_list;
	data.dsize = num_jobs * sizeof(struct vlp_job);
	tdb_store_bystring(tdb, keystr, data, TDB_REPLACE);
}

/* Return the next job number for a printer */

static int next_jobnum(char *printer)
{
	fstring keystr;
	int jobnum;

	slprintf(keystr, sizeof(keystr) - 1, "JOBNUM/%s", printer);

	tdb_lock_bystring(tdb, keystr);

	jobnum = tdb_fetch_int32(tdb, keystr);

	/* Create next job index if none exists */

	if (jobnum == -1) {
		jobnum = atoi(PRINT_FIRSTJOB);
	}

	jobnum++;
	tdb_store_int32(tdb, keystr, jobnum);

	tdb_unlock_bystring(tdb, keystr);

	return jobnum;
}

static void set_printer_status(char *printer, int status)
{
	fstring keystr;
	int result;

	slprintf(keystr, sizeof(keystr) - 1, "STATUS/%s", printer);
	result = tdb_store_int32(tdb, keystr, status);
}

static int get_printer_status(char *printer)
{
	fstring keystr;
	TDB_DATA data;

	slprintf(keystr, sizeof(keystr) - 1, "STATUS/%s", printer);

	data.dptr = (unsigned char *)keystr;
	data.dsize = strlen(keystr) + 1;

	if (!tdb_exists(tdb, data)) {
		set_printer_status(printer, LPSTAT_OK);
		return LPSTAT_OK;
	}

	return tdb_fetch_int32(tdb, keystr);
}

/* Display printer queue */

static int lpq_command(int argc, char **argv)
{
	char *printer;
	struct vlp_job *job_list = NULL;
	int i, num_jobs, job_count = 0;

	if (argc != 2) {
		printf("Usage: lpq <printername>\n");
		return 1;
	}

	printer = argv[1];

	/* Display printer status */

	switch (get_printer_status(printer)) {
	case LPSTAT_OK:
		printf("enabled\n");
		break;
	case LPSTAT_STOPPED:
		printf("disabled\n");
		break;
	case LPSTAT_ERROR:
	default:
		printf("error\n");
		break;
	}

	/* Print queued documents */

	get_job_list(printer, &job_list, &num_jobs);

	for (i = 0; i < num_jobs; i++) {
		if (job_list[i].deleted) continue;
		printf("%d\t%d\t%d\t%ld\t%s\t%s\n", job_list[i].jobid,
		       job_list[i].size, 
		       (i == 0 && job_list[i].status == LPQ_QUEUED) ? 
		       LPQ_SPOOLING : job_list[i].status,
		       (long int)job_list[i].submit_time, job_list[i].owner, 
		       job_list[i].jobname);
		job_count++;
	}

	free(job_list);

	return 0;
}

/* Remove a job */

static int lprm_command(int argc, char **argv)
{
	char *printer;
	int jobid, num_jobs, i;
	struct vlp_job *job_list;

	if (argc < 3) {
		printf("Usage: lprm <printername> <jobid>\n");
		return 1;
	}

	printer = argv[1];
	jobid = atoi(argv[2]);

	get_job_list(printer, &job_list, &num_jobs);

	for (i = 0; i < num_jobs; i++) {
		if (job_list[i].jobid == jobid) {
			job_list[i].deleted = 1;
			set_job_list(printer, job_list, num_jobs);
			break;
		}
	}

	return 0;
}

/* print command = print-test %p %s */

static int print_command(int argc, char **argv)
{
	char *printer;
	fstring keystr;
	struct passwd *pw;
	TDB_DATA value, queue;
	struct vlp_job job;

	if (argc < 3) {
		printf("Usage: print <printername> <jobname>\n");
		return 1;
	}

	printer = argv[1];

	ZERO_STRUCT(job);

	/* Create a job record */

	slprintf(job.jobname, sizeof(job.jobname) - 1, "%s", argv[2]);

	if (!(pw = getpwuid(getuid()))) {
		return 1;
	}

	slprintf(job.owner, sizeof(job.owner) - 1, "%s", pw->pw_name);

	job.jobid = next_jobnum(printer);
	job.size = 666;
	job.submit_time = time(NULL);

	/* Store job entry in queue */

	slprintf(keystr, sizeof(keystr) - 1, "LPQ/%s", printer);

	value = tdb_fetch_bystring(tdb, keystr);

	if (value.dptr) {

		/* Add job to end of queue */

		queue.dptr = (unsigned char *)malloc(value.dsize + sizeof(struct vlp_job));
		if (!queue.dptr) return 1;

		memcpy(queue.dptr, value.dptr, value.dsize);
		memcpy(queue.dptr + value.dsize, &job, sizeof(struct vlp_job));

		queue.dsize = value.dsize + sizeof(struct vlp_job);

		tdb_store_bystring(tdb, keystr, queue, TDB_REPLACE);

		free(queue.dptr);

	} else {

		/* Create new queue */
		queue.dptr = (unsigned char *)&job;
		queue.dsize = sizeof(struct vlp_job);

		tdb_store_bystring(tdb, keystr, queue, TDB_REPLACE);
	}

	return 0;
}

/* Pause the queue */

static int queuepause_command(int argc, char **argv)
{
	char *printer;

	if (argc != 2) {
		printf("Usage: queuepause <printername>\n");
		return 1;
	}

	printer = argv[1];
	set_printer_status(printer, LPSTAT_STOPPED);

	return 0;
}

/* Resume the queue */

static int queueresume_command(int argc, char **argv)
{
	char *printer;

	if (argc != 2) {
		printf("Usage: queueresume <printername>\n");
		return 1;
	}

	printer = argv[1];
	set_printer_status(printer, LPSTAT_OK);

	return 0;
}

/* Pause a job */

static int lppause_command(int argc, char **argv)
{
	struct vlp_job *job_list;
	char *printer;
	int jobid, num_jobs, i;

	if (argc != 3) {
		printf("Usage: lppause <printername> <jobid>\n");
		return 1;
	}

	printer = argv[1];
	jobid = atoi(argv[2]);

	get_job_list(printer, &job_list, &num_jobs);

	for (i = 0; i < num_jobs; i++) {
		if (job_list[i].jobid == jobid) {
			job_list[i].status = LPQ_PAUSED;
			set_job_list(printer, job_list, num_jobs);
			return 0;
		}
	}

	return 1;
}

/* Resume a job */

static int lpresume_command(int argc, char **argv)
{
	struct vlp_job *job_list;
	char *printer;
	int jobid, num_jobs, i;

	if (argc != 3) {
		printf("Usage: lpresume <printername> <jobid>\n");
		return 1;
	}

	printer = argv[1];
	jobid = atoi(argv[2]);

	get_job_list(printer, &job_list, &num_jobs);

	for (i = 0; i < num_jobs; i++) {
		if (job_list[i].jobid == jobid) {
			job_list[i].status = LPQ_QUEUED;
			set_job_list(printer, job_list, num_jobs);
			return 0;
		}
	}

	return 1;
}

int main(int argc, char **argv)
{
	/* Parameter check */
	const char *printdb_path = NULL;

	if (argc < 2) {
		usage();
		return 1;
	}

	if (strncmp(argv[1], "tdbfile", strlen("tdbfile")) != 0) {
		usage();
		return 1;
	}

	printdb_path = get_string_param(argv[1]);
	if (!printdb_path) {
		return 1;
	}

	if (!(tdb = tdb_open(printdb_path, 0, 0, O_RDWR | O_CREAT,
			     0666))) {
		printf("%s: unable to open %s\n", argv[0], printdb_path);
		return 1;
	}

	/* Ensure we are modes 666 */

	chmod(printdb_path, 0666);

	/* Do commands */

	if (strcmp(argv[2], "lpq") == 0) {
		return lpq_command(argc - 2, &argv[2]);
	}

	if (strcmp(argv[2], "lprm") == 0) {
		return lprm_command(argc - 2, &argv[2]);
	}

	if (strcmp(argv[2], "print") == 0) {
		return print_command(argc - 2, &argv[2]);
	}

	if (strcmp(argv[2], "queuepause") == 0) {
		return queuepause_command(argc - 2, &argv[2]);
	}

	if (strcmp(argv[2], "queueresume") == 0) {
		return queueresume_command(argc - 2, &argv[2]);
	}

	if (strcmp(argv[2], "lppause") == 0) {
		return lppause_command(argc - 2, &argv[2]);
	}

	if (strcmp(argv[2], "lpresume") == 0) {
		return lpresume_command(argc - 2, &argv[2]);
	}

	/* Unknown command */

	printf("%s: invalid command %s\n", argv[0], argv[1]);
	return 1;
}