r6303: Setting up for 3.0.15pre1
[Samba.git] / source / client / smbspool.c
blobaeb4c91b19c5dc56a4812538cb2a97c54edfe2dd
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
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.
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.
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.
23 #define NO_SYSLOG
25 #include "includes.h"
28 * Globals...
31 extern BOOL in_client; /* Boolean for client library */
35 * Local functions...
38 static void list_devices(void);
39 static struct cli_state *smb_connect(const char *, const char *, int, const char *, const char *, const char *);
40 static int smb_print(struct cli_state *, char *, FILE *);
44 * 'main()' - Main entry for SMB backend.
47 int /* O - Exit status */
48 main(int argc, /* I - Number of command-line arguments */
49 char *argv[]) /* I - Command-line arguments */
51 int i; /* Looping var */
52 int copies; /* Number of copies */
53 int port; /* Port number */
54 char uri[1024], /* URI */
55 *sep, /* Pointer to separator */
56 *password; /* Password */
57 const char *username, /* Username */
58 *server, /* Server name */
59 *printer; /* Printer name */
60 const char *workgroup; /* Workgroup */
61 FILE *fp; /* File to print */
62 int status=0; /* Status of LPD job */
63 struct cli_state *cli; /* SMB interface */
65 /* we expect the URI in argv[0]. Detect the case where it is in argv[1] and cope */
66 if (argc > 2 && strncmp(argv[0],"smb://", 6) && !strncmp(argv[1],"smb://", 6)) {
67 argv++;
68 argc--;
71 if (argc == 1)
74 * NEW! In CUPS 1.1 the backends are run with no arguments to list the
75 * available devices. These can be devices served by this backend
76 * or any other backends (i.e. you can have an SNMP backend that
77 * is only used to enumerate the available network printers... :)
80 list_devices();
81 return (0);
84 if (argc < 6 || argc > 7)
86 fprintf(stderr, "Usage: %s [DEVICE_URI] job-id user title copies options [file]\n",
87 argv[0]);
88 fputs(" The DEVICE_URI environment variable can also contain the\n", stderr);
89 fputs(" destination printer:\n", stderr);
90 fputs("\n", stderr);
91 fputs(" smb://[username:password@][workgroup/]server[:port]/printer\n", stderr);
92 return (1);
96 * If we have 7 arguments, print the file named on the command-line.
97 * Otherwise, print data from stdin...
100 if (argc == 6)
103 * Print from Copy stdin to a temporary file...
106 fp = stdin;
107 copies = 1;
109 else if ((fp = fopen(argv[6], "rb")) == NULL)
111 perror("ERROR: Unable to open print file");
112 return (1);
114 else
115 copies = atoi(argv[4]);
118 * Find the URI...
121 if (getenv("DEVICE_URI") != NULL)
122 strncpy(uri, getenv("DEVICE_URI"), sizeof(uri) - 1);
123 else if (strncmp(argv[0], "smb://", 6) == 0)
124 strncpy(uri, argv[0], sizeof(uri) - 1);
125 else
127 fputs("ERROR: No device URI found in DEVICE_URI environment variable or argv[0] !\n", stderr);
128 return (1);
131 uri[sizeof(uri) - 1] = '\0';
134 * Extract the destination from the URI...
137 if ((sep = strrchr_m(uri, '@')) != NULL)
139 username = uri + 6;
140 *sep++ = '\0';
142 server = sep;
145 * Extract password as needed...
148 if ((password = strchr_m(username, ':')) != NULL)
149 *password++ = '\0';
150 else
151 password = CONST_DISCARD(char *, "");
153 else
155 username = "";
156 password = CONST_DISCARD(char *, "");
157 server = uri + 6;
160 if ((sep = strchr_m(server, '/')) == NULL)
162 fputs("ERROR: Bad URI - need printer name!\n", stderr);
163 return (1);
166 *sep++ = '\0';
167 printer = sep;
169 if ((sep = strchr_m(printer, '/')) != NULL)
172 * Convert to smb://[username:password@]workgroup/server/printer...
175 *sep++ = '\0';
177 workgroup = server;
178 server = printer;
179 printer = sep;
181 else
182 workgroup = NULL;
184 if ((sep = strrchr_m(server, ':')) != NULL)
186 *sep++ = '\0';
188 port=atoi(sep);
190 else
191 port=0;
195 * Setup the SAMBA server state...
198 setup_logging("smbspool", True);
200 in_client = True; /* Make sure that we tell lp_load we are */
202 if (!lp_load(dyn_CONFIGFILE, True, False, False))
204 fprintf(stderr, "ERROR: Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
205 return (1);
208 if (workgroup == NULL)
209 workgroup = lp_workgroup();
211 load_interfaces();
215 if ((cli = smb_connect(workgroup, server, port, printer, username, password)) == NULL)
217 if (getenv("CLASS") == NULL)
219 fprintf(stderr, "ERROR: Unable to connect to SAMBA host, will retry in 60 seconds...");
220 sleep (60);
222 else
224 fprintf(stderr, "ERROR: Unable to connect to SAMBA host, trying next printer...");
225 return (1);
229 while (cli == NULL);
232 * Now that we are connected to the server, ignore SIGTERM so that we
233 * can finish out any page data the driver sends (e.g. to eject the
234 * current page... Only ignore SIGTERM if we are printing data from
235 * stdin (otherwise you can't cancel raw jobs...)
238 if (argc < 7)
239 CatchSignal(SIGTERM, SIG_IGN);
242 * Queue the job...
245 for (i = 0; i < copies; i ++)
246 if ((status = smb_print(cli, argv[3] /* title */, fp)) != 0)
247 break;
249 cli_shutdown(cli);
252 * Return the queue status...
255 return (status);
260 * 'list_devices()' - List the available printers seen on the network...
263 static void
264 list_devices(void)
267 * Eventually, search the local workgroup for available hosts and printers.
270 puts("network smb \"Unknown\" \"Windows Printer via SAMBA\"");
275 * 'smb_connect()' - Return a connection to a server.
278 static struct cli_state * /* O - SMB connection */
279 smb_connect(const char *workgroup, /* I - Workgroup */
280 const char *server, /* I - Server */
281 const int port, /* I - Port */
282 const char *share, /* I - Printer */
283 const char *username, /* I - Username */
284 const char *password) /* I - Password */
286 struct cli_state *c; /* New connection */
287 pstring myname; /* Client name */
288 NTSTATUS nt_status;
291 * Get the names and addresses of the client and server...
294 get_myname(myname);
296 nt_status = cli_full_connection(&c, myname, server, NULL, port, share, "?????",
297 username, workgroup, password, 0, Undefined, NULL);
299 if (!NT_STATUS_IS_OK(nt_status)) {
300 fprintf(stderr, "ERROR: Connection failed with error %s\n", nt_errstr(nt_status));
301 return NULL;
305 * Return the new connection...
308 return (c);
313 * 'smb_print()' - Queue a job for printing using the SMB protocol.
316 static int /* O - 0 = success, non-0 = failure */
317 smb_print(struct cli_state *cli, /* I - SMB connection */
318 char *title, /* I - Title/job name */
319 FILE *fp) /* I - File to print */
321 int fnum; /* File number */
322 int nbytes, /* Number of bytes read */
323 tbytes; /* Total bytes read */
324 char buffer[8192], /* Buffer for copy */
325 *ptr; /* Pointer into tile */
329 * Sanitize the title...
332 for (ptr = title; *ptr; ptr ++)
333 if (!isalnum((int)*ptr) && !isspace((int)*ptr))
334 *ptr = '_';
337 * Open the printer device...
340 if ((fnum = cli_open(cli, title, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE)) == -1)
342 fprintf(stderr, "ERROR: %s opening remote spool %s\n",
343 cli_errstr(cli), title);
344 return (1);
348 * Copy the file to the printer...
351 if (fp != stdin)
352 rewind(fp);
354 tbytes = 0;
356 while ((nbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0)
358 if (cli_write(cli, fnum, 0, buffer, tbytes, nbytes) != nbytes)
360 fprintf(stderr, "ERROR: Error writing spool: %s\n", cli_errstr(cli));
361 break;
364 tbytes += nbytes;
367 if (!cli_close(cli, fnum))
369 fprintf(stderr, "ERROR: %s closing remote spool %s\n",
370 cli_errstr(cli), title);
371 return (1);
373 else
374 return (0);