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 */
293 * Grab the agent and skip to the list of users and/or jobs.
298 for (; *list
&& !isspace(*list
& 255); list
++);
299 while (isspace(*list
& 255))
302 syslog(LOG_INFO
, "Remove jobs %s on %s by %s", list
, dest
, agent
);
304 status
= remove_jobs(dest
, agent
, list
);
313 syslog(LOG_INFO
, "Closing connection");
321 * 'create_job()' - Create a new print job.
324 static int /* O - Job ID or -1 on error */
325 create_job(http_t
*http
, /* I - HTTP connection */
326 const char *dest
, /* I - Destination name */
327 const char *title
, /* I - job-name */
328 const char *user
, /* I - requesting-user-name */
329 int num_options
, /* I - Number of options for job */
330 cups_option_t
*options
) /* I - Options for job */
332 ipp_t
*request
; /* IPP request */
333 ipp_t
*response
; /* IPP response */
334 ipp_attribute_t
*attr
; /* IPP attribute */
335 char uri
[HTTP_MAX_URI
]; /* Printer URI */
340 * Setup the Create-Job request...
343 request
= ippNewRequest(IPP_CREATE_JOB
);
345 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
346 "localhost", 0, "/printers/%s", dest
);
348 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
351 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
352 "requesting-user-name", NULL
, user
);
355 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
358 cupsEncodeOptions(request
, num_options
, options
);
364 snprintf(uri
, sizeof(uri
), "/printers/%s", dest
);
366 response
= cupsDoRequest(http
, request
, uri
);
368 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
370 syslog(LOG_ERR
, "Unable to create job - %s", cupsLastErrorString());
378 * Get the job-id value from the response and return it...
381 if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) == NULL
)
385 syslog(LOG_ERR
, "No job-id attribute found in response from server!");
389 id
= attr
->values
[0].integer
;
391 syslog(LOG_INFO
, "Print file - job ID = %d", id
);
401 * 'get_printer()' - Get the named printer and its options.
404 static int /* O - Number of options or -1 on error */
405 get_printer(http_t
*http
, /* I - HTTP connection */
406 const char *name
, /* I - Printer name from request */
407 char *dest
, /* I - Destination buffer */
408 int destsize
, /* I - Size of destination buffer */
409 cups_option_t
**options
, /* O - Printer options */
410 int *accepting
, /* O - printer-is-accepting-jobs value */
411 int *shared
, /* O - printer-is-shared value */
412 ipp_pstate_t
*state
) /* O - printer-state value */
414 int num_options
; /* Number of options */
415 cups_file_t
*fp
; /* lpoptions file */
416 char line
[1024], /* Line from lpoptions file */
417 *value
, /* Pointer to value on line */
418 *optptr
; /* Pointer to options on line */
419 int linenum
; /* Line number in file */
420 const char *cups_serverroot
; /* CUPS_SERVERROOT env var */
421 ipp_t
*request
; /* IPP request */
422 ipp_t
*response
; /* IPP response */
423 ipp_attribute_t
*attr
; /* IPP attribute */
424 char uri
[HTTP_MAX_URI
]; /* Printer URI */
425 static const char * const requested
[] =
426 { /* Requested attributes */
428 "printer-is-accepting-jobs",
436 * Initialize everything...
444 *state
= IPP_PRINTER_STOPPED
;
449 * See if the name is a queue name optionally with an instance name.
452 strlcpy(dest
, name
, destsize
);
453 if ((value
= strchr(dest
, '/')) != NULL
)
457 * Setup the Get-Printer-Attributes request...
460 request
= ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES
);
462 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
463 "localhost", 0, "/printers/%s", dest
);
465 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
468 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
469 "requested-attributes",
470 (int)(sizeof(requested
) / sizeof(requested
[0])),
477 response
= cupsDoRequest(http
, request
, "/");
479 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
482 * If we can't find the printer by name, look up the printer-name
483 * using the printer-info values...
486 ipp_attribute_t
*accepting_attr
,/* printer-is-accepting-jobs */
487 *info_attr
, /* printer-info */
488 *name_attr
, /* printer-name */
489 *shared_attr
, /* printer-is-shared */
490 *state_attr
; /* printer-state */
496 * Setup the CUPS-Get-Printers request...
499 request
= ippNewRequest(CUPS_GET_PRINTERS
);
501 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
502 "requested-attributes",
503 (int)(sizeof(requested
) / sizeof(requested
[0])),
510 response
= cupsDoRequest(http
, request
, "/");
512 if (!response
|| cupsLastError() > IPP_OK_CONFLICT
)
514 syslog(LOG_ERR
, "Unable to get list of printers - %s",
515 cupsLastErrorString());
523 * Scan the response for printers...
527 attr
= response
->attrs
;
532 * Skip to the next printer...
535 while (attr
&& attr
->group_tag
!= IPP_TAG_PRINTER
)
542 * Get all of the attributes for the current printer...
545 accepting_attr
= NULL
;
551 while (attr
&& attr
->group_tag
== IPP_TAG_PRINTER
)
553 if (!strcmp(attr
->name
, "printer-is-accepting-jobs") &&
554 attr
->value_tag
== IPP_TAG_BOOLEAN
)
555 accepting_attr
= attr
;
556 else if (!strcmp(attr
->name
, "printer-info") &&
557 attr
->value_tag
== IPP_TAG_TEXT
)
559 else if (!strcmp(attr
->name
, "printer-name") &&
560 attr
->value_tag
== IPP_TAG_NAME
)
562 else if (!strcmp(attr
->name
, "printer-is-shared") &&
563 attr
->value_tag
== IPP_TAG_BOOLEAN
)
565 else if (!strcmp(attr
->name
, "printer-state") &&
566 attr
->value_tag
== IPP_TAG_ENUM
)
572 if (info_attr
&& name_attr
&&
573 !strcasecmp(name
, info_attr
->values
[0].string
.text
))
576 * Found a match, use this one!
579 strlcpy(dest
, name_attr
->values
[0].string
.text
, destsize
);
581 if (accepting
&& accepting_attr
)
582 *accepting
= accepting_attr
->values
[0].boolean
;
584 if (shared
&& shared_attr
)
585 *shared
= shared_attr
->values
[0].boolean
;
587 if (state
&& state_attr
)
588 *state
= (ipp_pstate_t
)state_attr
->values
[0].integer
;
598 syslog(LOG_ERR
, "Unable to find \"%s\" in list of printers!", name
);
608 * Get values from the response...
613 if ((attr
= ippFindAttribute(response
, "printer-is-accepting-jobs",
614 IPP_TAG_BOOLEAN
)) == NULL
)
615 syslog(LOG_ERR
, "No printer-is-accepting-jobs attribute found in "
616 "response from server!");
618 *accepting
= attr
->values
[0].boolean
;
623 if ((attr
= ippFindAttribute(response
, "printer-is-shared",
624 IPP_TAG_BOOLEAN
)) == NULL
)
626 syslog(LOG_ERR
, "No printer-is-shared attribute found in "
627 "response from server!");
631 *shared
= attr
->values
[0].boolean
;
636 if ((attr
= ippFindAttribute(response
, "printer-state",
637 IPP_TAG_ENUM
)) == NULL
)
638 syslog(LOG_ERR
, "No printer-state attribute found in "
639 "response from server!");
641 *state
= (ipp_pstate_t
)attr
->values
[0].integer
;
648 * Override shared value for LPD using system-specific APIs...
651 #ifdef HAVE_CFPRIV_H /* MacOS X */
652 if (shared
&& *shared
)
654 CFURLRef prefsurl
; /* URL for preferences file */
655 CFDataRef xmldata
; /* XML data from preferences file */
656 CFPropertyListRef plist
; /* Property list from XML data */
657 CFStringRef queueid
; /* CFString of destination name */
658 CFArrayRef lprqarray
; /* Array of shared "LPR" printers */
659 CFBooleanRef serverflag
; /* State of the print service */
660 static const char printerprefsfile
[] =
661 "/Library/Preferences/com.apple.printservice.plist";
662 /* Preferences file */
666 * See if we are running on MacOS X Server...
669 CFDictionaryRef versdict
= _CFCopyServerVersionDictionary();
674 * Yes, use the LPR sharing preference...
681 prefsurl
= CFURLCreateFromFileSystemRepresentation(
683 (const UInt8
*)printerprefsfile
,
684 (CFIndex
)strlen(printerprefsfile
),
688 if (CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault
,
689 prefsurl
, &xmldata
, NULL
,
692 plist
= CFPropertyListCreateFromXMLData(kCFAllocatorDefault
, xmldata
,
693 kCFPropertyListImmutable
,
697 serverflag
= (CFBooleanRef
)CFDictionaryGetValue(
698 (CFDictionaryRef
)plist
,
699 CFSTR("serviceState"));
701 if (serverflag
&& CFBooleanGetValue(serverflag
))
703 lprqarray
= (CFArrayRef
)CFDictionaryGetValue(
704 (CFDictionaryRef
)plist
,
705 CFSTR("lprSharedQueues"));
709 queueid
= CFStringCreateWithCString(CFAllocatorGetDefault(),
711 kCFStringEncodingUTF8
);
715 *shared
= CFArrayContainsValue(lprqarray
,
717 CFArrayGetCount(lprqarray
)),
735 syslog(LOG_ERR
, "Warning - Print Service sharing disabled for LPD "
736 "on queue: %s", name
);
739 #endif /* HAVE_CFPRIV_H */
742 * Next look for the printer in the lpoptions file...
747 if (options
&& shared
&& accepting
)
749 if ((cups_serverroot
= getenv("CUPS_SERVERROOT")) == NULL
)
750 cups_serverroot
= CUPS_SERVERROOT
;
752 snprintf(line
, sizeof(line
), "%s/lpoptions", cups_serverroot
);
753 if ((fp
= cupsFileOpen(line
, "r")) != NULL
)
756 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
759 * Make sure we have "Dest name options" or "Default name options"...
762 if ((strcasecmp(line
, "Dest") && strcasecmp(line
, "Default")) || !value
)
766 * Separate destination name from options...
769 for (optptr
= value
; *optptr
&& !isspace(*optptr
& 255); optptr
++);
771 while (*optptr
== ' ')
775 * If this is our destination, parse the options and break out of
776 * the loop - we're done!
779 if (!strcasecmp(value
, name
))
781 num_options
= cupsParseOptions(optptr
, num_options
, options
);
793 * Return the number of options for this destination...
796 return (num_options
);
801 * 'print_file()' - Add a file to the current job.
804 static int /* O - 0 on success, -1 on failure */
805 print_file(http_t
*http
, /* I - HTTP connection */
806 int id
, /* I - Job ID */
807 const char *filename
, /* I - File to print */
808 const char *docname
, /* I - document-name */
809 const char *user
, /* I - requesting-user-name */
810 const char *format
, /* I - document-format */
811 int last
) /* I - 1 = last file in job */
813 ipp_t
*request
; /* IPP request */
814 char uri
[HTTP_MAX_URI
]; /* Printer URI */
818 * Setup the Send-Document request...
821 request
= ippNewRequest(IPP_SEND_DOCUMENT
);
823 snprintf(uri
, sizeof(uri
), "ipp://localhost/jobs/%d", id
);
824 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
826 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
827 "requesting-user-name", NULL
, user
);
830 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
831 "document-name", NULL
, docname
);
834 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_MIMETYPE
,
835 "document-format", NULL
, format
);
838 ippAddBoolean(request
, IPP_TAG_OPERATION
, "last-document", 1);
844 snprintf(uri
, sizeof(uri
), "/jobs/%d", id
);
846 ippDelete(cupsDoFileRequest(http
, request
, uri
, filename
));
848 if (cupsLastError() > IPP_OK_CONFLICT
)
850 syslog(LOG_ERR
, "Unable to send document - %s", cupsLastErrorString());
860 * 'recv_print_job()' - Receive a print job from the client.
863 static int /* O - Command status */
865 const char *queue
, /* I - Printer name */
866 int num_defaults
, /* I - Number of default options */
867 cups_option_t
*defaults
) /* I - Default options */
869 http_t
*http
; /* HTTP connection */
870 int i
; /* Looping var */
871 int status
; /* Command status */
872 int fd
; /* Temporary file */
873 FILE *fp
; /* File pointer */
874 char filename
[1024]; /* Temporary filename */
875 int bytes
; /* Bytes received */
876 char line
[256], /* Line from file/stdin */
877 command
, /* Command from line */
878 *count
, /* Number of bytes */
879 *name
; /* Name of file */
880 const char *job_sheets
; /* Job sheets */
881 int num_data
; /* Number of data files */
882 char control
[1024], /* Control filename */
883 data
[100][256], /* Data files */
884 temp
[100][1024]; /* Temporary files */
885 char user
[1024], /* User name */
886 title
[1024], /* Job title */
887 docname
[1024], /* Document name */
888 dest
[256]; /* Printer/class queue */
889 int accepting
, /* printer-is-accepting */
890 shared
, /* printer-is-shared */
891 num_options
; /* Number of options */
892 cups_option_t
*options
; /* Options */
894 int docnumber
, /* Current document number */
895 doccount
; /* Count of documents */
899 * Connect to the server...
902 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
905 syslog(LOG_ERR
, "Unable to connect to server: %s", strerror(errno
));
913 * See if the printer is available...
916 num_options
= get_printer(http
, queue
, dest
, sizeof(dest
), &options
,
917 &accepting
, &shared
, NULL
);
919 if (num_options
< 0 || !accepting
|| !shared
)
922 syslog(LOG_INFO
, "Rejecting job because \"%s\" is not %s", dest
,
923 !accepting
? "accepting jobs" : "shared");
925 syslog(LOG_ERR
, "Unable to get printer information for \"%s\"", queue
);
934 putchar(0); /* OK so far... */
937 * Read the request...
946 while (smart_gets(line
, sizeof(line
), stdin
) != NULL
)
948 if (strlen(line
) < 2)
957 for (name
= count
+ 1; *name
&& !isspace(*name
& 255); name
++);
958 while (isspace(*name
& 255))
964 case 0x01 : /* Abort */
968 case 0x02 : /* Receive control file */
969 if (strlen(name
) < 2)
971 syslog(LOG_ERR
, "Bad control file name \"%s\"", name
);
980 * Append to the existing control file - the LPD spec is
981 * not entirely clear, but at least the OS/2 LPD code sends
982 * multiple control files per connection...
985 if ((fd
= open(control
, O_WRONLY
)) < 0)
988 "Unable to append to temporary control file \"%s\" - %s",
989 control
, strerror(errno
));
995 lseek(fd
, 0, SEEK_END
);
999 if ((fd
= cupsTempFd(control
, sizeof(control
))) < 0)
1001 syslog(LOG_ERR
, "Unable to open temporary control file \"%s\" - %s",
1002 control
, strerror(errno
));
1008 strcpy(filename
, control
);
1012 case 0x03 : /* Receive data file */
1013 if (strlen(name
) < 2)
1015 syslog(LOG_ERR
, "Bad data file name \"%s\"", name
);
1021 if (num_data
>= (int)(sizeof(data
) / sizeof(data
[0])))
1024 * Too many data files...
1027 syslog(LOG_ERR
, "Too many data files (%d)", num_data
);
1033 strlcpy(data
[num_data
], name
, sizeof(data
[0]));
1035 if ((fd
= cupsTempFd(temp
[num_data
], sizeof(temp
[0]))) < 0)
1037 syslog(LOG_ERR
, "Unable to open temporary data file \"%s\" - %s",
1038 temp
[num_data
], strerror(errno
));
1044 strcpy(filename
, temp
[num_data
]);
1056 * Copy the data or control file from the client...
1059 for (i
= atoi(count
); i
> 0; i
-= bytes
)
1061 if (i
> sizeof(line
))
1062 bytes
= sizeof(line
);
1066 if ((bytes
= fread(line
, 1, bytes
, stdin
)) > 0)
1067 bytes
= write(fd
, line
, bytes
);
1071 syslog(LOG_ERR
, "Error while reading file - %s",
1079 * Read trailing nul...
1084 if (fread(line
, 1, 1, stdin
) < 1)
1087 syslog(LOG_ERR
, "Error while reading trailing nul - %s",
1093 syslog(LOG_ERR
, "Trailing character after file is not nul (%02X)!",
1099 * Close the file and send an acknowledgement...
1113 * Process the control file and print stuff...
1116 if ((fp
= fopen(control
, "rb")) == NULL
)
1121 * Copy the default options...
1124 for (i
= 0; i
< num_defaults
; i
++)
1125 num_options
= cupsAddOption(defaults
[i
].name
,
1127 num_options
, &options
);
1130 * Grab the job information...
1137 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1140 * Process control lines...
1145 case 'J' : /* Job name */
1146 strlcpy(title
, line
+ 1, sizeof(title
));
1149 case 'P' : /* User identification */
1150 strlcpy(user
, line
+ 1, sizeof(user
));
1153 case 'L' : /* Print banner page */
1155 * If a banner was requested and it's not overridden by a
1156 * command line option and the destination's default is none
1157 * then add the standard banner...
1160 if (cupsGetOption("job-sheets", num_defaults
, defaults
) == NULL
&&
1161 ((job_sheets
= cupsGetOption("job-sheets", num_options
,
1162 options
)) == NULL
||
1163 !strcmp(job_sheets
, "none,none")))
1165 num_options
= cupsAddOption("job-sheets", "standard",
1166 num_options
, &options
);
1170 case 'c' : /* Plot CIF file */
1171 case 'd' : /* Print DVI file */
1172 case 'f' : /* Print formatted file */
1173 case 'g' : /* Plot file */
1174 case 'l' : /* Print file leaving control characters (raw) */
1175 case 'n' : /* Print ditroff output file */
1176 case 'o' : /* Print PostScript output file */
1177 case 'p' : /* Print file with 'pr' format (prettyprint) */
1178 case 'r' : /* File to print with FORTRAN carriage control */
1179 case 't' : /* Print troff output file */
1180 case 'v' : /* Print raster file */
1183 if (line
[0] == 'l' &&
1184 !cupsGetOption("document-format", num_options
, options
))
1185 num_options
= cupsAddOption("raw", "", num_options
, &options
);
1188 num_options
= cupsAddOption("prettyprint", "", num_options
,
1198 * Check that we have a username...
1203 syslog(LOG_WARNING
, "No username specified by client! "
1204 "Using \"anonymous\"...");
1205 strcpy(user
, "anonymous");
1212 if ((id
= create_job(http
, dest
, title
, user
, num_options
, options
)) < 0)
1217 * Then print the job files...
1225 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
1228 * Process control lines...
1233 case 'N' : /* Document name */
1234 strlcpy(docname
, line
+ 1, sizeof(docname
));
1237 case 'c' : /* Plot CIF file */
1238 case 'd' : /* Print DVI file */
1239 case 'f' : /* Print formatted file */
1240 case 'g' : /* Plot file */
1241 case 'l' : /* Print file leaving control characters (raw) */
1242 case 'n' : /* Print ditroff output file */
1243 case 'o' : /* Print PostScript output file */
1244 case 'p' : /* Print file with 'pr' format (prettyprint) */
1245 case 'r' : /* File to print with FORTRAN carriage control */
1246 case 't' : /* Print troff output file */
1247 case 'v' : /* Print raster file */
1249 * Figure out which file we are printing...
1252 for (i
= 0; i
< num_data
; i
++)
1253 if (!strcmp(data
[i
], line
+ 1))
1263 * Send the print file...
1268 if (print_file(http
, id
, temp
[i
], docname
, user
,
1269 cupsGetOption("document-format", num_options
,
1271 docnumber
== doccount
))
1288 cupsFreeOptions(num_options
, options
);
1293 * Clean up all temporary files and return...
1298 for (i
= 0; i
< num_data
; i
++)
1306 * 'remove_jobs()' - Cancel one or more jobs.
1309 static int /* O - Command status */
1310 remove_jobs(const char *dest
, /* I - Destination */
1311 const char *agent
, /* I - User agent */
1312 const char *list
) /* I - List of jobs or users */
1314 int id
; /* Job ID */
1315 http_t
*http
; /* HTTP server connection */
1316 ipp_t
*request
; /* IPP Request */
1317 char uri
[HTTP_MAX_URI
]; /* Job URI */
1320 (void)dest
; /* Suppress compiler warnings... */
1323 * Try connecting to the local server...
1326 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1327 cupsEncryption())) == NULL
)
1329 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1335 * Loop for each job...
1338 while ((id
= atoi(list
)) > 0)
1341 * Skip job ID in list...
1344 while (isdigit(*list
& 255))
1346 while (isspace(*list
& 255))
1350 * Build an IPP_CANCEL_JOB request, which requires the following
1353 * attributes-charset
1354 * attributes-natural-language
1356 * requesting-user-name
1359 request
= ippNewRequest(IPP_CANCEL_JOB
);
1361 sprintf(uri
, "ipp://localhost/jobs/%d", id
);
1362 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1364 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1365 "requesting-user-name", NULL
, agent
);
1368 * Do the request and get back a response...
1371 ippDelete(cupsDoRequest(http
, request
, "/jobs"));
1373 if (cupsLastError() > IPP_OK_CONFLICT
)
1375 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
1376 cupsLastErrorString());
1381 syslog(LOG_INFO
, "Job ID %d canceled", id
);
1391 * 'send_state()' - Send the queue state.
1394 static int /* O - Command status */
1395 send_state(const char *queue
, /* I - Destination */
1396 const char *list
, /* I - Job or user */
1397 int longstatus
) /* I - List of jobs or users */
1399 int id
; /* Job ID from list */
1400 http_t
*http
; /* HTTP server connection */
1401 ipp_t
*request
, /* IPP Request */
1402 *response
; /* IPP Response */
1403 ipp_attribute_t
*attr
; /* Current attribute */
1404 ipp_pstate_t state
; /* Printer state */
1405 const char *jobdest
, /* Pointer into job-printer-uri */
1406 *jobuser
, /* Pointer to job-originating-user-name */
1407 *jobname
; /* Pointer to job-name */
1408 ipp_jstate_t jobstate
; /* job-state */
1409 int jobid
, /* job-id */
1410 jobsize
, /* job-k-octets */
1411 jobcount
, /* Number of jobs */
1412 jobcopies
, /* Number of copies */
1413 rank
; /* Rank of job */
1414 char rankstr
[255]; /* Rank string */
1415 char namestr
[1024]; /* Job name string */
1416 char uri
[HTTP_MAX_URI
]; /* Printer URI */
1417 char dest
[256]; /* Printer/class queue */
1418 static const char * const ranks
[10] = /* Ranking strings */
1431 static const char * const requested
[] =
1432 { /* Requested attributes */
1437 "job-originating-user-name",
1444 * Try connecting to the local server...
1447 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1448 cupsEncryption())) == NULL
)
1450 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1452 printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno
));
1457 * Get the actual destination name and printer state...
1460 if (get_printer(http
, queue
, dest
, sizeof(dest
), NULL
, NULL
, NULL
, &state
))
1462 syslog(LOG_ERR
, "Unable to get printer %s: %s", queue
,
1463 cupsLastErrorString());
1464 printf("Unable to get printer %s: %s", queue
, cupsLastErrorString());
1469 * Show the queue state...
1474 case IPP_PRINTER_IDLE
:
1475 printf("%s is ready\n", dest
);
1477 case IPP_PRINTER_PROCESSING
:
1478 printf("%s is ready and printing\n", dest
);
1480 case IPP_PRINTER_STOPPED
:
1481 printf("%s is not ready\n", dest
);
1486 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
1487 * the following attributes:
1489 * attributes-charset
1490 * attributes-natural-language
1491 * job-uri or printer-uri
1496 request
= ippNewRequest(id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
);
1498 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
1499 "localhost", 0, "/printers/%s", dest
);
1501 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1505 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1508 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1509 "requesting-user-name", NULL
, list
);
1510 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
1513 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1514 "requested-attributes",
1515 sizeof(requested
) / sizeof(requested
[0]),
1519 * Do the request and get back a response...
1523 response
= cupsDoRequest(http
, request
, "/");
1525 if (cupsLastError() > IPP_OK_CONFLICT
)
1527 printf("get-jobs failed: %s\n", cupsLastErrorString());
1528 ippDelete(response
);
1533 * Loop through the job list and display them...
1536 for (attr
= response
->attrs
, rank
= 1; attr
; attr
= attr
->next
)
1539 * Skip leading attributes until we hit a job...
1542 while (attr
&& (attr
->group_tag
!= IPP_TAG_JOB
|| !attr
->name
))
1549 * Pull the needed attributes from this job...
1554 jobstate
= IPP_JOB_PENDING
;
1555 jobname
= "untitled";
1560 while (attr
&& attr
->group_tag
== IPP_TAG_JOB
)
1562 if (!strcmp(attr
->name
, "job-id") &&
1563 attr
->value_tag
== IPP_TAG_INTEGER
)
1564 jobid
= attr
->values
[0].integer
;
1566 if (!strcmp(attr
->name
, "job-k-octets") &&
1567 attr
->value_tag
== IPP_TAG_INTEGER
)
1568 jobsize
= attr
->values
[0].integer
;
1570 if (!strcmp(attr
->name
, "job-state") &&
1571 attr
->value_tag
== IPP_TAG_ENUM
)
1572 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
1574 if (!strcmp(attr
->name
, "job-printer-uri") &&
1575 attr
->value_tag
== IPP_TAG_URI
)
1576 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
1579 if (!strcmp(attr
->name
, "job-originating-user-name") &&
1580 attr
->value_tag
== IPP_TAG_NAME
)
1581 jobuser
= attr
->values
[0].string
.text
;
1583 if (!strcmp(attr
->name
, "job-name") &&
1584 attr
->value_tag
== IPP_TAG_NAME
)
1585 jobname
= attr
->values
[0].string
.text
;
1587 if (!strcmp(attr
->name
, "copies") &&
1588 attr
->value_tag
== IPP_TAG_INTEGER
)
1589 jobcopies
= attr
->values
[0].integer
;
1595 * See if we have everything needed...
1598 if (!jobdest
|| !jobid
)
1606 if (!longstatus
&& jobcount
== 0)
1607 puts("Rank Owner Job File(s) Total Size");
1612 * Display the job...
1615 if (jobstate
== IPP_JOB_PROCESSING
)
1616 strcpy(rankstr
, "active");
1619 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
1628 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
1631 strlcpy(namestr
, jobname
, sizeof(namestr
));
1633 printf("%s: %-33.33s [job %d localhost]\n", jobuser
, rankstr
, jobid
);
1634 printf(" %-39.39s %.0f bytes\n", namestr
, 1024.0 * jobsize
);
1637 printf("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n", rankstr
, jobuser
,
1638 jobid
, jobname
, 1024.0 * jobsize
);
1644 ippDelete(response
);
1656 * 'smart_gets()' - Get a line of text, removing the trailing CR and/or LF.
1659 static char * /* O - Line read or NULL */
1660 smart_gets(char *s
, /* I - Pointer to line buffer */
1661 int len
, /* I - Size of line buffer */
1662 FILE *fp
) /* I - File to read from */
1664 char *ptr
, /* Pointer into line */
1665 *end
; /* End of line */
1666 int ch
; /* Character from file */
1670 * Read the line; unlike fgets(), we read the entire line but dump
1671 * characters that go past the end of the buffer. Also, we accept
1672 * CR, LF, or CR LF for the line endings to be "safe", although
1673 * RFC 1179 specifically says "just use LF".
1679 while ((ch
= getc(fp
)) != EOF
)
1683 else if (ch
== '\r')
1686 * See if a LF follows...
1702 if (ch
== EOF
&& ptr
== s
)
1710 * End of "$Id: cups-lpd.c 7221 2008-01-16 22:20:08Z mike $".