2 * "$Id: cups-lpd.c 7899 2008-09-03 12:57:17Z mike $"
4 * Line Printer Daemon interface for CUPS.
6 * Copyright 2007-2010 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/cups-private.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <netinet/in.h>
40 #ifdef HAVE_INTTYPES_H
41 # include <inttypes.h>
42 #endif /* HAVE_INTTYPES_H */
44 #ifdef HAVE_COREFOUNDATION_H
45 # include <CoreFoundation/CoreFoundation.h>
46 #endif /* HAVE_COREFOUNDATION_H */
48 # include <CoreFoundation/CFPriv.h>
49 #endif /* HAVE_CFPRIV_H */
53 * LPD "mini-daemon" for CUPS. This program must be used in conjunction
54 * with inetd or another similar program that monitors ports and starts
55 * daemons for each client connection. A typical configuration is:
57 * printer stream tcp nowait lp /usr/lib/cups/daemon/cups-lpd cups-lpd
59 * This daemon implements most of RFC 1179 (the unofficial LPD specification)
62 * - This daemon does not check to make sure that the source port is
63 * between 721 and 731, since it isn't necessary for proper
64 * functioning and port-based security is no security at all!
66 * - The "Print any waiting jobs" command is a no-op.
68 * The LPD-to-IPP mapping is as defined in RFC 2569. The report formats
69 * currently match the Solaris LPD mini-daemon.
76 static int create_job(http_t
*http
, const char *dest
, const char *title
,
77 const char *docname
, const char *user
,
78 int num_options
, cups_option_t
*options
);
79 static int get_printer(http_t
*http
, const char *name
, char *dest
,
80 int destsize
, cups_option_t
**options
,
81 int *accepting
, int *shared
, ipp_pstate_t
*state
);
82 static int print_file(http_t
*http
, int id
, const char *filename
,
83 const char *docname
, const char *user
,
84 const char *format
, int last
);
85 static int recv_print_job(const char *name
, int num_defaults
,
86 cups_option_t
*defaults
);
87 static int remove_jobs(const char *name
, const char *agent
,
89 static int send_state(const char *name
, const char *list
,
91 static char *smart_gets(char *s
, int len
, FILE *fp
);
95 * 'main()' - Process an incoming LPD request...
98 int /* O - Exit status */
99 main(int argc
, /* I - Number of command-line arguments */
100 char *argv
[]) /* I - Command-line arguments */
102 int i
; /* Looping var */
103 int num_defaults
; /* Number of default options */
104 cups_option_t
*defaults
; /* Default options */
105 char line
[256], /* Command string */
106 command
, /* Command code */
107 *dest
, /* Pointer to destination */
108 *list
, /* Pointer to list */
109 *agent
, /* Pointer to user */
110 status
; /* Status for client */
111 socklen_t hostlen
; /* Size of client address */
112 http_addr_t hostaddr
; /* Address of client */
113 char hostname
[256], /* Name of client */
114 hostip
[256], /* IP address */
115 *hostfamily
; /* Address family */
116 int hostlookups
; /* Do hostname lookups? */
120 * Don't buffer the output...
123 setbuf(stdout
, NULL
);
126 * Log things using the "cups-lpd" name...
129 openlog("cups-lpd", LOG_PID
, LOG_LPR
);
132 * Scan the command-line for options...
139 for (i
= 1; i
< argc
; i
++)
140 if (argv
[i
][0] == '-')
144 case 'h' : /* -h hostname[:port] */
146 cupsSetServer(argv
[i
] + 2);
151 cupsSetServer(argv
[i
]);
153 syslog(LOG_WARNING
, "Expected hostname string after -h option!");
157 case 'o' : /* Option */
159 num_defaults
= cupsParseOptions(argv
[i
] + 2, num_defaults
,
165 num_defaults
= cupsParseOptions(argv
[i
], num_defaults
,
168 syslog(LOG_WARNING
, "Expected option string after -o option!");
172 case 'n' : /* Don't do hostname lookups */
177 syslog(LOG_WARNING
, "Unknown option \"%c\" ignored!", argv
[i
][1]);
182 syslog(LOG_WARNING
, "Unknown command-line option \"%s\" ignored!",
186 * Get the address of the client...
189 hostlen
= sizeof(hostaddr
);
191 if (getpeername(0, (struct sockaddr
*)&hostaddr
, &hostlen
))
193 syslog(LOG_WARNING
, "Unable to get client address - %s", strerror(errno
));
194 strcpy(hostname
, "unknown");
198 httpAddrString(&hostaddr
, hostip
, sizeof(hostip
));
201 httpAddrLookup(&hostaddr
, hostname
, sizeof(hostname
));
203 strlcpy(hostname
, hostip
, sizeof(hostname
));
206 if (hostaddr
.addr
.sa_family
== AF_INET6
)
209 #endif /* AF_INET6 */
212 syslog(LOG_INFO
, "Connection from %s (%s %s)", hostname
, hostfamily
,
216 num_defaults
= cupsAddOption("job-originating-host-name", hostname
,
217 num_defaults
, &defaults
);
220 * RFC1179 specifies that only 1 daemon command can be received for
224 if (smart_gets(line
, sizeof(line
), stdin
) == NULL
)
227 * Unable to get command from client! Send an error status and return.
230 syslog(LOG_ERR
, "Unable to get command line from client!");
236 * The first byte is the command byte. After that will be the queue name,
237 * resource list, and/or user name.
247 for (list
= dest
+ 1; *list
&& !isspace(*list
& 255); list
++);
249 while (isspace(*list
& 255))
259 default : /* Unknown command */
260 syslog(LOG_ERR
, "Unknown LPD command 0x%02X!", command
);
261 syslog(LOG_ERR
, "Command line = %s", line
+ 1);
267 case 0x01 : /* Print any waiting jobs */
268 syslog(LOG_INFO
, "Print waiting jobs (no-op)");
274 case 0x02 : /* Receive a printer job */
275 syslog(LOG_INFO
, "Receive print job for %s", dest
);
276 /* recv_print_job() sends initial status byte */
278 status
= recv_print_job(dest
, num_defaults
, defaults
);
281 case 0x03 : /* Send queue state (short) */
282 syslog(LOG_INFO
, "Send queue state (short) for %s %s", dest
, list
);
283 /* no status byte for this command */
285 status
= send_state(dest
, list
, 0);
288 case 0x04 : /* Send queue state (long) */
289 syslog(LOG_INFO
, "Send queue state (long) for %s %s", dest
, list
);
290 /* no status byte for this command */
292 status
= send_state(dest
, list
, 1);
295 case 0x05 : /* Remove jobs */
299 * Grab the agent and skip to the list of users and/or jobs.
304 for (; *list
&& !isspace(*list
& 255); list
++);
305 while (isspace(*list
& 255))
308 syslog(LOG_INFO
, "Remove jobs %s on %s by %s", list
, dest
, agent
);
310 status
= remove_jobs(dest
, agent
, list
);
319 syslog(LOG_INFO
, "Closing connection");
327 * 'create_job()' - Create a new print job.
330 static int /* O - Job ID or -1 on error */
331 create_job(http_t
*http
, /* I - HTTP connection */
332 const char *dest
, /* I - Destination name */
333 const char *title
, /* I - job-name */
334 const char *docname
, /* I - Name of job file */
335 const char *user
, /* I - requesting-user-name */
336 int num_options
, /* I - Number of options for job */
337 cups_option_t
*options
) /* I - Options for job */
339 ipp_t
*request
; /* IPP request */
340 ipp_t
*response
; /* IPP response */
341 ipp_attribute_t
*attr
; /* IPP attribute */
342 char uri
[HTTP_MAX_URI
]; /* Printer URI */
347 * Setup the Create-Job request...
350 request
= ippNewRequest(IPP_CREATE_JOB
);
352 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
353 "localhost", 0, "/printers/%s", dest
);
355 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
358 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
359 "requesting-user-name", NULL
, user
);
362 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
366 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "document-name",
369 cupsEncodeOptions(request
, num_options
, options
);
375 snprintf(uri
, sizeof(uri
), "/printers/%s", dest
);
377 response
= cupsDoRequest(http
, request
, uri
);
379 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
381 syslog(LOG_ERR
, "Unable to create job - %s", cupsLastErrorString());
389 * Get the job-id value from the response and return it...
392 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) == NULL
)
396 syslog(LOG_ERR
, "No job-id attribute found in response from server!");
400 id
= attr
->values
[0].integer
;
402 syslog(LOG_INFO
, "Print file - job ID = %d", id
);
412 * 'get_printer()' - Get the named printer and its options.
415 static int /* O - Number of options or -1 on error */
416 get_printer(http_t
*http
, /* I - HTTP connection */
417 const char *name
, /* I - Printer name from request */
418 char *dest
, /* I - Destination buffer */
419 int destsize
, /* I - Size of destination buffer */
420 cups_option_t
**options
, /* O - Printer options */
421 int *accepting
, /* O - printer-is-accepting-jobs value */
422 int *shared
, /* O - printer-is-shared value */
423 ipp_pstate_t
*state
) /* O - printer-state value */
425 int num_options
; /* Number of options */
426 cups_file_t
*fp
; /* lpoptions file */
427 char line
[1024], /* Line from lpoptions file */
428 *value
, /* Pointer to value on line */
429 *optptr
; /* Pointer to options on line */
430 int linenum
; /* Line number in file */
431 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
432 ipp_t
*request
; /* IPP request */
433 ipp_t
*response
; /* IPP response */
434 ipp_attribute_t
*attr
; /* IPP attribute */
435 char uri
[HTTP_MAX_URI
]; /* Printer URI */
436 static const char * const requested
[] =
437 { /* Requested attributes */
439 "printer-is-accepting-jobs",
447 * Initialize everything...
455 *state
= IPP_PRINTER_STOPPED
;
460 * See if the name is a queue name optionally with an instance name.
463 strlcpy(dest
, name
, destsize
);
464 if ((value
= strchr(dest
, '/')) != NULL
)
468 * Setup the Get-Printer-Attributes request...
471 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
473 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
474 "localhost", 0, "/printers/%s", dest
);
476 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
479 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
480 "requested-attributes",
481 (int)(sizeof(requested
) / sizeof(requested
[0])),
488 response
= cupsDoRequest(http
, request
, "/");
490 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
493 * If we can't find the printer by name, look up the printer-name
494 * using the printer-info values...
497 ipp_attribute_t
*accepting_attr
,/* printer-is-accepting-jobs */
498 *info_attr
, /* printer-info */
499 *name_attr
, /* printer-name */
500 *shared_attr
, /* printer-is-shared */
501 *state_attr
; /* printer-state */
507 * Setup the CUPS-Get-Printers request...
510 request
= ippNewRequest(CUPS_GET_PRINTERS
);
512 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
513 "requested-attributes",
514 (int)(sizeof(requested
) / sizeof(requested
[0])),
521 response
= cupsDoRequest(http
, request
, "/");
523 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
525 syslog(LOG_ERR
, "Unable to get list of printers - %s",
526 cupsLastErrorString());
534 * Scan the response for printers...
538 attr
= response
->attrs
;
543 * Skip to the next printer...
546 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
553 * Get all of the attributes for the current printer...
556 accepting_attr
= NULL
;
562 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
564 if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
565 attr
->value_tag
== IPP_TAG_BOOLEAN
)
566 accepting_attr
= attr
;
567 else if (!strcmp(attr
->name
, "printer-info") &&
568 attr
->value_tag
== IPP_TAG_TEXT
)
570 else if (!strcmp(attr
->name
, "printer-name") &&
571 attr
->value_tag
== IPP_TAG_NAME
)
573 else if (!strcmp(attr
->name
, "printer-is-shared") &&
574 attr
->value_tag
== IPP_TAG_BOOLEAN
)
576 else if (!strcmp(attr
->name
, "printer-state") &&
577 attr
->value_tag
== IPP_TAG_ENUM
)
583 if (info_attr
&& name_attr
&&
584 !strcasecmp(name
, info_attr
->values
[0].string
.text
))
587 * Found a match, use this one!
590 strlcpy(dest
, name_attr
->values
[0].string
.text
, destsize
);
592 if (accepting
&& accepting_attr
)
593 *accepting
= accepting_attr
->values
[0].boolean
;
595 if (shared
&& shared_attr
)
596 *shared
= shared_attr
->values
[0].boolean
;
598 if (state
&& state_attr
)
599 *state
= (ipp_pstate_t
)state_attr
->values
[0].integer
;
609 syslog(LOG_ERR
, "Unable to find \"%s\" in list of printers!", name
);
619 * Get values from the response...
624 if ((attr
= ippFindAttribute(response
, "printer-is-accepting-jobs",
625 IPP_TAG_BOOLEAN
)) == NULL
)
626 syslog(LOG_ERR
, "No printer-is-accepting-jobs attribute found in "
627 "response from server!");
629 *accepting
= attr
->values
[0].boolean
;
634 if ((attr
= ippFindAttribute(response
, "printer-is-shared",
635 IPP_TAG_BOOLEAN
)) == NULL
)
637 syslog(LOG_ERR
, "No printer-is-shared attribute found in "
638 "response from server!");
642 *shared
= attr
->values
[0].boolean
;
647 if ((attr
= ippFindAttribute(response
, "printer-state",
648 IPP_TAG_ENUM
)) == NULL
)
649 syslog(LOG_ERR
, "No printer-state attribute found in "
650 "response from server!");
652 *state
= (ipp_pstate_t
)attr
->values
[0].integer
;
659 * Override shared value for LPD using system-specific APIs...
662 #ifdef HAVE_CFPRIV_H /* MacOS X */
663 if (shared
&& *shared
)
665 CFURLRef prefsurl
; /* URL for preferences file */
666 CFDataRef xmldata
; /* XML data from preferences file */
667 CFPropertyListRef plist
; /* Property list from XML data */
668 CFStringRef queueid
; /* CFString of destination name */
669 CFArrayRef lprqarray
; /* Array of shared "LPR" printers */
670 CFBooleanRef serverflag
; /* State of the print service */
671 static const char printerprefsfile
[] =
672 "/Library/Preferences/com.apple.printservice.plist";
673 /* Preferences file */
677 * See if we are running on MacOS X Server...
680 CFDictionaryRef versdict
= _CFCopyServerVersionDictionary();
685 * Yes, use the LPR sharing preference...
692 prefsurl
= CFURLCreateFromFileSystemRepresentation(
694 (const UInt8
*)printerprefsfile
,
695 (CFIndex
)strlen(printerprefsfile
),
699 if (CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault
,
700 prefsurl
, &xmldata
, NULL
,
703 plist
= CFPropertyListCreateFromXMLData(kCFAllocatorDefault
, xmldata
,
704 kCFPropertyListImmutable
,
708 serverflag
= (CFBooleanRef
)CFDictionaryGetValue(
709 (CFDictionaryRef
)plist
,
710 CFSTR("serviceState"));
712 if (serverflag
&& CFBooleanGetValue(serverflag
))
714 lprqarray
= (CFArrayRef
)CFDictionaryGetValue(
715 (CFDictionaryRef
)plist
,
716 CFSTR("lprSharedQueues"));
720 queueid
= CFStringCreateWithCString(CFAllocatorGetDefault(),
722 kCFStringEncodingUTF8
);
726 *shared
= CFArrayContainsValue(lprqarray
,
728 CFArrayGetCount(lprqarray
)),
746 syslog(LOG_ERR
, "Warning - Print Service sharing disabled for LPD "
747 "on queue: %s", name
);
750 #endif /* HAVE_CFPRIV_H */
753 * Next look for the printer in the lpoptions file...
758 if (options
&& shared
&& accepting
)
760 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
761 cups_serverroot
= CUPS_SERVERROOT
;
763 snprintf(line
, sizeof(line
), "%s/lpoptions", cups_serverroot
);
764 if ((fp
= cupsFileOpen(line
, "r")) != NULL
)
767 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
770 * Make sure we have "Dest name options" or "Default name options"...
773 if ((strcasecmp(line
, "Dest") && strcasecmp(line
, "Default")) || !value
)
777 * Separate destination name from options...
780 for (optptr
= value
; *optptr
&& !isspace(*optptr
& 255); optptr
++);
782 while (*optptr
== ' ')
786 * If this is our destination, parse the options and break out of
787 * the loop - we're done!
790 if (!strcasecmp(value
, name
))
792 num_options
= cupsParseOptions(optptr
, num_options
, options
);
804 * Return the number of options for this destination...
807 return (num_options
);
812 * 'print_file()' - Add a file to the current job.
815 static int /* O - 0 on success, -1 on failure */
816 print_file(http_t
*http
, /* I - HTTP connection */
817 int id
, /* I - Job ID */
818 const char *filename
, /* I - File to print */
819 const char *docname
, /* I - document-name */
820 const char *user
, /* I - requesting-user-name */
821 const char *format
, /* I - document-format */
822 int last
) /* I - 1 = last file in job */
824 ipp_t
*request
; /* IPP request */
825 char uri
[HTTP_MAX_URI
]; /* Printer URI */
829 * Setup the Send-Document request...
832 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
834 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", id
);
835 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
837 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
838 "requesting-user-name", NULL
, user
);
841 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
842 "document-name", NULL
, docname
);
845 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
846 "document-format", NULL
, format
);
849 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
855 snprintf(uri
, sizeof(uri
), "/jobs/%d", id
);
857 ippDelete(cupsDoFileRequest(http
, request
, uri
, filename
));
859 if (cupsLastError() > IPP_OK_CONFLICT
)
861 syslog(LOG_ERR
, "Unable to send document - %s", cupsLastErrorString());
871 * 'recv_print_job()' - Receive a print job from the client.
874 static int /* O - Command status */
876 const char *queue
, /* I - Printer name */
877 int num_defaults
, /* I - Number of default options */
878 cups_option_t
*defaults
) /* I - Default options */
880 http_t
*http
; /* HTTP connection */
881 int i
; /* Looping var */
882 int status
; /* Command status */
883 int fd
; /* Temporary file */
884 FILE *fp
; /* File pointer */
885 char filename
[1024]; /* Temporary filename */
886 int bytes
; /* Bytes received */
887 char line
[256], /* Line from file/stdin */
888 command
, /* Command from line */
889 *count
, /* Number of bytes */
890 *name
; /* Name of file */
891 const char *job_sheets
; /* Job sheets */
892 int num_data
; /* Number of data files */
893 char control
[1024], /* Control filename */
894 data
[100][256], /* Data files */
895 temp
[100][1024]; /* Temporary files */
896 char user
[1024], /* User name */
897 title
[1024], /* Job title */
898 docname
[1024], /* Document name */
899 dest
[256]; /* Printer/class queue */
900 int accepting
, /* printer-is-accepting */
901 shared
, /* printer-is-shared */
902 num_options
; /* Number of options */
903 cups_option_t
*options
; /* Options */
905 int docnumber
, /* Current document number */
906 doccount
; /* Count of documents */
910 * Connect to the server...
913 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
916 syslog(LOG_ERR
, "Unable to connect to server: %s", strerror(errno
));
924 * See if the printer is available...
927 num_options
= get_printer(http
, queue
, dest
, sizeof(dest
), &options
,
928 &accepting
, &shared
, NULL
);
930 if (num_options
< 0 || !accepting
|| !shared
)
933 syslog(LOG_INFO
, "Rejecting job because \"%s\" is not %s", dest
,
934 !accepting
? "accepting jobs" : "shared");
936 syslog(LOG_ERR
, "Unable to get printer information for \"%s\"", queue
);
945 putchar(0); /* OK so far... */
948 * Read the request...
957 while (smart_gets(line
, sizeof(line
), stdin
) != NULL
)
959 if (strlen(line
) < 2)
968 for (name
= count
+ 1; *name
&& !isspace(*name
& 255); name
++);
969 while (isspace(*name
& 255))
975 case 0x01 : /* Abort */
979 case 0x02 : /* Receive control file */
980 if (strlen(name
) < 2)
982 syslog(LOG_ERR
, "Bad control file name \"%s\"", name
);
991 * Append to the existing control file - the LPD spec is
992 * not entirely clear, but at least the OS/2 LPD code sends
993 * multiple control files per connection...
996 if ((fd
= open(control
, O_WRONLY
)) < 0)
999 "Unable to append to temporary control file \"%s\" - %s",
1000 control
, strerror(errno
));
1006 lseek(fd
, 0, SEEK_END
);
1010 if ((fd
= cupsTempFd(control
, sizeof(control
))) < 0)
1012 syslog(LOG_ERR
, "Unable to open temporary control file \"%s\" - %s",
1013 control
, strerror(errno
));
1019 strcpy(filename
, control
);
1023 case 0x03 : /* Receive data file */
1024 if (strlen(name
) < 2)
1026 syslog(LOG_ERR
, "Bad data file name \"%s\"", name
);
1032 if (num_data
>= (int)(sizeof(data
) / sizeof(data
[0])))
1035 * Too many data files...
1038 syslog(LOG_ERR
, "Too many data files (%d)", num_data
);
1044 strlcpy(data
[num_data
], name
, sizeof(data
[0]));
1046 if ((fd
= cupsTempFd(temp
[num_data
], sizeof(temp
[0]))) < 0)
1048 syslog(LOG_ERR
, "Unable to open temporary data file \"%s\" - %s",
1049 temp
[num_data
], strerror(errno
));
1055 strcpy(filename
, temp
[num_data
]);
1067 * Copy the data or control file from the client...
1070 for (i
= atoi(count
); i
> 0; i
-= bytes
)
1072 if (i
> sizeof(line
))
1073 bytes
= sizeof(line
);
1077 if ((bytes
= fread(line
, 1, bytes
, stdin
)) > 0)
1078 bytes
= write(fd
, line
, bytes
);
1082 syslog(LOG_ERR
, "Error while reading file - %s",
1090 * Read trailing nul...
1095 if (fread(line
, 1, 1, stdin
) < 1)
1098 syslog(LOG_ERR
, "Error while reading trailing nul - %s",
1104 syslog(LOG_ERR
, "Trailing character after file is not nul (%02X)!",
1110 * Close the file and send an acknowledgement...
1124 * Process the control file and print stuff...
1127 if ((fp
= fopen(control
, "rb")) == NULL
)
1132 * Copy the default options...
1135 for (i
= 0; i
< num_defaults
; i
++)
1136 num_options
= cupsAddOption(defaults
[i
].name
,
1138 num_options
, &options
);
1141 * Grab the job information...
1149 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1152 * Process control lines...
1157 case 'J' : /* Job name */
1158 strlcpy(title
, line
+ 1, sizeof(title
));
1161 case 'N' : /* Document name */
1162 strlcpy(docname
, line
+ 1, sizeof(docname
));
1165 case 'P' : /* User identification */
1166 strlcpy(user
, line
+ 1, sizeof(user
));
1169 case 'L' : /* Print banner page */
1171 * If a banner was requested and it's not overridden by a
1172 * command line option and the destination's default is none
1173 * then add the standard banner...
1176 if (cupsGetOption("job-sheets", num_defaults
, defaults
) == NULL
&&
1177 ((job_sheets
= cupsGetOption("job-sheets", num_options
,
1178 options
)) == NULL
||
1179 !strcmp(job_sheets
, "none,none")))
1181 num_options
= cupsAddOption("job-sheets", "standard",
1182 num_options
, &options
);
1186 case 'c' : /* Plot CIF file */
1187 case 'd' : /* Print DVI file */
1188 case 'f' : /* Print formatted file */
1189 case 'g' : /* Plot file */
1190 case 'l' : /* Print file leaving control characters (raw) */
1191 case 'n' : /* Print ditroff output file */
1192 case 'o' : /* Print PostScript output file */
1193 case 'p' : /* Print file with 'pr' format (prettyprint) */
1194 case 'r' : /* File to print with FORTRAN carriage control */
1195 case 't' : /* Print troff output file */
1196 case 'v' : /* Print raster file */
1199 if (line
[0] == 'l' &&
1200 !cupsGetOption("document-format", num_options
, options
))
1201 num_options
= cupsAddOption("raw", "", num_options
, &options
);
1204 num_options
= cupsAddOption("prettyprint", "", num_options
,
1214 * Check that we have a username...
1219 syslog(LOG_WARNING
, "No username specified by client! "
1220 "Using \"anonymous\"...");
1221 strcpy(user
, "anonymous");
1228 if ((id
= create_job(http
, dest
, title
, docname
, user
, num_options
,
1234 * Then print the job files...
1242 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1245 * Process control lines...
1250 case 'N' : /* Document name */
1251 strlcpy(docname
, line
+ 1, sizeof(docname
));
1254 case 'c' : /* Plot CIF file */
1255 case 'd' : /* Print DVI file */
1256 case 'f' : /* Print formatted file */
1257 case 'g' : /* Plot file */
1258 case 'l' : /* Print file leaving control characters (raw) */
1259 case 'n' : /* Print ditroff output file */
1260 case 'o' : /* Print PostScript output file */
1261 case 'p' : /* Print file with 'pr' format (prettyprint) */
1262 case 'r' : /* File to print with FORTRAN carriage control */
1263 case 't' : /* Print troff output file */
1264 case 'v' : /* Print raster file */
1266 * Figure out which file we are printing...
1269 for (i
= 0; i
< num_data
; i
++)
1270 if (!strcmp(data
[i
], line
+ 1))
1280 * Send the print file...
1285 if (print_file(http
, id
, temp
[i
], docname
, user
,
1286 cupsGetOption("document-format", num_options
,
1288 docnumber
== doccount
))
1305 cupsFreeOptions(num_options
, options
);
1310 * Clean up all temporary files and return...
1315 for (i
= 0; i
< num_data
; i
++)
1323 * 'remove_jobs()' - Cancel one or more jobs.
1326 static int /* O - Command status */
1327 remove_jobs(const char *dest
, /* I - Destination */
1328 const char *agent
, /* I - User agent */
1329 const char *list
) /* I - List of jobs or users */
1331 int id
; /* Job ID */
1332 http_t
*http
; /* HTTP server connection */
1333 ipp_t
*request
; /* IPP Request */
1334 char uri
[HTTP_MAX_URI
]; /* Job URI */
1337 (void)dest
; /* Suppress compiler warnings... */
1340 * Try connecting to the local server...
1343 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1344 cupsEncryption())) == NULL
)
1346 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1352 * Loop for each job...
1355 while ((id
= atoi(list
)) > 0)
1358 * Skip job ID in list...
1361 while (isdigit(*list
& 255))
1363 while (isspace(*list
& 255))
1367 * Build an IPP_CANCEL_JOB request, which requires the following
1370 * attributes-charset
1371 * attributes-natural-language
1373 * requesting-user-name
1376 request
= ippNewRequest(IPP_CANCEL_JOB
);
1378 sprintf(uri
, "ipp://localhost/jobs/%d", id
);
1379 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1381 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1382 "requesting-user-name", NULL
, agent
);
1385 * Do the request and get back a response...
1388 ippDelete(cupsDoRequest(http
, request
, "/jobs"));
1390 if (cupsLastError() > IPP_OK_CONFLICT
)
1392 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
1393 cupsLastErrorString());
1398 syslog(LOG_INFO
, "Job ID %d canceled", id
);
1408 * 'send_state()' - Send the queue state.
1411 static int /* O - Command status */
1412 send_state(const char *queue
, /* I - Destination */
1413 const char *list
, /* I - Job or user */
1414 int longstatus
) /* I - List of jobs or users */
1416 int id
; /* Job ID from list */
1417 http_t
*http
; /* HTTP server connection */
1418 ipp_t
*request
, /* IPP Request */
1419 *response
; /* IPP Response */
1420 ipp_attribute_t
*attr
; /* Current attribute */
1421 ipp_pstate_t state
; /* Printer state */
1422 const char *jobdest
, /* Pointer into job-printer-uri */
1423 *jobuser
, /* Pointer to job-originating-user-name */
1424 *jobname
; /* Pointer to job-name */
1425 ipp_jstate_t jobstate
; /* job-state */
1426 int jobid
, /* job-id */
1427 jobsize
, /* job-k-octets */
1428 jobcount
, /* Number of jobs */
1429 jobcopies
, /* Number of copies */
1430 rank
; /* Rank of job */
1431 char rankstr
[255]; /* Rank string */
1432 char namestr
[1024]; /* Job name string */
1433 char uri
[HTTP_MAX_URI
]; /* Printer URI */
1434 char dest
[256]; /* Printer/class queue */
1435 static const char * const ranks
[10] = /* Ranking strings */
1448 static const char * const requested
[] =
1449 { /* Requested attributes */
1454 "job-originating-user-name",
1461 * Try connecting to the local server...
1464 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1465 cupsEncryption())) == NULL
)
1467 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1469 printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno
));
1474 * Get the actual destination name and printer state...
1477 if (get_printer(http
, queue
, dest
, sizeof(dest
), NULL
, NULL
, NULL
, &state
))
1479 syslog(LOG_ERR
, "Unable to get printer %s: %s", queue
,
1480 cupsLastErrorString());
1481 printf("Unable to get printer %s: %s", queue
, cupsLastErrorString());
1486 * Show the queue state...
1491 case IPP_PRINTER_IDLE
:
1492 printf("%s is ready\n", dest
);
1494 case IPP_PRINTER_PROCESSING
:
1495 printf("%s is ready and printing\n", dest
);
1497 case IPP_PRINTER_STOPPED
:
1498 printf("%s is not ready\n", dest
);
1503 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
1504 * the following attributes:
1506 * attributes-charset
1507 * attributes-natural-language
1508 * job-uri or printer-uri
1513 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
1515 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1516 "localhost", 0, "/printers/%s", dest
);
1518 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1522 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1525 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1526 "requesting-user-name", NULL
, list
);
1527 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
1530 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1531 "requested-attributes",
1532 sizeof(requested
) / sizeof(requested
[0]),
1536 * Do the request and get back a response...
1540 response
= cupsDoRequest(http
, request
, "/");
1542 if (cupsLastError() > IPP_OK_CONFLICT
)
1544 printf("get-jobs failed: %s\n", cupsLastErrorString());
1545 ippDelete(response
);
1550 * Loop through the job list and display them...
1553 for (attr
= response
->attrs
, rank
= 1; attr
; attr
= attr
->next
)
1556 * Skip leading attributes until we hit a job...
1559 while (attr
&& (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
))
1566 * Pull the needed attributes from this job...
1571 jobstate
= IPP_JOB_PENDING
;
1572 jobname
= "untitled";
1577 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
1579 if (!strcmp(attr
->name
, "job-id") &&
1580 attr
->value_tag
== IPP_TAG_INTEGER
)
1581 jobid
= attr
->values
[0].integer
;
1583 if (!strcmp(attr
->name
, "job-k-octets") &&
1584 attr
->value_tag
== IPP_TAG_INTEGER
)
1585 jobsize
= attr
->values
[0].integer
;
1587 if (!strcmp(attr
->name
, "job-state") &&
1588 attr
->value_tag
== IPP_TAG_ENUM
)
1589 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
1591 if (!strcmp(attr
->name
, "job-printer-uri") &&
1592 attr
->value_tag
== IPP_TAG_URI
)
1593 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
1596 if (!strcmp(attr
->name
, "job-originating-user-name") &&
1597 attr
->value_tag
== IPP_TAG_NAME
)
1598 jobuser
= attr
->values
[0].string
.text
;
1600 if (!strcmp(attr
->name
, "job-name") &&
1601 attr
->value_tag
== IPP_TAG_NAME
)
1602 jobname
= attr
->values
[0].string
.text
;
1604 if (!strcmp(attr
->name
, "copies") &&
1605 attr
->value_tag
== IPP_TAG_INTEGER
)
1606 jobcopies
= attr
->values
[0].integer
;
1612 * See if we have everything needed...
1615 if (!jobdest
|| !jobid
)
1623 if (!longstatus
&& jobcount
== 0)
1624 puts("Rank Owner Job File(s) Total Size");
1629 * Display the job...
1632 if (jobstate
== IPP_JOB_PROCESSING
)
1633 strcpy(rankstr
, "active");
1636 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
1645 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
1648 strlcpy(namestr
, jobname
, sizeof(namestr
));
1650 printf("%s: %-33.33s [job %d localhost]\n", jobuser
, rankstr
, jobid
);
1651 printf(" %-39.39s %.0f bytes\n", namestr
, 1024.0 * jobsize
);
1654 printf("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n", rankstr
, jobuser
,
1655 jobid
, jobname
, 1024.0 * jobsize
);
1661 ippDelete(response
);
1673 * 'smart_gets()' - Get a line of text, removing the trailing CR and/or LF.
1676 static char * /* O - Line read or NULL */
1677 smart_gets(char *s
, /* I - Pointer to line buffer */
1678 int len
, /* I - Size of line buffer */
1679 FILE *fp
) /* I - File to read from */
1681 char *ptr
, /* Pointer into line */
1682 *end
; /* End of line */
1683 int ch
; /* Character from file */
1687 * Read the line; unlike fgets(), we read the entire line but dump
1688 * characters that go past the end of the buffer. Also, we accept
1689 * CR, LF, or CR LF for the line endings to be "safe", although
1690 * RFC 1179 specifically says "just use LF".
1696 while ((ch
= getc(fp
)) != EOF
)
1700 else if (ch
== '\r')
1703 * See if a LF follows...
1719 if (ch
== EOF
&& ptr
== s
)
1727 * End of "$Id: cups-lpd.c 7899 2008-09-03 12:57:17Z mike $".