2 * "$Id: cups-lpd.c 5878 2006-08-24 15:55:42Z mike $"
4 * Line Printer Daemon interface for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Process an incoming LPD request...
27 * create_job() - Create a new print job.
28 * get_printer() - Get the named printer and its options.
29 * print_file() - Add a file to the current job.
30 * recv_print_job() - Receive a print job from the client.
31 * remove_jobs() - Cancel one or more jobs.
32 * send_state() - Send the queue state.
33 * smart_gets() - Get a line of text, removing the trailing CR and/or LF.
37 * Include necessary headers...
40 #include <cups/http-private.h>
41 #include <cups/cups.h>
42 #include <cups/string.h>
43 #include <cups/language.h>
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
56 #ifdef HAVE_INTTYPES_H
57 # include <inttypes.h>
58 #endif /* HAVE_INTTYPES_H */
60 #ifdef HAVE_COREFOUNDATION_H
61 # include <CoreFoundation/CoreFoundation.h>
62 #endif /* HAVE_COREFOUNDATION_H */
64 # include <CoreFoundation/CFPriv.h>
65 #endif /* HAVE_CFPRIV_H */
69 * LPD "mini-daemon" for CUPS. This program must be used in conjunction
70 * with inetd or another similar program that monitors ports and starts
71 * daemons for each client connection. A typical configuration is:
73 * printer stream tcp nowait lp /usr/lib/cups/daemon/cups-lpd cups-lpd
75 * This daemon implements most of RFC 1179 (the unofficial LPD specification)
78 * - This daemon does not check to make sure that the source port is
79 * between 721 and 731, since it isn't necessary for proper
80 * functioning and port-based security is no security at all!
82 * - The "Print any waiting jobs" command is a no-op.
84 * The LPD-to-IPP mapping is as defined in RFC 2569. The report formats
85 * currently match the Solaris LPD mini-daemon.
92 static int create_job(http_t
*http
, const char *dest
, const char *title
,
93 const char *user
, int num_options
,
94 cups_option_t
*options
);
95 static int get_printer(http_t
*http
, const char *name
, char *dest
,
96 int destsize
, cups_option_t
**options
,
97 int *accepting
, int *shared
, ipp_pstate_t
*state
);
98 static int print_file(http_t
*http
, int id
, const char *filename
,
99 const char *docname
, const char *user
, int last
);
100 static int recv_print_job(const char *name
, int num_defaults
,
101 cups_option_t
*defaults
);
102 static int remove_jobs(const char *name
, const char *agent
,
104 static int send_state(const char *name
, const char *list
,
106 static char *smart_gets(char *s
, int len
, FILE *fp
);
110 * 'main()' - Process an incoming LPD request...
113 int /* O - Exit status */
114 main(int argc
, /* I - Number of command-line arguments */
115 char *argv
[]) /* I - Command-line arguments */
117 int i
; /* Looping var */
118 int num_defaults
; /* Number of default options */
119 cups_option_t
*defaults
; /* Default options */
120 char line
[256], /* Command string */
121 command
, /* Command code */
122 *dest
, /* Pointer to destination */
123 *list
, /* Pointer to list */
124 *agent
, /* Pointer to user */
125 status
; /* Status for client */
126 socklen_t hostlen
; /* Size of client address */
127 http_addr_t hostaddr
; /* Address of client */
128 char hostname
[256], /* Name of client */
129 hostip
[256], /* IP address */
130 *hostfamily
; /* Address family */
131 int hostlookups
; /* Do hostname lookups? */
135 * Don't buffer the output...
138 setbuf(stdout
, NULL
);
141 * Log things using the "cups-lpd" name...
144 openlog("cups-lpd", LOG_PID
, LOG_LPR
);
147 * Scan the command-line for options...
153 num_defaults
= cupsAddOption("job-originating-host-name", hostname
,
154 num_defaults
, &defaults
);
156 for (i
= 1; i
< argc
; i
++)
157 if (argv
[i
][0] == '-')
161 case 'o' : /* Option */
163 num_defaults
= cupsParseOptions(argv
[i
] + 2, num_defaults
,
169 num_defaults
= cupsParseOptions(argv
[i
], num_defaults
,
172 syslog(LOG_WARNING
, "Expected option string after -o option!");
176 case 'n' : /* Don't do hostname lookups */
181 syslog(LOG_WARNING
, "Unknown option \"%c\" ignored!", argv
[i
][1]);
186 syslog(LOG_WARNING
, "Unknown command-line option \"%s\" ignored!",
190 * Get the address of the client...
193 hostlen
= sizeof(hostaddr
);
195 if (getpeername(0, (struct sockaddr
*)&hostaddr
, &hostlen
))
197 syslog(LOG_WARNING
, "Unable to get client address - %s", strerror(errno
));
198 strcpy(hostname
, "unknown");
202 httpAddrString(&hostaddr
, hostip
, sizeof(hostip
));
205 httpAddrLookup(&hostaddr
, hostname
, sizeof(hostname
));
207 strlcpy(hostname
, hostip
, sizeof(hostname
));
210 if (hostaddr
.addr
.sa_family
== AF_INET6
)
213 #endif /* AF_INET6 */
216 syslog(LOG_INFO
, "Connection from %s (%s %s)", hostname
, hostfamily
,
221 * RFC1179 specifies that only 1 daemon command can be received for
225 if (smart_gets(line
, sizeof(line
), stdin
) == NULL
)
228 * Unable to get command from client! Send an error status and return.
231 syslog(LOG_ERR
, "Unable to get command line from client!");
237 * The first byte is the command byte. After that will be the queue name,
238 * resource list, and/or user name.
248 for (list
= dest
+ 1; *list
&& !isspace(*list
& 255); list
++);
250 while (isspace(*list
& 255))
260 default : /* Unknown command */
261 syslog(LOG_ERR
, "Unknown LPD command 0x%02X!", command
);
262 syslog(LOG_ERR
, "Command line = %s", line
+ 1);
268 case 0x01 : /* Print any waiting jobs */
269 syslog(LOG_INFO
, "Print waiting jobs (no-op)");
275 case 0x02 : /* Receive a printer job */
276 syslog(LOG_INFO
, "Receive print job for %s", dest
);
277 /* recv_print_job() sends initial status byte */
279 status
= recv_print_job(dest
, num_defaults
, defaults
);
282 case 0x03 : /* Send queue state (short) */
283 syslog(LOG_INFO
, "Send queue state (short) for %s %s", dest
, list
);
284 /* no status byte for this command */
286 status
= send_state(dest
, list
, 0);
289 case 0x04 : /* Send queue state (long) */
290 syslog(LOG_INFO
, "Send queue state (long) for %s %s", dest
, list
);
291 /* no status byte for this command */
293 status
= send_state(dest
, list
, 1);
296 case 0x05 : /* Remove jobs */
298 * Grab the agent and skip to the list of users and/or jobs.
303 for (; *list
&& !isspace(*list
& 255); list
++);
304 while (isspace(*list
& 255))
307 syslog(LOG_INFO
, "Remove jobs %s on %s by %s", list
, dest
, agent
);
309 status
= remove_jobs(dest
, agent
, list
);
315 syslog(LOG_INFO
, "Closing connection");
323 * 'create_job()' - Create a new print job.
326 static int /* O - Job ID or -1 on error */
327 create_job(http_t
*http
, /* I - HTTP connection */
328 const char *dest
, /* I - Destination name */
329 const char *title
, /* I - job-name */
330 const char *user
, /* I - requesting-user-name */
331 int num_options
, /* I - Number of options for job */
332 cups_option_t
*options
) /* I - Options for job */
334 ipp_t
*request
; /* IPP request */
335 ipp_t
*response
; /* IPP response */
336 ipp_attribute_t
*attr
; /* IPP attribute */
337 char uri
[HTTP_MAX_URI
]; /* Printer URI */
342 * Setup the Create-Job request...
345 request
= ippNewRequest(IPP_CREATE_JOB
);
347 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
348 "localhost", 0, "/printers/%s", dest
);
350 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
353 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
354 "requesting-user-name", NULL
, user
);
357 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
360 cupsEncodeOptions(request
, num_options
, options
);
366 snprintf(uri
, sizeof(uri
), "/printers/%s", dest
);
368 response
= cupsDoRequest(http
, request
, uri
);
370 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
372 syslog(LOG_ERR
, "Unable to create job - %s", cupsLastErrorString());
380 * Get the job-id value from the response and return it...
383 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) == NULL
)
387 syslog(LOG_ERR
, "No job-id attribute found in response from server!");
391 id
= attr
->values
[0].integer
;
393 syslog(LOG_INFO
, "Print file - job ID = %d", id
);
403 * 'get_printer()' - Get the named printer and its options.
406 static int /* O - Number of options or -1 on error */
407 get_printer(http_t
*http
, /* I - HTTP connection */
408 const char *name
, /* I - Printer name from request */
409 char *dest
, /* I - Destination buffer */
410 int destsize
, /* I - Size of destination buffer */
411 cups_option_t
**options
, /* O - Printer options */
412 int *accepting
, /* O - printer-is-accepting-jobs value */
413 int *shared
, /* O - printer-is-shared value */
414 ipp_pstate_t
*state
) /* O - printer-state value */
416 int num_options
; /* Number of options */
417 cups_file_t
*fp
; /* lpoptions file */
418 char line
[1024], /* Line from lpoptions file */
419 *value
, /* Pointer to value on line */
420 *optptr
; /* Pointer to options on line */
421 int linenum
; /* Line number in file */
422 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
423 ipp_t
*request
; /* IPP request */
424 ipp_t
*response
; /* IPP response */
425 ipp_attribute_t
*attr
; /* IPP attribute */
426 char uri
[HTTP_MAX_URI
]; /* Printer URI */
427 static const char * const requested
[] =
428 { /* Requested attributes */
430 "printer-is-accepting-jobs",
438 * Initialize everything...
446 *state
= IPP_PRINTER_STOPPED
;
451 * If the queue name contains a space, lookup the printer-name using
452 * the printer-info value...
455 if (strchr(name
, ' '))
458 * Lookup the printer-info...
461 ipp_attribute_t
*accepting_attr
,/* printer-is-accepting-jobs */
462 *info_attr
, /* printer-info */
463 *name_attr
, /* printer-name */
464 *shared_attr
, /* printer-is-shared */
465 *state_attr
; /* printer-state */
469 * Setup the CUPS-Get-Printers request...
472 request
= ippNewRequest(CUPS_GET_PRINTERS
);
474 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
475 "requested-attributes",
476 (int)(sizeof(requested
) / sizeof(requested
[0])),
483 response
= cupsDoRequest(http
, request
, "/");
485 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
487 syslog(LOG_ERR
, "Unable to get list of printers - %s",
488 cupsLastErrorString());
496 * Scan the response for printers...
500 attr
= response
->attrs
;
505 * Skip to the next printer...
508 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
515 * Get all of the attributes for the current printer...
518 accepting_attr
= NULL
;
524 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
526 if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
527 attr
->value_tag
== IPP_TAG_BOOLEAN
)
528 accepting_attr
= attr
;
529 else if (!strcmp(attr
->name
, "printer-info") &&
530 attr
->value_tag
== IPP_TAG_TEXT
)
532 else if (!strcmp(attr
->name
, "printer-name") &&
533 attr
->value_tag
== IPP_TAG_NAME
)
535 else if (!strcmp(attr
->name
, "printer-is-shared") &&
536 attr
->value_tag
== IPP_TAG_BOOLEAN
)
538 else if (!strcmp(attr
->name
, "printer-state") &&
539 attr
->value_tag
== IPP_TAG_ENUM
)
545 if (info_attr
&& name_attr
&&
546 !strcasecmp(name
, info_attr
->values
[0].string
.text
))
549 * Found a match, use this one!
552 strlcpy(dest
, name_attr
->values
[0].string
.text
, destsize
);
554 if (accepting
&& accepting_attr
)
555 *accepting
= accepting_attr
->values
[0].boolean
;
557 if (shared
&& shared_attr
)
558 *shared
= shared_attr
->values
[0].boolean
;
560 if (state
&& state_attr
)
561 *state
= (ipp_pstate_t
)state_attr
->values
[0].integer
;
571 syslog(LOG_ERR
, "Unable to find \"%s\" in list of printers!", name
);
581 * Otherwise treat it as a queue name optionally with an instance name.
584 strlcpy(dest
, name
, destsize
);
585 if ((value
= strchr(dest
, '/')) != NULL
)
589 * Setup the Get-Printer-Attributes request...
592 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
594 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
595 "localhost", 0, "/printers/%s", dest
);
597 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
600 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
601 "requested-attributes",
602 (int)(sizeof(requested
) / sizeof(requested
[0])),
609 response
= cupsDoRequest(http
, request
, "/");
611 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
613 syslog(LOG_ERR
, "Unable to check printer status - %s",
614 cupsLastErrorString());
622 * Get values from the response...
627 if ((attr
= ippFindAttribute(response
, "printer-is-accepting-jobs",
628 IPP_TAG_BOOLEAN
)) == NULL
)
629 syslog(LOG_ERR
, "No printer-is-accepting-jobs attribute found in "
630 "response from server!");
632 *accepting
= attr
->values
[0].boolean
;
637 if ((attr
= ippFindAttribute(response
, "printer-is-shared",
638 IPP_TAG_BOOLEAN
)) == NULL
)
640 syslog(LOG_ERR
, "No printer-is-shared attribute found in "
641 "response from server!");
645 *shared
= attr
->values
[0].boolean
;
650 if ((attr
= ippFindAttribute(response
, "printer-state",
651 IPP_TAG_ENUM
)) == NULL
)
652 syslog(LOG_ERR
, "No printer-state attribute found in "
653 "response from server!");
655 *state
= (ipp_pstate_t
)attr
->values
[0].integer
;
662 * Override shared value for LPD using system-specific APIs...
665 #ifdef HAVE_CFPRIV_H /* MacOS X */
666 if (shared
&& *shared
)
668 CFURLRef prefsurl
; /* */
669 CFDataRef xmldata
; /* */
670 CFPropertyListRef plist
; /* */
671 CFStringRef queueid
; /* */
672 CFArrayRef lprqarray
; /* */
673 CFBooleanRef serverflag
; /* */
674 Boolean prefsok
; /* */
675 static const char printerprefsfile
[] =
676 "/Library/Preferences/com.apple.printservice.plist";
677 /* Preferences file */
681 * See if we are running on MacOS X Server...
684 CFDictionaryRef versdict
= _CFCopyServerVersionDictionary();
689 * Yes, use the LPR sharing preference...
696 prefsurl
= CFURLCreateFromFileSystemRepresentation(
698 (const UInt8
*)printerprefsfile
,
699 (CFIndex
)strlen(printerprefsfile
),
703 prefsok
= CFURLCreateDataAndPropertiesFromResource(
704 kCFAllocatorDefault
, prefsurl
, &xmldata
,
708 plist
= CFPropertyListCreateFromXMLData(kCFAllocatorDefault
, xmldata
,
709 kCFPropertyListImmutable
, NULL
);
712 serverflag
= (CFBooleanRef
)CFDictionaryGetValue(
713 (CFDictionaryRef
)plist
, CFSTR("serviceState"));
715 if (serverflag
&& CFBooleanGetValue(serverflag
))
717 lprqarray
= (CFArrayRef
)CFDictionaryGetValue(
718 (CFDictionaryRef
)plist
, CFSTR("lprSharedQueues"));
722 queueid
= CFStringCreateWithCString(CFAllocatorGetDefault(),
724 kCFStringEncodingUTF8
);
728 *shared
= CFArrayContainsValue(
730 CFRangeMake(0, CFArrayGetCount(lprqarray
)),
736 CFRelease(lprqarray
);
741 CFRelease(serverflag
);
751 syslog(LOG_ERR
, "Warning - Print Service sharing disabled for LPD "
752 "on queue: %s", name
);
755 #endif /* HAVE_CFPRIV_H */
758 * Next look for the printer in the lpoptions file...
763 if (options
&& shared
&& accepting
)
765 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
766 cups_serverroot
= CUPS_SERVERROOT
;
768 snprintf(line
, sizeof(line
), "%s/lpoptions", cups_serverroot
);
769 if ((fp
= cupsFileOpen(line
, "r")) != NULL
)
772 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
775 * Make sure we have "Dest name options" or "Default name options"...
778 if ((strcasecmp(line
, "Dest") && strcasecmp(line
, "Default")) || !value
)
782 * Separate destination name from options...
785 for (optptr
= value
; *optptr
&& !isspace(*optptr
& 255); optptr
++);
787 while (*optptr
== ' ')
791 * If this is our destination, parse the options and break out of
792 * the loop - we're done!
795 if (!strcasecmp(value
, name
))
797 num_options
= cupsParseOptions(optptr
, num_options
, options
);
809 * Return the number of options for this destination...
812 return (num_options
);
817 * 'print_file()' - Add a file to the current job.
820 static int /* O - 0 on success, -1 on failure */
821 print_file(http_t
*http
, /* I - HTTP connection */
822 int id
, /* I - Job ID */
823 const char *filename
, /* I - File to print */
824 const char *docname
, /* I - document-name */
825 const char *user
, /* I - requesting-user-name */
826 int last
) /* I - 1 = last file in job */
828 ipp_t
*request
; /* IPP request */
829 char uri
[HTTP_MAX_URI
]; /* Printer URI */
833 * Setup the Send-Document request...
836 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
838 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", id
);
839 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
841 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
842 "requesting-user-name", NULL
, user
);
845 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
846 "document-name", NULL
, docname
);
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...
1148 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1151 * Process control lines...
1156 case 'J' : /* Job name */
1157 strlcpy(title
, line
+ 1, sizeof(title
));
1160 case 'P' : /* User identification */
1161 strlcpy(user
, line
+ 1, sizeof(user
));
1164 case 'L' : /* Print banner page */
1166 * If a banner was requested and it's not overridden by a
1167 * command line option and the destination's default is none
1168 * then add the standard banner...
1171 if (cupsGetOption("job-sheets", num_defaults
, defaults
) == NULL
&&
1172 ((job_sheets
= cupsGetOption("job-sheets", num_options
,
1173 options
)) == NULL
||
1174 !strcmp(job_sheets
, "none,none")))
1176 num_options
= cupsAddOption("job-sheets", "standard",
1177 num_options
, &options
);
1181 case 'c' : /* Plot CIF file */
1182 case 'd' : /* Print DVI file */
1183 case 'f' : /* Print formatted file */
1184 case 'g' : /* Plot file */
1185 case 'l' : /* Print file leaving control characters (raw) */
1186 case 'n' : /* Print ditroff output file */
1187 case 'o' : /* Print PostScript output file */
1188 case 'p' : /* Print file with 'pr' format (prettyprint) */
1189 case 'r' : /* File to print with FORTRAN carriage control */
1190 case 't' : /* Print troff output file */
1191 case 'v' : /* Print raster file */
1194 if (line
[0] == 'l' &&
1195 !cupsGetOption("document-format", num_options
, options
))
1196 num_options
= cupsAddOption("raw", "", num_options
, &options
);
1199 num_options
= cupsAddOption("prettyprint", "", num_options
,
1209 * Check that we have a username...
1214 syslog(LOG_WARNING
, "No username specified by client! "
1215 "Using \"anonymous\"...");
1216 strcpy(user
, "anonymous");
1223 if ((id
= create_job(http
, dest
, title
, user
, num_options
, options
)) < 0)
1228 * Then print the job files...
1236 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1239 * Process control lines...
1244 case 'N' : /* Document name */
1245 strlcpy(docname
, line
+ 1, sizeof(docname
));
1248 case 'c' : /* Plot CIF file */
1249 case 'd' : /* Print DVI file */
1250 case 'f' : /* Print formatted file */
1251 case 'g' : /* Plot file */
1252 case 'l' : /* Print file leaving control characters (raw) */
1253 case 'n' : /* Print ditroff output file */
1254 case 'o' : /* Print PostScript output file */
1255 case 'p' : /* Print file with 'pr' format (prettyprint) */
1256 case 'r' : /* File to print with FORTRAN carriage control */
1257 case 't' : /* Print troff output file */
1258 case 'v' : /* Print raster file */
1260 * Figure out which file we are printing...
1263 for (i
= 0; i
< num_data
; i
++)
1264 if (!strcmp(data
[i
], line
+ 1))
1274 * Send the print file...
1279 if (print_file(http
, id
, temp
[i
], docname
, user
,
1280 docnumber
== doccount
))
1297 cupsFreeOptions(num_options
, options
);
1302 * Clean up all temporary files and return...
1307 for (i
= 0; i
< num_data
; i
++)
1315 * 'remove_jobs()' - Cancel one or more jobs.
1318 static int /* O - Command status */
1319 remove_jobs(const char *dest
, /* I - Destination */
1320 const char *agent
, /* I - User agent */
1321 const char *list
) /* I - List of jobs or users */
1323 int id
; /* Job ID */
1324 http_t
*http
; /* HTTP server connection */
1325 ipp_t
*request
; /* IPP Request */
1326 char uri
[HTTP_MAX_URI
]; /* Job URI */
1329 (void)dest
; /* Suppress compiler warnings... */
1332 * Try connecting to the local server...
1335 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1336 cupsEncryption())) == NULL
)
1338 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1344 * Loop for each job...
1347 while ((id
= atoi(list
)) > 0)
1350 * Skip job ID in list...
1353 while (isdigit(*list
& 255))
1355 while (isspace(*list
& 255))
1359 * Build an IPP_CANCEL_JOB request, which requires the following
1362 * attributes-charset
1363 * attributes-natural-language
1365 * requesting-user-name
1368 request
= ippNewRequest(IPP_CANCEL_JOB
);
1370 sprintf(uri
, "ipp://localhost/jobs/%d", id
);
1371 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1373 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1374 "requesting-user-name", NULL
, agent
);
1377 * Do the request and get back a response...
1380 ippDelete(cupsDoRequest(http
, request
, "/jobs"));
1382 if (cupsLastError() > IPP_OK_CONFLICT
)
1384 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
1385 cupsLastErrorString());
1390 syslog(LOG_INFO
, "Job ID %d canceled", id
);
1400 * 'send_state()' - Send the queue state.
1403 static int /* O - Command status */
1404 send_state(const char *queue
, /* I - Destination */
1405 const char *list
, /* I - Job or user */
1406 int longstatus
) /* I - List of jobs or users */
1408 int id
; /* Job ID from list */
1409 http_t
*http
; /* HTTP server connection */
1410 ipp_t
*request
, /* IPP Request */
1411 *response
; /* IPP Response */
1412 ipp_attribute_t
*attr
; /* Current attribute */
1413 ipp_pstate_t state
; /* Printer state */
1414 const char *jobdest
, /* Pointer into job-printer-uri */
1415 *jobuser
, /* Pointer to job-originating-user-name */
1416 *jobname
; /* Pointer to job-name */
1417 ipp_jstate_t jobstate
; /* job-state */
1418 int jobid
, /* job-id */
1419 jobsize
, /* job-k-octets */
1420 jobcount
, /* Number of jobs */
1421 jobcopies
, /* Number of copies */
1422 rank
; /* Rank of job */
1423 char rankstr
[255]; /* Rank string */
1424 char namestr
[1024]; /* Job name string */
1425 char uri
[HTTP_MAX_URI
]; /* Printer URI */
1426 char dest
[256]; /* Printer/class queue */
1427 static const char * const ranks
[10] = /* Ranking strings */
1440 static const char * const requested
[] =
1441 { /* Requested attributes */
1446 "job-originating-user-name",
1453 * Try connecting to the local server...
1456 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1457 cupsEncryption())) == NULL
)
1459 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1461 printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno
));
1466 * Get the actual destination name and printer state...
1469 if (get_printer(http
, queue
, dest
, sizeof(dest
), NULL
, NULL
, NULL
, &state
))
1471 syslog(LOG_ERR
, "Unable to get printer %s: %s", queue
,
1472 cupsLastErrorString());
1473 printf("Unable to get printer %s: %s", queue
, cupsLastErrorString());
1478 * Show the queue state...
1483 case IPP_PRINTER_IDLE
:
1484 printf("%s is ready\n", dest
);
1486 case IPP_PRINTER_PROCESSING
:
1487 printf("%s is ready and printing\n", dest
);
1489 case IPP_PRINTER_STOPPED
:
1490 printf("%s is not ready\n", dest
);
1495 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
1496 * the following attributes:
1498 * attributes-charset
1499 * attributes-natural-language
1500 * job-uri or printer-uri
1505 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
1507 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1508 "localhost", 0, "/printers/%s", dest
);
1510 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1514 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1517 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1518 "requesting-user-name", NULL
, list
);
1519 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
1522 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1523 "requested-attributes",
1524 sizeof(requested
) / sizeof(requested
[0]),
1528 * Do the request and get back a response...
1532 response
= cupsDoRequest(http
, request
, "/");
1534 if (cupsLastError() > IPP_OK_CONFLICT
)
1536 printf("get-jobs failed: %s\n", cupsLastErrorString());
1537 ippDelete(response
);
1542 * Loop through the job list and display them...
1545 for (attr
= response
->attrs
, rank
= 1; attr
; attr
= attr
->next
)
1548 * Skip leading attributes until we hit a job...
1551 while (attr
&& (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
))
1558 * Pull the needed attributes from this job...
1563 jobstate
= IPP_JOB_PENDING
;
1564 jobname
= "untitled";
1569 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
1571 if (!strcmp(attr
->name
, "job-id") &&
1572 attr
->value_tag
== IPP_TAG_INTEGER
)
1573 jobid
= attr
->values
[0].integer
;
1575 if (!strcmp(attr
->name
, "job-k-octets") &&
1576 attr
->value_tag
== IPP_TAG_INTEGER
)
1577 jobsize
= attr
->values
[0].integer
;
1579 if (!strcmp(attr
->name
, "job-state") &&
1580 attr
->value_tag
== IPP_TAG_ENUM
)
1581 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
1583 if (!strcmp(attr
->name
, "job-printer-uri") &&
1584 attr
->value_tag
== IPP_TAG_URI
)
1585 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
1588 if (!strcmp(attr
->name
, "job-originating-user-name") &&
1589 attr
->value_tag
== IPP_TAG_NAME
)
1590 jobuser
= attr
->values
[0].string
.text
;
1592 if (!strcmp(attr
->name
, "job-name") &&
1593 attr
->value_tag
== IPP_TAG_NAME
)
1594 jobname
= attr
->values
[0].string
.text
;
1596 if (!strcmp(attr
->name
, "copies") &&
1597 attr
->value_tag
== IPP_TAG_INTEGER
)
1598 jobcopies
= attr
->values
[0].integer
;
1604 * See if we have everything needed...
1607 if (!jobdest
|| !jobid
)
1615 if (!longstatus
&& jobcount
== 0)
1616 puts("Rank Owner Job File(s) Total Size");
1621 * Display the job...
1624 if (jobstate
== IPP_JOB_PROCESSING
)
1625 strcpy(rankstr
, "active");
1628 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
1637 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
1640 strlcpy(namestr
, jobname
, sizeof(namestr
));
1642 printf("%s: %-33.33s [job %d localhost]\n", jobuser
, rankstr
, jobid
);
1643 printf(" %-39.39s %.0f bytes\n", namestr
, 1024.0 * jobsize
);
1646 printf("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n", rankstr
, jobuser
,
1647 jobid
, jobname
, 1024.0 * jobsize
);
1653 ippDelete(response
);
1665 * 'smart_gets()' - Get a line of text, removing the trailing CR and/or LF.
1668 static char * /* O - Line read or NULL */
1669 smart_gets(char *s
, /* I - Pointer to line buffer */
1670 int len
, /* I - Size of line buffer */
1671 FILE *fp
) /* I - File to read from */
1673 char *ptr
, /* Pointer into line */
1674 *end
; /* End of line */
1675 int ch
; /* Character from file */
1679 * Read the line; unlike fgets(), we read the entire line but dump
1680 * characters that go past the end of the buffer. Also, we accept
1681 * CR, LF, or CR LF for the line endings to be "safe", although
1682 * RFC 1179 specifically says "just use LF".
1688 while ((ch
= getc(fp
)) != EOF
)
1692 else if (ch
== '\r')
1695 * See if a LF follows...
1711 if (ch
== EOF
&& ptr
== s
)
1719 * End of "$Id: cups-lpd.c 5878 2006-08-24 15:55:42Z mike $".