2 * "$Id: cups-lpd.c 5204 2006-02-28 20:32:43Z 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 */
134 * Don't buffer the output...
137 setbuf(stdout
, NULL
);
140 * Log things using the "cups-lpd" name...
143 openlog("cups-lpd", LOG_PID
, LOG_LPR
);
146 * Get the address of the client...
149 hostlen
= sizeof(hostaddr
);
151 if (getpeername(0, (struct sockaddr
*)&hostaddr
, &hostlen
))
153 syslog(LOG_WARNING
, "Unable to get client address - %s", strerror(errno
));
154 strcpy(hostname
, "unknown");
158 httpAddrLookup(&hostaddr
, hostname
, sizeof(hostname
));
159 httpAddrString(&hostaddr
, hostip
, sizeof(hostip
));
162 if (hostaddr
.addr
.sa_family
== AF_INET6
)
165 #endif /* AF_INET6 */
168 syslog(LOG_INFO
, "Connection from %s (%s %s)", hostname
, hostfamily
,
173 * Scan the command-line for options...
179 num_defaults
= cupsAddOption("job-originating-host-name", hostname
,
180 num_defaults
, &defaults
);
182 for (i
= 1; i
< argc
; i
++)
183 if (argv
[i
][0] == '-')
187 case 'o' : /* Option */
189 num_defaults
= cupsParseOptions(argv
[i
] + 2, num_defaults
,
195 num_defaults
= cupsParseOptions(argv
[i
], num_defaults
,
198 syslog(LOG_WARNING
, "Expected option string after -o option!");
202 syslog(LOG_WARNING
, "Unknown option \"%c\" ignored!", argv
[i
][1]);
207 syslog(LOG_WARNING
, "Unknown command-line option \"%s\" ignored!",
211 * RFC1179 specifies that only 1 daemon command can be received for
215 if (smart_gets(line
, sizeof(line
), stdin
) == NULL
)
218 * Unable to get command from client! Send an error status and return.
221 syslog(LOG_ERR
, "Unable to get command line from client!");
227 * The first byte is the command byte. After that will be the queue name,
228 * resource list, and/or user name.
238 for (list
= dest
+ 1; *list
&& !isspace(*list
& 255); list
++);
240 while (isspace(*list
& 255))
250 default : /* Unknown command */
251 syslog(LOG_ERR
, "Unknown LPD command 0x%02X!", command
);
252 syslog(LOG_ERR
, "Command line = %s", line
+ 1);
258 case 0x01 : /* Print any waiting jobs */
259 syslog(LOG_INFO
, "Print waiting jobs (no-op)");
265 case 0x02 : /* Receive a printer job */
266 syslog(LOG_INFO
, "Receive print job for %s", dest
);
267 /* recv_print_job() sends initial status byte */
269 status
= recv_print_job(dest
, num_defaults
, defaults
);
272 case 0x03 : /* Send queue state (short) */
273 syslog(LOG_INFO
, "Send queue state (short) for %s %s", dest
, list
);
274 /* no status byte for this command */
276 status
= send_state(dest
, list
, 0);
279 case 0x04 : /* Send queue state (long) */
280 syslog(LOG_INFO
, "Send queue state (long) for %s %s", dest
, list
);
281 /* no status byte for this command */
283 status
= send_state(dest
, list
, 1);
286 case 0x05 : /* Remove jobs */
288 * Grab the agent and skip to the list of users and/or jobs.
293 for (; *list
&& !isspace(*list
& 255); list
++);
294 while (isspace(*list
& 255))
297 syslog(LOG_INFO
, "Remove jobs %s on %s by %s", list
, dest
, agent
);
299 status
= remove_jobs(dest
, agent
, list
);
305 syslog(LOG_INFO
, "Closing connection");
313 * 'create_job()' - Create a new print job.
316 static int /* O - Job ID or -1 on error */
317 create_job(http_t
*http
, /* I - HTTP connection */
318 const char *dest
, /* I - Destination name */
319 const char *title
, /* I - job-name */
320 const char *user
, /* I - requesting-user-name */
321 int num_options
, /* I - Number of options for job */
322 cups_option_t
*options
) /* I - Options for job */
324 ipp_t
*request
; /* IPP request */
325 ipp_t
*response
; /* IPP response */
326 ipp_attribute_t
*attr
; /* IPP attribute */
327 char uri
[HTTP_MAX_URI
]; /* Printer URI */
332 * Setup the Create-Job request...
335 request
= ippNewRequest(IPP_CREATE_JOB
);
337 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
338 "localhost", 0, "/printers/%s", dest
);
340 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
343 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
344 "requesting-user-name", NULL
, user
);
347 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
350 cupsEncodeOptions(request
, num_options
, options
);
356 snprintf(uri
, sizeof(uri
), "/printers/%s", dest
);
358 response
= cupsDoRequest(http
, request
, uri
);
360 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
362 syslog(LOG_ERR
, "Unable to create job - %s", cupsLastErrorString());
370 * Get the job-id value from the response and return it...
373 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) == NULL
)
377 syslog(LOG_ERR
, "No job-id attribute found in response from server!");
381 id
= attr
->values
[0].integer
;
383 syslog(LOG_INFO
, "Print file - job ID = %d", id
);
393 * 'get_printer()' - Get the named printer and its options.
396 static int /* O - Number of options or -1 on error */
397 get_printer(http_t
*http
, /* I - HTTP connection */
398 const char *name
, /* I - Printer name from request */
399 char *dest
, /* I - Destination buffer */
400 int destsize
, /* I - Size of destination buffer */
401 cups_option_t
**options
, /* O - Printer options */
402 int *accepting
, /* O - printer-is-accepting-jobs value */
403 int *shared
, /* O - printer-is-shared value */
404 ipp_pstate_t
*state
) /* O - printer-state value */
406 int num_options
; /* Number of options */
407 cups_file_t
*fp
; /* lpoptions file */
408 char line
[1024], /* Line from lpoptions file */
409 *value
, /* Pointer to value on line */
410 *optptr
; /* Pointer to options on line */
411 int linenum
; /* Line number in file */
412 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
413 ipp_t
*request
; /* IPP request */
414 ipp_t
*response
; /* IPP response */
415 ipp_attribute_t
*attr
; /* IPP attribute */
416 char uri
[HTTP_MAX_URI
]; /* Printer URI */
417 static const char * const requested
[] =
418 { /* Requested attributes */
420 "printer-is-accepting-jobs",
428 * Initialize everything...
436 *state
= IPP_PRINTER_STOPPED
;
441 * If the queue name contains a space, lookup the printer-name using
442 * the printer-info value...
445 if (strchr(name
, ' '))
448 * Lookup the printer-info...
451 ipp_attribute_t
*accepting_attr
,/* printer-is-accepting-jobs */
452 *info_attr
, /* printer-info */
453 *name_attr
, /* printer-name */
454 *shared_attr
, /* printer-is-shared */
455 *state_attr
; /* printer-state */
459 * Setup the CUPS-Get-Printers request...
462 request
= ippNewRequest(CUPS_GET_PRINTERS
);
464 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
465 "requested-attributes",
466 (int)(sizeof(requested
) / sizeof(requested
[0])),
473 response
= cupsDoRequest(http
, request
, "/");
475 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
477 syslog(LOG_ERR
, "Unable to get list of printers - %s",
478 cupsLastErrorString());
486 * Scan the response for printers...
490 attr
= response
->attrs
;
495 * Skip to the next printer...
498 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
505 * Get all of the attributes for the current printer...
508 accepting_attr
= NULL
;
514 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
516 if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
517 attr
->value_tag
== IPP_TAG_BOOLEAN
)
518 accepting_attr
= attr
;
519 else if (!strcmp(attr
->name
, "printer-info") &&
520 attr
->value_tag
== IPP_TAG_TEXT
)
522 else if (!strcmp(attr
->name
, "printer-name") &&
523 attr
->value_tag
== IPP_TAG_NAME
)
525 else if (!strcmp(attr
->name
, "printer-is-shared") &&
526 attr
->value_tag
== IPP_TAG_BOOLEAN
)
528 else if (!strcmp(attr
->name
, "printer-state") &&
529 attr
->value_tag
== IPP_TAG_ENUM
)
535 if (info_attr
&& name_attr
&&
536 !strcasecmp(name
, info_attr
->values
[0].string
.text
))
539 * Found a match, use this one!
542 strlcpy(dest
, name_attr
->values
[0].string
.text
, destsize
);
544 if (accepting
&& accepting_attr
)
545 *accepting
= accepting_attr
->values
[0].boolean
;
547 if (shared
&& shared_attr
)
548 *shared
= shared_attr
->values
[0].boolean
;
550 if (state
&& state_attr
)
551 *state
= (ipp_pstate_t
)state_attr
->values
[0].integer
;
561 syslog(LOG_ERR
, "Unable to find \"%s\" in list of printers!", name
);
571 * Otherwise treat it as a queue name optionally with an instance name.
574 strlcpy(dest
, name
, destsize
);
575 if ((value
= strchr(dest
, '/')) != NULL
)
579 * Setup the Get-Printer-Attributes request...
582 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
584 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
585 "localhost", 0, "/printers/%s", dest
);
587 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
590 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
591 "requested-attributes",
592 (int)(sizeof(requested
) / sizeof(requested
[0])),
599 response
= cupsDoRequest(http
, request
, "/");
601 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
603 syslog(LOG_ERR
, "Unable to check printer status - %s",
604 cupsLastErrorString());
612 * Get values from the response...
617 if ((attr
= ippFindAttribute(response
, "printer-is-accepting-jobs",
618 IPP_TAG_BOOLEAN
)) == NULL
)
619 syslog(LOG_ERR
, "No printer-is-accepting-jobs attribute found in "
620 "response from server!");
622 *accepting
= attr
->values
[0].boolean
;
627 if ((attr
= ippFindAttribute(response
, "printer-is-shared",
628 IPP_TAG_BOOLEAN
)) == NULL
)
630 syslog(LOG_ERR
, "No printer-is-shared attribute found in "
631 "response from server!");
635 *shared
= attr
->values
[0].boolean
;
640 if ((attr
= ippFindAttribute(response
, "printer-state",
641 IPP_TAG_INTEGER
)) == NULL
)
642 syslog(LOG_ERR
, "No printer-state attribute found in "
643 "response from server!");
645 *state
= (ipp_pstate_t
)attr
->values
[0].integer
;
652 * Override shared value for LPD using system-specific APIs...
655 #ifdef HAVE_CFPRIV_H /* MacOS X */
656 if (shared
&& *shared
)
658 CFURLRef prefsurl
; /* */
659 CFDataRef xmldata
; /* */
660 CFPropertyListRef plist
; /* */
661 CFStringRef queueid
; /* */
662 CFArrayRef lprqarray
; /* */
663 CFBooleanRef serverflag
; /* */
664 Boolean prefsok
; /* */
665 static const char printerprefsfile
[] =
666 "/Library/Preferences/com.apple.printservice.plist";
667 /* Preferences file */
671 * See if we are running on MacOS X Server...
674 CFDictionaryRef versdict
= _CFCopyServerVersionDictionary();
679 * Yes, use the LPR sharing preference...
686 prefsurl
= CFURLCreateFromFileSystemRepresentation(
688 (const UInt8
*)printerprefsfile
,
689 (CFIndex
)strlen(printerprefsfile
),
693 prefsok
= CFURLCreateDataAndPropertiesFromResource(
694 kCFAllocatorDefault
, prefsurl
, &xmldata
,
698 plist
= CFPropertyListCreateFromXMLData(kCFAllocatorDefault
, xmldata
,
699 kCFPropertyListImmutable
, NULL
);
702 serverflag
= (CFBooleanRef
)CFDictionaryGetValue(
703 (CFDictionaryRef
)plist
, CFSTR("serviceState"));
705 if (serverflag
&& CFBooleanGetValue(serverflag
))
707 lprqarray
= (CFArrayRef
)CFDictionaryGetValue(
708 (CFDictionaryRef
)plist
, CFSTR("lprSharedQueues"));
712 queueid
= CFStringCreateWithCString(CFAllocatorGetDefault(),
714 kCFStringEncodingUTF8
);
718 *shared
= CFArrayContainsValue(
720 CFRangeMake(0, CFArrayGetCount(lprqarray
)),
726 CFRelease(lprqarray
);
731 CFRelease(serverflag
);
741 syslog(LOG_ERR
, "Warning - Print Service sharing disabled for LPD "
742 "on queue: %s", name
);
745 #endif /* HAVE_CFPRIV_H */
748 * Next look for the printer in the lpoptions file...
753 if (options
&& shared
&& accepting
)
755 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
756 cups_serverroot
= CUPS_SERVERROOT
;
758 snprintf(line
, sizeof(line
), "%s/lpoptions", cups_serverroot
);
759 if ((fp
= cupsFileOpen(line
, "r")) != NULL
)
762 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
765 * Make sure we have "Dest name options" or "Default name options"...
768 if ((strcasecmp(line
, "Dest") && strcasecmp(line
, "Default")) || !value
)
772 * Separate destination name from options...
775 for (optptr
= value
; *optptr
&& !isspace(*optptr
& 255); optptr
++);
777 while (*optptr
== ' ')
781 * If this is our destination, parse the options and break out of
782 * the loop - we're done!
785 if (!strcasecmp(value
, name
))
787 num_options
= cupsParseOptions(optptr
, num_options
, options
);
799 * Return the number of options for this destination...
802 return (num_options
);
807 * 'print_file()' - Add a file to the current job.
810 static int /* O - 0 on success, -1 on failure */
811 print_file(http_t
*http
, /* I - HTTP connection */
812 int id
, /* I - Job ID */
813 const char *filename
, /* I - File to print */
814 const char *docname
, /* I - document-name */
815 const char *user
, /* I - requesting-user-name */
816 int last
) /* I - 1 = last file in job */
818 ipp_t
*request
; /* IPP request */
819 char uri
[HTTP_MAX_URI
]; /* Printer URI */
823 * Setup the Send-Document request...
826 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
828 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", id
);
829 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
831 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
832 "requesting-user-name", NULL
, user
);
835 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
836 "document-name", NULL
, docname
);
839 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
845 snprintf(uri
, sizeof(uri
), "/jobs/%d", id
);
847 ippDelete(cupsDoFileRequest(http
, request
, uri
, filename
));
849 if (cupsLastError() > IPP_OK_CONFLICT
)
851 syslog(LOG_ERR
, "Unable to send document - %s", cupsLastErrorString());
861 * 'recv_print_job()' - Receive a print job from the client.
864 static int /* O - Command status */
866 const char *queue
, /* I - Printer name */
867 int num_defaults
, /* I - Number of default options */
868 cups_option_t
*defaults
) /* I - Default options */
870 http_t
*http
; /* HTTP connection */
871 int i
; /* Looping var */
872 int status
; /* Command status */
873 int fd
; /* Temporary file */
874 FILE *fp
; /* File pointer */
875 char filename
[1024]; /* Temporary filename */
876 int bytes
; /* Bytes received */
877 char line
[256], /* Line from file/stdin */
878 command
, /* Command from line */
879 *count
, /* Number of bytes */
880 *name
; /* Name of file */
881 const char *job_sheets
; /* Job sheets */
882 int num_data
; /* Number of data files */
883 char control
[1024], /* Control filename */
884 data
[100][256], /* Data files */
885 temp
[100][1024]; /* Temporary files */
886 char user
[1024], /* User name */
887 title
[1024], /* Job title */
888 docname
[1024], /* Document name */
889 dest
[256]; /* Printer/class queue */
890 int accepting
, /* printer-is-accepting */
891 shared
, /* printer-is-shared */
892 num_options
; /* Number of options */
893 cups_option_t
*options
; /* Options */
895 int docnumber
, /* Current document number */
896 doccount
; /* Count of documents */
900 * Connect to the server...
903 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
906 syslog(LOG_ERR
, "Unable to connect to server: %s", strerror(errno
));
914 * See if the printer is available...
917 num_options
= get_printer(http
, queue
, dest
, sizeof(dest
), &options
,
918 &accepting
, &shared
, NULL
);
920 if (num_options
< 0 || !accepting
|| !shared
)
923 syslog(LOG_INFO
, "Rejecting job because \"%s\" is not %s", dest
,
924 !accepting
? "accepting jobs" : "shared");
926 syslog(LOG_ERR
, "Unable to get printer information for \"%s\"", queue
);
935 putchar(0); /* OK so far... */
938 * Read the request...
947 while (smart_gets(line
, sizeof(line
), stdin
) != NULL
)
949 if (strlen(line
) < 2)
958 for (name
= count
+ 1; *name
&& !isspace(*name
& 255); name
++);
959 while (isspace(*name
& 255))
965 case 0x01 : /* Abort */
969 case 0x02 : /* Receive control file */
970 if (strlen(name
) < 2)
972 syslog(LOG_ERR
, "Bad control file name \"%s\"", name
);
981 * Append to the existing control file - the LPD spec is
982 * not entirely clear, but at least the OS/2 LPD code sends
983 * multiple control files per connection...
986 if ((fd
= open(control
, O_WRONLY
)) < 0)
989 "Unable to append to temporary control file \"%s\" - %s",
990 control
, strerror(errno
));
996 lseek(fd
, 0, SEEK_END
);
1000 if ((fd
= cupsTempFd(control
, sizeof(control
))) < 0)
1002 syslog(LOG_ERR
, "Unable to open temporary control file \"%s\" - %s",
1003 control
, strerror(errno
));
1009 strcpy(filename
, control
);
1013 case 0x03 : /* Receive data file */
1014 if (strlen(name
) < 2)
1016 syslog(LOG_ERR
, "Bad data file name \"%s\"", name
);
1022 if (num_data
>= (int)(sizeof(data
) / sizeof(data
[0])))
1025 * Too many data files...
1028 syslog(LOG_ERR
, "Too many data files (%d)", num_data
);
1034 strlcpy(data
[num_data
], name
, sizeof(data
[0]));
1036 if ((fd
= cupsTempFd(temp
[num_data
], sizeof(temp
[0]))) < 0)
1038 syslog(LOG_ERR
, "Unable to open temporary data file \"%s\" - %s",
1039 temp
[num_data
], strerror(errno
));
1045 strcpy(filename
, temp
[num_data
]);
1057 * Copy the data or control file from the client...
1060 for (i
= atoi(count
); i
> 0; i
-= bytes
)
1062 if (i
> sizeof(line
))
1063 bytes
= sizeof(line
);
1067 if ((bytes
= fread(line
, 1, bytes
, stdin
)) > 0)
1068 bytes
= write(fd
, line
, bytes
);
1072 syslog(LOG_ERR
, "Error while reading file - %s",
1080 * Read trailing nul...
1085 if (fread(line
, 1, 1, stdin
) < 1)
1088 syslog(LOG_ERR
, "Error while reading trailing nul - %s",
1094 syslog(LOG_ERR
, "Trailing character after file is not nul (%02X)!",
1100 * Close the file and send an acknowledgement...
1114 * Process the control file and print stuff...
1117 if ((fp
= fopen(control
, "rb")) == NULL
)
1122 * Copy the default options...
1125 for (i
= 0; i
< num_defaults
; i
++)
1126 num_options
= cupsAddOption(defaults
[i
].name
,
1128 num_options
, &options
);
1131 * Grab the job information...
1138 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1141 * Process control lines...
1146 case 'J' : /* Job name */
1147 strlcpy(title
, line
+ 1, sizeof(title
));
1150 case 'P' : /* User identification */
1151 strlcpy(user
, line
+ 1, sizeof(user
));
1154 case 'L' : /* Print banner page */
1156 * If a banner was requested and it's not overridden by a
1157 * command line option and the destination's default is none
1158 * then add the standard banner...
1161 if (cupsGetOption("job-sheets", num_defaults
, defaults
) == NULL
&&
1162 ((job_sheets
= cupsGetOption("job-sheets", num_options
,
1163 options
)) == NULL
||
1164 !strcmp(job_sheets
, "none,none")))
1166 num_options
= cupsAddOption("job-sheets", "standard",
1167 num_options
, &options
);
1171 case 'c' : /* Plot CIF file */
1172 case 'd' : /* Print DVI file */
1173 case 'f' : /* Print formatted file */
1174 case 'g' : /* Plot file */
1175 case 'l' : /* Print file leaving control characters (raw) */
1176 case 'n' : /* Print ditroff output file */
1177 case 'o' : /* Print PostScript output file */
1178 case 'p' : /* Print file with 'pr' format (prettyprint) */
1179 case 'r' : /* File to print with FORTRAN carriage control */
1180 case 't' : /* Print troff output file */
1181 case 'v' : /* Print raster file */
1184 if (line
[0] == 'l' &&
1185 !cupsGetOption("document-format", num_options
, options
))
1186 num_options
= cupsAddOption("raw", "", num_options
, &options
);
1189 num_options
= cupsAddOption("prettyprint", "", num_options
,
1199 * Check that we have a username...
1204 syslog(LOG_WARNING
, "No username specified by client! "
1205 "Using \"anonymous\"...");
1206 strcpy(user
, "anonymous");
1213 if ((id
= create_job(http
, dest
, title
, user
, num_options
, options
)) < 0)
1218 * Then print the job files...
1226 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1229 * Process control lines...
1234 case 'N' : /* Document name */
1235 strlcpy(docname
, line
+ 1, sizeof(docname
));
1238 case 'c' : /* Plot CIF file */
1239 case 'd' : /* Print DVI file */
1240 case 'f' : /* Print formatted file */
1241 case 'g' : /* Plot file */
1242 case 'l' : /* Print file leaving control characters (raw) */
1243 case 'n' : /* Print ditroff output file */
1244 case 'o' : /* Print PostScript output file */
1245 case 'p' : /* Print file with 'pr' format (prettyprint) */
1246 case 'r' : /* File to print with FORTRAN carriage control */
1247 case 't' : /* Print troff output file */
1248 case 'v' : /* Print raster file */
1250 * Figure out which file we are printing...
1253 for (i
= 0; i
< num_data
; i
++)
1254 if (!strcmp(data
[i
], line
+ 1))
1264 * Send the print file...
1269 if (print_file(http
, id
, temp
[i
], docname
, user
,
1270 docnumber
== doccount
))
1287 cupsFreeOptions(num_options
, options
);
1292 * Clean up all temporary files and return...
1297 for (i
= 0; i
< num_data
; i
++)
1305 * 'remove_jobs()' - Cancel one or more jobs.
1308 static int /* O - Command status */
1309 remove_jobs(const char *dest
, /* I - Destination */
1310 const char *agent
, /* I - User agent */
1311 const char *list
) /* I - List of jobs or users */
1313 int id
; /* Job ID */
1314 http_t
*http
; /* HTTP server connection */
1315 ipp_t
*request
; /* IPP Request */
1316 char uri
[HTTP_MAX_URI
]; /* Job URI */
1319 (void)dest
; /* Suppress compiler warnings... */
1322 * Try connecting to the local server...
1325 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1326 cupsEncryption())) == NULL
)
1328 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1334 * Loop for each job...
1337 while ((id
= atoi(list
)) > 0)
1340 * Skip job ID in list...
1343 while (isdigit(*list
& 255))
1345 while (isspace(*list
& 255))
1349 * Build an IPP_CANCEL_JOB request, which requires the following
1352 * attributes-charset
1353 * attributes-natural-language
1355 * requesting-user-name
1358 request
= ippNewRequest(IPP_CANCEL_JOB
);
1360 sprintf(uri
, "ipp://localhost/jobs/%d", id
);
1361 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1363 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1364 "requesting-user-name", NULL
, agent
);
1367 * Do the request and get back a response...
1370 ippDelete(cupsDoRequest(http
, request
, "/jobs"));
1372 if (cupsLastError() > IPP_OK_CONFLICT
)
1374 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
1375 cupsLastErrorString());
1380 syslog(LOG_INFO
, "Job ID %d cancelled", id
);
1390 * 'send_state()' - Send the queue state.
1393 static int /* O - Command status */
1394 send_state(const char *queue
, /* I - Destination */
1395 const char *list
, /* I - Job or user */
1396 int longstatus
) /* I - List of jobs or users */
1398 int id
; /* Job ID from list */
1399 http_t
*http
; /* HTTP server connection */
1400 ipp_t
*request
, /* IPP Request */
1401 *response
; /* IPP Response */
1402 ipp_attribute_t
*attr
; /* Current attribute */
1403 ipp_pstate_t state
; /* Printer state */
1404 const char *jobdest
, /* Pointer into job-printer-uri */
1405 *jobuser
, /* Pointer to job-originating-user-name */
1406 *jobname
; /* Pointer to job-name */
1407 ipp_jstate_t jobstate
; /* job-state */
1408 int jobid
, /* job-id */
1409 jobsize
, /* job-k-octets */
1410 jobcount
, /* Number of jobs */
1411 jobcopies
, /* Number of copies */
1412 rank
; /* Rank of job */
1413 char rankstr
[255]; /* Rank string */
1414 char namestr
[1024]; /* Job name string */
1415 char uri
[HTTP_MAX_URI
]; /* Printer URI */
1416 char dest
[256]; /* Printer/class queue */
1417 static const char * const ranks
[10] = /* Ranking strings */
1430 static const char * const requested
[] =
1431 { /* Requested attributes */
1436 "job-originating-user-name",
1443 * Try connecting to the local server...
1446 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1447 cupsEncryption())) == NULL
)
1449 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1451 printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno
));
1456 * Get the actual destination name and printer state...
1459 if (get_printer(http
, queue
, dest
, sizeof(dest
), NULL
, NULL
, NULL
, &state
))
1461 syslog(LOG_ERR
, "Unable to get printer %s: %s", queue
,
1462 cupsLastErrorString());
1463 printf("Unable to get printer %s: %s", queue
, cupsLastErrorString());
1468 * Show the queue state...
1473 case IPP_PRINTER_IDLE
:
1474 printf("%s is ready\n", dest
);
1476 case IPP_PRINTER_PROCESSING
:
1477 printf("%s is ready and printing\n", dest
);
1479 case IPP_PRINTER_STOPPED
:
1480 printf("%s is not ready\n", dest
);
1485 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
1486 * the following attributes:
1488 * attributes-charset
1489 * attributes-natural-language
1490 * job-uri or printer-uri
1495 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
1497 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1498 "localhost", 0, "/printers/%s", dest
);
1500 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1504 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1507 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1508 "requesting-user-name", NULL
, list
);
1509 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
1512 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1513 "requested-attributes",
1514 sizeof(requested
) / sizeof(requested
[0]),
1518 * Do the request and get back a response...
1522 response
= cupsDoRequest(http
, request
, "/");
1524 if (cupsLastError() > IPP_OK_CONFLICT
)
1526 printf("get-jobs failed: %s\n", cupsLastErrorString());
1527 ippDelete(response
);
1532 * Loop through the job list and display them...
1535 for (attr
= response
->attrs
, rank
= 1; attr
; attr
= attr
->next
)
1538 * Skip leading attributes until we hit a job...
1541 while (attr
&& (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
))
1548 * Pull the needed attributes from this job...
1553 jobstate
= IPP_JOB_PENDING
;
1554 jobname
= "untitled";
1559 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
1561 if (!strcmp(attr
->name
, "job-id") &&
1562 attr
->value_tag
== IPP_TAG_INTEGER
)
1563 jobid
= attr
->values
[0].integer
;
1565 if (!strcmp(attr
->name
, "job-k-octets") &&
1566 attr
->value_tag
== IPP_TAG_INTEGER
)
1567 jobsize
= attr
->values
[0].integer
;
1569 if (!strcmp(attr
->name
, "job-state") &&
1570 attr
->value_tag
== IPP_TAG_ENUM
)
1571 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
1573 if (!strcmp(attr
->name
, "job-printer-uri") &&
1574 attr
->value_tag
== IPP_TAG_URI
)
1575 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
1578 if (!strcmp(attr
->name
, "job-originating-user-name") &&
1579 attr
->value_tag
== IPP_TAG_NAME
)
1580 jobuser
= attr
->values
[0].string
.text
;
1582 if (!strcmp(attr
->name
, "job-name") &&
1583 attr
->value_tag
== IPP_TAG_NAME
)
1584 jobname
= attr
->values
[0].string
.text
;
1586 if (!strcmp(attr
->name
, "copies") &&
1587 attr
->value_tag
== IPP_TAG_INTEGER
)
1588 jobcopies
= attr
->values
[0].integer
;
1594 * See if we have everything needed...
1597 if (!jobdest
|| !jobid
)
1605 if (!longstatus
&& jobcount
== 0)
1606 puts("Rank Owner Job File(s) Total Size");
1611 * Display the job...
1614 if (jobstate
== IPP_JOB_PROCESSING
)
1615 strcpy(rankstr
, "active");
1618 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
1627 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
1630 strlcpy(namestr
, jobname
, sizeof(namestr
));
1632 printf("%s: %-33.33s [job %d localhost]\n", jobuser
, rankstr
, jobid
);
1633 printf(" %-39.39s %.0f bytes\n", namestr
, 1024.0 * jobsize
);
1636 printf("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n", rankstr
, jobuser
,
1637 jobid
, jobname
, 1024.0 * jobsize
);
1643 ippDelete(response
);
1655 * 'smart_gets()' - Get a line of text, removing the trailing CR and/or LF.
1658 static char * /* O - Line read or NULL */
1659 smart_gets(char *s
, /* I - Pointer to line buffer */
1660 int len
, /* I - Size of line buffer */
1661 FILE *fp
) /* I - File to read from */
1663 char *ptr
, /* Pointer into line */
1664 *end
; /* End of line */
1665 int ch
; /* Character from file */
1669 * Read the line; unlike fgets(), we read the entire line but dump
1670 * characters that go past the end of the buffer. Also, we accept
1671 * CR, LF, or CR LF for the line endings to be "safe", although
1672 * RFC 1179 specifically says "just use LF".
1678 while ((ch
= getc(fp
)) != EOF
)
1682 else if (ch
== '\r')
1685 * See if a LF follows...
1701 if (ch
== EOF
&& ptr
== s
)
1709 * End of "$Id: cups-lpd.c 5204 2006-02-28 20:32:43Z mike $".