2 * "$Id: cups-lpd.c 4807 2005-10-21 19:17:52Z mike $"
4 * Line Printer Daemon interface for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 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 * print_file() - Print a file to a printer or class.
28 * recv_print_job() - Receive a print job from the client.
29 * remove_jobs() - Cancel one or more jobs.
30 * send_state() - Send the queue state.
31 * smart_gets() - Get a line of text, removing the trailing CR
36 * Include necessary headers...
39 #include <cups/cups.h>
40 #include <cups/string.h>
41 #include <cups/language.h>
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
56 * LPD "mini-daemon" for CUPS. This program must be used in conjunction
57 * with inetd or another similar program that monitors ports and starts
58 * daemons for each client connection. A typical configuration is:
60 * printer stream tcp nowait lp /usr/lib/cups/daemon/cups-lpd cups-lpd
62 * This daemon implements most of RFC 1179 (the unofficial LPD specification)
65 * - This daemon does not check to make sure that the source port is
66 * between 721 and 731, since it isn't necessary for proper
67 * functioning and port-based security is no security at all!
69 * - The "Print any waiting jobs" command is a no-op.
71 * The LPD-to-IPP mapping is as defined in RFC 2569. The report formats
72 * currently match the Solaris LPD mini-daemon.
79 int print_file(const char *name
, const char *file
,
80 const char *title
, const char *docname
,
81 const char *user
, int num_options
,
82 cups_option_t
*options
);
83 int recv_print_job(const char *dest
, int num_defaults
, cups_option_t
*defaults
);
84 int remove_jobs(const char *dest
, const char *agent
, const char *list
);
85 int send_state(const char *dest
, const char *list
, int longstatus
);
86 char *smart_gets(char *s
, int len
, FILE *fp
);
90 * 'main()' - Process an incoming LPD request...
93 int /* O - Exit status */
94 main(int argc
, /* I - Number of command-line arguments */
95 char *argv
[]) /* I - Command-line arguments */
97 int i
; /* Looping var */
98 int num_defaults
; /* Number of default options */
99 cups_option_t
*defaults
; /* Default options */
100 char line
[256], /* Command string */
101 command
, /* Command code */
102 *dest
, /* Pointer to destination */
103 *list
, /* Pointer to list */
104 *agent
, /* Pointer to user */
105 status
; /* Status for client */
106 socklen_t hostlen
; /* Size of client address */
107 http_addr_t hostaddr
; /* Address of client */
108 char hostname
[256], /* Name of client */
109 hostip
[256], /* IP address */
110 *hostfamily
; /* Address family */
114 * Don't buffer the output...
117 setbuf(stdout
, NULL
);
120 * Log things using the "cups-lpd" name...
123 openlog("cups-lpd", LOG_PID
, LOG_LPR
);
126 * Get the address of the client...
129 hostlen
= sizeof(hostaddr
);
131 if (getpeername(0, (struct sockaddr
*)&hostaddr
, &hostlen
))
133 syslog(LOG_WARNING
, "Unable to get client address - %s", strerror(errno
));
134 strcpy(hostname
, "unknown");
138 httpAddrLookup(&hostaddr
, hostname
, sizeof(hostname
));
139 httpAddrString(&hostaddr
, hostip
, sizeof(hostip
));
142 if (hostaddr
.addr
.sa_family
== AF_INET6
)
145 #endif /* AF_INET6 */
148 syslog(LOG_INFO
, "Connection from %s (%s %s)", hostname
, hostfamily
, hostip
);
152 * Scan the command-line for options...
158 num_defaults
= cupsAddOption("job-originating-host-name", hostname
,
159 num_defaults
, &defaults
);
161 for (i
= 1; i
< argc
; i
++)
162 if (argv
[i
][0] == '-')
166 case 'o' : /* Option */
168 num_defaults
= cupsParseOptions(argv
[i
] + 2, num_defaults
,
174 num_defaults
= cupsParseOptions(argv
[i
], num_defaults
, &defaults
);
176 syslog(LOG_WARNING
, "Expected option string after -o option!");
180 syslog(LOG_WARNING
, "Unknown option \"%c\" ignored!", argv
[i
][1]);
185 syslog(LOG_WARNING
, "Unknown command-line option \"%s\" ignored!", argv
[i
]);
188 * RFC1179 specifies that only 1 daemon command can be received for
192 if (smart_gets(line
, sizeof(line
), stdin
) == NULL
)
195 * Unable to get command from client! Send an error status and return.
198 syslog(LOG_ERR
, "Unable to get command line from client!");
204 * The first byte is the command byte. After that will be the queue name,
205 * resource list, and/or user name.
211 for (list
= dest
+ 1; *list
&& !isspace(*list
& 255); list
++);
213 while (isspace(*list
& 255))
222 default : /* Unknown command */
223 syslog(LOG_ERR
, "Unknown LPD command 0x%02X!", command
);
224 syslog(LOG_ERR
, "Command line = %s", line
+ 1);
230 case 0x01 : /* Print any waiting jobs */
231 syslog(LOG_INFO
, "Print waiting jobs (no-op)");
237 case 0x02 : /* Receive a printer job */
238 syslog(LOG_INFO
, "Receive print job for %s", dest
);
239 /* recv_print_job() sends initial status byte */
241 status
= recv_print_job(dest
, num_defaults
, defaults
);
244 case 0x03 : /* Send queue state (short) */
245 syslog(LOG_INFO
, "Send queue state (short) for %s %s", dest
, list
);
246 /* no status byte for this command */
248 status
= send_state(dest
, list
, 0);
251 case 0x04 : /* Send queue state (long) */
252 syslog(LOG_INFO
, "Send queue state (long) for %s %s", dest
, list
);
253 /* no status byte for this command */
255 status
= send_state(dest
, list
, 1);
258 case 0x05 : /* Remove jobs */
260 * Grab the agent and skip to the list of users and/or jobs.
265 for (; *list
&& !isspace(*list
& 255); list
++);
266 while (isspace(*list
& 255))
269 syslog(LOG_INFO
, "Remove jobs %s on %s by %s", list
, dest
, agent
);
271 status
= remove_jobs(dest
, agent
, list
);
277 syslog(LOG_INFO
, "Closing connection");
285 * 'check_printer()' - Check that a printer exists and is accepting jobs.
289 check_printer(const char *name
) /* I - Printer or class name */
291 http_t
*http
; /* Connection to server */
292 ipp_t
*request
; /* IPP request */
293 ipp_t
*response
; /* IPP response */
294 ipp_attribute_t
*attr
; /* IPP job-id attribute */
295 char uri
[HTTP_MAX_URI
]; /* Printer URI */
296 cups_lang_t
*language
; /* Language to use */
297 int accepting
; /* printer-is-accepting-jobs value */
301 * Setup a connection and request data...
304 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
305 cupsEncryption())) == NULL
)
307 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
313 * Build a standard CUPS URI for the printer and fill the standard IPP
317 if ((request
= ippNew()) == NULL
)
319 syslog(LOG_ERR
, "Unable to create request: %s", strerror(errno
));
324 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
325 request
->request
.op
.request_id
= 1;
327 httpAssembleURIf(uri
, sizeof(uri
), "ipp", NULL
, "localhost", 0,
328 "/printers/%s", name
);
330 language
= cupsLangDefault();
332 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
333 "attributes-charset", NULL
, cupsLangEncoding(language
));
335 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
336 "attributes-natural-language", NULL
,
337 language
!= NULL
? language
->language
: "C");
339 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
342 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requested-attributes",
343 NULL
, "printer-is-accepting-jobs");
349 response
= cupsDoRequest(http
, request
, "/");
351 if (response
== NULL
)
353 syslog(LOG_ERR
, "Unable to check printer status - %s",
354 ippErrorString(cupsLastError()));
357 else if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
359 syslog(LOG_ERR
, "Unable to check printer status - %s",
360 ippErrorString(response
->request
.status
.status_code
));
363 else if ((attr
= ippFindAttribute(response
, "printer-is-accepting-jobs",
364 IPP_TAG_BOOLEAN
)) == NULL
)
366 syslog(LOG_ERR
, "No printer-is-accepting-jobs attribute found in response from server!");
370 accepting
= attr
->values
[0].boolean
;
372 if (response
!= NULL
)
376 cupsLangFree(language
);
383 * 'print_file()' - Print a file to a printer or class.
387 print_file(const char *name
, /* I - Printer or class name */
388 const char *file
, /* I - File to print */
389 const char *title
, /* I - Title of job */
390 const char *docname
, /* I - Name of job file */
391 const char *user
, /* I - Owner of job */
392 int num_options
, /* I - Number of options */
393 cups_option_t
*options
) /* I - Options */
395 http_t
*http
; /* Connection to server */
396 ipp_t
*request
; /* IPP request */
397 ipp_t
*response
; /* IPP response */
398 ipp_attribute_t
*attr
; /* IPP job-id attribute */
399 char uri
[HTTP_MAX_URI
]; /* Printer URI */
400 cups_lang_t
*language
; /* Language to use */
401 int jobid
; /* New job ID */
405 * Setup a connection and request data...
408 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
409 cupsEncryption())) == NULL
)
411 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
417 * Build a standard CUPS URI for the printer and fill the standard IPP
421 if ((request
= ippNew()) == NULL
)
423 syslog(LOG_ERR
, "Unable to create request: %s", strerror(errno
));
428 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
429 request
->request
.op
.request_id
= 1;
431 httpAssembleURIf(uri
, sizeof(uri
), "ipp", NULL
, "localhost", 0,
432 "/printers/%s", name
);
434 language
= cupsLangDefault();
436 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
437 "attributes-charset", NULL
, cupsLangEncoding(language
));
439 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
440 "attributes-natural-language", NULL
,
441 language
!= NULL
? language
->language
: "C");
443 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
446 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
450 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name", NULL
, title
);
452 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "document-name", NULL
, docname
);
455 * Then add all options on the command-line...
458 cupsEncodeOptions(request
, num_options
, options
);
464 snprintf(uri
, sizeof(uri
), "/printers/%s", name
);
466 response
= cupsDoFileRequest(http
, request
, uri
, file
);
468 if (response
== NULL
)
470 syslog(LOG_ERR
, "Unable to print file - %s",
471 ippErrorString(cupsLastError()));
474 else if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
476 syslog(LOG_ERR
, "Unable to print file - %s",
477 ippErrorString(response
->request
.status
.status_code
));
480 else if ((attr
= ippFindAttribute(response
, "job-id", IPP_TAG_INTEGER
)) == NULL
)
482 syslog(LOG_ERR
, "No job-id attribute found in response from server!");
487 jobid
= attr
->values
[0].integer
;
489 syslog(LOG_INFO
, "Print file - job ID = %d", jobid
);
492 if (response
!= NULL
)
496 cupsLangFree(language
);
503 * 'recv_print_job()' - Receive a print job from the client.
506 int /* O - Command status */
507 recv_print_job(const char *dest
, /* I - Destination */
509 /* I - Number of default options */
510 cups_option_t
*defaults
) /* I - Default options */
512 int i
; /* Looping var */
513 int status
; /* Command status */
514 int fd
; /* Temporary file */
515 FILE *fp
; /* File pointer */
516 char filename
[1024]; /* Temporary filename */
517 int bytes
; /* Bytes received */
518 char line
[256], /* Line from file/stdin */
519 command
, /* Command from line */
520 *count
, /* Number of bytes */
521 *name
; /* Name of file */
522 const char *cupsd_job_sheets
; /* Job sheets */
523 int num_data
; /* Number of data files */
524 char control
[1024], /* Control filename */
525 data
[32][256], /* Data files */
526 temp
[32][1024]; /* Temporary files */
527 char user
[1024], /* User name */
528 title
[1024], /* Job title */
529 docname
[1024], /* Document name */
530 queue
[256], /* Printer/class queue */
531 *instance
; /* Printer/class instance */
532 int num_dests
; /* Number of destinations */
533 cups_dest_t
*dests
, /* Destinations */
534 *destptr
; /* Current destination */
535 int num_options
; /* Number of options */
536 cups_option_t
*options
; /* Options */
537 int banner
; /* Print banner? */
546 strlcpy(queue
, dest
, sizeof(queue
));
548 if ((instance
= strrchr(queue
, '/')) != NULL
)
551 num_dests
= cupsGetDests(&dests
);
552 if ((destptr
= cupsGetDest(queue
, instance
, num_dests
, dests
)) == NULL
)
555 * If the queue name is blank or "lp" then use the default queue.
558 if (!queue
[0] || !strcmp(queue
, "lp"))
559 if ((destptr
= cupsGetDest(NULL
, NULL
, num_dests
, dests
)) != NULL
)
560 strlcpy(queue
, destptr
->name
, sizeof(queue
));
565 syslog(LOG_ERR
, "Unknown destination %s/%s!", queue
, instance
);
567 syslog(LOG_ERR
, "Unknown destination %s!", queue
);
569 cupsFreeDests(num_dests
, dests
);
577 if (!check_printer(queue
))
579 cupsFreeDests(num_dests
, dests
);
588 while (smart_gets(line
, sizeof(line
), stdin
) != NULL
)
590 if (strlen(line
) < 2)
599 for (name
= count
+ 1; *name
&& !isspace(*name
& 255); name
++);
600 while (isspace(*name
& 255))
606 case 0x01 : /* Abort */
610 case 0x02 : /* Receive control file */
611 if (strlen(name
) < 2)
613 syslog(LOG_ERR
, "Bad control file name \"%s\"", name
);
622 * Append to the existing control file - the LPD spec is
623 * not entirely clear, but at least the OS/2 LPD code sends
624 * multiple control files per connection...
627 if ((fd
= open(control
, O_WRONLY
)) < 0)
630 "Unable to append to temporary control file \"%s\" - %s",
631 control
, strerror(errno
));
637 lseek(fd
, 0, SEEK_END
);
641 if ((fd
= cupsTempFd(control
, sizeof(control
))) < 0)
643 syslog(LOG_ERR
, "Unable to open temporary control file \"%s\" - %s",
644 control
, strerror(errno
));
650 strcpy(filename
, control
);
654 case 0x03 : /* Receive data file */
655 if (strlen(name
) < 2)
657 syslog(LOG_ERR
, "Bad data file name \"%s\"", name
);
663 if (num_data
>= (sizeof(data
) / sizeof(data
[0])))
666 * Too many data files...
669 syslog(LOG_ERR
, "Too many data files (%d)", num_data
);
675 strlcpy(data
[num_data
], name
, sizeof(data
[0]));
677 if ((fd
= cupsTempFd(temp
[num_data
], sizeof(temp
[0]))) < 0)
679 syslog(LOG_ERR
, "Unable to open temporary data file \"%s\" - %s",
680 temp
[num_data
], strerror(errno
));
686 strcpy(filename
, temp
[num_data
]);
698 * Copy the data or control file from the client...
701 for (i
= atoi(count
); i
> 0; i
-= bytes
)
703 if (i
> sizeof(line
))
704 bytes
= sizeof(line
);
708 if ((bytes
= fread(line
, 1, bytes
, stdin
)) > 0)
709 bytes
= write(fd
, line
, bytes
);
713 syslog(LOG_ERR
, "Error while reading file - %s",
721 * Read trailing nul...
726 if (fread(line
, 1, 1, stdin
) < 1)
729 syslog(LOG_ERR
, "Error while reading trailing nul - %s",
735 syslog(LOG_ERR
, "Trailing character after file is not nul (%02X)!",
741 * Close the file and send an acknowledgement...
755 * Process the control file and print stuff...
758 if ((fp
= fopen(control
, "rb")) == NULL
)
763 * Grab the job information first...
771 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
774 * Process control lines...
779 case 'J' : /* Job name */
780 strlcpy(title
, line
+ 1, sizeof(title
));
783 case 'N' : /* Document name */
784 strlcpy(docname
, line
+ 1, sizeof(docname
));
787 case 'P' : /* User identification */
788 strlcpy(user
, line
+ 1, sizeof(user
));
791 case 'L' : /* Print banner page */
801 * Then print the jobs...
806 while (smart_gets(line
, sizeof(line
), fp
) != NULL
)
809 * Process control lines...
814 case 'c' : /* Plot CIF file */
815 case 'd' : /* Print DVI file */
816 case 'f' : /* Print formatted file */
817 case 'g' : /* Plot file */
818 case 'l' : /* Print file leaving control characters (raw) */
819 case 'n' : /* Print ditroff output file */
820 case 'o' : /* Print PostScript output file */
821 case 'p' : /* Print file with 'pr' format (prettyprint) */
822 case 'r' : /* File to print with FORTRAN carriage control */
823 case 't' : /* Print troff output file */
824 case 'v' : /* Print raster file */
826 * Check that we have a username...
831 syslog(LOG_WARNING
, "No username specified by client! "
832 "Using \"anonymous\"...");
833 strcpy(user
, "anonymous");
837 * Copy the default options...
843 for (i
= 0; i
< destptr
->num_options
; i
++)
844 num_options
= cupsAddOption(destptr
->options
[i
].name
,
845 destptr
->options
[i
].value
,
846 num_options
, &options
);
847 for (i
= 0; i
< num_defaults
; i
++)
848 num_options
= cupsAddOption(defaults
[i
].name
,
850 num_options
, &options
);
853 * If a banner was requested and it's not overridden by a
854 * command line option and the destination's default is none
855 * then add the standard banner...
859 cupsGetOption("job-sheets", num_defaults
, defaults
) == NULL
&&
860 ((cupsd_job_sheets
= cupsGetOption("job-sheets",
861 destptr
->num_options
,
862 destptr
->options
)) == NULL
||
863 !strcmp(cupsd_job_sheets
, "none,none")))
865 num_options
= cupsAddOption("job-sheets", "standard",
866 num_options
, &options
);
870 * Add additional options as needed...
874 num_options
= cupsAddOption("raw", "", num_options
, &options
);
877 num_options
= cupsAddOption("prettyprint", "", num_options
,
881 * Figure out which file we are printing...
884 for (i
= 0; i
< num_data
; i
++)
885 if (strcmp(data
[i
], line
+ 1) == 0)
895 * Send the print request...
898 if (print_file(queue
, temp
[i
], title
, docname
, user
, num_options
,
904 cupsFreeOptions(num_options
, options
);
917 * Clean up all temporary files and return...
922 for (i
= 0; i
< num_data
; i
++)
925 cupsFreeDests(num_dests
, dests
);
932 * 'remove_jobs()' - Cancel one or more jobs.
935 int /* O - Command status */
936 remove_jobs(const char *dest
, /* I - Destination */
937 const char *agent
, /* I - User agent */
938 const char *list
) /* I - List of jobs or users */
941 http_t
*http
; /* HTTP server connection */
942 ipp_t
*request
, /* IPP Request */
943 *response
; /* IPP Response */
944 cups_lang_t
*language
; /* Default language */
945 char uri
[HTTP_MAX_URI
]; /* Job URI */
948 (void)dest
; /* Suppress compiler warnings... */
951 * Try connecting to the local server...
954 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
955 cupsEncryption())) == NULL
)
957 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
962 language
= cupsLangDefault();
965 * Loop for each job...
968 while ((id
= atoi(list
)) > 0)
971 * Skip job ID in list...
974 while (isdigit(*list
& 255))
976 while (isspace(*list
& 255))
980 * Build an IPP_CANCEL_JOB request, which requires the following
984 * attributes-natural-language
986 * requesting-user-name
991 request
->request
.op
.operation_id
= IPP_CANCEL_JOB
;
992 request
->request
.op
.request_id
= 1;
994 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
995 "attributes-charset", NULL
, cupsLangEncoding(language
));
997 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
998 "attributes-natural-language", NULL
, language
->language
);
1000 sprintf(uri
, "ipp://localhost/jobs/%d", id
);
1001 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "job-uri", NULL
, uri
);
1003 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1004 "requesting-user-name", NULL
, agent
);
1007 * Do the request and get back a response...
1010 if ((response
= cupsDoRequest(http
, request
, "/jobs")) != NULL
)
1012 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
1014 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
1015 ippErrorString(response
->request
.status
.status_code
));
1016 ippDelete(response
);
1017 cupsLangFree(language
);
1022 syslog(LOG_INFO
, "Job ID %d cancelled", id
);
1024 ippDelete(response
);
1028 syslog(LOG_WARNING
, "Cancel of job ID %d failed: %s\n", id
,
1029 ippErrorString(cupsLastError()));
1030 cupsLangFree(language
);
1036 cupsLangFree(language
);
1044 * 'send_state()' - Send the queue state.
1047 int /* O - Command status */
1048 send_state(const char *dest
, /* I - Destination */
1049 const char *list
, /* I - Job or user */
1050 int longstatus
) /* I - List of jobs or users */
1052 int id
; /* Job ID from list */
1053 http_t
*http
; /* HTTP server connection */
1054 ipp_t
*request
, /* IPP Request */
1055 *response
; /* IPP Response */
1056 ipp_attribute_t
*attr
; /* Current attribute */
1057 cups_lang_t
*language
; /* Default language */
1058 ipp_pstate_t state
; /* Printer state */
1059 const char *jobdest
, /* Pointer into job-printer-uri */
1060 *jobuser
, /* Pointer to job-originating-user-name */
1061 *jobname
; /* Pointer to job-name */
1062 ipp_jstate_t jobstate
; /* job-state */
1063 int jobid
, /* job-id */
1064 jobsize
, /* job-k-octets */
1065 jobcount
, /* Number of jobs */
1066 jobcopies
, /* Number of copies */
1067 rank
; /* Rank of job */
1068 char rankstr
[255]; /* Rank string */
1069 char namestr
[1024]; /* Job name string */
1070 char uri
[HTTP_MAX_URI
]; /* Printer URI */
1071 char queue
[256], /* Printer/class queue */
1072 *instance
; /* Printer/class instance */
1073 static const char * const ranks
[10] = /* Ranking strings */
1086 static const char * const requested
[] =
1087 { /* Requested attributes */
1092 "job-originating-user-name",
1099 * Remove instance from destination, if any...
1102 strlcpy(queue
, dest
, sizeof(queue
));
1104 if ((instance
= strrchr(queue
, '/')) != NULL
)
1108 * Try connecting to the local server...
1111 if ((http
= httpConnectEncrypt(cupsServer(), ippPort(),
1112 cupsEncryption())) == NULL
)
1114 syslog(LOG_ERR
, "Unable to connect to server %s: %s", cupsServer(),
1116 printf("Unable to connect to server %s: %s", cupsServer(), strerror(errno
));
1121 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
1124 * attributes-charset
1125 * attributes-natural-language
1131 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
1132 request
->request
.op
.request_id
= 1;
1134 language
= cupsLangDefault();
1136 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1137 "attributes-charset", NULL
, cupsLangEncoding(language
));
1139 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1140 "attributes-natural-language", NULL
, language
->language
);
1142 httpAssembleURIf(uri
, sizeof(uri
), "ipp", NULL
, "localhost", 0,
1143 "/printers/%s", queue
);
1144 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
1145 "printer-uri", NULL
, uri
);
1147 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1148 "requested-attributes", NULL
, "printer-state");
1151 * Do the request and get back a response...
1154 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1156 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
1158 syslog(LOG_WARNING
, "Unable to get printer list: %s\n",
1159 ippErrorString(response
->request
.status
.status_code
));
1160 printf("Unable to get printer list: %s\n",
1161 ippErrorString(response
->request
.status
.status_code
));
1162 ippDelete(response
);
1166 if ((attr
= ippFindAttribute(response
, "printer-state", IPP_TAG_ENUM
)) != NULL
)
1167 state
= (ipp_pstate_t
)attr
->values
[0].integer
;
1169 state
= IPP_PRINTER_STOPPED
;
1173 case IPP_PRINTER_IDLE
:
1174 printf("%s is ready\n", dest
);
1176 case IPP_PRINTER_PROCESSING
:
1177 printf("%s is ready and printing\n", dest
);
1179 case IPP_PRINTER_STOPPED
:
1180 printf("%s is not ready\n", dest
);
1184 ippDelete(response
);
1188 syslog(LOG_WARNING
, "Unable to get printer list: %s\n",
1189 ippErrorString(cupsLastError()));
1190 printf("Unable to get printer list: %s\n",
1191 ippErrorString(cupsLastError()));
1196 * Build an IPP_GET_JOBS or IPP_GET_JOB_ATTRIBUTES request, which requires
1197 * the following attributes:
1199 * attributes-charset
1200 * attributes-natural-language
1201 * job-uri or printer-uri
1208 request
->request
.op
.operation_id
= id
? IPP_GET_JOB_ATTRIBUTES
: IPP_GET_JOBS
;
1209 request
->request
.op
.request_id
= 1;
1211 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
1212 "attributes-charset", NULL
, cupsLangEncoding(language
));
1214 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
1215 "attributes-natural-language", NULL
, language
->language
);
1217 httpAssembleURIf(uri
, sizeof(uri
), "ipp", NULL
, "localhost", 0,
1218 "/printers/%s", queue
);
1220 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
1224 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_INTEGER
, "job-id", id
);
1227 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
1228 "requesting-user-name", NULL
, list
);
1229 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
1232 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
1233 "requested-attributes", sizeof(requested
) / sizeof(requested
[0]),
1237 * Do the request and get back a response...
1242 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
1244 if (response
->request
.status
.status_code
> IPP_OK_CONFLICT
)
1246 printf("get-jobs failed: %s\n",
1247 ippErrorString(response
->request
.status
.status_code
));
1248 ippDelete(response
);
1255 * Loop through the job list and display them...
1258 for (attr
= response
->attrs
; attr
!= NULL
; attr
= attr
->next
)
1261 * Skip leading attributes until we hit a job...
1264 while (attr
!= NULL
&&
1265 (attr
->group_tag
!= IPP_TAG_JOB
|| attr
->name
== NULL
))
1272 * Pull the needed attributes from this job...
1277 jobstate
= IPP_JOB_PENDING
;
1278 jobname
= "untitled";
1283 while (attr
!= NULL
&& attr
->group_tag
== IPP_TAG_JOB
)
1285 if (strcmp(attr
->name
, "job-id") == 0 &&
1286 attr
->value_tag
== IPP_TAG_INTEGER
)
1287 jobid
= attr
->values
[0].integer
;
1289 if (strcmp(attr
->name
, "job-k-octets") == 0 &&
1290 attr
->value_tag
== IPP_TAG_INTEGER
)
1291 jobsize
= attr
->values
[0].integer
;
1293 if (strcmp(attr
->name
, "job-state") == 0 &&
1294 attr
->value_tag
== IPP_TAG_ENUM
)
1295 jobstate
= (ipp_jstate_t
)attr
->values
[0].integer
;
1297 if (strcmp(attr
->name
, "job-printer-uri") == 0 &&
1298 attr
->value_tag
== IPP_TAG_URI
)
1299 if ((jobdest
= strrchr(attr
->values
[0].string
.text
, '/')) != NULL
)
1302 if (strcmp(attr
->name
, "job-originating-user-name") == 0 &&
1303 attr
->value_tag
== IPP_TAG_NAME
)
1304 jobuser
= attr
->values
[0].string
.text
;
1306 if (strcmp(attr
->name
, "job-name") == 0 &&
1307 attr
->value_tag
== IPP_TAG_NAME
)
1308 jobname
= attr
->values
[0].string
.text
;
1310 if (strcmp(attr
->name
, "copies") == 0 &&
1311 attr
->value_tag
== IPP_TAG_INTEGER
)
1312 jobcopies
= attr
->values
[0].integer
;
1318 * See if we have everything needed...
1321 if (jobdest
== NULL
|| jobid
== 0)
1329 if (!longstatus
&& jobcount
== 0)
1330 puts("Rank Owner Job File(s) Total Size");
1335 * Display the job...
1338 if (jobstate
== IPP_JOB_PROCESSING
)
1339 strcpy(rankstr
, "active");
1342 snprintf(rankstr
, sizeof(rankstr
), "%d%s", rank
, ranks
[rank
% 10]);
1351 snprintf(namestr
, sizeof(namestr
), "%d copies of %s", jobcopies
,
1354 strlcpy(namestr
, jobname
, sizeof(namestr
));
1356 printf("%s: %-33.33s [job %d localhost]\n", jobuser
, rankstr
, jobid
);
1357 printf(" %-39.39s %.0f bytes\n", namestr
, 1024.0 * jobsize
);
1360 printf("%-7s %-7.7s %-7d %-31.31s %.0f bytes\n", rankstr
, jobuser
,
1361 jobid
, jobname
, 1024.0 * jobsize
);
1367 ippDelete(response
);
1371 printf("get-jobs failed: %s\n", ippErrorString(cupsLastError()));
1378 cupsLangFree(language
);
1386 * 'smart_gets()' - Get a line of text, removing the trailing CR and/or LF.
1389 char * /* O - Line read or NULL */
1390 smart_gets(char *s
, /* I - Pointer to line buffer */
1391 int len
, /* I - Size of line buffer */
1392 FILE *fp
) /* I - File to read from */
1394 char *ptr
, /* Pointer into line */
1395 *end
; /* End of line */
1396 int ch
; /* Character from file */
1400 * Read the line; unlike fgets(), we read the entire line but dump
1401 * characters that go past the end of the buffer. Also, we accept
1402 * CR, LF, or CR LF for the line endings to be "safe", although
1403 * RFC 1179 specifically says "just use LF".
1409 while ((ch
= getc(fp
)) != EOF
)
1413 else if (ch
== '\r')
1416 * See if a LF follows...
1432 if (ch
== EOF
&& ptr
== s
)
1440 * End of "$Id: cups-lpd.c 4807 2005-10-21 19:17:52Z mike $".