s3-printing: pass lpq command to job_submit
[ddiss/samba.git] / source3 / printing / print_iprint.c
1 /*
2  * Support code for Novell iPrint using the Common UNIX Printing
3  * System ("CUPS") libraries
4  *
5  * Copyright 1999-2003 by Michael R Sweet.
6  * Portions Copyright 2005 by Joel J. Smith.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "includes.h"
23 #include "printing.h"
24 #include "printing/pcap.h"
25
26 #ifdef HAVE_IPRINT
27 #include <cups/cups.h>
28 #include <cups/language.h>
29
30 #define OPERATION_NOVELL_LIST_PRINTERS          0x401A
31 #define OPERATION_NOVELL_MGMT                   0x401C
32 #define NOVELL_SERVER_SYSNAME                   "sysname="
33 #define NOVELL_SERVER_SYSNAME_NETWARE           "NetWare IA32"
34 #define NOVELL_SERVER_VERSION_STRING            "iprintserverversion="
35 #define NOVELL_SERVER_VERSION_OES_SP1           33554432
36
37 /*
38  * 'iprint_passwd_cb()' - The iPrint password callback...
39  */
40
41 static const char *                             /* O - Password or NULL */
42 iprint_passwd_cb(const char *prompt)    /* I - Prompt */
43 {
44        /*
45         * Always return NULL to indicate that no password is available...
46         */
47
48         return (NULL);
49 }
50
51 static const char *iprint_server(void)
52 {
53         if ((lp_iprint_server() != NULL) && (strlen(lp_iprint_server()) > 0)) {
54                 DEBUG(10, ("iprint server explicitly set to %s\n",
55                            lp_iprint_server()));
56                 return lp_iprint_server();
57         }
58
59         DEBUG(10, ("iprint server left to default %s\n", cupsServer()));
60         return cupsServer();
61 }
62
63 /*
64  * Pass in an already connected http_t*
65  * Returns the server version if one can be found, multiplied by
66  * -1 for all NetWare versions.  Returns 0 if a server version
67  * cannot be determined
68  */
69
70 static int iprint_get_server_version(http_t *http, char* serviceUri)
71 {
72         ipp_t           *request = NULL,        /* IPP Request */
73                         *response = NULL;       /* IPP Response */
74         ipp_attribute_t *attr;                  /* Current attribute */
75         cups_lang_t     *language = NULL;       /* Default language */
76         char            *ver;                   /* server version pointer */
77         char            *vertmp;                /* server version tmp pointer */
78         int             serverVersion = 0;      /* server version */
79         char            *os;                    /* server os */
80         int             osFlag = 0;             /* 0 for NetWare, 1 for anything else */
81         char            *temp;                  /* pointer for string manipulation */
82
83        /*
84         * Build an OPERATION_NOVELL_MGMT("get-server-version") request,
85         * which requires the following attributes:
86         *
87         *    attributes-charset
88         *    attributes-natural-language
89         *    operation-name
90         *    service-uri
91         */
92
93         request = ippNew();
94
95         request->request.op.operation_id = (ipp_op_t)OPERATION_NOVELL_MGMT;
96         request->request.op.request_id   = 1;
97
98         language = cupsLangDefault();
99
100         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
101                      "attributes-charset", NULL, "utf-8");
102
103         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
104                      "attributes-natural-language", NULL, language->language);
105
106         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
107                      "service-uri", NULL, serviceUri);
108
109         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
110                       "operation-name", NULL, "get-server-version");
111
112        /*
113         * Do the request and get back a response...
114         */
115
116         if (((response = cupsDoRequest(http, request, "/ipp/")) == NULL) ||
117             (response->request.status.status_code >= IPP_OK_CONFLICT))
118                 goto out;
119
120         if (((attr = ippFindAttribute(response, "server-version",
121                                       IPP_TAG_STRING)) != NULL)) {
122                 if ((ver = strstr(attr->values[0].string.text,
123                                   NOVELL_SERVER_VERSION_STRING)) != NULL) {
124                         ver += strlen(NOVELL_SERVER_VERSION_STRING);
125                        /*
126                         * Strangely, libcups stores a IPP_TAG_STRING (octet
127                         * string) as a null-terminated string with no length
128                         * even though it could be binary data with nulls in
129                         * it.  Luckily, in this case the value is not binary.
130                         */
131                         serverVersion = strtol(ver, &vertmp, 10);
132
133                         /* Check for not found, overflow or negative version */
134                         if ((ver == vertmp) || (serverVersion < 0))
135                                 serverVersion = 0;
136                 }
137
138                 if ((os = strstr(attr->values[0].string.text,
139                                   NOVELL_SERVER_SYSNAME)) != NULL) {
140                         os += strlen(NOVELL_SERVER_SYSNAME);
141                         if ((temp = strchr(os,'<')) != NULL)
142                                 *temp = '\0';
143                         if (strcmp(os,NOVELL_SERVER_SYSNAME_NETWARE))
144                                 osFlag = 1; /* 1 for non-NetWare systems */
145                 }
146         }
147
148  out:
149         if (response)
150                 ippDelete(response);
151
152         if (language)
153                 cupsLangFree(language);
154
155         if (osFlag == 0)
156                 serverVersion *= -1;
157
158         return serverVersion;
159 }
160
161
162 static int iprint_cache_add_printer(http_t *http,
163                                    int reqId,
164                                    char* url)
165 {
166         ipp_t           *request = NULL,        /* IPP Request */
167                         *response = NULL;       /* IPP Response */
168         ipp_attribute_t *attr;                  /* Current attribute */
169         cups_lang_t     *language = NULL;       /* Default language */
170         char            *name,                  /* printer-name attribute */
171                         *info,                  /* printer-info attribute */
172                         smb_enabled,            /* smb-enabled attribute */
173                         secure;                 /* security-enabled attrib. */
174
175         char            *httpPath;      /* path portion of the printer-uri */
176
177         static const char *pattrs[] =   /* Requested printer attributes */
178                         {
179                           "printer-name",
180                           "security-enabled",
181                           "printer-info",
182                           "smb-enabled"
183                         };       
184
185         request = ippNew();
186
187         request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
188         request->request.op.request_id   = reqId;
189
190         language = cupsLangDefault();
191
192         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
193                      "attributes-charset", NULL, "utf-8");
194
195         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
196                      "attributes-natural-language", NULL, language->language);
197
198         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, url);
199
200         ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
201                       "requested-attributes",
202                       (sizeof(pattrs) / sizeof(pattrs[0])),
203                       NULL, pattrs);
204
205         /*
206          * Do the request and get back a response...
207          */
208
209         if ((httpPath = strstr(url,"://")) == NULL ||
210                         (httpPath = strchr(httpPath+3,'/')) == NULL)
211         {
212                 ippDelete(request);
213                 request = NULL;
214                 goto out;
215         }
216
217         if ((response = cupsDoRequest(http, request, httpPath)) == NULL) {
218                 ipp_status_t lastErr = cupsLastError();
219
220                /*
221                 * Ignore printers that cannot be queried without credentials
222                 */
223                 if (lastErr == IPP_FORBIDDEN || 
224                     lastErr == IPP_NOT_AUTHENTICATED ||
225                     lastErr == IPP_NOT_AUTHORIZED)
226                         goto out;
227
228                 DEBUG(0,("Unable to get printer list - %s\n",
229                       ippErrorString(lastErr)));
230                 goto out;
231         }
232
233         for (attr = response->attrs; attr != NULL;) {
234                /*
235                 * Skip leading attributes until we hit a printer...
236                 */
237
238                 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
239                         attr = attr->next;
240
241                 if (attr == NULL)
242                         break;
243
244                /*
245                 * Pull the needed attributes from this printer...
246                 */
247
248                 name       = NULL;
249                 info       = NULL;
250                 smb_enabled= 1;
251                 secure     = 0;
252
253                 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER) {
254                         if (strcmp(attr->name, "printer-name") == 0 &&
255                             attr->value_tag == IPP_TAG_NAME)
256                                 name = attr->values[0].string.text;
257
258                         if (strcmp(attr->name, "printer-info") == 0 &&
259                             (attr->value_tag == IPP_TAG_TEXT ||
260                             attr->value_tag == IPP_TAG_TEXTLANG))
261                                 info = attr->values[0].string.text;
262
263                        /*
264                         * If the smb-enabled attribute is present and the
265                         * value is set to 0, don't show the printer.
266                         * If the attribute is not present, assume that the
267                         * printer should show up
268                         */
269                         if (!strcmp(attr->name, "smb-enabled") &&
270                             ((attr->value_tag == IPP_TAG_INTEGER &&
271                             !attr->values[0].integer) ||
272                             (attr->value_tag == IPP_TAG_BOOLEAN &&
273                             !attr->values[0].boolean)))
274                                 smb_enabled = 0;
275
276                        /*
277                         * If the security-enabled attribute is present and the
278                         * value is set to 1, don't show the printer.
279                         * If the attribute is not present, assume that the
280                         * printer should show up
281                         */
282                         if (!strcmp(attr->name, "security-enabled") &&
283                             ((attr->value_tag == IPP_TAG_INTEGER &&
284                             attr->values[0].integer) ||
285                             (attr->value_tag == IPP_TAG_BOOLEAN &&
286                             attr->values[0].boolean)))
287                                 secure = 1;
288
289                         attr = attr->next;
290                 }
291
292                /*
293                 * See if we have everything needed...
294                 * Make sure the printer is not a secure printer
295                 * and make sure smb printing hasn't been explicitly
296                 * disabled for the printer
297                 */
298
299                 if (name != NULL && !secure && smb_enabled) 
300                         pcap_cache_add(name, info, NULL);
301         }
302
303  out:
304         if (response)
305                 ippDelete(response);
306         return(0);
307 }
308
309 bool iprint_cache_reload(void)
310 {
311         http_t          *http = NULL;           /* HTTP connection to server */
312         ipp_t           *request = NULL,        /* IPP Request */
313                         *response = NULL;       /* IPP Response */
314         ipp_attribute_t *attr;                  /* Current attribute */
315         cups_lang_t     *language = NULL;       /* Default language */
316         int             i;
317         bool ret = False;
318
319         DEBUG(5, ("reloading iprint printcap cache\n"));
320
321        /*
322         * Make sure we don't ask for passwords...
323         */
324
325         cupsSetPasswordCB(iprint_passwd_cb);
326
327        /*
328         * Try to connect to the server...
329         */
330
331         if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
332                 DEBUG(0,("Unable to connect to iPrint server %s - %s\n", 
333                          iprint_server(), strerror(errno)));
334                 goto out;
335         }
336
337        /*
338         * Build a OPERATION_NOVELL_LIST_PRINTERS request, which requires the following attributes:
339         *
340         *    attributes-charset
341         *    attributes-natural-language
342         */
343
344         request = ippNew();
345
346         request->request.op.operation_id =
347                 (ipp_op_t)OPERATION_NOVELL_LIST_PRINTERS;
348         request->request.op.request_id   = 1;
349
350         language = cupsLangDefault();
351
352         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
353                      "attributes-charset", NULL, "utf-8");
354
355         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
356                      "attributes-natural-language", NULL, language->language);
357
358         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
359                      "ipp-server", NULL, "ippSrvr");
360
361        /*
362         * Do the request and get back a response...
363         */
364
365         if ((response = cupsDoRequest(http, request, "/ipp")) == NULL) {
366                 DEBUG(0,("Unable to get printer list - %s\n",
367                          ippErrorString(cupsLastError())));
368                 goto out;
369         }
370
371         for (attr = response->attrs; attr != NULL;) {
372                /*
373                 * Skip leading attributes until we hit a printer...
374                 */
375
376                 while (attr != NULL && attr->group_tag != IPP_TAG_PRINTER)
377                         attr = attr->next;
378
379                 if (attr == NULL)
380                         break;
381
382                /*
383                 * Pull the needed attributes from this printer...
384                 */
385
386                 while (attr != NULL && attr->group_tag == IPP_TAG_PRINTER)
387                 {
388                         if (strcmp(attr->name, "printer-name") == 0 &&
389                             (attr->value_tag == IPP_TAG_URI ||
390                              attr->value_tag == IPP_TAG_NAME ||
391                              attr->value_tag == IPP_TAG_TEXT ||
392                              attr->value_tag == IPP_TAG_NAMELANG ||
393                              attr->value_tag == IPP_TAG_TEXTLANG))
394                         {
395                                 for (i = 0; i<attr->num_values; i++)
396                                 {
397                                         char *url = attr->values[i].string.text;
398                                         if (!url || !strlen(url))
399                                                 continue;
400                                         iprint_cache_add_printer(http, i+2, url);
401                                 }
402                         }
403                         attr = attr->next;
404                 }
405         }
406
407         ret = True;
408
409  out:
410         if (response)
411                 ippDelete(response);
412
413         if (language)
414                 cupsLangFree(language);
415
416         if (http)
417                 httpClose(http);
418
419         return ret;
420 }
421
422
423 /*
424  * 'iprint_job_delete()' - Delete a job.
425  */
426
427 static int iprint_job_delete(const char *sharename, const char *lprm_command, struct printjob *pjob)
428 {
429         int             ret = 1;                /* Return value */
430         http_t          *http = NULL;           /* HTTP connection to server */
431         ipp_t           *request = NULL,        /* IPP Request */
432                         *response = NULL;       /* IPP Response */
433         cups_lang_t     *language = NULL;       /* Default language */
434         char            uri[HTTP_MAX_URI];      /* printer-uri attribute */
435         char            httpPath[HTTP_MAX_URI]; /* path portion of the printer-uri */
436
437
438         DEBUG(5,("iprint_job_delete(%s, %p (%d))\n", sharename, pjob, pjob->sysjob));
439
440        /*
441         * Make sure we don't ask for passwords...
442         */
443
444         cupsSetPasswordCB(iprint_passwd_cb);
445
446        /*
447         * Try to connect to the server...
448         */
449
450         if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
451                 DEBUG(0,("Unable to connect to iPrint server %s - %s\n", 
452                          iprint_server(), strerror(errno)));
453                 goto out;
454         }
455
456        /*
457         * Build an IPP_CANCEL_JOB request, which uses the following
458         * attributes:
459         *
460         *    attributes-charset
461         *    attributes-natural-language
462         *    printer-uri
463         *    job-id
464         *    requesting-user-name
465         */
466
467         request = ippNew();
468
469         request->request.op.operation_id = IPP_CANCEL_JOB;
470         request->request.op.request_id   = 1;
471
472         language = cupsLangDefault();
473
474         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
475                      "attributes-charset", NULL, "utf-8");
476
477         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
478                      "attributes-natural-language", NULL, language->language);
479
480         slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(), sharename);
481
482         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
483
484         ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", pjob->sysjob);
485
486         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
487                      NULL, pjob->user);
488
489        /*
490         * Do the request and get back a response...
491         */
492
493         slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", sharename);
494
495         if ((response = cupsDoRequest(http, request, httpPath)) != NULL) {
496                 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
497                         DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
498                                 ippErrorString(cupsLastError())));
499                 } else {
500                         ret = 0;
501                 }
502         } else {
503                 DEBUG(0,("Unable to cancel job %d - %s\n", pjob->sysjob,
504                         ippErrorString(cupsLastError())));
505         }
506
507  out:
508         if (response)
509                 ippDelete(response);
510
511         if (language)
512                 cupsLangFree(language);
513
514         if (http)
515                 httpClose(http);
516
517         return ret;
518 }
519
520
521 /*
522  * 'iprint_job_pause()' - Pause a job.
523  */
524
525 static int iprint_job_pause(int snum, struct printjob *pjob)
526 {
527         int             ret = 1;                /* Return value */
528         http_t          *http = NULL;           /* HTTP connection to server */
529         ipp_t           *request = NULL,        /* IPP Request */
530                         *response = NULL;       /* IPP Response */
531         cups_lang_t     *language = NULL;       /* Default language */
532         char            uri[HTTP_MAX_URI];      /* printer-uri attribute */
533         char            httpPath[HTTP_MAX_URI]; /* path portion of the printer-uri */
534
535
536         DEBUG(5,("iprint_job_pause(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
537
538        /*
539         * Make sure we don't ask for passwords...
540         */
541
542         cupsSetPasswordCB(iprint_passwd_cb);
543
544        /*
545         * Try to connect to the server...
546         */
547
548         if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
549                 DEBUG(0,("Unable to connect to iPrint server %s - %s\n", 
550                          iprint_server(), strerror(errno)));
551                 goto out;
552         }
553
554        /*
555         * Build an IPP_HOLD_JOB request, which requires the following
556         * attributes:
557         *
558         *    attributes-charset
559         *    attributes-natural-language
560         *    printer-uri
561         *    job-id
562         *    requesting-user-name
563         */
564
565         request = ippNew();
566
567         request->request.op.operation_id = IPP_HOLD_JOB;
568         request->request.op.request_id   = 1;
569
570         language = cupsLangDefault();
571
572         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
573                      "attributes-charset", NULL, "utf-8");
574
575         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
576                      "attributes-natural-language", NULL, language->language);
577
578         slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(),
579                  lp_printername(snum));
580
581         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
582
583         ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", pjob->sysjob);
584
585         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
586                      NULL, pjob->user);
587
588        /*
589         * Do the request and get back a response...
590         */
591
592         slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s",
593                  lp_printername(snum));
594
595         if ((response = cupsDoRequest(http, request, httpPath)) != NULL) {
596                 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
597                         DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
598                                 ippErrorString(cupsLastError())));
599                 } else {
600                         ret = 0;
601                 }
602         } else {
603                 DEBUG(0,("Unable to hold job %d - %s\n", pjob->sysjob,
604                         ippErrorString(cupsLastError())));
605         }
606
607  out:
608         if (response)
609                 ippDelete(response);
610
611         if (language)
612                 cupsLangFree(language);
613
614         if (http)
615                 httpClose(http);
616
617         return ret;
618 }
619
620
621 /*
622  * 'iprint_job_resume()' - Resume a paused job.
623  */
624
625 static int iprint_job_resume(int snum, struct printjob *pjob)
626 {
627         int             ret = 1;                /* Return value */
628         http_t          *http = NULL;           /* HTTP connection to server */
629         ipp_t           *request = NULL,        /* IPP Request */
630                         *response = NULL;       /* IPP Response */
631         cups_lang_t     *language = NULL;       /* Default language */
632         char            uri[HTTP_MAX_URI];      /* printer-uri attribute */
633         char            httpPath[HTTP_MAX_URI]; /* path portion of the printer-uri */
634
635
636         DEBUG(5,("iprint_job_resume(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
637
638        /*
639         * Make sure we don't ask for passwords...
640         */
641
642         cupsSetPasswordCB(iprint_passwd_cb);
643
644        /*
645         * Try to connect to the server...
646         */
647
648         if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
649                 DEBUG(0,("Unable to connect to iPrint server %s - %s\n", 
650                          iprint_server(), strerror(errno)));
651                 goto out;
652         }
653
654        /*
655         * Build an IPP_RELEASE_JOB request, which requires the following
656         * attributes:
657         *
658         *    attributes-charset
659         *    attributes-natural-language
660         *    printer-uri
661         *    job-id
662         *    requesting-user-name
663         */
664
665         request = ippNew();
666
667         request->request.op.operation_id = IPP_RELEASE_JOB;
668         request->request.op.request_id   = 1;
669
670         language = cupsLangDefault();
671
672         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
673                      "attributes-charset", NULL, "utf-8");
674
675         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
676                      "attributes-natural-language", NULL, language->language);
677
678         slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(),
679                  lp_printername(snum));
680
681         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
682
683         ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", pjob->sysjob);
684
685         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
686                      NULL, pjob->user);
687
688        /*
689         * Do the request and get back a response...
690         */
691
692         slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s",
693                  lp_printername(snum));
694
695         if ((response = cupsDoRequest(http, request, httpPath)) != NULL) {
696                 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
697                         DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
698                                 ippErrorString(cupsLastError())));
699                 } else {
700                         ret = 0;
701                 }
702         } else {
703                 DEBUG(0,("Unable to release job %d - %s\n", pjob->sysjob,
704                         ippErrorString(cupsLastError())));
705         }
706
707  out:
708         if (response)
709                 ippDelete(response);
710
711         if (language)
712                 cupsLangFree(language);
713
714         if (http)
715                 httpClose(http);
716
717         return ret;
718 }
719
720
721 /*
722  * 'iprint_job_submit()' - Submit a job for printing.
723  */
724
725 static int iprint_job_submit(int snum, struct printjob *pjob,
726                              enum printing_types printing_type,
727                              char *lpq_cmd)
728 {
729         int             ret = 1;                /* Return value */
730         http_t          *http = NULL;           /* HTTP connection to server */
731         ipp_t           *request = NULL,        /* IPP Request */
732                         *response = NULL;       /* IPP Response */
733         ipp_attribute_t *attr;          /* Current attribute */
734         cups_lang_t     *language = NULL;       /* Default language */
735         char            uri[HTTP_MAX_URI]; /* printer-uri attribute */
736
737         DEBUG(5,("iprint_job_submit(%d, %p (%d))\n", snum, pjob, pjob->sysjob));
738
739        /*
740         * Make sure we don't ask for passwords...
741         */
742
743         cupsSetPasswordCB(iprint_passwd_cb);
744
745        /*
746         * Try to connect to the server...
747         */
748
749         if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
750                 DEBUG(0,("Unable to connect to iPrint server %s - %s\n", 
751                          iprint_server(), strerror(errno)));
752                 goto out;
753         }
754
755        /*
756         * Build an IPP_PRINT_JOB request, which requires the following
757         * attributes:
758         *
759         *    attributes-charset
760         *    attributes-natural-language
761         *    printer-uri
762         *    requesting-user-name
763         *    [document-data]
764         */
765
766         request = ippNew();
767
768         request->request.op.operation_id = IPP_PRINT_JOB;
769         request->request.op.request_id   = 1;
770
771         language = cupsLangDefault();
772
773         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
774                      "attributes-charset", NULL, "utf-8");
775
776         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
777                      "attributes-natural-language", NULL, language->language);
778
779         slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(),
780                  lp_printername(snum));
781
782         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
783                      "printer-uri", NULL, uri);
784
785         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
786                      NULL, pjob->user);
787
788         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
789                      "job-originating-host-name", NULL,
790                      pjob->clientmachine);
791
792         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
793                      pjob->jobname);
794
795        /*
796         * Do the request and get back a response...
797         */
798
799         slprintf(uri, sizeof(uri) - 1, "/ipp/%s", lp_printername(snum));
800
801         if ((response = cupsDoFileRequest(http, request, uri, pjob->filename)) != NULL) {
802                 if (response->request.status.status_code >= IPP_OK_CONFLICT) {
803                         DEBUG(0,("Unable to print file to %s - %s\n",
804                                  lp_printername(snum),
805                                  ippErrorString(cupsLastError())));
806                 } else {
807                         ret = 0;
808                 }
809         } else {
810                 DEBUG(0,("Unable to print file to `%s' - %s\n",
811                          lp_printername(snum),
812                          ippErrorString(cupsLastError())));
813         }
814
815         if ( ret == 0 )
816                 unlink(pjob->filename);
817         /* else print_job_end will do it for us */
818
819         if ( ret == 0 ) {
820
821                 attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER);
822                 if (attr != NULL && attr->group_tag == IPP_TAG_JOB)
823                 {
824                         pjob->sysjob = attr->values[0].integer;
825                 }
826         }
827
828  out:
829         if (response)
830                 ippDelete(response);
831
832         if (language)
833                 cupsLangFree(language);
834
835         if (http)
836                 httpClose(http);
837
838         return ret;
839 }
840
841 /*
842  * 'iprint_queue_get()' - Get all the jobs in the print queue.
843  */
844
845 static int iprint_queue_get(const char *sharename,
846                             enum printing_types printing_type,
847                             char *lpq_command,
848                             print_queue_struct **q, 
849                             print_status_struct *status)
850 {
851         fstring         printername;
852         http_t          *http = NULL;           /* HTTP connection to server */
853         ipp_t           *request = NULL,        /* IPP Request */
854                         *response = NULL;       /* IPP Response */
855         ipp_attribute_t *attr = NULL;           /* Current attribute */
856         cups_lang_t     *language = NULL;       /* Default language */
857         char            uri[HTTP_MAX_URI]; /* printer-uri attribute */
858         char            serviceUri[HTTP_MAX_URI]; /* service-uri attribute */
859         char            httpPath[HTTP_MAX_URI]; /* path portion of the uri */
860         int             jobUseUnixTime = 0;     /* Whether job times should
861                                                  * be assumed to be Unix time */
862         int             qcount = 0,             /* Number of active queue entries */
863                         qalloc = 0;             /* Number of queue entries allocated */
864         print_queue_struct *queue = NULL,       /* Queue entries */
865                         *temp;          /* Temporary pointer for queue */
866         const char      *user_name,     /* job-originating-user-name attribute */
867                         *job_name;      /* job-name attribute */
868         int             job_id;         /* job-id attribute */
869         int             job_k_octets;   /* job-k-octets attribute */
870         time_t          job_time;       /* time-at-creation attribute */
871         time_t          printer_current_time = 0;       /* printer's current time */
872         time_t          printer_up_time = 0;    /* printer's uptime */
873         ipp_jstate_t    job_status;     /* job-status attribute */
874         int             job_priority;   /* job-priority attribute */
875         static const char *jattrs[] =   /* Requested job attributes */
876                         {
877                           "job-id",
878                           "job-k-octets",
879                           "job-name",
880                           "job-originating-user-name",
881                           "job-priority",
882                           "job-state",
883                           "time-at-creation",
884                         };
885         static const char *pattrs[] =   /* Requested printer attributes */
886                         {
887                           "printer-state",
888                           "printer-state-message",
889                           "printer-current-time",
890                           "printer-up-time"
891                         };
892
893         *q = NULL;
894
895         /* HACK ALERT!!!  The porblem with support the 'printer name' 
896            option is that we key the tdb off the sharename.  So we will 
897            overload the lpq_command string to pass in the printername 
898            (which is basically what we do for non-cups printers ... using 
899            the lpq_command to get the queue listing). */
900
901         fstrcpy( printername, lpq_command );
902
903         DEBUG(5,("iprint_queue_get(%s, %p, %p)\n", printername, q, status));
904
905        /*
906         * Make sure we don't ask for passwords...
907         */
908
909         cupsSetPasswordCB(iprint_passwd_cb);
910
911        /*
912         * Try to connect to the server...
913         */
914
915         if ((http = httpConnect(iprint_server(), ippPort())) == NULL) {
916                 DEBUG(0,("Unable to connect to iPrint server %s - %s\n", 
917                          iprint_server(), strerror(errno)));
918                 goto out;
919         }
920
921        /*
922         * Generate the printer URI and the service URI that goes with it...
923         */
924
925         slprintf(uri, sizeof(uri) - 1, "ipp://%s/ipp/%s", iprint_server(), printername);
926         slprintf(serviceUri, sizeof(serviceUri) - 1, "ipp://%s/ipp/", iprint_server());
927
928        /*
929         * For Linux iPrint servers from OES SP1 on, the iPrint server
930         * uses Unix time for job start times unless it detects the iPrint
931         * client in an http User-Agent header.  (This was done to accomodate
932         * CUPS broken behavior.  According to RFC 2911, section 4.3.14, job
933         * start times are supposed to be relative to how long the printer has
934         * been up.)  Since libcups doesn't allow us to set that header before
935         * the request is sent, this ugly hack allows us to detect the server
936         * version and decide how to interpret the job time.
937         */
938         if (iprint_get_server_version(http, serviceUri) >=
939             NOVELL_SERVER_VERSION_OES_SP1)
940                 jobUseUnixTime = 1;
941
942         request = ippNew();
943
944         request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
945         request->request.op.request_id   = 2;
946
947         language = cupsLangDefault();
948
949         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
950                      "attributes-charset", NULL, "utf-8");
951
952         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
953                      "attributes-natural-language", NULL, language->language);
954
955         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
956                      "printer-uri", NULL, uri);
957
958         ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
959                       "requested-attributes",
960                       (sizeof(pattrs) / sizeof(pattrs[0])),
961                       NULL, pattrs);
962
963        /*
964         * Do the request and get back a response...
965         */
966
967         slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", printername);
968
969         if ((response = cupsDoRequest(http, request, httpPath)) == NULL) {
970                 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
971                          ippErrorString(cupsLastError())));
972                 *q = queue;
973                 goto out;
974         }
975
976         if (response->request.status.status_code >= IPP_OK_CONFLICT) {
977                 DEBUG(0,("Unable to get printer status for %s - %s\n", printername,
978                          ippErrorString(response->request.status.status_code)));
979                 *q = queue;
980                 goto out;
981         }
982
983        /*
984         * Get the current printer status and convert it to the SAMBA values.
985         */
986
987         if ((attr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
988                 if (attr->values[0].integer == IPP_PRINTER_STOPPED)
989                         status->status = LPSTAT_STOPPED;
990                 else
991                         status->status = LPSTAT_OK;
992         }
993
994         if ((attr = ippFindAttribute(response, "printer-state-message",
995                                      IPP_TAG_TEXT)) != NULL)
996                 fstrcpy(status->message, attr->values[0].string.text);
997
998         if ((attr = ippFindAttribute(response, "printer-current-time",
999                                      IPP_TAG_DATE)) != NULL)
1000                 printer_current_time = ippDateToTime(attr->values[0].date);
1001
1002         if ((attr = ippFindAttribute(response, "printer-up-time",
1003                                      IPP_TAG_INTEGER)) != NULL)
1004                 printer_up_time = attr->values[0].integer;
1005
1006         ippDelete(response);
1007         response = NULL;
1008
1009        /*
1010         * Build an IPP_GET_JOBS request, which requires the following
1011         * attributes:
1012         *
1013         *    attributes-charset
1014         *    attributes-natural-language
1015         *    requested-attributes
1016         *    printer-uri
1017         */
1018
1019         request = ippNew();
1020
1021         request->request.op.operation_id = IPP_GET_JOBS;
1022         request->request.op.request_id   = 3;
1023
1024         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
1025                      "attributes-charset", NULL, "utf-8");
1026
1027         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
1028                      "attributes-natural-language", NULL, language->language);
1029
1030         ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
1031                      "printer-uri", NULL, uri);
1032
1033         ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1034                       "requested-attributes",
1035                       (sizeof(jattrs) / sizeof(jattrs[0])),
1036                       NULL, jattrs);
1037
1038        /*
1039         * Do the request and get back a response...
1040         */
1041
1042         slprintf(httpPath, sizeof(httpPath) - 1, "/ipp/%s", printername);
1043
1044         if ((response = cupsDoRequest(http, request, httpPath)) == NULL) {
1045                 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1046                          ippErrorString(cupsLastError())));
1047                 goto out;
1048         }
1049
1050         if (response->request.status.status_code >= IPP_OK_CONFLICT) {
1051                 DEBUG(0,("Unable to get jobs for %s - %s\n", uri,
1052                          ippErrorString(response->request.status.status_code)));
1053                 goto out;
1054         }
1055
1056        /*
1057         * Process the jobs...
1058         */
1059
1060         qcount = 0;
1061         qalloc = 0;
1062         queue  = NULL;
1063
1064         for (attr = response->attrs; attr != NULL; attr = attr->next) {
1065                /*
1066                 * Skip leading attributes until we hit a job...
1067                 */
1068
1069                 while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
1070                         attr = attr->next;
1071
1072                 if (attr == NULL)
1073                         break;
1074
1075                /*
1076                 * Allocate memory as needed...
1077                 */
1078                 if (qcount >= qalloc) {
1079                         qalloc += 16;
1080
1081                         queue = SMB_REALLOC_ARRAY(queue, print_queue_struct, qalloc);
1082
1083                         if (queue == NULL) {
1084                                 DEBUG(0,("iprint_queue_get: Not enough memory!"));
1085                                 qcount = 0;
1086                                 goto out;
1087                         }
1088                 }
1089
1090                 temp = queue + qcount;
1091                 memset(temp, 0, sizeof(print_queue_struct));
1092
1093                /*
1094                 * Pull the needed attributes from this job...
1095                 */
1096
1097                 job_id       = 0;
1098                 job_priority = 50;
1099                 job_status   = IPP_JOB_PENDING;
1100                 job_time     = 0;
1101                 job_k_octets = 0;
1102                 user_name    = NULL;
1103                 job_name     = NULL;
1104
1105                 while (attr != NULL && attr->group_tag == IPP_TAG_JOB) {
1106                         if (attr->name == NULL) {
1107                                 attr = attr->next;
1108                                 break;
1109                         }
1110
1111                         if (strcmp(attr->name, "job-id") == 0 &&
1112                             attr->value_tag == IPP_TAG_INTEGER)
1113                                 job_id = attr->values[0].integer;
1114
1115                         if (strcmp(attr->name, "job-k-octets") == 0 &&
1116                             attr->value_tag == IPP_TAG_INTEGER)
1117                                 job_k_octets = attr->values[0].integer;
1118
1119                         if (strcmp(attr->name, "job-priority") == 0 &&
1120                             attr->value_tag == IPP_TAG_INTEGER)
1121                                 job_priority = attr->values[0].integer;
1122
1123                         if (strcmp(attr->name, "job-state") == 0 &&
1124                             attr->value_tag == IPP_TAG_ENUM)
1125                                 job_status = (ipp_jstate_t)(attr->values[0].integer);
1126
1127                         if (strcmp(attr->name, "time-at-creation") == 0 &&
1128                             attr->value_tag == IPP_TAG_INTEGER)
1129                         {
1130                                /*
1131                                 * If jobs times are in Unix time, the accuracy of the job
1132                                 * start time depends upon the iPrint server's time being
1133                                 * set correctly.  Otherwise, the accuracy depends upon
1134                                 * the Samba server's time being set correctly
1135                                 */
1136
1137                                 if (jobUseUnixTime)
1138                                         job_time = attr->values[0].integer; 
1139                                 else
1140                                         job_time = time(NULL) - printer_up_time + attr->values[0].integer;
1141                         }
1142
1143                         if (strcmp(attr->name, "job-name") == 0 &&
1144                             (attr->value_tag == IPP_TAG_NAMELANG ||
1145                              attr->value_tag == IPP_TAG_NAME))
1146                                 job_name = attr->values[0].string.text;
1147
1148                         if (strcmp(attr->name, "job-originating-user-name") == 0 &&
1149                             (attr->value_tag == IPP_TAG_NAMELANG ||
1150                              attr->value_tag == IPP_TAG_NAME))
1151                                 user_name = attr->values[0].string.text;
1152
1153                         attr = attr->next;
1154                 }
1155
1156                /*
1157                 * See if we have everything needed...
1158                 */
1159
1160                 if (user_name == NULL || job_name == NULL || job_id == 0) {
1161                         if (attr == NULL)
1162                                 break;
1163                         else
1164                                 continue;
1165                 }
1166
1167                 temp->sysjob   = job_id;
1168                 temp->size     = job_k_octets * 1024;
1169                 temp->status   = job_status == IPP_JOB_PENDING ? LPQ_QUEUED :
1170                                  job_status == IPP_JOB_STOPPED ? LPQ_PAUSED :
1171                                  job_status == IPP_JOB_HELD ? LPQ_PAUSED :
1172                                  LPQ_PRINTING;
1173                 temp->priority = job_priority;
1174                 temp->time     = job_time;
1175                 strncpy(temp->fs_user, user_name, sizeof(temp->fs_user) - 1);
1176                 strncpy(temp->fs_file, job_name, sizeof(temp->fs_file) - 1);
1177
1178                 qcount ++;
1179
1180                 if (attr == NULL)
1181                         break;
1182         }
1183
1184        /*
1185         * Return the job queue...
1186         */
1187
1188         *q = queue;
1189
1190  out:
1191         if (response)
1192                 ippDelete(response);
1193
1194         if (language)
1195                 cupsLangFree(language);
1196
1197         if (http)
1198                 httpClose(http);
1199
1200         return qcount;
1201 }
1202
1203
1204 /*
1205  * 'iprint_queue_pause()' - Pause a print queue.
1206  */
1207
1208 static int iprint_queue_pause(int snum)
1209 {
1210         return(-1); /* Not supported without credentials */
1211 }
1212
1213
1214 /*
1215  * 'iprint_queue_resume()' - Restart a print queue.
1216  */
1217
1218 static int iprint_queue_resume(int snum)
1219 {
1220         return(-1); /* Not supported without credentials */
1221 }
1222
1223 /*******************************************************************
1224  * iPrint printing interface definitions...
1225  ******************************************************************/
1226
1227 struct printif  iprint_printif =
1228 {
1229         PRINT_IPRINT,
1230         iprint_queue_get,
1231         iprint_queue_pause,
1232         iprint_queue_resume,
1233         iprint_job_delete,
1234         iprint_job_pause,
1235         iprint_job_resume,
1236         iprint_job_submit,
1237 };
1238
1239 #else
1240  /* this keeps fussy compilers happy */
1241  void print_iprint_dummy(void);
1242  void print_iprint_dummy(void) {}
1243 #endif /* HAVE_IPRINT */