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 3 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, see <http://www.gnu.org/licenses/>.
28 static void list_devices(void);
29 static struct smbcli_state *smb_connect(const char *, const char *, const char **, const char *, const char *, const char *);
30 static int smb_print(struct smbcli_state *, char *, FILE *);
34 * 'main()' - Main entry for SMB backend.
37 int /* O - Exit status */
38 main(int argc, /* I - Number of command-line arguments */
39 char *argv[]) /* I - Command-line arguments */
41 int i; /* Looping var */
42 int copies; /* Number of copies */
43 char uri[1024], /* URI */
44 *sep, /* Pointer to separator */
45 *password; /* Password */
46 const char *username, /* Username */
47 *server, /* Server name */
48 *printer; /* Printer name */
49 const char *workgroup; /* Workgroup */
50 FILE *fp; /* File to print */
51 int status=0; /* Status of LPD job */
52 struct smbcli_state *cli; /* SMB interface */
53 struct loadparm_context *lp_ctx;
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)) {
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... :)
74 if (argc < 6 || argc > 7)
76 fprintf(stderr, "Usage: %s [DEVICE_URI] job-id user title copies options [file]\n",
78 fputs(" The DEVICE_URI environment variable can also contain the\n", stderr);
79 fputs(" destination printer:\n", stderr);
81 fputs(" smb://[username:password@][workgroup/]server/printer\n", stderr);
86 * If we have 7 arguments, print the file named on the command-line.
87 * Otherwise, print data from stdin...
93 * Print from Copy stdin to a temporary file...
99 else if ((fp = fopen(argv[6], "rb")) == NULL)
101 perror("ERROR: Unable to open print file");
105 copies = atoi(argv[4]);
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);
117 fputs("ERROR: No device URI found in argv[0] or DEVICE_URI environment variable!\n", stderr);
121 uri[sizeof(uri) - 1] = '\0';
124 * Extract the destination from the URI...
127 if ((sep = strrchr_m(uri, '@')) != NULL)
135 * Extract password as needed...
138 if ((password = strchr_m(username, ':')) != NULL)
150 if ((sep = strchr_m(server, '/')) == NULL)
152 fputs("ERROR: Bad URI - need printer name!\n", stderr);
159 if ((sep = strchr_m(printer, '/')) != NULL)
162 * Convert to smb://[username:password@]workgroup/server/printer...
175 * Setup the SAMBA server state...
178 setup_logging(argv[0], DEBUG_STDOUT);
180 lp_ctx = loadparm_init(talloc_autofree_context());
182 if (!lp_load(lp_ctx, dyn_CONFIGFILE)) {
183 fprintf(stderr, "ERROR: Can't load %s - run testparm to debug it\n", lp_config_file());
187 if (workgroup == NULL)
188 workgroup = lp_workgroup(lp_ctx);
192 if ((cli = smb_connect(workgroup, server, lp_smb_ports(lp_ctx), printer, username, password)) == NULL)
194 if (getenv("CLASS") == NULL)
196 fprintf(stderr, "ERROR: Unable to connect to SAMBA host, will retry in 60 seconds...");
201 fprintf(stderr, "ERROR: Unable to connect to SAMBA host, trying next printer...");
209 * Now that we are connected to the server, ignore SIGTERM so that we
210 * can finish out any page data the driver sends (e.g. to eject the
211 * current page... Only ignore SIGTERM if we are printing data from
212 * stdin (otherwise you can't cancel raw jobs...)
216 CatchSignal(SIGTERM, SIG_IGN);
222 for (i = 0; i < copies; i ++)
223 if ((status = smb_print(cli, argv[3] /* title */, fp)) != 0)
229 * Return the queue status...
237 * 'list_devices()' - List the available printers seen on the network...
244 * Eventually, search the local workgroup for available hosts and printers.
247 puts("network smb \"Unknown\" \"Windows Printer via SAMBA\"");
252 * 'smb_connect()' - Return a connection to a server.
255 static struct smbcli_state * /* O - SMB connection */
256 smb_connect(const char *workgroup, /* I - Workgroup */
257 const char *server, /* I - Server */
258 const char **ports, /* I - Ports */
259 const char *share, /* I - Printer */
260 const char *username, /* I - Username */
261 const char *password) /* I - Password */
263 struct smbcli_state *c; /* New connection */
264 char *myname; /* Client name */
268 * Get the names and addresses of the client and server...
271 myname = get_myname();
273 nt_status = smbcli_full_connection(NULL, &c, myname, server, ports, share,
274 NULL, username, workgroup, password, NULL);
277 if (!NT_STATUS_IS_OK(nt_status)) {
278 fprintf(stderr, "ERROR: Connection failed with error %s\n", nt_errstr(nt_status));
283 * Return the new connection...
291 * 'smb_print()' - Queue a job for printing using the SMB protocol.
294 static int /* O - 0 = success, non-0 = failure */
295 smb_print(struct smbcli_state *cli, /* I - SMB connection */
296 char *title, /* I - Title/job name */
297 FILE *fp) /* I - File to print */
299 int fnum; /* File number */
300 int nbytes, /* Number of bytes read */
301 tbytes; /* Total bytes read */
302 char buffer[8192], /* Buffer for copy */
303 *ptr; /* Pointer into tile */
307 * Sanitize the title...
310 for (ptr = title; *ptr; ptr ++)
311 if (!isalnum((int)*ptr) && !isspace((int)*ptr))
315 * Open the printer device...
318 if ((fnum = smbcli_open(cli, title, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE)) == -1)
320 fprintf(stderr, "ERROR: %s opening remote file %s\n",
321 smbcli_errstr(cli), title);
326 * Copy the file to the printer...
334 while ((nbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0)
336 if (smbcli_write(cli, fnum, 0, buffer, tbytes, nbytes) != nbytes)
338 fprintf(stderr, "ERROR: Error writing file: %s\n", smbcli_errstr(cli));
345 if (!smbcli_close(cli, fnum))
347 fprintf(stderr, "ERROR: %s closing remote file %s\n",
348 smbcli_errstr(cli), title);