2 Unix SMB/CIFS implementation.
3 SMB backend for the Common UNIX Printing System ("CUPS")
5 Copyright (C) Michael R Sweet 1999
6 Copyright (C) Andrew Tridgell 1994-1998
7 Copyright (C) Andrew Bartlett 2002
8 Copyright (C) Rodrigo Fernandez-Vizarra 2005
9 Copyright (C) James Peach 2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "system/filesys.h"
27 #include "system/passwd.h"
28 #include "system/kerberos.h"
29 #include "libsmb/libsmb.h"
30 #include "lib/param/param.h"
31 #include "lib/krb5_wrap/krb5_samba.h"
34 * Starting with CUPS 1.3, Kerberos support is provided by cupsd including
35 * the forwarding of user credentials via the authenticated session between
36 * user and server and the KRB5CCNAME environment variable which will point
37 * to a temporary file or an in-memory representation depending on the version
38 * of Kerberos you use. As a result, all of the ticket code that used to
39 * live here has been removed, and we depend on the user session (if you
40 * run smbspool by hand) or cupsd to provide the necessary Kerberos info.
42 * Also, the AUTH_USERNAME and AUTH_PASSWORD environment variables provide
43 * for per-job authentication for non-Kerberized printing. We use those
44 * if there is no username and password specified in the device URI.
46 * Finally, if we have an authentication failure we return exit code 2
47 * which tells CUPS to hold the job for authentication and bug the user
48 * to get the necessary credentials.
51 #define MAX_RETRY_CONNECT 3
64 static int get_exit_code(struct cli_state * cli, NTSTATUS nt_status);
65 static void list_devices(void);
66 static struct cli_state *smb_complete_connection(const char *, const char *,
67 int, const char *, const char *, const char *, const char *, int, bool *need_auth);
68 static struct cli_state *smb_connect(const char *, const char *, int, const
69 char *, const char *, const char *, const char *, bool *need_auth);
70 static int smb_print(struct cli_state *, const char *, FILE *);
71 static char *uri_unescape_alloc(const char *);
73 static bool smb_encrypt;
76 static const char *auth_info_required;
79 * 'main()' - Main entry for SMB backend.
82 int /* O - Exit status */
83 main(int argc, /* I - Number of command-line arguments */
85 { /* I - Command-line arguments */
86 int i; /* Looping var */
87 int copies; /* Number of copies */
88 int port; /* Port number */
89 char uri[1024], /* URI */
90 *sep, /* Pointer to separator */
91 *tmp, *tmp2, /* Temp pointers to do escaping */
92 *password; /* Password */
93 char *username, /* Username */
94 *server, /* Server name */
95 *printer;/* Printer name */
96 const char *workgroup; /* Workgroup */
97 FILE *fp; /* File to print */
98 int status = 1; /* Status of LPD job */
99 struct cli_state *cli; /* SMB interface */
100 char empty_str[] = "";
102 bool need_auth = true;
103 const char *dev_uri = NULL;
104 const char *env = NULL;
105 const char *config_file = NULL;
106 TALLOC_CTX *frame = talloc_stackframe();
107 const char *print_user = NULL;
108 const char *print_title = NULL;
109 const char *print_file = NULL;
110 const char *print_copies = NULL;
116 * NEW! In CUPS 1.1 the backends are run with no arguments
117 * to list the available devices. These can be devices
118 * served by this backend or any other backends (i.e. you
119 * can have an SNMP backend that is only used to enumerate
120 * the available network printers... :)
129 * We need at least 5 options if the DEVICE_URI is passed via an env
130 * variable and printing data comes via stdin.
131 * We don't accept more than 7 options in total, including optional.
133 if (argc < 5 || argc > 8) {
135 "Usage: %s [DEVICE_URI] job-id user title copies options [file]\n"
136 " The DEVICE_URI environment variable can also contain the\n"
137 " destination printer:\n"
139 " smb://[username:password@][workgroup/]server[:port]/printer\n",
145 * Find out if we have the device_uri in the command line.
147 * If we are started as a CUPS backend argv[0] is normally the
152 * smbspool <uri> <job> <user> <title> <copies> <options> <file>
158 print_user = argv[3];
159 print_title = argv[4];
160 print_copies = argv[5];
161 print_file = argv[7];
162 } else if (argc == 7) {
167 * <uri> <job> <user> <title> <copies> <options> <file>
168 * smbspool <uri> <job> <user> <title> <copies> <options>
169 * smbspool <job> <user> <title> <copies> <options> <file> | DEVICE_URI
171 cmp1 = strncmp(argv[0], "smb://", 6);
172 cmp2 = strncmp(argv[1], "smb://", 6);
176 * <uri> <job> <user> <title> <copies> <options> <file>
181 print_user = argv[2];
182 print_title = argv[3];
183 print_copies = argv[4];
184 print_file = argv[6];
185 } else if (cmp2 == 0) {
187 * smbspool <uri> <job> <user> <title> <copies> <options>
192 print_user = argv[3];
193 print_title = argv[4];
194 print_copies = argv[5];
198 * smbspool <job> <user> <title> <copies> <options> <file> | DEVICE_URI
201 print_user = argv[2];
202 print_title = argv[3];
203 print_copies = argv[4];
204 print_file = argv[6];
206 } else if (argc == 6) {
208 * <uri> <job> <user> <title> <copies> <options>
209 * smbspool <job> <user> <title> <copies> <options> | DEVICE_URI
212 cmp = strncmp(argv[0], "smb://", 6);
217 print_user = argv[2];
218 print_title = argv[3];
219 print_copies = argv[4];
222 if (print_file != NULL) {
225 fp = fopen(print_file, "rb");
227 perror("ERROR: Unable to open print file");
231 copies = strtol(print_copies, &endp, 10);
232 if (print_copies == endp) {
233 perror("ERROR: Unable to determine number of copies");
244 if (dev_uri == NULL) {
245 env = getenv("DEVICE_URI");
246 if (env != NULL && env[0] != '\0') {
251 if (dev_uri == NULL) {
253 "ERROR: No valid device URI has been specified\n");
257 cmp = strncmp(dev_uri, "smb://", 6);
260 "ERROR: No valid device URI has been specified\n");
263 len = snprintf(uri, sizeof(uri), "%s", dev_uri);
264 if (len >= sizeof(uri)) {
266 "ERROR: The URI is too long.\n");
270 auth_info_required = getenv("AUTH_INFO_REQUIRED");
271 if (auth_info_required == NULL) {
272 auth_info_required = "none";
276 * Extract the destination from the URI...
279 if ((sep = strrchr_m(uri, '@')) != NULL) {
283 /* username is in tmp */
288 * Extract password as needed...
291 if ((tmp2 = strchr_m(tmp, ':')) != NULL) {
293 password = uri_unescape_alloc(tmp2);
295 password = empty_str;
297 username = uri_unescape_alloc(tmp);
299 if ((username = getenv("AUTH_USERNAME")) == NULL) {
300 username = empty_str;
303 if ((password = getenv("AUTH_PASSWORD")) == NULL) {
304 password = empty_str;
310 if (password != empty_str) {
311 auth_info_required = "username,password";
316 if ((sep = strchr_m(tmp, '/')) == NULL) {
317 fputs("ERROR: Bad URI - need printer name!\n", stderr);
324 if ((sep = strchr_m(tmp2, '/')) != NULL) {
326 * Convert to smb://[username:password@]workgroup/server/printer...
331 workgroup = uri_unescape_alloc(tmp);
332 server = uri_unescape_alloc(tmp2);
333 printer = uri_unescape_alloc(sep);
336 server = uri_unescape_alloc(tmp);
337 printer = uri_unescape_alloc(tmp2);
340 if ((sep = strrchr_m(server, ':')) != NULL) {
349 * Setup the SAMBA server state...
352 setup_logging("smbspool", DEBUG_STDERR);
356 config_file = lp_default_path();
357 if (!lp_load_client(config_file)) {
359 "ERROR: Can't load %s - run testparm to debug it\n",
364 if (workgroup == NULL) {
365 workgroup = lp_workgroup();
371 cli = smb_connect(workgroup,
382 } else if (getenv("CLASS") == NULL) {
383 fprintf(stderr, "ERROR: Unable to connect to CIFS host, will retry in 60 seconds...\n");
387 fprintf(stderr, "ERROR: Unable to connect to CIFS host, trying next printer...\n");
391 } while ((cli == NULL) && (tries < MAX_RETRY_CONNECT));
394 fprintf(stderr, "ERROR: Unable to connect to CIFS host after (tried %d times)\n", tries);
399 * Now that we are connected to the server, ignore SIGTERM so that we
400 * can finish out any page data the driver sends (e.g. to eject the
401 * current page... Only ignore SIGTERM if we are printing data from
402 * stdin (otherwise you can't cancel raw jobs...)
406 CatchSignal(SIGTERM, SIG_IGN);
413 for (i = 0; i < copies; i++) {
414 status = smb_print(cli, print_title, fp);
423 * Return the queue status...
434 * 'get_exit_code()' - Get the backend exit code based on the current error.
438 get_exit_code(struct cli_state * cli,
443 /* List of NTSTATUS errors that are considered
444 * authentication errors
446 static const NTSTATUS auth_errors[] =
448 NT_STATUS_ACCESS_DENIED, NT_STATUS_ACCESS_VIOLATION,
449 NT_STATUS_SHARING_VIOLATION, NT_STATUS_PRIVILEGE_NOT_HELD,
450 NT_STATUS_INVALID_ACCOUNT_NAME, NT_STATUS_NO_SUCH_USER,
451 NT_STATUS_WRONG_PASSWORD, NT_STATUS_LOGON_FAILURE,
452 NT_STATUS_ACCOUNT_RESTRICTION, NT_STATUS_INVALID_LOGON_HOURS,
453 NT_STATUS_PASSWORD_EXPIRED, NT_STATUS_ACCOUNT_DISABLED
457 fprintf(stderr, "DEBUG: get_exit_code(cli=%p, nt_status=%s [%x])\n",
458 cli, nt_errstr(nt_status), NT_STATUS_V(nt_status));
460 for (i = 0; i < ARRAY_SIZE(auth_errors); i++) {
461 if (!NT_STATUS_EQUAL(nt_status, auth_errors[i])) {
466 fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
470 * 2 = authentication required...
486 * 'list_devices()' - List the available printers seen on the network...
493 * Eventually, search the local workgroup for available hosts and printers.
496 puts("network smb \"Unknown\" \"Windows Printer via SAMBA\"");
500 static struct cli_state *
501 smb_complete_connection(const char *myname,
504 const char *username,
505 const char *password,
506 const char *workgroup,
511 struct cli_state *cli; /* New connection */
513 struct cli_credentials *creds = NULL;
514 bool use_kerberos = false;
516 /* Start the SMB connection */
518 nt_status = cli_start_connection(&cli, myname, server, NULL, port,
519 SMB_SIGNING_DEFAULT, flags);
520 if (!NT_STATUS_IS_OK(nt_status)) {
521 fprintf(stderr, "ERROR: Connection failed: %s\n", nt_errstr(nt_status));
526 * We pretty much guarantee password must be valid or a pointer to a
534 if (flags & CLI_FULL_CONNECTION_USE_KERBEROS) {
535 auth_info_required = "negotiate";
539 creds = cli_session_creds_init(cli,
545 false, /* fallback_after_kerberos */
546 false, /* use_ccache */
547 false); /* password_is_nt_hash */
549 fprintf(stderr, "ERROR: cli_session_creds_init failed\n");
554 nt_status = cli_session_setup_creds(cli, creds);
555 if (!NT_STATUS_IS_OK(nt_status)) {
556 fprintf(stderr, "ERROR: Session setup failed: %s\n", nt_errstr(nt_status));
558 if (get_exit_code(cli, nt_status) == 2) {
567 nt_status = cli_tree_connect_creds(cli, share, "?????", creds);
568 if (!NT_STATUS_IS_OK(nt_status)) {
569 fprintf(stderr, "ERROR: Tree connect failed (%s)\n",
570 nt_errstr(nt_status));
572 if (get_exit_code(cli, nt_status) == 2) {
581 /* Need to work out how to specify this on the URL. */
583 if (!cli_cm_force_encryption_creds(cli, creds, share)) {
584 fprintf(stderr, "ERROR: encryption setup failed\n");
594 static bool kerberos_ccache_is_valid(void) {
596 const char *ccache_name = NULL;
597 krb5_ccache ccache = NULL;
598 krb5_error_code code;
600 code = smb_krb5_init_context_common(&ctx);
602 DBG_ERR("kerberos init context failed (%s)\n",
603 error_message(code));
607 ccache_name = krb5_cc_default_name(ctx);
608 if (ccache_name == NULL) {
609 krb5_free_context(ctx);
613 code = krb5_cc_resolve(ctx, ccache_name, &ccache);
615 krb5_free_context(ctx);
618 krb5_principal default_princ = NULL;
620 code = krb5_cc_get_principal(ctx,
624 krb5_cc_close(ctx, ccache);
625 krb5_free_context(ctx);
628 krb5_free_principal(ctx, default_princ);
630 krb5_cc_close(ctx, ccache);
631 krb5_free_context(ctx);
637 * 'smb_connect()' - Return a connection to a server.
640 static struct cli_state * /* O - SMB connection */
641 smb_connect(const char *workgroup, /* I - Workgroup */
642 const char *server, /* I - Server */
643 const int port, /* I - Port */
644 const char *share, /* I - Printer */
645 const char *username, /* I - Username */
646 const char *password, /* I - Password */
647 const char *jobusername, /* I - User who issued the print job */
649 { /* O - Need authentication? */
650 struct cli_state *cli; /* New connection */
651 char *myname = NULL; /* Client name */
655 * Get the names and addresses of the client and server...
657 myname = get_myname(talloc_tos());
663 * See if we have a username first. This is for backwards compatible
664 * behavior with 3.0.14a
667 if (username != NULL && username[0] != '\0') {
668 if (kerberos_ccache_is_valid()) {
673 cli = smb_complete_connection(myname,
683 fputs("DEBUG: Connected with username/password...\n", stderr);
689 * Try to use the user kerberos credentials (if any) to authenticate
691 cli = smb_complete_connection(myname, server, port, jobusername, "",
693 CLI_FULL_CONNECTION_USE_KERBEROS, need_auth);
696 fputs("DEBUG: Connected using Kerberos...\n", stderr);
700 /* give a chance for a passwordless NTLMSSP session setup */
701 pwd = getpwuid(geteuid());
706 cli = smb_complete_connection(myname, server, port, pwd->pw_name, "",
707 workgroup, share, 0, need_auth);
710 fputs("DEBUG: Connected with NTLMSSP...\n", stderr);
715 * last try. Use anonymous authentication
718 cli = smb_complete_connection(myname, server, port, "", "",
719 workgroup, share, 0, need_auth);
721 * Return the new connection...
729 * 'smb_print()' - Queue a job for printing using the SMB protocol.
732 static int /* O - 0 = success, non-0 = failure */
733 smb_print(struct cli_state * cli, /* I - SMB connection */
734 const char *print_title, /* I - Title/job name */
736 { /* I - File to print */
737 uint16_t fnum; /* File number */
738 int nbytes, /* Number of bytes read */
739 tbytes; /* Total bytes read */
740 char buffer[8192], /* Buffer for copy */
741 *ptr; /* Pointer into title */
742 char title[1024] = {0};
748 * Sanitize the title...
750 len = snprintf(title, sizeof(title), "%s", print_title);
751 if (len != strlen(print_title)) {
755 for (ptr = title; *ptr; ptr++) {
756 if (!isalnum((int) *ptr) && !isspace((int) *ptr)) {
762 * Open the printer device...
765 nt_status = cli_open(cli, title, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE,
767 if (!NT_STATUS_IS_OK(nt_status)) {
768 fprintf(stderr, "ERROR: %s opening remote spool %s\n",
769 nt_errstr(nt_status), title);
770 return get_exit_code(cli, nt_status);
774 * Copy the file to the printer...
782 while ((nbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
785 status = cli_writeall(cli, fnum, 0, (uint8_t *)buffer,
786 tbytes, nbytes, NULL);
787 if (!NT_STATUS_IS_OK(status)) {
788 int ret = get_exit_code(cli, status);
789 fprintf(stderr, "ERROR: Error writing spool: %s\n",
791 fprintf(stderr, "DEBUG: Returning status %d...\n",
793 cli_close(cli, fnum);
800 nt_status = cli_close(cli, fnum);
801 if (!NT_STATUS_IS_OK(nt_status)) {
802 fprintf(stderr, "ERROR: %s closing remote spool %s\n",
803 nt_errstr(nt_status), title);
804 return get_exit_code(cli, nt_status);
811 uri_unescape_alloc(const char *uritok)
815 ret = (char *) SMB_STRDUP(uritok);
820 end = rfc1738_unescape(ret);