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.
29 extern BOOL in_client; /* Boolean for client library */
36 static void list_devices(void);
37 static struct cli_state *smb_connect(const char *, const char *, const char *, const char *, const char *);
38 static int smb_print(struct cli_state *, char *, FILE *);
42 * 'main()' - Main entry for SMB backend.
45 int /* O - Exit status */
46 main(int argc, /* I - Number of command-line arguments */
47 char *argv[]) /* I - Command-line arguments */
49 int i; /* Looping var */
50 int copies; /* Number of copies */
51 char uri[1024], /* URI */
52 *sep, /* Pointer to separator */
53 *password; /* Password */
54 const char *username, /* Username */
55 *server, /* Server name */
56 *printer; /* Printer name */
57 const char *workgroup; /* Workgroup */
58 FILE *fp; /* File to print */
59 int status=0; /* Status of LPD job */
60 struct cli_state *cli; /* SMB interface */
62 /* we expect the URI in argv[0]. Detect the case where it is in argv[1] and cope */
63 if (argc > 2 && strncmp(argv[0],"smb://", 6) && !strncmp(argv[1],"smb://", 6)) {
71 * NEW! In CUPS 1.1 the backends are run with no arguments to list the
72 * available devices. These can be devices served by this backend
73 * or any other backends (i.e. you can have an SNMP backend that
74 * is only used to enumerate the available network printers... :)
81 if (argc < 6 || argc > 7)
83 fprintf(stderr, "Usage: %s [DEVICE_URI] job-id user title copies options [file]\n",
85 fputs(" The DEVICE_URI environment variable can also contain the\n", stderr);
86 fputs(" destination printer:\n", stderr);
88 fputs(" smb://[username:password@][workgroup/]server/printer\n", stderr);
93 * If we have 7 arguments, print the file named on the command-line.
94 * Otherwise, print data from stdin...
100 * Print from Copy stdin to a temporary file...
106 else if ((fp = fopen(argv[6], "rb")) == NULL)
108 perror("ERROR: Unable to open print file");
112 copies = atoi(argv[4]);
118 if (strncmp(argv[0], "smb://", 6) == 0)
119 strncpy(uri, argv[0], sizeof(uri) - 1);
120 else if (getenv("DEVICE_URI") != NULL)
121 strncpy(uri, getenv("DEVICE_URI"), sizeof(uri) - 1);
124 fputs("ERROR: No device URI found in argv[0] or DEVICE_URI environment variable!\n", stderr);
128 uri[sizeof(uri) - 1] = '\0';
131 * Extract the destination from the URI...
134 if ((sep = strrchr_m(uri, '@')) != NULL)
142 * Extract password as needed...
145 if ((password = strchr_m(username, ':')) != NULL)
157 if ((sep = strchr_m(server, '/')) == NULL)
159 fputs("ERROR: Bad URI - need printer name!\n", stderr);
166 if ((sep = strchr_m(printer, '/')) != NULL)
169 * Convert to smb://[username:password@]workgroup/server/printer...
182 * Setup the SAMBA server state...
185 setup_logging("smbspool", DEBUG_STDOUT);
187 in_client = True; /* Make sure that we tell lp_load we are */
189 if (!lp_load(dyn_CONFIGFILE, True, False, False))
191 fprintf(stderr, "ERROR: Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
195 if (workgroup == NULL)
196 workgroup = lp_workgroup();
202 if ((cli = smb_connect(workgroup, server, printer, username, password)) == NULL)
204 if (getenv("CLASS") == NULL)
206 fprintf(stderr, "ERROR: Unable to connect to SAMBA host, will retry in 60 seconds...");
211 fprintf(stderr, "ERROR: Unable to connect to SAMBA host, trying next printer...");
219 * Now that we are connected to the server, ignore SIGTERM so that we
220 * can finish out any page data the driver sends (e.g. to eject the
221 * current page... Only ignore SIGTERM if we are printing data from
222 * stdin (otherwise you can't cancel raw jobs...)
226 CatchSignal(SIGTERM, SIG_IGN);
232 for (i = 0; i < copies; i ++)
233 if ((status = smb_print(cli, argv[3] /* title */, fp)) != 0)
239 * Return the queue status...
247 * 'list_devices()' - List the available printers seen on the network...
254 * Eventually, search the local workgroup for available hosts and printers.
257 puts("network smb \"Unknown\" \"Windows Printer via SAMBA\"");
262 * 'smb_connect()' - Return a connection to a server.
265 static struct cli_state * /* O - SMB connection */
266 smb_connect(const char *workgroup, /* I - Workgroup */
267 const char *server, /* I - Server */
268 const char *share, /* I - Printer */
269 const char *username, /* I - Username */
270 const char *password) /* I - Password */
272 struct cli_state *c; /* New connection */
273 char *myname; /* Client name */
277 * Get the names and addresses of the client and server...
280 myname = get_myname();
282 nt_status = cli_full_connection(&c, myname, server, NULL, 0, share, "?????",
283 username, workgroup, password, 0, NULL);
286 if (!NT_STATUS_IS_OK(nt_status)) {
287 fprintf(stderr, "ERROR: Connection failed with error %s\n", nt_errstr(nt_status));
292 * Return the new connection...
300 * 'smb_print()' - Queue a job for printing using the SMB protocol.
303 static int /* O - 0 = success, non-0 = failure */
304 smb_print(struct cli_state *cli, /* I - SMB connection */
305 char *title, /* I - Title/job name */
306 FILE *fp) /* I - File to print */
308 int fnum; /* File number */
309 int nbytes, /* Number of bytes read */
310 tbytes; /* Total bytes read */
311 char buffer[8192], /* Buffer for copy */
312 *ptr; /* Pointer into tile */
316 * Sanitize the title...
319 for (ptr = title; *ptr; ptr ++)
320 if (!isalnum((int)*ptr) && !isspace((int)*ptr))
324 * Open the printer device...
327 if ((fnum = cli_open(cli, title, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE)) == -1)
329 fprintf(stderr, "ERROR: %s opening remote file %s\n",
330 cli_errstr(cli), title);
335 * Copy the file to the printer...
343 while ((nbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0)
345 if (cli_write(cli, fnum, 0, buffer, tbytes, nbytes) != nbytes)
347 fprintf(stderr, "ERROR: Error writing file: %s\n", cli_errstr(cli));
354 if (!cli_close(cli, fnum))
356 fprintf(stderr, "ERROR: %s closing remote file %s\n",
357 cli_errstr(cli), title);