2 Unix SMB/Netbios implementation.
4 SMB backend for the Common UNIX Printing System ("CUPS")
5 Copyright 1999 by Easy Software Products
6 Copyright Andrew Tridgell 1994-1998
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.
31 extern BOOL in_client
; /* Boolean for client library */
32 extern struct in_addr ipzero
; /* Any address */
39 static void list_devices(void);
40 static struct cli_state
*smb_connect(char *, char *, char *, char *, char *);
41 static int smb_print(struct cli_state
*, char *, FILE *);
45 * 'main()' - Main entry for SMB backend.
48 int /* O - Exit status */
49 main(int argc
, /* I - Number of command-line arguments */
50 char *argv
[]) /* I - Command-line arguments */
52 int i
; /* Looping var */
53 int copies
; /* Number of copies */
54 char uri
[1024], /* URI */
55 *sep
, /* Pointer to separator */
56 *username
, /* Username */
57 *password
, /* Password */
58 *workgroup
, /* Workgroup */
59 *server
, /* Server name */
60 *printer
; /* Printer name */
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)) {
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... :)
84 if (argc
< 6 || argc
> 7)
86 fprintf(stderr
, "Usage: %s [DEVICE_URI] job-id user title copies options [file]\n",
88 fputs(" The DEVICE_URI environment variable can also contain the\n", stderr
);
89 fputs(" destination printer:\n", stderr
);
91 fputs(" smb://[username:password@][workgroup/]server/printer\n", stderr
);
96 * If we have 7 arguments, print the file named on the command-line.
97 * Otherwise, print data from stdin...
103 * Print from Copy stdin to a temporary file...
109 else if ((fp
= fopen(argv
[6], "rb")) == NULL
)
111 perror("ERROR: Unable to open print file");
115 copies
= atoi(argv
[4]);
121 if (strncmp(argv
[0], "smb://", 6) == 0)
122 strncpy(uri
, argv
[0], sizeof(uri
) - 1);
123 else if (getenv("DEVICE_URI") != NULL
)
124 strncpy(uri
, getenv("DEVICE_URI"), sizeof(uri
) - 1);
127 fputs("ERROR: No device URI found in argv[0] or DEVICE_URI environment variable!\n", stderr
);
131 uri
[sizeof(uri
) - 1] = '\0';
134 * Extract the destination from the URI...
137 if ((sep
= strrchr(uri
, '@')) != NULL
)
145 * Extract password as needed...
148 if ((password
= strchr(username
, ':')) != NULL
)
160 if ((sep
= strchr(server
, '/')) == NULL
)
162 fputs("ERROR: Bad URI - need printer name!\n", stderr
);
169 if ((sep
= strchr(printer
, '/')) != NULL
)
172 * Convert to smb://[username:password@]workgroup/server/printer...
185 * Setup the SAMBA server state...
188 setup_logging("smbspool", True
);
191 charset_initialise();
193 in_client
= True
; /* Make sure that we tell lp_load we are */
195 if (!lp_load(CONFIGFILE
, True
, False
, False
))
197 fprintf(stderr
, "ERROR: Can't load %s - run testparm to debug it\n", CONFIGFILE
);
201 if (workgroup
== NULL
)
202 workgroup
= lp_workgroup();
204 codepage_initialise(lp_client_code_page());
210 if ((cli
= smb_connect(workgroup
, server
, printer
, username
, password
)) == NULL
)
212 if (getenv("CLASS") == NULL
)
214 perror("ERROR: Unable to connect to SAMBA host, will retry in 60 seconds...");
219 perror("ERROR: Unable to connect to SAMBA host, trying next printer...");
227 * Now that we are connected to the server, ignore SIGTERM so that we
228 * can finish out any page data the driver sends (e.g. to eject the
229 * current page... Only ignore SIGTERM if we are printing data from
230 * stdin (otherwise you can't cancel raw jobs...)
234 CatchSignal(SIGTERM
, SIG_IGN
);
240 for (i
= 0; i
< copies
; i
++)
241 if ((status
= smb_print(cli
, argv
[3] /* title */, fp
)) != 0)
247 * Return the queue status...
255 * 'list_devices()' - List the available printers seen on the network...
262 * Eventually, search the local workgroup for available hosts and printers.
265 puts("network smb \"Unknown\" \"Windows Printer via SAMBA\"");
270 * 'smb_connect()' - Return a connection to a server.
273 static struct cli_state
* /* O - SMB connection */
274 smb_connect(char *workgroup
, /* I - Workgroup */
275 char *server
, /* I - Server */
276 char *share
, /* I - Printer */
277 char *username
, /* I - Username */
278 char *password
) /* I - Password */
280 struct cli_state
*c
; /* New connection */
281 struct nmb_name called
, /* NMB name of server */
282 calling
; /* NMB name of client */
283 struct in_addr ip
; /* IP address of server */
284 pstring myname
; /* Client name */
288 * Get the names and addresses of the client and server...
295 make_nmb_name(&calling
, myname
, 0x0);
296 make_nmb_name(&called
, server
, 0x20);
299 * Open a new connection to the SMB server...
302 if ((c
= cli_initialise(NULL
)) == NULL
)
304 fputs("ERROR: cli_initialise() failed...\n", stderr
);
308 if (!cli_set_port(c
, SMB_PORT
))
310 fputs("ERROR: cli_set_port() failed...\n", stderr
);
314 if (!cli_connect(c
, server
, &ip
))
316 fputs("ERROR: cli_connect() failed...\n", stderr
);
320 if (!cli_session_request(c
, &calling
, &called
))
322 fputs("ERROR: cli_session_request() failed...\n", stderr
);
328 fputs("ERROR: SMB protocol negotiation failed\n", stderr
);
334 * Do password stuff...
337 if (!cli_session_setup(c
, username
,
338 password
, strlen(password
),
339 password
, strlen(password
),
342 fprintf(stderr
, "ERROR: SMB session setup failed: %s\n", cli_errstr(c
));
346 if (!cli_send_tconX(c
, share
, "?????",
347 password
, strlen(password
)+1))
349 fprintf(stderr
, "ERROR: SMB tree connect failed: %s\n", cli_errstr(c
));
355 * Return the new connection...
363 * 'smb_print()' - Queue a job for printing using the SMB protocol.
366 static int /* O - 0 = success, non-0 = failure */
367 smb_print(struct cli_state
*cli
, /* I - SMB connection */
368 char *title
, /* I - Title/job name */
369 FILE *fp
) /* I - File to print */
371 int fnum
; /* File number */
372 int nbytes
, /* Number of bytes read */
373 tbytes
; /* Total bytes read */
374 char buffer
[8192], /* Buffer for copy */
375 *ptr
; /* Pointer into tile */
379 * Sanitize the title...
382 for (ptr
= title
; *ptr
; ptr
++)
383 if (!isalnum((int)*ptr
) && !isspace((int)*ptr
))
387 * Open the printer device...
390 if ((fnum
= cli_open(cli
, title
, O_RDWR
| O_CREAT
| O_TRUNC
, DENY_NONE
)) == -1)
392 fprintf(stderr
, "ERROR: %s opening remote file %s\n",
393 cli_errstr(cli
), title
);
398 * Copy the file to the printer...
406 while ((nbytes
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0)
408 if (cli_write(cli
, fnum
, 0, buffer
, tbytes
, nbytes
) != nbytes
)
410 fprintf(stderr
, "ERROR: Error writing file: %s\n", cli_errstr(cli
));
417 if (!cli_close(cli
, fnum
))
419 fprintf(stderr
, "ERROR: %s closing remote file %s\n",
420 cli_errstr(cli
), title
);