2 * "$Id: cups-lpd.c 7221 2008-01-16 22:20:08Z 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 *user
, int num_options
,
85 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 'o' : /* Option */
153 num_defaults
= cupsParseOptions(argv
[i
] + 2, num_defaults
,
159 num_defaults
= cupsParseOptions(argv
[i
], num_defaults
,
162 syslog(LOG_WARNING
, "Expected option string after -o option!");
166 case 'n' : /* Don't do hostname lookups */
171 syslog(LOG_WARNING
, "Unknown option \"%c\" ignored!", argv
[i
][1]);
176 syslog(LOG_WARNING
, "Unknown command-line option \"%s\" ignored!",
180 * Get the address of the client...
183 hostlen
= sizeof(hostaddr
);
185 if (getpeername(0, (struct sockaddr
*)&hostaddr
, &hostlen
))
187 syslog(LOG_WARNING
, "Unable to get client address - %s", strerror(errno
));
188 strcpy(hostname
, "unknown");
192 httpAddrString(&hostaddr
, hostip
, sizeof(hostip
));
195 httpAddrLookup(&hostaddr
, hostname
, sizeof(hostname
));
197 strlcpy(hostname
, hostip
, sizeof(hostname
));
200 if (hostaddr
.addr
.sa_family
== AF_INET6
)
203 #endif /* AF_INET6 */
206 syslog(LOG_INFO
, "Connection from %s (%s %s)", hostname
, hostfamily
,
210 num_defaults
= cupsAddOption("job-originating-host-name", hostname
,
211 num_defaults
, &defaults
);
214 * RFC1179 specifies that only 1 daemon command can be received for
218 if (smart_gets(line
, sizeof(line
), stdin
) == NULL
)
221 * Unable to get command from client! Send an error status and return.
224 syslog(LOG_ERR
, "Unable to get command line from client!");
230 * The first byte is the command byte. After that will be the queue name,
231 * resource list, and/or user name.
241 for (list
= dest
+ 1; *list
&& !isspace(*list
& 255); list
++);
243 while (isspace(*list
& 255))
253 default : /* Unknown command */
254 syslog(LOG_ERR
, "Unknown LPD command 0x%02X!", command
);
255 syslog(LOG_ERR
, "Command line = %s", line
+ 1);
261 case 0x01 : /* Print any waiting jobs */
262 syslog(LOG_INFO
, "Print waiting jobs (no-op)");
268 case 0x02 : /* Receive a printer job */
269 syslog(LOG_INFO
, "Receive print job for %s", dest
);
270 /* recv_print_job() sends initial status byte */
272 status
= recv_print_job(dest
, num_defaults
, defaults
);
275 case 0x03 : /* Send queue state (short) */
276 syslog(LOG_INFO
, "Send queue state (short) for %s %s", dest
, list
);
277 /* no status byte for this command */
279 status
= send_state(dest
, list
, 0);
282 case 0x04 : /* Send queue state (long) */
283 syslog(LOG_INFO
, "Send queue state (long) for %s %s", dest
, list
);
284 /* no status byte for this command */
286 status
= send_state(dest
, list
, 1);
289 case 0x05 : /* Remove jobs */
291 * Grab the agent and skip to the list of users and/or jobs.
296 for (; *list
&& !isspace(*list
& 255); list
++);
297 while (isspace(*list
& 255))
300 syslog(LOG_INFO
, "Remove jobs %s on %s by %s", list
, dest
, agent
);
302 status
= remove_jobs(dest
, agent
, list
);
308 syslog(LOG_INFO
, "Closing connection");
316 * 'create_job()' - Create a new print job.
319 static int /* O - Job ID or -1 on error */
320 create_job(http_t
*http
, /* I - HTTP connection */
321 const char *dest
, /* I - Destination name */
322 const char *title
, /* I - job-name */
323 const char *user
, /* I - requesting-user-name */
324 int num_options
, /* I - Number of options for job */
325 cups_option_t
*options
) /* I - Options for job */
327 ipp_t
*request
; /* IPP request */
328 ipp_t
*response
; /* IPP response */
329 ipp_attribute_t
*attr
; /* IPP attribute */
330 char uri
[HTTP_MAX_URI
]; /* Printer URI */
335 * Setup the Create-Job request...
338 request
= ippNewRequest(IPP_CREATE_JOB
);
340 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
341 "localhost", 0, "/printers/%s", dest
);
343 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
346 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
347 "requesting-user-name", NULL
, user
);
350 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
353 cupsEncodeOptions(request
, num_options
, options
);
359 snprintf(uri
, sizeof(uri
), "/printers/%s", dest
);
361 response
= cupsDoRequest(http
, request
, uri
);
363 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
365 syslog(LOG_ERR
, "Unable to create job - %s", cupsLastErrorString());
373 * Get the job-id value from the response and return it...
376 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) == NULL
)
380 syslog(LOG_ERR
, "No job-id attribute found in response from server!");
384 id
= attr
->values
[0].integer
;
386 syslog(LOG_INFO
, "Print file - job ID = %d", id
);
396 * 'get_printer()' - Get the named printer and its options.
399 static int /* O - Number of options or -1 on error */
400 get_printer(http_t
*http
, /* I - HTTP connection */
401 const char *name
, /* I - Printer name from request */
402 char *dest
, /* I - Destination buffer */
403 int destsize
, /* I - Size of destination buffer */
404 cups_option_t
**options
, /* O - Printer options */
405 int *accepting
, /* O - printer-is-accepting-jobs value */
406 int *shared
, /* O - printer-is-shared value */
407 ipp_pstate_t
*state
) /* O - printer-state value */
409 int num_options
; /* Number of options */
410 cups_file_t
*fp
; /* lpoptions file */
411 char line
[1024], /* Line from lpoptions file */
412 *value
, /* Pointer to value on line */
413 *optptr
; /* Pointer to options on line */
414 int linenum
; /* Line number in file */
415 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
416 ipp_t
*request
; /* IPP request */
417 ipp_t
*response
; /* IPP response */
418 ipp_attribute_t
*attr
; /* IPP attribute */
419 char uri
[HTTP_MAX_URI
]; /* Printer URI */
420 static const char * const requested
[] =
421 { /* Requested attributes */
423 "printer-is-accepting-jobs",
431 * Initialize everything...
439 *state
= IPP_PRINTER_STOPPED
;
444 * See if the name is a queue name optionally with an instance name.
447 strlcpy(dest
, name
, destsize
);
448 if ((value
= strchr(dest
, '/')) != NULL
)
452 * Setup the Get-Printer-Attributes request...
455 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
457 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
458 "localhost", 0, "/printers/%s", dest
);
460 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
463 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
464 "requested-attributes",
465 (int)(sizeof(requested
) / sizeof(requested
[0])),
472 response
= cupsDoRequest(http
, request
, "/");
474 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
477 * If we can't find the printer by name, look up the printer-name
478 * using the printer-info values...
481 ipp_attribute_t
*accepting_attr
,/* printer-is-accepting-jobs */
482 *info_attr
, /* printer-info */
483 *name_attr
, /* printer-name */
484 *shared_attr
, /* printer-is-shared */
485 *state_attr
; /* printer-state */
491 * Setup the CUPS-Get-Printers request...
494 request
= ippNewRequest(CUPS_GET_PRINTERS
);
496 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
497 "requested-attributes",
498 (int)(sizeof(requested
) / sizeof(requested
[0])),
505 response
= cupsDoRequest(http
, request
, "/");
507 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
509 syslog(LOG_ERR
, "Unable to get list of printers - %s",
510 cupsLastErrorString());
518 * Scan the response for printers...
522 attr
= response
->attrs
;
527 * Skip to the next printer...
530 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
537 * Get all of the attributes for the current printer...
540 accepting_attr
= NULL
;
546 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
548 if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
549 attr
->value_tag
== IPP_TAG_BOOLEAN
)
550 accepting_attr
= attr
;
551 else if (!strcmp(attr
->name
, "printer-info") &&
552 attr
->value_tag
== IPP_TAG_TEXT
)
554 else if (!strcmp(attr
->name
, "printer-name") &&
555 attr
->value_tag
== IPP_TAG_NAME
)
557 else if (!strcmp(attr
->name
, "printer-is-shared") &&
558 attr
->value_tag
== IPP_TAG_BOOLEAN
)
560 else if (!strcmp(attr
->name
, "printer-state") &&
561 attr
->value_tag
== IPP_TAG_ENUM
)
567 if (info_attr
&& name_attr
&&
568 !strcasecmp(name
, info_attr
->values
[0].string
.text
))
571 * Found a match, use this one!
574 strlcpy(dest
, name_attr
->values
[0].string
.text
, destsize
);
576 if (accepting
&& accepting_attr
)
577 *accepting
= accepting_attr
->values
[0].boolean
;
579 if (shared
&& shared_attr
)
580 *shared
= shared_attr
->values
[0].boolean
;
582 if (state
&& state_attr
)
583 *state
= (ipp_pstate_t
)state_attr
->values
[0].integer
;
593 syslog(LOG_ERR
, "Unable to find \"%s\" in list of printers!", name
);
603 * Get values from the response...
608 if ((attr
= ippFindAttribute(response
, "printer-is-accepting-jobs",
609 IPP_TAG_BOOLEAN
)) == NULL
)
610 syslog(LOG_ERR
, "No printer-is-accepting-jobs attribute found in "
611 "response from server!");
613 *accepting
= attr
->values
[0].boolean
;
618 if ((attr
= ippFindAttribute(response
, "printer-is-shared",
619 IPP_TAG_BOOLEAN
)) == NULL
)
621 syslog(LOG_ERR
, "No printer-is-shared attribute found in "
622 "response from server!");
626 *shared
= attr
->values
[0].boolean
;
631 if ((attr
= ippFindAttribute(response
, "printer-state",
632 IPP_TAG_ENUM
)) == NULL
)
633 syslog(LOG_ERR
, "No printer-state attribute found in "
634 "response from server!");
636 *state
= (ipp_pstate_t
)attr
->values
[0].integer
;
643 * Override shared value for LPD using system-specific APIs...
646 #ifdef HAVE_CFPRIV_H /* MacOS X */
647 if (shared
&& *shared
)
649 CFURLRef prefsurl
; /* URL for preferences file */
650 CFDataRef xmldata
; /* XML data from preferences file */
651 CFPropertyListRef plist
; /* Property list from XML data */
652 CFStringRef queueid
; /* CFString of destination name */
653 CFArrayRef lprqarray
; /* Array of shared "LPR" printers */
654 CFBooleanRef serverflag
; /* State of the print service */
655 static const char printerprefsfile
[] =
656 "/Library/Preferences/com.apple.printservice.plist";
657 /* Preferences file */
661 * See if we are running on MacOS X Server...
664 CFDictionaryRef versdict
= _CFCopyServerVersionDictionary();
669 * Yes, use the LPR sharing preference...
676 prefsurl
= CFURLCreateFromFileSystemRepresentation(
678 (const UInt8
*)printerprefsfile
,
679 (CFIndex
)strlen(printerprefsfile
),
683 if (CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault
,
684 prefsurl
, &xmldata
, NULL
,
687 plist
= CFPropertyListCreateFromXMLData(kCFAllocatorDefault
, xmldata
,
688 kCFPropertyListImmutable
,
692 serverflag
= (CFBooleanRef
)CFDictionaryGetValue(
693 (CFDictionaryRef
)plist
,
694 CFSTR("serviceState"));
696 if (serverflag
&& CFBooleanGetValue(serverflag
))
698 lprqarray
= (CFArrayRef
)CFDictionaryGetValue(
699 (CFDictionaryRef
)plist
,
700 CFSTR("lprSharedQueues"));
704 queueid
= CFStringCreateWithCString(CFAllocatorGetDefault(),
706 kCFStringEncodingUTF8
);
710 *shared
= CFArrayContainsValue(lprqarray
,
712 CFArrayGetCount(lprqarray
)),
730 syslog(LOG_ERR
, "Warning - Print Service sharing disabled for LPD "
731 "on queue: %s", name
);
734 #endif /* HAVE_CFPRIV_H */
737 * Next look for the printer in the lpoptions file...
742 if (options
&& shared
&& accepting
)
744 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
745 cups_serverroot
= CUPS_SERVERROOT
;
747 snprintf(line
, sizeof(line
), "%s/lpoptions", cups_serverroot
);
748 if ((fp
= cupsFileOpen(line
, "r")) != NULL
)
751 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
754 * Make sure we have "Dest name options" or "Default name options"...
757 if ((strcasecmp(line
, "Dest") && strcasecmp(line
, "Default")) || !value
)
761 * Separate destination name from options...
764 for (optptr
= value
; *optptr
&& !isspace(*optptr
& 255); optptr
++);
766 while (*optptr
== ' ')
770 * If this is our destination, parse the options and break out of
771 * the loop - we're done!
774 if (!strcasecmp(value
, name
))
776 num_options
= cupsParseOptions(optptr
, num_options
, options
);
788 * Return the number of options for this destination...
791 return (num_options
);
796 * 'print_file()' - Add a file to the current job.
799 static int /* O - 0 on success, -1 on failure */
800 print_file(http_t
*http
, /* I - HTTP connection */
801 int id
, /* I - Job ID */
802 const char *filename
, /* I - File to print */
803 const char *docname
, /* I - document-name */
804 const char *user
, /* I - requesting-user-name */
805 const char *format
, /* I - document-format */
806 int last
) /* I - 1 = last file in job */
808 ipp_t
*request
; /* IPP request */
809 char uri
[HTTP_MAX_URI
]; /* Printer URI */
813 * Setup the Send-Document request...
816 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
818 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", id
);
819 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
821 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
822 "requesting-user-name", NULL
, user
);
825 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
826 "document-name", NULL
, docname
);
829 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
830 "document-format", NULL
, format
);
833 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
839 snprintf(uri
, sizeof(uri
), "/jobs/%d", id
);
841 ippDelete(cupsDoFileRequest(http
, request
, uri
, filename
));
843 if (cupsLastError() > IPP_OK_CONFLICT
)
845 syslog(LOG_ERR
, "Unable to send document - %s", cupsLastErrorString());
855 * 'recv_print_job()' - Receive a print job from the client.
858 static int /* O - Command status */
860 const char *queue
, /* I - Printer name */
861 int num_defaults
, /* I - Number of default options */
862 cups_option_t
*defaults
) /* I - Default options */
864 http_t
*http
; /* HTTP connection */
865 int i
; /* Looping var */
866 int status
; /* Command status */
867 int fd
; /* Temporary file */
868 FILE *fp
; /* File pointer */
869 char filename
[1024]; /* Temporary filename */
870 int bytes
; /* Bytes received */
871 char line
[256], /* Line from file/stdin */
872 command
, /* Command from line */
873 *count
, /* Number of bytes */
874 *name
; /* Name of file */
875 const char *job_sheets
; /* Job sheets */
876 int num_data
; /* Number of data files */
877 char control
[1024], /* Control filename */
878 data
[100][256], /* Data files */
879 temp
[100][1024]; /* Temporary files */
880 char user
[1024], /* User name */
881 title
[1024], /* Job title */
882 docname
[1024], /* Document name */
883 dest
[256]; /* Printer/class queue */
884 int accepting
, /* printer-is-accepting */
885 shared
, /* printer-is-shared */
886 num_options
; /* Number of options */
887 cups_option_t
*options
; /* Options */
889 int docnumber
, /* Current document number */
890 doccount
; /* Count of documents */
894 * Connect to the server...
897 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
900 syslog(LOG_ERR
, "Unable to connect to server: %s", strerror(errno
));
908 * See if the printer is available...
911 num_options
= get_printer(http
, queue
, dest
, sizeof(dest
), &options
,
912 &accepting
, &shared
, NULL
);
914 if (num_options
< 0 || !accepting
|| !shared
)
917 syslog(LOG_INFO
, "Rejecting job because \"%s\" is not %s", dest
,
918 !accepting
? "accepting jobs" : "shared");
920 syslog(LOG_ERR
, "Unable to get printer information for \"%s\"", queue
);
929 putchar(0); /* OK so far... */
932 * Read the request...
941 while (smart_gets(line
, sizeof(line
), stdin
) != NULL
)
943 if (strlen(line
) < 2)
952 for (name
= count
+ 1; *name
&& !isspace(*name
& 255); name
++);
953 while (isspace(*name
& 255))
959 case 0x01 : /* Abort */
963 case 0x02 : /* Receive control file */
964 if (strlen(name
) < 2)
966 syslog(LOG_ERR
, "Bad control file name \"%s\"", name
);
975 * Append to the existing control file - the LPD spec is
976 * not entirely clear, but at least the OS/2 LPD code sends
977 * multiple control files per connection...
980 if ((fd
= open(control
, O_WRONLY
)) < 0)
983 "Unable to append to temporary control file \"%s\" - %s",
984 control
, strerror(errno
));
990 lseek(fd
, 0, SEEK_END
);
994 if ((fd
= cupsTempFd(control
, sizeof(control
))) < 0)
996 syslog(LOG_ERR
, "Unable to open temporary control file \"%s\" - %s",
997 control
, strerror(errno
));
1003 strcpy(filename
, control
);
1007 case 0x03 : /* Receive data file */
1008 if (strlen(name
) < 2)
1010 syslog(LOG_ERR
, "Bad data file name \"%s\"", name
);
1016 if (num_data
>= (int)(sizeof(data
) / sizeof(data
[0])))
1019 * Too many data files...
1022 syslog(LOG_ERR
, "Too many data files (%d)", num_data
);
1028 strlcpy(data
[num_data
], name
, sizeof(data
[0]));
1030 if ((fd
= cupsTempFd(temp
[num_data
], sizeof(temp
[0]))) < 0)
1032 syslog(LOG_ERR
, "Unable to open temporary data file \"%s\" - %s",
1033 temp
[num_data
], strerror(errno
));
1039 strcpy(filename
, temp
[num_data
]);
1051 * Copy the data or control file from the client...
1054 for (i
= atoi(count
); i
> 0; i
-= bytes
)
1056 if (i
> sizeof(line
))
1057 bytes
= sizeof(line
);
1061 if ((bytes
= fread(line
, 1, bytes
, stdin
)) > 0)
1062 bytes
= write(fd
, line
, bytes
);
1066 syslog(LOG_ERR
, "Error while reading file - %s",
1074 * Read trailing nul...
1079 if (fread(line
, 1, 1, stdin
) < 1)
1082 syslog(LOG_ERR
, "Error while reading trailing nul - %s",
1088 syslog(LOG_ERR
, "Trailing character after file is not nul (%02X)!",
1094 * Close the file and send an acknowledgement...
1108 * Process the control file and print stuff...
1111 if ((fp
= fopen(control
, "rb")) == NULL
)
1116 * Copy the default options...
1119 for (i
= 0; i
< num_defaults
; i
++)
1120 num_options
= cupsAddOption(defaults
[i
].name
,
1122 num_options
, &options
);
1125 * Grab the job information...
1132 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1135 * Process control lines...
1140 case 'J' : /* Job name */
1141 strlcpy(title
, line
+ 1, sizeof(title
));
1144 case 'P' : /* User identification */
1145 strlcpy(user
, line
+ 1, sizeof(user
));
1148 case 'L' : /* Print banner page */
1150 * If a banner was requested and it's not overridden by a
1151 * command line option and the destination's default is none
1152 * then add the standard banner...
1155 if (cupsGetOption("job-sheets", num_defaults
, defaults
) == NULL
&&
1156 ((job_sheets
= cupsGetOption("job-sheets", num_options
,
1157 options
)) == NULL
||
1158 !strcmp(job_sheets
, "none,none")))
1160 num_options
= cupsAddOption("job-sheets", "standard",
1161 num_options
, &options
);
1165 case 'c' : /* Plot CIF file */
1166 case 'd' : /* Print DVI file */
1167 case 'f' : /* Print formatted file */
1168 case 'g' : /* Plot file */
1169 case 'l' : /* Print file leaving control characters (raw) */
1170 case 'n' : /* Print ditroff output file */
1171 case 'o' : /* Print PostScript output file */
1172 case 'p' : /* Print file with 'pr' format (prettyprint) */
1173 case 'r' : /* File to print with FORTRAN carriage control */
1174 case 't' : /* Print troff output file */
1175 case 'v' : /* Print raster file */
1178 if (line
[0] == 'l' &&
1179 !cupsGetOption("document-format", num_options
, options
))
1180 num_options
= cupsAddOption("raw", "", num_options
, &options
);
1183 num_options
= cupsAddOption("prettyprint", "", num_options
,
1193 * Check that we have a username...
1198 syslog(LOG_WARNING
, "No username specified by client! "
1199 "Using \"anonymous\"...");
1200 strcpy(user
, "anonymous");
1207 if ((id
= create_job(http
, dest
, title
, user
, num_options
, options
)) < 0)
1212 * Then print the job files...
1220 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1223 * Process control lines...
1228 case 'N' : /* Document name */
1229 strlcpy(docname
, line
+ 1, sizeof(docname
));
1232 case 'c' : /* Plot CIF file */
1233 case 'd' : /* Print DVI file */
1234 case 'f' : /* Print formatted file */
1235 case 'g' : /* Plot file */
1236 case 'l' : /* Print file leaving control characters (raw) */
1237 case 'n' : /* Print ditroff output file */
1238 case 'o' : /* Print PostScript output file */
1239 case 'p' : /* Print file with 'pr' format (prettyprint) */
1240 case 'r' : /* File to print with FORTRAN carriage control */
1241 case 't' : /* Print troff output file */
1242 case 'v' : /* Print raster file */
1244 * Figure out which file we are printing...
1247 for (i
= 0; i
< num_data
; i
++)
1248 if (!strcmp(data
[i
], line
+ 1))
1258 * Send the print file...
1263 if (print_file(http
, id
, temp
[i
], docname
, user
,
1264 cupsGetOption("document-format", num_options
,
1266 docnumber
== doccount
))
1283 cupsFreeOptions(num_options
, options
);
1288 * Clean up all temporary files and return...
1293 for (i
= 0; i
< num_data
; i
++)
1301 * 'remove_jobs()' - Cancel one or more jobs.
1304 static int /* O - Command status */
1305 remove_jobs(const char *dest
, /* I - Destination */
1306 const char *agent
, /* I - User agent */
1307 const char *list
) /* I - List of jobs or users */
1309 int id
; /* Job ID */
1310 http_t
*http
; /* HTTP server connection */
1311 ipp_t
*request
; /* IPP Request */
1312 char uri
[HTTP_MAX_URI
]; /* Job URI */
1315 (void)dest
; /* Suppress compiler warnings... */
1318 * Try connecting to the local server...
1321 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1322 cupsEncryption())) == NULL
)
1324 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1330 * Loop for each job...
1333 while ((id
= atoi(list
)) > 0)
1336 * Skip job ID in list...
1339 while (isdigit(*list
& 255))
1341 while (isspace(*list
& 255))
1345 * Build an IPP_CANCEL_JOB request, which requires the following
1348 * attributes-charset
1349 * attributes-natural-language
1351 * requesting-user-name
1354 request
= ippNewRequest(IPP_CANCEL_JOB
);
1356 sprintf(uri
, "ipp://localhost/jobs/%d", id
);
1357 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1359 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1360 "requesting-user-name", NULL
, agent
);
1363 * Do the request and get back a response...
1366 ippDelete(cupsDoRequest(http
, request
, "/jobs"));
1368 if (cupsLastError() > IPP_OK_CONFLICT
)
1370 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
1371 cupsLastErrorString());
1376 syslog(LOG_INFO
, "Job ID %d canceled", id
);
1386 * 'send_state()' - Send the queue state.
1389 static int /* O - Command status */
1390 send_state(const char *queue
, /* I - Destination */
1391 const char *list
, /* I - Job or user */
1392 int longstatus
) /* I - List of jobs or users */
1394 int id
; /* Job ID from list */
1395 http_t
*http
; /* HTTP server connection */
1396 ipp_t
*request
, /* IPP Request */
1397 *response
; /* IPP Response */
1398 ipp_attribute_t
*attr
; /* Current attribute */
1399 ipp_pstate_t state
; /* Printer state */
1400 const char *jobdest
, /* Pointer into job-printer-uri */
1401 *jobuser
, /* Pointer to job-originating-user-name */
1402 *jobname
; /* Pointer to job-name */
1403 ipp_jstate_t jobstate
; /* job-state */
1404 int jobid
, /* job-id */
1405 jobsize
, /* job-k-octets */
1406 jobcount
, /* Number of jobs */
1407 jobcopies
, /* Number of copies */
1408 rank
; /* Rank of job */
1409 char rankstr
[255]; /* Rank string */
1410 char namestr
[1024]; /* Job name string */
1411 char uri
[HTTP_MAX_URI
]; /* Printer URI */
1412 char dest
[256]; /* Printer/class queue */
1413 static const char * const ranks
[10] = /* Ranking strings */
1426 static const char * const requested
[] =
1427 { /* Requested attributes */
1432 "job-originating-user-name",
1439 * Try connecting to the local server...
1442 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1443 cupsEncryption())) == NULL
)
1445 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1447 printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno
));
1452 * Get the actual destination name and printer state...
1455 if (get_printer(http
, queue
, dest
, sizeof(dest
), NULL
, NULL
, NULL
, &state
))
1457 syslog(LOG_ERR
, "Unable to get printer %s: %s", queue
,
1458 cupsLastErrorString());
1459 printf("Unable to get printer %s: %s", queue
, cupsLastErrorString());
1464 * Show the queue state...
1469 case IPP_PRINTER_IDLE
:
1470 printf("%s is ready\n", dest
);
1472 case IPP_PRINTER_PROCESSING
:
1473 printf("%s is ready and printing\n", dest
);
1475 case IPP_PRINTER_STOPPED
:
1476 printf("%s is not ready\n", dest
);
1481 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
1482 * the following attributes:
1484 * attributes-charset
1485 * attributes-natural-language
1486 * job-uri or printer-uri
1491 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
1493 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1494 "localhost", 0, "/printers/%s", dest
);
1496 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1500 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1503 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1504 "requesting-user-name", NULL
, list
);
1505 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
1508 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1509 "requested-attributes",
1510 sizeof(requested
) / sizeof(requested
[0]),
1514 * Do the request and get back a response...
1518 response
= cupsDoRequest(http
, request
, "/");
1520 if (cupsLastError() > IPP_OK_CONFLICT
)
1522 printf("get-jobs failed: %s\n", cupsLastErrorString());
1523 ippDelete(response
);
1528 * Loop through the job list and display them...
1531 for (attr
= response
->attrs
, rank
= 1; attr
; attr
= attr
->next
)
1534 * Skip leading attributes until we hit a job...
1537 while (attr
&& (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
))
1544 * Pull the needed attributes from this job...
1549 jobstate
= IPP_JOB_PENDING
;
1550 jobname
= "untitled";
1555 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
1557 if (!strcmp(attr
->name
, "job-id") &&
1558 attr
->value_tag
== IPP_TAG_INTEGER
)
1559 jobid
= attr
->values
[0].integer
;
1561 if (!strcmp(attr
->name
, "job-k-octets") &&
1562 attr
->value_tag
== IPP_TAG_INTEGER
)
1563 jobsize
= attr
->values
[0].integer
;
1565 if (!strcmp(attr
->name
, "job-state") &&
1566 attr
->value_tag
== IPP_TAG_ENUM
)
1567 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
1569 if (!strcmp(attr
->name
, "job-printer-uri") &&
1570 attr
->value_tag
== IPP_TAG_URI
)
1571 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
1574 if (!strcmp(attr
->name
, "job-originating-user-name") &&
1575 attr
->value_tag
== IPP_TAG_NAME
)
1576 jobuser
= attr
->values
[0].string
.text
;
1578 if (!strcmp(attr
->name
, "job-name") &&
1579 attr
->value_tag
== IPP_TAG_NAME
)
1580 jobname
= attr
->values
[0].string
.text
;
1582 if (!strcmp(attr
->name
, "copies") &&
1583 attr
->value_tag
== IPP_TAG_INTEGER
)
1584 jobcopies
= attr
->values
[0].integer
;
1590 * See if we have everything needed...
1593 if (!jobdest
|| !jobid
)
1601 if (!longstatus
&& jobcount
== 0)
1602 puts("Rank Owner Job File(s) Total Size");
1607 * Display the job...
1610 if (jobstate
== IPP_JOB_PROCESSING
)
1611 strcpy(rankstr
, "active");
1614 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
1623 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
1626 strlcpy(namestr
, jobname
, sizeof(namestr
));
1628 printf("%s: %-33.33s [job %d localhost]\n", jobuser
, rankstr
, jobid
);
1629 printf(" %-39.39s %.0f bytes\n", namestr
, 1024.0 * jobsize
);
1632 printf("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n", rankstr
, jobuser
,
1633 jobid
, jobname
, 1024.0 * jobsize
);
1639 ippDelete(response
);
1651 * 'smart_gets()' - Get a line of text, removing the trailing CR and/or LF.
1654 static char * /* O - Line read or NULL */
1655 smart_gets(char *s
, /* I - Pointer to line buffer */
1656 int len
, /* I - Size of line buffer */
1657 FILE *fp
) /* I - File to read from */
1659 char *ptr
, /* Pointer into line */
1660 *end
; /* End of line */
1661 int ch
; /* Character from file */
1665 * Read the line; unlike fgets(), we read the entire line but dump
1666 * characters that go past the end of the buffer. Also, we accept
1667 * CR, LF, or CR LF for the line endings to be "safe", although
1668 * RFC 1179 specifically says "just use LF".
1674 while ((ch
= getc(fp
)) != EOF
)
1678 else if (ch
== '\r')
1681 * See if a LF follows...
1697 if (ch
== EOF
&& ptr
== s
)
1705 * End of "$Id: cups-lpd.c 7221 2008-01-16 22:20:08Z mike $".