2 * "$Id: cups-lpd.c 7899 2008-09-03 12:57:17Z mike $"
4 * Line Printer Daemon interface for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * main() - Process an incoming LPD request...
18 * create_job() - Create a new print job.
19 * get_printer() - Get the named printer and its options.
20 * print_file() - Add a file to the current job.
21 * recv_print_job() - Receive a print job from the client.
22 * remove_jobs() - Cancel one or more jobs.
23 * send_state() - Send the queue state.
24 * smart_gets() - Get a line of text, removing the trailing CR and/or LF.
28 * Include necessary headers...
31 #include <cups/http-private.h>
32 #include <cups/cups.h>
33 #include <cups/string.h>
34 #include <cups/language.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
47 #ifdef HAVE_INTTYPES_H
48 # include <inttypes.h>
49 #endif /* HAVE_INTTYPES_H */
51 #ifdef HAVE_COREFOUNDATION_H
52 # include <CoreFoundation/CoreFoundation.h>
53 #endif /* HAVE_COREFOUNDATION_H */
55 # include <CoreFoundation/CFPriv.h>
56 #endif /* HAVE_CFPRIV_H */
60 * LPD "mini-daemon" for CUPS. This program must be used in conjunction
61 * with inetd or another similar program that monitors ports and starts
62 * daemons for each client connection. A typical configuration is:
64 * printer stream tcp nowait lp /usr/lib/cups/daemon/cups-lpd cups-lpd
66 * This daemon implements most of RFC 1179 (the unofficial LPD specification)
69 * - This daemon does not check to make sure that the source port is
70 * between 721 and 731, since it isn't necessary for proper
71 * functioning and port-based security is no security at all!
73 * - The "Print any waiting jobs" command is a no-op.
75 * The LPD-to-IPP mapping is as defined in RFC 2569. The report formats
76 * currently match the Solaris LPD mini-daemon.
83 static int create_job(http_t
*http
, const char *dest
, const char *title
,
84 const char *docname
, const char *user
,
85 int num_options
, cups_option_t
*options
);
86 static int get_printer(http_t
*http
, const char *name
, char *dest
,
87 int destsize
, cups_option_t
**options
,
88 int *accepting
, int *shared
, ipp_pstate_t
*state
);
89 static int print_file(http_t
*http
, int id
, const char *filename
,
90 const char *docname
, const char *user
,
91 const char *format
, int last
);
92 static int recv_print_job(const char *name
, int num_defaults
,
93 cups_option_t
*defaults
);
94 static int remove_jobs(const char *name
, const char *agent
,
96 static int send_state(const char *name
, const char *list
,
98 static char *smart_gets(char *s
, int len
, FILE *fp
);
102 * 'main()' - Process an incoming LPD request...
105 int /* O - Exit status */
106 main(int argc
, /* I - Number of command-line arguments */
107 char *argv
[]) /* I - Command-line arguments */
109 int i
; /* Looping var */
110 int num_defaults
; /* Number of default options */
111 cups_option_t
*defaults
; /* Default options */
112 char line
[256], /* Command string */
113 command
, /* Command code */
114 *dest
, /* Pointer to destination */
115 *list
, /* Pointer to list */
116 *agent
, /* Pointer to user */
117 status
; /* Status for client */
118 socklen_t hostlen
; /* Size of client address */
119 http_addr_t hostaddr
; /* Address of client */
120 char hostname
[256], /* Name of client */
121 hostip
[256], /* IP address */
122 *hostfamily
; /* Address family */
123 int hostlookups
; /* Do hostname lookups? */
127 * Don't buffer the output...
130 setbuf(stdout
, NULL
);
133 * Log things using the "cups-lpd" name...
136 openlog("cups-lpd", LOG_PID
, LOG_LPR
);
139 * Scan the command-line for options...
146 for (i
= 1; i
< argc
; i
++)
147 if (argv
[i
][0] == '-')
151 case 'h' : /* -h hostname[:port] */
153 cupsSetServer(argv
[i
] + 2);
158 cupsSetServer(argv
[i
]);
160 syslog(LOG_WARNING
, "Expected hostname string after -h option!");
164 case 'o' : /* Option */
166 num_defaults
= cupsParseOptions(argv
[i
] + 2, num_defaults
,
172 num_defaults
= cupsParseOptions(argv
[i
], num_defaults
,
175 syslog(LOG_WARNING
, "Expected option string after -o option!");
179 case 'n' : /* Don't do hostname lookups */
184 syslog(LOG_WARNING
, "Unknown option \"%c\" ignored!", argv
[i
][1]);
189 syslog(LOG_WARNING
, "Unknown command-line option \"%s\" ignored!",
193 * Get the address of the client...
196 hostlen
= sizeof(hostaddr
);
198 if (getpeername(0, (struct sockaddr
*)&hostaddr
, &hostlen
))
200 syslog(LOG_WARNING
, "Unable to get client address - %s", strerror(errno
));
201 strcpy(hostname
, "unknown");
205 httpAddrString(&hostaddr
, hostip
, sizeof(hostip
));
208 httpAddrLookup(&hostaddr
, hostname
, sizeof(hostname
));
210 strlcpy(hostname
, hostip
, sizeof(hostname
));
213 if (hostaddr
.addr
.sa_family
== AF_INET6
)
216 #endif /* AF_INET6 */
219 syslog(LOG_INFO
, "Connection from %s (%s %s)", hostname
, hostfamily
,
223 num_defaults
= cupsAddOption("job-originating-host-name", hostname
,
224 num_defaults
, &defaults
);
227 * RFC1179 specifies that only 1 daemon command can be received for
231 if (smart_gets(line
, sizeof(line
), stdin
) == NULL
)
234 * Unable to get command from client! Send an error status and return.
237 syslog(LOG_ERR
, "Unable to get command line from client!");
243 * The first byte is the command byte. After that will be the queue name,
244 * resource list, and/or user name.
254 for (list
= dest
+ 1; *list
&& !isspace(*list
& 255); list
++);
256 while (isspace(*list
& 255))
266 default : /* Unknown command */
267 syslog(LOG_ERR
, "Unknown LPD command 0x%02X!", command
);
268 syslog(LOG_ERR
, "Command line = %s", line
+ 1);
274 case 0x01 : /* Print any waiting jobs */
275 syslog(LOG_INFO
, "Print waiting jobs (no-op)");
281 case 0x02 : /* Receive a printer job */
282 syslog(LOG_INFO
, "Receive print job for %s", dest
);
283 /* recv_print_job() sends initial status byte */
285 status
= recv_print_job(dest
, num_defaults
, defaults
);
288 case 0x03 : /* Send queue state (short) */
289 syslog(LOG_INFO
, "Send queue state (short) for %s %s", dest
, list
);
290 /* no status byte for this command */
292 status
= send_state(dest
, list
, 0);
295 case 0x04 : /* Send queue state (long) */
296 syslog(LOG_INFO
, "Send queue state (long) for %s %s", dest
, list
);
297 /* no status byte for this command */
299 status
= send_state(dest
, list
, 1);
302 case 0x05 : /* Remove jobs */
306 * Grab the agent and skip to the list of users and/or jobs.
311 for (; *list
&& !isspace(*list
& 255); list
++);
312 while (isspace(*list
& 255))
315 syslog(LOG_INFO
, "Remove jobs %s on %s by %s", list
, dest
, agent
);
317 status
= remove_jobs(dest
, agent
, list
);
326 syslog(LOG_INFO
, "Closing connection");
334 * 'create_job()' - Create a new print job.
337 static int /* O - Job ID or -1 on error */
338 create_job(http_t
*http
, /* I - HTTP connection */
339 const char *dest
, /* I - Destination name */
340 const char *title
, /* I - job-name */
341 const char *docname
, /* I - Name of job file */
342 const char *user
, /* I - requesting-user-name */
343 int num_options
, /* I - Number of options for job */
344 cups_option_t
*options
) /* I - Options for job */
346 ipp_t
*request
; /* IPP request */
347 ipp_t
*response
; /* IPP response */
348 ipp_attribute_t
*attr
; /* IPP attribute */
349 char uri
[HTTP_MAX_URI
]; /* Printer URI */
354 * Setup the Create-Job request...
357 request
= ippNewRequest(IPP_CREATE_JOB
);
359 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
360 "localhost", 0, "/printers/%s", dest
);
362 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
365 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
366 "requesting-user-name", NULL
, user
);
369 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
373 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "document-name",
376 cupsEncodeOptions(request
, num_options
, options
);
382 snprintf(uri
, sizeof(uri
), "/printers/%s", dest
);
384 response
= cupsDoRequest(http
, request
, uri
);
386 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
388 syslog(LOG_ERR
, "Unable to create job - %s", cupsLastErrorString());
396 * Get the job-id value from the response and return it...
399 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) == NULL
)
403 syslog(LOG_ERR
, "No job-id attribute found in response from server!");
407 id
= attr
->values
[0].integer
;
409 syslog(LOG_INFO
, "Print file - job ID = %d", id
);
419 * 'get_printer()' - Get the named printer and its options.
422 static int /* O - Number of options or -1 on error */
423 get_printer(http_t
*http
, /* I - HTTP connection */
424 const char *name
, /* I - Printer name from request */
425 char *dest
, /* I - Destination buffer */
426 int destsize
, /* I - Size of destination buffer */
427 cups_option_t
**options
, /* O - Printer options */
428 int *accepting
, /* O - printer-is-accepting-jobs value */
429 int *shared
, /* O - printer-is-shared value */
430 ipp_pstate_t
*state
) /* O - printer-state value */
432 int num_options
; /* Number of options */
433 cups_file_t
*fp
; /* lpoptions file */
434 char line
[1024], /* Line from lpoptions file */
435 *value
, /* Pointer to value on line */
436 *optptr
; /* Pointer to options on line */
437 int linenum
; /* Line number in file */
438 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
439 ipp_t
*request
; /* IPP request */
440 ipp_t
*response
; /* IPP response */
441 ipp_attribute_t
*attr
; /* IPP attribute */
442 char uri
[HTTP_MAX_URI
]; /* Printer URI */
443 static const char * const requested
[] =
444 { /* Requested attributes */
446 "printer-is-accepting-jobs",
454 * Initialize everything...
462 *state
= IPP_PRINTER_STOPPED
;
467 * See if the name is a queue name optionally with an instance name.
470 strlcpy(dest
, name
, destsize
);
471 if ((value
= strchr(dest
, '/')) != NULL
)
475 * Setup the Get-Printer-Attributes request...
478 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
480 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
481 "localhost", 0, "/printers/%s", dest
);
483 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
486 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
487 "requested-attributes",
488 (int)(sizeof(requested
) / sizeof(requested
[0])),
495 response
= cupsDoRequest(http
, request
, "/");
497 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
500 * If we can't find the printer by name, look up the printer-name
501 * using the printer-info values...
504 ipp_attribute_t
*accepting_attr
,/* printer-is-accepting-jobs */
505 *info_attr
, /* printer-info */
506 *name_attr
, /* printer-name */
507 *shared_attr
, /* printer-is-shared */
508 *state_attr
; /* printer-state */
514 * Setup the CUPS-Get-Printers request...
517 request
= ippNewRequest(CUPS_GET_PRINTERS
);
519 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
520 "requested-attributes",
521 (int)(sizeof(requested
) / sizeof(requested
[0])),
528 response
= cupsDoRequest(http
, request
, "/");
530 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
532 syslog(LOG_ERR
, "Unable to get list of printers - %s",
533 cupsLastErrorString());
541 * Scan the response for printers...
545 attr
= response
->attrs
;
550 * Skip to the next printer...
553 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
560 * Get all of the attributes for the current printer...
563 accepting_attr
= NULL
;
569 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
571 if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
572 attr
->value_tag
== IPP_TAG_BOOLEAN
)
573 accepting_attr
= attr
;
574 else if (!strcmp(attr
->name
, "printer-info") &&
575 attr
->value_tag
== IPP_TAG_TEXT
)
577 else if (!strcmp(attr
->name
, "printer-name") &&
578 attr
->value_tag
== IPP_TAG_NAME
)
580 else if (!strcmp(attr
->name
, "printer-is-shared") &&
581 attr
->value_tag
== IPP_TAG_BOOLEAN
)
583 else if (!strcmp(attr
->name
, "printer-state") &&
584 attr
->value_tag
== IPP_TAG_ENUM
)
590 if (info_attr
&& name_attr
&&
591 !strcasecmp(name
, info_attr
->values
[0].string
.text
))
594 * Found a match, use this one!
597 strlcpy(dest
, name_attr
->values
[0].string
.text
, destsize
);
599 if (accepting
&& accepting_attr
)
600 *accepting
= accepting_attr
->values
[0].boolean
;
602 if (shared
&& shared_attr
)
603 *shared
= shared_attr
->values
[0].boolean
;
605 if (state
&& state_attr
)
606 *state
= (ipp_pstate_t
)state_attr
->values
[0].integer
;
616 syslog(LOG_ERR
, "Unable to find \"%s\" in list of printers!", name
);
626 * Get values from the response...
631 if ((attr
= ippFindAttribute(response
, "printer-is-accepting-jobs",
632 IPP_TAG_BOOLEAN
)) == NULL
)
633 syslog(LOG_ERR
, "No printer-is-accepting-jobs attribute found in "
634 "response from server!");
636 *accepting
= attr
->values
[0].boolean
;
641 if ((attr
= ippFindAttribute(response
, "printer-is-shared",
642 IPP_TAG_BOOLEAN
)) == NULL
)
644 syslog(LOG_ERR
, "No printer-is-shared attribute found in "
645 "response from server!");
649 *shared
= attr
->values
[0].boolean
;
654 if ((attr
= ippFindAttribute(response
, "printer-state",
655 IPP_TAG_ENUM
)) == NULL
)
656 syslog(LOG_ERR
, "No printer-state attribute found in "
657 "response from server!");
659 *state
= (ipp_pstate_t
)attr
->values
[0].integer
;
666 * Override shared value for LPD using system-specific APIs...
669 #ifdef HAVE_CFPRIV_H /* MacOS X */
670 if (shared
&& *shared
)
672 CFURLRef prefsurl
; /* URL for preferences file */
673 CFDataRef xmldata
; /* XML data from preferences file */
674 CFPropertyListRef plist
; /* Property list from XML data */
675 CFStringRef queueid
; /* CFString of destination name */
676 CFArrayRef lprqarray
; /* Array of shared "LPR" printers */
677 CFBooleanRef serverflag
; /* State of the print service */
678 static const char printerprefsfile
[] =
679 "/Library/Preferences/com.apple.printservice.plist";
680 /* Preferences file */
684 * See if we are running on MacOS X Server...
687 CFDictionaryRef versdict
= _CFCopyServerVersionDictionary();
692 * Yes, use the LPR sharing preference...
699 prefsurl
= CFURLCreateFromFileSystemRepresentation(
701 (const UInt8
*)printerprefsfile
,
702 (CFIndex
)strlen(printerprefsfile
),
706 if (CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault
,
707 prefsurl
, &xmldata
, NULL
,
710 plist
= CFPropertyListCreateFromXMLData(kCFAllocatorDefault
, xmldata
,
711 kCFPropertyListImmutable
,
715 serverflag
= (CFBooleanRef
)CFDictionaryGetValue(
716 (CFDictionaryRef
)plist
,
717 CFSTR("serviceState"));
719 if (serverflag
&& CFBooleanGetValue(serverflag
))
721 lprqarray
= (CFArrayRef
)CFDictionaryGetValue(
722 (CFDictionaryRef
)plist
,
723 CFSTR("lprSharedQueues"));
727 queueid
= CFStringCreateWithCString(CFAllocatorGetDefault(),
729 kCFStringEncodingUTF8
);
733 *shared
= CFArrayContainsValue(lprqarray
,
735 CFArrayGetCount(lprqarray
)),
753 syslog(LOG_ERR
, "Warning - Print Service sharing disabled for LPD "
754 "on queue: %s", name
);
757 #endif /* HAVE_CFPRIV_H */
760 * Next look for the printer in the lpoptions file...
765 if (options
&& shared
&& accepting
)
767 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
768 cups_serverroot
= CUPS_SERVERROOT
;
770 snprintf(line
, sizeof(line
), "%s/lpoptions", cups_serverroot
);
771 if ((fp
= cupsFileOpen(line
, "r")) != NULL
)
774 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
777 * Make sure we have "Dest name options" or "Default name options"...
780 if ((strcasecmp(line
, "Dest") && strcasecmp(line
, "Default")) || !value
)
784 * Separate destination name from options...
787 for (optptr
= value
; *optptr
&& !isspace(*optptr
& 255); optptr
++);
789 while (*optptr
== ' ')
793 * If this is our destination, parse the options and break out of
794 * the loop - we're done!
797 if (!strcasecmp(value
, name
))
799 num_options
= cupsParseOptions(optptr
, num_options
, options
);
811 * Return the number of options for this destination...
814 return (num_options
);
819 * 'print_file()' - Add a file to the current job.
822 static int /* O - 0 on success, -1 on failure */
823 print_file(http_t
*http
, /* I - HTTP connection */
824 int id
, /* I - Job ID */
825 const char *filename
, /* I - File to print */
826 const char *docname
, /* I - document-name */
827 const char *user
, /* I - requesting-user-name */
828 const char *format
, /* I - document-format */
829 int last
) /* I - 1 = last file in job */
831 ipp_t
*request
; /* IPP request */
832 char uri
[HTTP_MAX_URI
]; /* Printer URI */
836 * Setup the Send-Document request...
839 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
841 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", id
);
842 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
844 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
845 "requesting-user-name", NULL
, user
);
848 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
849 "document-name", NULL
, docname
);
852 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
853 "document-format", NULL
, format
);
856 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
862 snprintf(uri
, sizeof(uri
), "/jobs/%d", id
);
864 ippDelete(cupsDoFileRequest(http
, request
, uri
, filename
));
866 if (cupsLastError() > IPP_OK_CONFLICT
)
868 syslog(LOG_ERR
, "Unable to send document - %s", cupsLastErrorString());
878 * 'recv_print_job()' - Receive a print job from the client.
881 static int /* O - Command status */
883 const char *queue
, /* I - Printer name */
884 int num_defaults
, /* I - Number of default options */
885 cups_option_t
*defaults
) /* I - Default options */
887 http_t
*http
; /* HTTP connection */
888 int i
; /* Looping var */
889 int status
; /* Command status */
890 int fd
; /* Temporary file */
891 FILE *fp
; /* File pointer */
892 char filename
[1024]; /* Temporary filename */
893 int bytes
; /* Bytes received */
894 char line
[256], /* Line from file/stdin */
895 command
, /* Command from line */
896 *count
, /* Number of bytes */
897 *name
; /* Name of file */
898 const char *job_sheets
; /* Job sheets */
899 int num_data
; /* Number of data files */
900 char control
[1024], /* Control filename */
901 data
[100][256], /* Data files */
902 temp
[100][1024]; /* Temporary files */
903 char user
[1024], /* User name */
904 title
[1024], /* Job title */
905 docname
[1024], /* Document name */
906 dest
[256]; /* Printer/class queue */
907 int accepting
, /* printer-is-accepting */
908 shared
, /* printer-is-shared */
909 num_options
; /* Number of options */
910 cups_option_t
*options
; /* Options */
912 int docnumber
, /* Current document number */
913 doccount
; /* Count of documents */
917 * Connect to the server...
920 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
923 syslog(LOG_ERR
, "Unable to connect to server: %s", strerror(errno
));
931 * See if the printer is available...
934 num_options
= get_printer(http
, queue
, dest
, sizeof(dest
), &options
,
935 &accepting
, &shared
, NULL
);
937 if (num_options
< 0 || !accepting
|| !shared
)
940 syslog(LOG_INFO
, "Rejecting job because \"%s\" is not %s", dest
,
941 !accepting
? "accepting jobs" : "shared");
943 syslog(LOG_ERR
, "Unable to get printer information for \"%s\"", queue
);
952 putchar(0); /* OK so far... */
955 * Read the request...
964 while (smart_gets(line
, sizeof(line
), stdin
) != NULL
)
966 if (strlen(line
) < 2)
975 for (name
= count
+ 1; *name
&& !isspace(*name
& 255); name
++);
976 while (isspace(*name
& 255))
982 case 0x01 : /* Abort */
986 case 0x02 : /* Receive control file */
987 if (strlen(name
) < 2)
989 syslog(LOG_ERR
, "Bad control file name \"%s\"", name
);
998 * Append to the existing control file - the LPD spec is
999 * not entirely clear, but at least the OS/2 LPD code sends
1000 * multiple control files per connection...
1003 if ((fd
= open(control
, O_WRONLY
)) < 0)
1006 "Unable to append to temporary control file \"%s\" - %s",
1007 control
, strerror(errno
));
1013 lseek(fd
, 0, SEEK_END
);
1017 if ((fd
= cupsTempFd(control
, sizeof(control
))) < 0)
1019 syslog(LOG_ERR
, "Unable to open temporary control file \"%s\" - %s",
1020 control
, strerror(errno
));
1026 strcpy(filename
, control
);
1030 case 0x03 : /* Receive data file */
1031 if (strlen(name
) < 2)
1033 syslog(LOG_ERR
, "Bad data file name \"%s\"", name
);
1039 if (num_data
>= (int)(sizeof(data
) / sizeof(data
[0])))
1042 * Too many data files...
1045 syslog(LOG_ERR
, "Too many data files (%d)", num_data
);
1051 strlcpy(data
[num_data
], name
, sizeof(data
[0]));
1053 if ((fd
= cupsTempFd(temp
[num_data
], sizeof(temp
[0]))) < 0)
1055 syslog(LOG_ERR
, "Unable to open temporary data file \"%s\" - %s",
1056 temp
[num_data
], strerror(errno
));
1062 strcpy(filename
, temp
[num_data
]);
1074 * Copy the data or control file from the client...
1077 for (i
= atoi(count
); i
> 0; i
-= bytes
)
1079 if (i
> sizeof(line
))
1080 bytes
= sizeof(line
);
1084 if ((bytes
= fread(line
, 1, bytes
, stdin
)) > 0)
1085 bytes
= write(fd
, line
, bytes
);
1089 syslog(LOG_ERR
, "Error while reading file - %s",
1097 * Read trailing nul...
1102 if (fread(line
, 1, 1, stdin
) < 1)
1105 syslog(LOG_ERR
, "Error while reading trailing nul - %s",
1111 syslog(LOG_ERR
, "Trailing character after file is not nul (%02X)!",
1117 * Close the file and send an acknowledgement...
1131 * Process the control file and print stuff...
1134 if ((fp
= fopen(control
, "rb")) == NULL
)
1139 * Copy the default options...
1142 for (i
= 0; i
< num_defaults
; i
++)
1143 num_options
= cupsAddOption(defaults
[i
].name
,
1145 num_options
, &options
);
1148 * Grab the job information...
1156 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1159 * Process control lines...
1164 case 'J' : /* Job name */
1165 strlcpy(title
, line
+ 1, sizeof(title
));
1168 case 'N' : /* Document name */
1169 strlcpy(docname
, line
+ 1, sizeof(docname
));
1172 case 'P' : /* User identification */
1173 strlcpy(user
, line
+ 1, sizeof(user
));
1176 case 'L' : /* Print banner page */
1178 * If a banner was requested and it's not overridden by a
1179 * command line option and the destination's default is none
1180 * then add the standard banner...
1183 if (cupsGetOption("job-sheets", num_defaults
, defaults
) == NULL
&&
1184 ((job_sheets
= cupsGetOption("job-sheets", num_options
,
1185 options
)) == NULL
||
1186 !strcmp(job_sheets
, "none,none")))
1188 num_options
= cupsAddOption("job-sheets", "standard",
1189 num_options
, &options
);
1193 case 'c' : /* Plot CIF file */
1194 case 'd' : /* Print DVI file */
1195 case 'f' : /* Print formatted file */
1196 case 'g' : /* Plot file */
1197 case 'l' : /* Print file leaving control characters (raw) */
1198 case 'n' : /* Print ditroff output file */
1199 case 'o' : /* Print PostScript output file */
1200 case 'p' : /* Print file with 'pr' format (prettyprint) */
1201 case 'r' : /* File to print with FORTRAN carriage control */
1202 case 't' : /* Print troff output file */
1203 case 'v' : /* Print raster file */
1206 if (line
[0] == 'l' &&
1207 !cupsGetOption("document-format", num_options
, options
))
1208 num_options
= cupsAddOption("raw", "", num_options
, &options
);
1211 num_options
= cupsAddOption("prettyprint", "", num_options
,
1221 * Check that we have a username...
1226 syslog(LOG_WARNING
, "No username specified by client! "
1227 "Using \"anonymous\"...");
1228 strcpy(user
, "anonymous");
1235 if ((id
= create_job(http
, dest
, title
, docname
, user
, num_options
,
1241 * Then print the job files...
1249 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1252 * Process control lines...
1257 case 'N' : /* Document name */
1258 strlcpy(docname
, line
+ 1, sizeof(docname
));
1261 case 'c' : /* Plot CIF file */
1262 case 'd' : /* Print DVI file */
1263 case 'f' : /* Print formatted file */
1264 case 'g' : /* Plot file */
1265 case 'l' : /* Print file leaving control characters (raw) */
1266 case 'n' : /* Print ditroff output file */
1267 case 'o' : /* Print PostScript output file */
1268 case 'p' : /* Print file with 'pr' format (prettyprint) */
1269 case 'r' : /* File to print with FORTRAN carriage control */
1270 case 't' : /* Print troff output file */
1271 case 'v' : /* Print raster file */
1273 * Figure out which file we are printing...
1276 for (i
= 0; i
< num_data
; i
++)
1277 if (!strcmp(data
[i
], line
+ 1))
1287 * Send the print file...
1292 if (print_file(http
, id
, temp
[i
], docname
, user
,
1293 cupsGetOption("document-format", num_options
,
1295 docnumber
== doccount
))
1312 cupsFreeOptions(num_options
, options
);
1317 * Clean up all temporary files and return...
1322 for (i
= 0; i
< num_data
; i
++)
1330 * 'remove_jobs()' - Cancel one or more jobs.
1333 static int /* O - Command status */
1334 remove_jobs(const char *dest
, /* I - Destination */
1335 const char *agent
, /* I - User agent */
1336 const char *list
) /* I - List of jobs or users */
1338 int id
; /* Job ID */
1339 http_t
*http
; /* HTTP server connection */
1340 ipp_t
*request
; /* IPP Request */
1341 char uri
[HTTP_MAX_URI
]; /* Job URI */
1344 (void)dest
; /* Suppress compiler warnings... */
1347 * Try connecting to the local server...
1350 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1351 cupsEncryption())) == NULL
)
1353 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1359 * Loop for each job...
1362 while ((id
= atoi(list
)) > 0)
1365 * Skip job ID in list...
1368 while (isdigit(*list
& 255))
1370 while (isspace(*list
& 255))
1374 * Build an IPP_CANCEL_JOB request, which requires the following
1377 * attributes-charset
1378 * attributes-natural-language
1380 * requesting-user-name
1383 request
= ippNewRequest(IPP_CANCEL_JOB
);
1385 sprintf(uri
, "ipp://localhost/jobs/%d", id
);
1386 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1388 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1389 "requesting-user-name", NULL
, agent
);
1392 * Do the request and get back a response...
1395 ippDelete(cupsDoRequest(http
, request
, "/jobs"));
1397 if (cupsLastError() > IPP_OK_CONFLICT
)
1399 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
1400 cupsLastErrorString());
1405 syslog(LOG_INFO
, "Job ID %d canceled", id
);
1415 * 'send_state()' - Send the queue state.
1418 static int /* O - Command status */
1419 send_state(const char *queue
, /* I - Destination */
1420 const char *list
, /* I - Job or user */
1421 int longstatus
) /* I - List of jobs or users */
1423 int id
; /* Job ID from list */
1424 http_t
*http
; /* HTTP server connection */
1425 ipp_t
*request
, /* IPP Request */
1426 *response
; /* IPP Response */
1427 ipp_attribute_t
*attr
; /* Current attribute */
1428 ipp_pstate_t state
; /* Printer state */
1429 const char *jobdest
, /* Pointer into job-printer-uri */
1430 *jobuser
, /* Pointer to job-originating-user-name */
1431 *jobname
; /* Pointer to job-name */
1432 ipp_jstate_t jobstate
; /* job-state */
1433 int jobid
, /* job-id */
1434 jobsize
, /* job-k-octets */
1435 jobcount
, /* Number of jobs */
1436 jobcopies
, /* Number of copies */
1437 rank
; /* Rank of job */
1438 char rankstr
[255]; /* Rank string */
1439 char namestr
[1024]; /* Job name string */
1440 char uri
[HTTP_MAX_URI
]; /* Printer URI */
1441 char dest
[256]; /* Printer/class queue */
1442 static const char * const ranks
[10] = /* Ranking strings */
1455 static const char * const requested
[] =
1456 { /* Requested attributes */
1461 "job-originating-user-name",
1468 * Try connecting to the local server...
1471 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1472 cupsEncryption())) == NULL
)
1474 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1476 printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno
));
1481 * Get the actual destination name and printer state...
1484 if (get_printer(http
, queue
, dest
, sizeof(dest
), NULL
, NULL
, NULL
, &state
))
1486 syslog(LOG_ERR
, "Unable to get printer %s: %s", queue
,
1487 cupsLastErrorString());
1488 printf("Unable to get printer %s: %s", queue
, cupsLastErrorString());
1493 * Show the queue state...
1498 case IPP_PRINTER_IDLE
:
1499 printf("%s is ready\n", dest
);
1501 case IPP_PRINTER_PROCESSING
:
1502 printf("%s is ready and printing\n", dest
);
1504 case IPP_PRINTER_STOPPED
:
1505 printf("%s is not ready\n", dest
);
1510 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
1511 * the following attributes:
1513 * attributes-charset
1514 * attributes-natural-language
1515 * job-uri or printer-uri
1520 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
1522 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1523 "localhost", 0, "/printers/%s", dest
);
1525 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1529 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1532 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1533 "requesting-user-name", NULL
, list
);
1534 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
1537 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1538 "requested-attributes",
1539 sizeof(requested
) / sizeof(requested
[0]),
1543 * Do the request and get back a response...
1547 response
= cupsDoRequest(http
, request
, "/");
1549 if (cupsLastError() > IPP_OK_CONFLICT
)
1551 printf("get-jobs failed: %s\n", cupsLastErrorString());
1552 ippDelete(response
);
1557 * Loop through the job list and display them...
1560 for (attr
= response
->attrs
, rank
= 1; attr
; attr
= attr
->next
)
1563 * Skip leading attributes until we hit a job...
1566 while (attr
&& (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
))
1573 * Pull the needed attributes from this job...
1578 jobstate
= IPP_JOB_PENDING
;
1579 jobname
= "untitled";
1584 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
1586 if (!strcmp(attr
->name
, "job-id") &&
1587 attr
->value_tag
== IPP_TAG_INTEGER
)
1588 jobid
= attr
->values
[0].integer
;
1590 if (!strcmp(attr
->name
, "job-k-octets") &&
1591 attr
->value_tag
== IPP_TAG_INTEGER
)
1592 jobsize
= attr
->values
[0].integer
;
1594 if (!strcmp(attr
->name
, "job-state") &&
1595 attr
->value_tag
== IPP_TAG_ENUM
)
1596 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
1598 if (!strcmp(attr
->name
, "job-printer-uri") &&
1599 attr
->value_tag
== IPP_TAG_URI
)
1600 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
1603 if (!strcmp(attr
->name
, "job-originating-user-name") &&
1604 attr
->value_tag
== IPP_TAG_NAME
)
1605 jobuser
= attr
->values
[0].string
.text
;
1607 if (!strcmp(attr
->name
, "job-name") &&
1608 attr
->value_tag
== IPP_TAG_NAME
)
1609 jobname
= attr
->values
[0].string
.text
;
1611 if (!strcmp(attr
->name
, "copies") &&
1612 attr
->value_tag
== IPP_TAG_INTEGER
)
1613 jobcopies
= attr
->values
[0].integer
;
1619 * See if we have everything needed...
1622 if (!jobdest
|| !jobid
)
1630 if (!longstatus
&& jobcount
== 0)
1631 puts("Rank Owner Job File(s) Total Size");
1636 * Display the job...
1639 if (jobstate
== IPP_JOB_PROCESSING
)
1640 strcpy(rankstr
, "active");
1643 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
1652 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
1655 strlcpy(namestr
, jobname
, sizeof(namestr
));
1657 printf("%s: %-33.33s [job %d localhost]\n", jobuser
, rankstr
, jobid
);
1658 printf(" %-39.39s %.0f bytes\n", namestr
, 1024.0 * jobsize
);
1661 printf("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n", rankstr
, jobuser
,
1662 jobid
, jobname
, 1024.0 * jobsize
);
1668 ippDelete(response
);
1680 * 'smart_gets()' - Get a line of text, removing the trailing CR and/or LF.
1683 static char * /* O - Line read or NULL */
1684 smart_gets(char *s
, /* I - Pointer to line buffer */
1685 int len
, /* I - Size of line buffer */
1686 FILE *fp
) /* I - File to read from */
1688 char *ptr
, /* Pointer into line */
1689 *end
; /* End of line */
1690 int ch
; /* Character from file */
1694 * Read the line; unlike fgets(), we read the entire line but dump
1695 * characters that go past the end of the buffer. Also, we accept
1696 * CR, LF, or CR LF for the line endings to be "safe", although
1697 * RFC 1179 specifically says "just use LF".
1703 while ((ch
= getc(fp
)) != EOF
)
1707 else if (ch
== '\r')
1710 * See if a LF follows...
1726 if (ch
== EOF
&& ptr
== s
)
1734 * End of "$Id: cups-lpd.c 7899 2008-09-03 12:57:17Z mike $".