r12531: 'make quicktest' was taking 15 minutes on my system due to failing DNS
[kai/samba-autobuild/.git] / source4 / client / smbspool.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB backend for the Common UNIX Printing System ("CUPS")
4    Copyright 1999 by Easy Software Products
5    Copyright Andrew Tridgell 1994-1998
6    Copyright Andrew Bartlett 2002
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 /*
26  * Local functions...
27  */
28
29 static void             list_devices(void);
30 static struct smbcli_state      *smb_connect(const char *, const char *, const char *, const char *, const char *);
31 static int              smb_print(struct smbcli_state *, char *, FILE *);
32
33
34 /*
35  * 'main()' - Main entry for SMB backend.
36  */
37
38  int                            /* O - Exit status */
39  main(int  argc,                        /* I - Number of command-line arguments */
40      char *argv[])              /* I - Command-line arguments */
41 {
42   int           i;              /* Looping var */
43   int           copies;         /* Number of copies */
44   char          uri[1024],      /* URI */
45                 *sep,           /* Pointer to separator */
46                 *password;      /* Password */
47   const char    *username,      /* Username */
48                 *server,        /* Server name */
49                 *printer;       /* Printer name */
50   const char    *workgroup;     /* Workgroup */
51   FILE          *fp;            /* File to print */
52   int           status=0;               /* Status of LPD job */
53   struct smbcli_state *cli;     /* SMB interface */
54
55   /* we expect the URI in argv[0]. Detect the case where it is in argv[1] and cope */
56   if (argc > 2 && strncmp(argv[0],"smb://", 6) && !strncmp(argv[1],"smb://", 6)) {
57           argv++;
58           argc--;
59   }
60
61   if (argc == 1)
62   {
63    /*
64     * NEW!  In CUPS 1.1 the backends are run with no arguments to list the
65     *       available devices.  These can be devices served by this backend
66     *       or any other backends (i.e. you can have an SNMP backend that
67     *       is only used to enumerate the available network printers... :)
68     */
69
70     list_devices();
71     return (0);
72   }
73
74   if (argc < 6 || argc > 7)
75   {
76     fprintf(stderr, "Usage: %s [DEVICE_URI] job-id user title copies options [file]\n",
77             argv[0]);
78     fputs("       The DEVICE_URI environment variable can also contain the\n", stderr);
79     fputs("       destination printer:\n", stderr);
80     fputs("\n", stderr);
81     fputs("           smb://[username:password@][workgroup/]server/printer\n", stderr);
82     return (1);
83   }
84
85  /*
86   * If we have 7 arguments, print the file named on the command-line.
87   * Otherwise, print data from stdin...
88   */
89
90   if (argc == 6)
91   {
92    /*
93     * Print from Copy stdin to a temporary file...
94     */
95
96     fp     = stdin;
97     copies = 1;
98   }
99   else if ((fp = fopen(argv[6], "rb")) == NULL)
100   {
101     perror("ERROR: Unable to open print file");
102     return (1);
103   }
104   else
105     copies = atoi(argv[4]);
106
107  /*
108   * Find the URI...
109   */
110
111   if (strncmp(argv[0], "smb://", 6) == 0)
112     strncpy(uri, argv[0], sizeof(uri) - 1);
113   else if (getenv("DEVICE_URI") != NULL)
114     strncpy(uri, getenv("DEVICE_URI"), sizeof(uri) - 1);
115   else
116   {
117     fputs("ERROR: No device URI found in argv[0] or DEVICE_URI environment variable!\n", stderr);
118     return (1);
119   }
120
121   uri[sizeof(uri) - 1] = '\0';
122
123  /*
124   * Extract the destination from the URI...
125   */
126
127   if ((sep = strrchr_m(uri, '@')) != NULL)
128   {
129     username = uri + 6;
130     *sep++ = '\0';
131
132     server = sep;
133
134    /*
135     * Extract password as needed...
136     */
137
138     if ((password = strchr_m(username, ':')) != NULL)
139       *password++ = '\0';
140     else
141       password = "";
142   }
143   else
144   {
145     username = "";
146     password = "";
147     server   = uri + 6;
148   }
149
150   if ((sep = strchr_m(server, '/')) == NULL)
151   {
152     fputs("ERROR: Bad URI - need printer name!\n", stderr);
153     return (1);
154   }
155
156   *sep++ = '\0';
157   printer = sep;
158
159   if ((sep = strchr_m(printer, '/')) != NULL)
160   {
161    /*
162     * Convert to smb://[username:password@]workgroup/server/printer...
163     */
164
165     *sep++ = '\0';
166
167     workgroup = server;
168     server    = printer;
169     printer   = sep;
170   }
171   else
172     workgroup = NULL;
173
174  /*
175   * Setup the SAMBA server state...
176   */
177
178   setup_logging(argv[0], DEBUG_STDOUT);
179
180   if (!lp_load()) {
181           fprintf(stderr, "ERROR: Can't load %s - run testparm to debug it\n", lp_config_file());
182           return (1);
183   }
184
185   if (workgroup == NULL)
186     workgroup = lp_workgroup();
187
188   do
189   {
190     if ((cli = smb_connect(workgroup, server, printer, username, password)) == NULL)
191     {
192       if (getenv("CLASS") == NULL)
193       {
194         fprintf(stderr, "ERROR: Unable to connect to SAMBA host, will retry in 60 seconds...");
195         sleep (60);
196       }
197       else
198       {
199         fprintf(stderr, "ERROR: Unable to connect to SAMBA host, trying next printer...");
200         return (1);
201       }
202     }
203   }
204   while (cli == NULL);
205
206  /*
207   * Now that we are connected to the server, ignore SIGTERM so that we
208   * can finish out any page data the driver sends (e.g. to eject the
209   * current page...  Only ignore SIGTERM if we are printing data from
210   * stdin (otherwise you can't cancel raw jobs...)
211   */
212
213   if (argc < 7)
214     CatchSignal(SIGTERM, SIG_IGN);
215
216  /*
217   * Queue the job...
218   */
219
220   for (i = 0; i < copies; i ++)
221     if ((status = smb_print(cli, argv[3] /* title */, fp)) != 0)
222       break;
223
224   talloc_free(cli);
225
226  /*
227   * Return the queue status...
228   */
229
230   return (status);
231 }
232
233
234 /*
235  * 'list_devices()' - List the available printers seen on the network...
236  */
237
238 static void
239 list_devices(void)
240 {
241  /*
242   * Eventually, search the local workgroup for available hosts and printers.
243   */
244
245   puts("network smb \"Unknown\" \"Windows Printer via SAMBA\"");
246 }
247
248
249 /*
250  * 'smb_connect()' - Return a connection to a server.
251  */
252
253 static struct smbcli_state *            /* O - SMB connection */
254 smb_connect(const char *workgroup,              /* I - Workgroup */
255             const char *server,         /* I - Server */
256             const char *share,          /* I - Printer */
257             const char *username,               /* I - Username */
258             const char *password)               /* I - Password */
259 {
260   struct smbcli_state   *c;             /* New connection */
261   char *myname;         /* Client name */
262   NTSTATUS nt_status;
263
264  /*
265   * Get the names and addresses of the client and server...
266   */
267
268   myname = get_myname();  
269         
270   nt_status = smbcli_full_connection(NULL, &c, myname, server, 0, share, NULL,
271                                      username, workgroup, password, NULL);
272   
273   free(myname);
274   if (!NT_STATUS_IS_OK(nt_status)) {
275           fprintf(stderr, "ERROR:  Connection failed with error %s\n", nt_errstr(nt_status));
276           return NULL;
277   }
278
279   /*
280    * Return the new connection...
281    */
282   
283   return (c);
284 }
285
286
287 /*
288  * 'smb_print()' - Queue a job for printing using the SMB protocol.
289  */
290
291 static int                              /* O - 0 = success, non-0 = failure */
292 smb_print(struct smbcli_state *cli,     /* I - SMB connection */
293           char             *title,      /* I - Title/job name */
294           FILE             *fp)         /* I - File to print */
295 {
296   int   fnum;           /* File number */
297   int   nbytes,         /* Number of bytes read */
298         tbytes;         /* Total bytes read */
299   char  buffer[8192],   /* Buffer for copy */
300         *ptr;           /* Pointer into tile */
301
302
303  /*
304   * Sanitize the title...
305   */
306
307   for (ptr = title; *ptr; ptr ++)
308     if (!isalnum((int)*ptr) && !isspace((int)*ptr))
309       *ptr = '_';
310
311  /*
312   * Open the printer device...
313   */
314
315   if ((fnum = smbcli_open(cli, title, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE)) == -1)
316   {
317     fprintf(stderr, "ERROR: %s opening remote file %s\n",
318             smbcli_errstr(cli), title);
319     return (1);
320   }
321
322  /*
323   * Copy the file to the printer...
324   */
325
326   if (fp != stdin)
327     rewind(fp);
328
329   tbytes = 0;
330
331   while ((nbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0)
332   {
333     if (smbcli_write(cli, fnum, 0, buffer, tbytes, nbytes) != nbytes)
334     {
335       fprintf(stderr, "ERROR: Error writing file: %s\n", smbcli_errstr(cli));
336       break;
337     }
338
339     tbytes += nbytes;
340   } 
341
342   if (!smbcli_close(cli, fnum))
343   {
344     fprintf(stderr, "ERROR: %s closing remote file %s\n",
345             smbcli_errstr(cli), title);
346     return (1);
347   }
348   else
349     return (0);
350 }