-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathippeveprinter.c
7227 lines (5934 loc) · 212 KB
/
ippeveprinter.c
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* IPP Everywhere printer application for CUPS.
*
* Copyright © 2010-2019 by Apple Inc.
*
* Licensed under Apache License v2.0. See the file "LICENSE" for more
* information.
*
* Note: This program began life as the "ippserver" sample code that first
* appeared in CUPS 1.4. The name has been changed in order to distinguish it
* from the PWG's much more ambitious "ippserver" program, which supports
* different kinds of IPP services and multiple services per instance - the
* "ippeveprinter" program exposes a single print service conforming to the
* current IPP Everywhere specification, thus the new name.
*/
/*
* Include necessary headers...
*/
#include <cups/cups-private.h>
#if !CUPS_LITE
# include <cups/ppd-private.h>
#endif /* !CUPS_LITE */
#include <limits.h>
#include <sys/stat.h>
#ifdef _WIN32
# include <fcntl.h>
# include <io.h>
# include <process.h>
# define WEXITSTATUS(s) (s)
# include <winsock2.h>
typedef ULONG nfds_t;
# define poll WSAPoll
#else
extern char **environ;
# include <sys/fcntl.h>
# include <sys/wait.h>
# include <poll.h>
#endif /* _WIN32 */
#ifdef HAVE_DNSSD
# include <dns_sd.h>
#elif defined(HAVE_AVAHI)
# include <avahi-client/client.h>
# include <avahi-client/publish.h>
# include <avahi-common/error.h>
# include <avahi-common/thread-watch.h>
#endif /* HAVE_DNSSD */
#ifdef HAVE_SYS_MOUNT_H
# include <sys/mount.h>
#endif /* HAVE_SYS_MOUNT_H */
#ifdef HAVE_SYS_STATFS_H
# include <sys/statfs.h>
#endif /* HAVE_SYS_STATFS_H */
#ifdef HAVE_SYS_STATVFS_H
# include <sys/statvfs.h>
#endif /* HAVE_SYS_STATVFS_H */
#ifdef HAVE_SYS_VFS_H
# include <sys/vfs.h>
#endif /* HAVE_SYS_VFS_H */
#include "printer-png.h"
/*
* Constants...
*/
enum ippeve_preason_e /* printer-state-reasons bit values */
{
IPPEVE_PREASON_NONE = 0x0000, /* none */
IPPEVE_PREASON_OTHER = 0x0001, /* other */
IPPEVE_PREASON_COVER_OPEN = 0x0002, /* cover-open */
IPPEVE_PREASON_INPUT_TRAY_MISSING = 0x0004,
/* input-tray-missing */
IPPEVE_PREASON_MARKER_SUPPLY_EMPTY = 0x0008,
/* marker-supply-empty */
IPPEVE_PREASON_MARKER_SUPPLY_LOW = 0x0010,
/* marker-supply-low */
IPPEVE_PREASON_MARKER_WASTE_ALMOST_FULL = 0x0020,
/* marker-waste-almost-full */
IPPEVE_PREASON_MARKER_WASTE_FULL = 0x0040,
/* marker-waste-full */
IPPEVE_PREASON_MEDIA_EMPTY = 0x0080, /* media-empty */
IPPEVE_PREASON_MEDIA_JAM = 0x0100, /* media-jam */
IPPEVE_PREASON_MEDIA_LOW = 0x0200, /* media-low */
IPPEVE_PREASON_MEDIA_NEEDED = 0x0400, /* media-needed */
IPPEVE_PREASON_MOVING_TO_PAUSED = 0x0800,
/* moving-to-paused */
IPPEVE_PREASON_PAUSED = 0x1000, /* paused */
IPPEVE_PREASON_SPOOL_AREA_FULL = 0x2000,/* spool-area-full */
IPPEVE_PREASON_TONER_EMPTY = 0x4000, /* toner-empty */
IPPEVE_PREASON_TONER_LOW = 0x8000 /* toner-low */
};
typedef unsigned int ippeve_preason_t; /* Bitfield for printer-state-reasons */
static const char * const ippeve_preason_strings[] =
{ /* Strings for each bit */
/* "none" is implied for no bits set */
"other",
"cover-open",
"input-tray-missing",
"marker-supply-empty",
"marker-supply-low",
"marker-waste-almost-full",
"marker-waste-full",
"media-empty",
"media-jam",
"media-low",
"media-needed",
"moving-to-paused",
"paused",
"spool-area-full",
"toner-empty",
"toner-low"
};
/*
* URL scheme for web resources...
*/
#ifdef HAVE_SSL
# define WEB_SCHEME "https"
#else
# define WEB_SCHEME "http"
#endif /* HAVE_SSL */
/*
* Structures...
*/
#ifdef HAVE_DNSSD
typedef DNSServiceRef ippeve_srv_t; /* Service reference */
typedef TXTRecordRef ippeve_txt_t; /* TXT record */
#elif defined(HAVE_AVAHI)
typedef AvahiEntryGroup *ippeve_srv_t; /* Service reference */
typedef AvahiStringList *ippeve_txt_t; /* TXT record */
#else
typedef void *_ipp_srv_t; /* Service reference */
typedef void *_ipp_txt_t; /* TXT record */
#endif /* HAVE_DNSSD */
typedef struct ippeve_filter_s /**** Attribute filter ****/
{
cups_array_t *ra; /* Requested attributes */
ipp_tag_t group_tag; /* Group to copy */
} ippeve_filter_t;
typedef struct ippeve_job_s ippeve_job_t;
typedef struct ippeve_printer_s /**** Printer data ****/
{
/* TODO: One IPv4 and one IPv6 listener are really not sufficient */
int ipv4, /* IPv4 listener */
ipv6; /* IPv6 listener */
ippeve_srv_t ipp_ref, /* Bonjour IPP service */
ipps_ref, /* Bonjour IPPS service */
http_ref, /* Bonjour HTTP service */
printer_ref; /* Bonjour LPD service */
char *dnssd_name, /* printer-dnssd-name */
*name, /* printer-name */
*icon, /* Icon filename */
*directory, /* Spool directory */
*hostname, /* Hostname */
*uri, /* printer-uri-supported */
*device_uri, /* Device URI (if any) */
#if !CUPS_LITE
*ppdfile, /* PPD file (if any) */
#endif /* !CUPS_LITE */
*command; /* Command to run with job file */
int port; /* Port */
size_t urilen; /* Length of printer URI */
ipp_t *attrs; /* Static attributes */
time_t start_time; /* Startup time */
time_t config_time; /* printer-config-change-time */
ipp_pstate_t state; /* printer-state value */
ippeve_preason_t state_reasons; /* printer-state-reasons values */
time_t state_time; /* printer-state-change-time */
cups_array_t *jobs; /* Jobs */
ippeve_job_t *active_job; /* Current active/pending job */
int next_job_id; /* Next job-id value */
_cups_rwlock_t rwlock; /* Printer lock */
} ippeve_printer_t;
struct ippeve_job_s /**** Job data ****/
{
int id; /* Job ID */
const char *name, /* job-name */
*username, /* job-originating-user-name */
*format; /* document-format */
ipp_jstate_t state; /* job-state value */
time_t created, /* time-at-creation value */
processing, /* time-at-processing value */
completed; /* time-at-completed value */
int impressions, /* job-impressions value */
impcompleted; /* job-impressions-completed value */
ipp_t *attrs; /* Static attributes */
int cancel; /* Non-zero when job canceled */
char *filename; /* Print file name */
int fd; /* Print file descriptor */
ippeve_printer_t *printer; /* Printer */
};
typedef struct ippeve_client_s /**** Client data ****/
{
http_t *http; /* HTTP connection */
ipp_t *request, /* IPP request */
*response; /* IPP response */
time_t start; /* Request start time */
http_state_t operation; /* Request operation */
ipp_op_t operation_id; /* IPP operation-id */
char uri[1024], /* Request URI */
*options; /* URI options */
http_addr_t addr; /* Client address */
char hostname[256]; /* Client hostname */
ippeve_printer_t *printer; /* Printer */
ippeve_job_t *job; /* Current job, if any */
} ippeve_client_t;
/*
* Local functions...
*/
static void clean_jobs(ippeve_printer_t *printer);
static int compare_jobs(ippeve_job_t *a, ippeve_job_t *b);
static void copy_attributes(ipp_t *to, ipp_t *from, cups_array_t *ra, ipp_tag_t group_tag, int quickcopy);
static void copy_job_attributes(ippeve_client_t *client, ippeve_job_t *job, cups_array_t *ra);
static ippeve_client_t *create_client(ippeve_printer_t *printer, int sock);
static ippeve_job_t *create_job(ippeve_client_t *client);
static int create_job_file(ippeve_job_t *job, char *fname, size_t fnamesize, const char *dir, const char *ext);
static int create_listener(const char *name, int port, int family);
static ipp_t *create_media_col(const char *media, const char *source, const char *type, int width, int length, int bottom, int left, int right, int top);
static ipp_t *create_media_size(int width, int length);
static ippeve_printer_t *create_printer(const char *servername, int serverport, const char *name, const char *location, const char *icon, cups_array_t *docformats, const char *subtypes, const char *directory, const char *command, const char *device_uri, ipp_t *attrs);
static void debug_attributes(const char *title, ipp_t *ipp, int response);
static void delete_client(ippeve_client_t *client);
static void delete_job(ippeve_job_t *job);
static void delete_printer(ippeve_printer_t *printer);
#ifdef HAVE_DNSSD
static void DNSSD_API dnssd_callback(DNSServiceRef sdRef, DNSServiceFlags flags, DNSServiceErrorType errorCode, const char *name, const char *regtype, const char *domain, ippeve_printer_t *printer);
#elif defined(HAVE_AVAHI)
static void dnssd_callback(AvahiEntryGroup *p, AvahiEntryGroupState state, void *context);
static void dnssd_client_cb(AvahiClient *c, AvahiClientState state, void *userdata);
#endif /* HAVE_DNSSD */
static void dnssd_init(void);
static int filter_cb(ippeve_filter_t *filter, ipp_t *dst, ipp_attribute_t *attr);
static ippeve_job_t *find_job(ippeve_client_t *client);
static void finish_document_data(ippeve_client_t *client, ippeve_job_t *job);
static void finish_document_uri(ippeve_client_t *client, ippeve_job_t *job);
static void html_escape(ippeve_client_t *client, const char *s, size_t slen);
static void html_footer(ippeve_client_t *client);
static void html_header(ippeve_client_t *client, const char *title, int refresh);
static void html_printf(ippeve_client_t *client, const char *format, ...) _CUPS_FORMAT(2, 3);
static void ipp_cancel_job(ippeve_client_t *client);
static void ipp_close_job(ippeve_client_t *client);
static void ipp_create_job(ippeve_client_t *client);
static void ipp_get_job_attributes(ippeve_client_t *client);
static void ipp_get_jobs(ippeve_client_t *client);
static void ipp_get_printer_attributes(ippeve_client_t *client);
static void ipp_identify_printer(ippeve_client_t *client);
static void ipp_print_job(ippeve_client_t *client);
static void ipp_print_uri(ippeve_client_t *client);
static void ipp_send_document(ippeve_client_t *client);
static void ipp_send_uri(ippeve_client_t *client);
static void ipp_validate_job(ippeve_client_t *client);
static ipp_t *load_ippserver_attributes(const char *servername, int serverport, const char *filename, cups_array_t *docformats);
static ipp_t *load_legacy_attributes(const char *make, const char *model, int ppm, int ppm_color, int duplex, cups_array_t *docformats);
#if !CUPS_LITE
static ipp_t *load_ppd_attributes(const char *ppdfile, cups_array_t *docformats);
#endif /* !CUPS_LITE */
static int parse_options(ippeve_client_t *client, cups_option_t **options);
static void process_attr_message(ippeve_job_t *job, char *message);
static void *process_client(ippeve_client_t *client);
static int process_http(ippeve_client_t *client);
static int process_ipp(ippeve_client_t *client);
static void *process_job(ippeve_job_t *job);
static void process_state_message(ippeve_job_t *job, char *message);
static int register_printer(ippeve_printer_t *printer, const char *subtypes);
static int respond_http(ippeve_client_t *client, http_status_t code, const char *content_coding, const char *type, size_t length);
static void respond_ipp(ippeve_client_t *client, ipp_status_t status, const char *message, ...) _CUPS_FORMAT(3, 4);
static void respond_unsupported(ippeve_client_t *client, ipp_attribute_t *attr);
static void run_printer(ippeve_printer_t *printer);
static int show_media(ippeve_client_t *client);
static int show_status(ippeve_client_t *client);
static int show_supplies(ippeve_client_t *client);
static char *time_string(time_t tv, char *buffer, size_t bufsize);
static void usage(int status) _CUPS_NORETURN;
static int valid_doc_attributes(ippeve_client_t *client);
static int valid_job_attributes(ippeve_client_t *client);
/*
* Globals...
*/
#ifdef HAVE_DNSSD
static DNSServiceRef DNSSDMaster = NULL;
#elif defined(HAVE_AVAHI)
static AvahiThreadedPoll *DNSSDMaster = NULL;
static AvahiClient *DNSSDClient = NULL;
#endif /* HAVE_DNSSD */
static int KeepFiles = 0, /* Keep spooled job files? */
MaxVersion = 20,/* Maximum IPP version (20 = 2.0, 11 = 1.1, etc.) */
Verbosity = 0; /* Verbosity level */
/*
* 'main()' - Main entry to the sample server.
*/
int /* O - Exit status */
main(int argc, /* I - Number of command-line args */
char *argv[]) /* I - Command-line arguments */
{
int i; /* Looping var */
const char *opt, /* Current option character */
*attrfile = NULL, /* ippserver attributes file */
*command = NULL, /* Command to run with job files */
*device_uri = NULL, /* Device URI */
*icon = NULL, /* Icon file */
#ifdef HAVE_SSL
*keypath = NULL, /* Keychain path */
#endif /* HAVE_SSL */
*location = "", /* Location of printer */
*make = "Test", /* Manufacturer */
*model = "Printer", /* Model */
*name = NULL, /* Printer name */
#if !CUPS_LITE
*ppdfile = NULL, /* PPD file */
#endif /* !CUPS_LITE */
*subtypes = "_print"; /* DNS-SD service subtype */
int legacy = 0, /* Legacy mode? */
duplex = 0, /* Duplex mode */
ppm = 10, /* Pages per minute for mono */
ppm_color = 0; /* Pages per minute for color */
ipp_t *attrs = NULL; /* Printer attributes */
char directory[1024] = ""; /* Spool directory */
cups_array_t *docformats = NULL; /* Supported formats */
const char *servername = NULL; /* Server host name */
int serverport = 0; /* Server port number (0 = auto) */
ippeve_printer_t *printer; /* Printer object */
/*
* Parse command-line arguments...
*/
for (i = 1; i < argc; i ++)
{
if (!strcmp(argv[i], "--help"))
{
usage(0);
}
else if (!strcmp(argv[i], "--version"))
{
puts(CUPS_SVERSION);
return (0);
}
else if (!strncmp(argv[i], "--", 2))
{
_cupsLangPrintf(stderr, _("%s: Unknown option \"%s\"."), argv[0], argv[i]);
usage(1);
}
else if (argv[i][0] == '-')
{
for (opt = argv[i] + 1; *opt; opt ++)
{
switch (*opt)
{
case '2' : /* -2 (enable 2-sided printing) */
duplex = 1;
legacy = 1;
break;
case 'D' : /* -D device-uri */
i ++;
if (i >= argc)
usage(1);
device_uri = argv[i];
break;
#ifdef HAVE_SSL
case 'K' : /* -K keypath */
i ++;
if (i >= argc)
usage(1);
keypath = argv[i];
break;
#endif /* HAVE_SSL */
case 'M' : /* -M manufacturer */
i ++;
if (i >= argc)
usage(1);
make = argv[i];
legacy = 1;
break;
#if !CUPS_LITE
case 'P' : /* -P filename.ppd */
i ++;
if (i >= argc)
usage(1);
ppdfile = argv[i];
break;
#endif /* !CUPS_LITE */
case 'V' : /* -V max-version */
i ++;
if (i >= argc)
usage(1);
if (!strcmp(argv[i], "2.0"))
MaxVersion = 20;
else if (!strcmp(argv[i], "1.1"))
MaxVersion = 11;
else
usage(1);
break;
case 'a' : /* -a attributes-file */
i ++;
if (i >= argc)
usage(1);
attrfile = argv[i];
break;
case 'c' : /* -c command */
i ++;
if (i >= argc)
usage(1);
command = argv[i];
break;
case 'd' : /* -d spool-directory */
i ++;
if (i >= argc)
usage(1);
strlcpy(directory, argv[i], sizeof(directory));
break;
case 'f' : /* -f type/subtype[,...] */
i ++;
if (i >= argc)
usage(1);
docformats = _cupsArrayNewStrings(argv[i], ',');
legacy = 1;
break;
case 'i' : /* -i icon.png */
i ++;
if (i >= argc)
usage(1);
icon = argv[i];
break;
case 'k' : /* -k (keep files) */
KeepFiles = 1;
break;
case 'l' : /* -l location */
i ++;
if (i >= argc)
usage(1);
location = argv[i];
break;
case 'm' : /* -m model */
i ++;
if (i >= argc)
usage(1);
model = argv[i];
legacy = 1;
break;
case 'n' : /* -n hostname */
i ++;
if (i >= argc)
usage(1);
servername = argv[i];
break;
case 'p' : /* -p port */
i ++;
if (i >= argc || !isdigit(argv[i][0] & 255))
usage(1);
serverport = atoi(argv[i]);
break;
case 'r' : /* -r subtype */
i ++;
if (i >= argc)
usage(1);
subtypes = argv[i];
break;
case 's' : /* -s speed[,color-speed] */
i ++;
if (i >= argc)
usage(1);
if (sscanf(argv[i], "%d,%d", &ppm, &ppm_color) < 1)
usage(1);
legacy = 1;
break;
case 'v' : /* -v (be verbose) */
Verbosity ++;
break;
default : /* Unknown */
_cupsLangPrintf(stderr, _("%s: Unknown option \"-%c\"."), argv[0], *opt);
usage(1);
}
}
}
else if (!name)
{
name = argv[i];
}
else
{
_cupsLangPrintf(stderr, _("%s: Unknown option \"%s\"."), argv[0], argv[i]);
usage(1);
}
}
if (!name)
usage(1);
#if CUPS_LITE
if (attrfile != NULL && legacy)
usage(1);
#else
if (((ppdfile != NULL) + (attrfile != NULL) + legacy) > 1)
usage(1);
#endif /* CUPS_LITE */
/*
* Apply defaults as needed...
*/
if (!docformats)
docformats = _cupsArrayNewStrings("application/pdf,image/jpeg,image/pwg-raster", ',');
if (!serverport)
{
#ifdef _WIN32
/*
* Windows is almost always used as a single user system, so use a default
* port number of 8631.
*/
serverport = 8631;
#else
/*
* Use 8000 + UID mod 1000 for the default port number...
*/
serverport = 8000 + ((int)getuid() % 1000);
#endif /* _WIN32 */
_cupsLangPrintf(stderr, _("Listening on port %d."), serverport);
}
if (!directory[0])
{
const char *tmpdir; /* Temporary directory */
#ifdef _WIN32
if ((tmpdir = getenv("TEMP")) == NULL)
tmpdir = "C:/TEMP";
#elif defined(__APPLE__) && TARGET_OS_OSX
if ((tmpdir = getenv("TMPDIR")) == NULL)
tmpdir = "/private/tmp";
#else
if ((tmpdir = getenv("TMPDIR")) == NULL)
tmpdir = "/tmp";
#endif /* _WIN32 */
snprintf(directory, sizeof(directory), "%s/ippeveprinter.%d", tmpdir, (int)getpid());
if (mkdir(directory, 0755) && errno != EEXIST)
{
_cupsLangPrintf(stderr, _("Unable to create spool directory \"%s\": %s"), directory, strerror(errno));
usage(1);
}
if (Verbosity)
_cupsLangPrintf(stderr, _("Using spool directory \"%s\"."), directory);
}
#ifdef HAVE_SSL
cupsSetServerCredentials(keypath, servername, 1);
#endif /* HAVE_SSL */
/*
* Initialize DNS-SD...
*/
dnssd_init();
/*
* Create the printer...
*/
if (!docformats)
docformats = _cupsArrayNewStrings("image/pwg-raster", ',');
if (attrfile)
attrs = load_ippserver_attributes(servername, serverport, attrfile, docformats);
#if !CUPS_LITE
else if (ppdfile)
attrs = load_ppd_attributes(ppdfile, docformats);
#endif /* !CUPS_LITE */
else
attrs = load_legacy_attributes(make, model, ppm, ppm_color, duplex, docformats);
if ((printer = create_printer(servername, serverport, name, location, icon, docformats, subtypes, directory, command, device_uri, attrs)) == NULL)
return (1);
#if !CUPS_LITE
if (ppdfile)
printer->ppdfile = strdup(ppdfile);
#endif /* !CUPS_LITE */
/*
* Run the print service...
*/
run_printer(printer);
/*
* Destroy the printer and exit...
*/
delete_printer(printer);
return (0);
}
/*
* 'clean_jobs()' - Clean out old (completed) jobs.
*/
static void
clean_jobs(ippeve_printer_t *printer) /* I - Printer */
{
ippeve_job_t *job; /* Current job */
time_t cleantime; /* Clean time */
if (cupsArrayCount(printer->jobs) == 0)
return;
cleantime = time(NULL) - 60;
_cupsRWLockWrite(&(printer->rwlock));
for (job = (ippeve_job_t *)cupsArrayFirst(printer->jobs);
job;
job = (ippeve_job_t *)cupsArrayNext(printer->jobs))
if (job->completed && job->completed < cleantime)
{
cupsArrayRemove(printer->jobs, job);
delete_job(job);
}
else
break;
_cupsRWUnlock(&(printer->rwlock));
}
/*
* 'compare_jobs()' - Compare two jobs.
*/
static int /* O - Result of comparison */
compare_jobs(ippeve_job_t *a, /* I - First job */
ippeve_job_t *b) /* I - Second job */
{
return (b->id - a->id);
}
/*
* 'copy_attributes()' - Copy attributes from one request to another.
*/
static void
copy_attributes(ipp_t *to, /* I - Destination request */
ipp_t *from, /* I - Source request */
cups_array_t *ra, /* I - Requested attributes */
ipp_tag_t group_tag, /* I - Group to copy */
int quickcopy) /* I - Do a quick copy? */
{
ippeve_filter_t filter; /* Filter data */
filter.ra = ra;
filter.group_tag = group_tag;
ippCopyAttributes(to, from, quickcopy, (ipp_copycb_t)filter_cb, &filter);
}
/*
* 'copy_job_attrs()' - Copy job attributes to the response.
*/
static void
copy_job_attributes(
ippeve_client_t *client, /* I - Client */
ippeve_job_t *job, /* I - Job */
cups_array_t *ra) /* I - requested-attributes */
{
copy_attributes(client->response, job->attrs, ra, IPP_TAG_JOB, 0);
if (!ra || cupsArrayFind(ra, "date-time-at-completed"))
{
if (job->completed)
ippAddDate(client->response, IPP_TAG_JOB, "date-time-at-completed", ippTimeToDate(job->completed));
else
ippAddOutOfBand(client->response, IPP_TAG_JOB, IPP_TAG_NOVALUE, "date-time-at-completed");
}
if (!ra || cupsArrayFind(ra, "date-time-at-processing"))
{
if (job->processing)
ippAddDate(client->response, IPP_TAG_JOB, "date-time-at-processing", ippTimeToDate(job->processing));
else
ippAddOutOfBand(client->response, IPP_TAG_JOB, IPP_TAG_NOVALUE, "date-time-at-processing");
}
if (!ra || cupsArrayFind(ra, "job-impressions"))
ippAddInteger(client->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-impressions", job->impressions);
if (!ra || cupsArrayFind(ra, "job-impressions-completed"))
ippAddInteger(client->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-impressions-completed", job->impcompleted);
if (!ra || cupsArrayFind(ra, "job-printer-up-time"))
ippAddInteger(client->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-printer-up-time", (int)(time(NULL) - client->printer->start_time));
if (!ra || cupsArrayFind(ra, "job-state"))
ippAddInteger(client->response, IPP_TAG_JOB, IPP_TAG_ENUM,
"job-state", job->state);
if (!ra || cupsArrayFind(ra, "job-state-message"))
{
switch (job->state)
{
case IPP_JSTATE_PENDING :
ippAddString(client->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_TEXT), "job-state-message", NULL, "Job pending.");
break;
case IPP_JSTATE_HELD :
if (job->fd >= 0)
ippAddString(client->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_TEXT), "job-state-message", NULL, "Job incoming.");
else if (ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_ZERO))
ippAddString(client->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_TEXT), "job-state-message", NULL, "Job held.");
else
ippAddString(client->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_TEXT), "job-state-message", NULL, "Job created.");
break;
case IPP_JSTATE_PROCESSING :
if (job->cancel)
ippAddString(client->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_TEXT), "job-state-message", NULL, "Job canceling.");
else
ippAddString(client->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_TEXT), "job-state-message", NULL, "Job printing.");
break;
case IPP_JSTATE_STOPPED :
ippAddString(client->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_TEXT), "job-state-message", NULL, "Job stopped.");
break;
case IPP_JSTATE_CANCELED :
ippAddString(client->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_TEXT), "job-state-message", NULL, "Job canceled.");
break;
case IPP_JSTATE_ABORTED :
ippAddString(client->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_TEXT), "job-state-message", NULL, "Job aborted.");
break;
case IPP_JSTATE_COMPLETED :
ippAddString(client->response, IPP_TAG_JOB, IPP_CONST_TAG(IPP_TAG_TEXT), "job-state-message", NULL, "Job completed.");
break;
}
}
if (!ra || cupsArrayFind(ra, "job-state-reasons"))
{
switch (job->state)
{
case IPP_JSTATE_PENDING :
ippAddString(client->response, IPP_TAG_JOB,
IPP_CONST_TAG(IPP_TAG_KEYWORD), "job-state-reasons",
NULL, "none");
break;
case IPP_JSTATE_HELD :
if (job->fd >= 0)
ippAddString(client->response, IPP_TAG_JOB,
IPP_CONST_TAG(IPP_TAG_KEYWORD),
"job-state-reasons", NULL, "job-incoming");
else if (ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_ZERO))
ippAddString(client->response, IPP_TAG_JOB,
IPP_CONST_TAG(IPP_TAG_KEYWORD),
"job-state-reasons", NULL, "job-hold-until-specified");
else
ippAddString(client->response, IPP_TAG_JOB,
IPP_CONST_TAG(IPP_TAG_KEYWORD),
"job-state-reasons", NULL, "job-data-insufficient");
break;
case IPP_JSTATE_PROCESSING :
if (job->cancel)
ippAddString(client->response, IPP_TAG_JOB,
IPP_CONST_TAG(IPP_TAG_KEYWORD),
"job-state-reasons", NULL, "processing-to-stop-point");
else
ippAddString(client->response, IPP_TAG_JOB,
IPP_CONST_TAG(IPP_TAG_KEYWORD),
"job-state-reasons", NULL, "job-printing");
break;
case IPP_JSTATE_STOPPED :
ippAddString(client->response, IPP_TAG_JOB,
IPP_CONST_TAG(IPP_TAG_KEYWORD), "job-state-reasons",
NULL, "job-stopped");
break;
case IPP_JSTATE_CANCELED :
ippAddString(client->response, IPP_TAG_JOB,
IPP_CONST_TAG(IPP_TAG_KEYWORD), "job-state-reasons",
NULL, "job-canceled-by-user");
break;
case IPP_JSTATE_ABORTED :
ippAddString(client->response, IPP_TAG_JOB,
IPP_CONST_TAG(IPP_TAG_KEYWORD), "job-state-reasons",
NULL, "aborted-by-system");
break;
case IPP_JSTATE_COMPLETED :
ippAddString(client->response, IPP_TAG_JOB,
IPP_CONST_TAG(IPP_TAG_KEYWORD), "job-state-reasons",
NULL, "job-completed-successfully");
break;
}
}
if (!ra || cupsArrayFind(ra, "time-at-completed"))
ippAddInteger(client->response, IPP_TAG_JOB,
job->completed ? IPP_TAG_INTEGER : IPP_TAG_NOVALUE,
"time-at-completed", (int)(job->completed - client->printer->start_time));
if (!ra || cupsArrayFind(ra, "time-at-processing"))
ippAddInteger(client->response, IPP_TAG_JOB,
job->processing ? IPP_TAG_INTEGER : IPP_TAG_NOVALUE,
"time-at-processing", (int)(job->processing - client->printer->start_time));
}
/*
* 'create_client()' - Accept a new network connection and create a client
* object.
*/
static ippeve_client_t * /* O - Client */
create_client(ippeve_printer_t *printer, /* I - Printer */
int sock) /* I - Listen socket */
{
ippeve_client_t *client; /* Client */
if ((client = calloc(1, sizeof(ippeve_client_t))) == NULL)
{
perror("Unable to allocate memory for client");
return (NULL);
}
client->printer = printer;
/*
* Accept the client and get the remote address...
*/
if ((client->http = httpAcceptConnection(sock, 1)) == NULL)
{
perror("Unable to accept client connection");
free(client);
return (NULL);
}
httpGetHostname(client->http, client->hostname, sizeof(client->hostname));
if (Verbosity)
fprintf(stderr, "Accepted connection from %s\n", client->hostname);
return (client);
}
/*
* 'create_job()' - Create a new job object from a Print-Job or Create-Job
* request.
*/
static ippeve_job_t * /* O - Job */
create_job(ippeve_client_t *client) /* I - Client */
{
ippeve_job_t *job; /* Job */
ipp_attribute_t *attr; /* Job attribute */
char uri[1024], /* job-uri value */
uuid[64]; /* job-uuid value */
_cupsRWLockWrite(&(client->printer->rwlock));
if (client->printer->active_job &&
client->printer->active_job->state < IPP_JSTATE_CANCELED)
{
/*
* Only accept a single job at a time...
*/
_cupsRWUnlock(&(client->printer->rwlock));
return (NULL);
}
/*
* Allocate and initialize the job object...
*/
if ((job = calloc(1, sizeof(ippeve_job_t))) == NULL)
{
perror("Unable to allocate memory for job");
return (NULL);
}
job->printer = client->printer;
job->attrs = ippNew();
job->state = IPP_JSTATE_HELD;
job->fd = -1;
/*
* Copy all of the job attributes...
*/
copy_attributes(job->attrs, client->request, NULL, IPP_TAG_JOB, 0);
/*
* Get the requesting-user-name, document format, and priority...
*/
if ((attr = ippFindAttribute(client->request, "requesting-user-name", IPP_TAG_NAME)) != NULL)
job->username = ippGetString(attr, 0, NULL);
else
job->username = "anonymous";
ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-originating-user-name", NULL, job->username);
if (ippGetOperation(client->request) != IPP_OP_CREATE_JOB)
{
if ((attr = ippFindAttribute(job->attrs, "document-format-detected", IPP_TAG_MIMETYPE)) != NULL)
job->format = ippGetString(attr, 0, NULL);
else if ((attr = ippFindAttribute(job->attrs, "document-format-supplied", IPP_TAG_MIMETYPE)) != NULL)
job->format = ippGetString(attr, 0, NULL);
else
job->format = "application/octet-stream";
}
if ((attr = ippFindAttribute(client->request, "job-impressions", IPP_TAG_INTEGER)) != NULL)
job->impressions = ippGetInteger(attr, 0);