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(NTSTATUS nt_status
);
65 static void list_devices(void);
67 smb_connect(struct cli_state
**output_cli
,
68 const char *workgroup
,
74 const char *jobusername
);
75 static int smb_print(struct cli_state
*, const char *, FILE *);
76 static char *uri_unescape_alloc(const char *);
78 static bool smb_encrypt
;
81 static const char *auth_info_required
;
84 * 'main()' - Main entry for SMB backend.
87 int /* O - Exit status */
88 main(int argc
, /* I - Number of command-line arguments */
90 { /* I - Command-line arguments */
91 int i
; /* Looping var */
92 int copies
; /* Number of copies */
93 int port
; /* Port number */
94 char uri
[1024], /* URI */
95 *sep
, /* Pointer to separator */
96 *tmp
, *tmp2
; /* Temp pointers to do escaping */
97 const char *password
= NULL
; /* Password */
98 const char *username
= NULL
; /* Username */
99 char *server
, /* Server name */
100 *printer
;/* Printer name */
101 const char *workgroup
; /* Workgroup */
102 FILE *fp
; /* File to print */
103 int status
= 1; /* Status of LPD job */
104 NTSTATUS nt_status
= NT_STATUS_UNSUCCESSFUL
;
105 struct cli_state
*cli
= NULL
; /* SMB interface */
107 const char *dev_uri
= NULL
;
108 const char *env
= NULL
;
109 const char *config_file
= NULL
;
110 TALLOC_CTX
*frame
= talloc_stackframe();
111 const char *print_user
= NULL
;
112 const char *print_title
= NULL
;
113 const char *print_file
= NULL
;
114 const char *print_copies
= NULL
;
120 * NEW! In CUPS 1.1 the backends are run with no arguments
121 * to list the available devices. These can be devices
122 * served by this backend or any other backends (i.e. you
123 * can have an SNMP backend that is only used to enumerate
124 * the available network printers... :)
133 * We need at least 5 options if the DEVICE_URI is passed via an env
134 * variable and printing data comes via stdin.
135 * We don't accept more than 7 options in total, including optional.
137 if (argc
< 5 || argc
> 8) {
139 "Usage: %s [DEVICE_URI] job-id user title copies options [file]\n"
140 " The DEVICE_URI environment variable can also contain the\n"
141 " destination printer:\n"
143 " smb://[username:password@][workgroup/]server[:port]/printer\n",
149 * Find out if we have the device_uri in the command line.
151 * If we are started as a CUPS backend argv[0] is normally the
156 * smbspool <uri> <job> <user> <title> <copies> <options> <file>
162 print_user
= argv
[3];
163 print_title
= argv
[4];
164 print_copies
= argv
[5];
165 print_file
= argv
[7];
166 } else if (argc
== 7) {
171 * <uri> <job> <user> <title> <copies> <options> <file>
172 * smbspool <uri> <job> <user> <title> <copies> <options>
173 * smbspool <job> <user> <title> <copies> <options> <file> | DEVICE_URI
175 cmp1
= strncmp(argv
[0], "smb://", 6);
176 cmp2
= strncmp(argv
[1], "smb://", 6);
180 * <uri> <job> <user> <title> <copies> <options> <file>
185 print_user
= argv
[2];
186 print_title
= argv
[3];
187 print_copies
= argv
[4];
188 print_file
= argv
[6];
189 } else if (cmp2
== 0) {
191 * smbspool <uri> <job> <user> <title> <copies> <options>
196 print_user
= argv
[3];
197 print_title
= argv
[4];
198 print_copies
= argv
[5];
202 * smbspool <job> <user> <title> <copies> <options> <file> | DEVICE_URI
205 print_user
= argv
[2];
206 print_title
= argv
[3];
207 print_copies
= argv
[4];
208 print_file
= argv
[6];
210 } else if (argc
== 6) {
212 * <uri> <job> <user> <title> <copies> <options>
213 * smbspool <job> <user> <title> <copies> <options> | DEVICE_URI
216 cmp
= strncmp(argv
[0], "smb://", 6);
221 print_user
= argv
[2];
222 print_title
= argv
[3];
223 print_copies
= argv
[4];
226 if (print_file
!= NULL
) {
229 fp
= fopen(print_file
, "rb");
232 "ERROR: Unable to open print file: %s",
237 copies
= strtol(print_copies
, &endp
, 10);
238 if (print_copies
== endp
) {
239 perror("ERROR: Unable to determine number of copies");
250 * The URI in argv[0] is sanitized to remove username/password, so
251 * use DEVICE_URI if available. Otherwise keep the URI already
252 * discovered in argv.
254 env
= getenv("DEVICE_URI");
255 if (env
!= NULL
&& env
[0] != '\0') {
259 if (dev_uri
== NULL
) {
261 "ERROR: No valid device URI has been specified\n");
265 cmp
= strncmp(dev_uri
, "smb://", 6);
268 "ERROR: No valid device URI has been specified\n");
271 len
= snprintf(uri
, sizeof(uri
), "%s", dev_uri
);
272 if (len
>= sizeof(uri
)) {
274 "ERROR: The URI is too long.\n");
278 auth_info_required
= getenv("AUTH_INFO_REQUIRED");
279 if (auth_info_required
== NULL
) {
280 auth_info_required
= "samba";
284 * Extract the destination from the URI...
287 if ((sep
= strrchr_m(uri
, '@')) != NULL
) {
291 /* username is in tmp */
296 * Extract password as needed...
299 if ((tmp2
= strchr_m(tmp
, ':')) != NULL
) {
301 password
= uri_unescape_alloc(tmp2
);
303 username
= uri_unescape_alloc(tmp
);
305 env
= getenv("AUTH_USERNAME");
306 if (env
!= NULL
&& strlen(env
) > 0) {
310 env
= getenv("AUTH_PASSWORD");
311 if (env
!= NULL
&& strlen(env
) > 0) {
318 if (password
!= NULL
) {
319 auth_info_required
= "username,password";
324 if ((sep
= strchr_m(tmp
, '/')) == NULL
) {
325 fputs("ERROR: Bad URI - need printer name!\n", stderr
);
332 if ((sep
= strchr_m(tmp2
, '/')) != NULL
) {
334 * Convert to smb://[username:password@]workgroup/server/printer...
339 workgroup
= uri_unescape_alloc(tmp
);
340 server
= uri_unescape_alloc(tmp2
);
341 printer
= uri_unescape_alloc(sep
);
344 server
= uri_unescape_alloc(tmp
);
345 printer
= uri_unescape_alloc(tmp2
);
348 if ((sep
= strrchr_m(server
, ':')) != NULL
) {
357 * Setup the SAMBA server state...
360 setup_logging("smbspool", DEBUG_STDERR
);
364 config_file
= lp_default_path();
365 if (!lp_load_client(config_file
)) {
367 "ERROR: Can't load %s - run testparm to debug it\n",
372 if (workgroup
== NULL
) {
373 workgroup
= lp_workgroup();
379 nt_status
= smb_connect(&cli
,
387 if (!NT_STATUS_IS_OK(nt_status
)) {
388 status
= get_exit_code(nt_status
);
391 "DEBUG: Unable to connect to CIFS "
393 nt_errstr(nt_status
));
395 } else if (getenv("CLASS") == NULL
) {
397 "ERROR: Unable to connect to CIFS "
398 "host: %s. Will retry in 60 "
400 nt_errstr(nt_status
));
405 "ERROR: Unable to connect to CIFS "
406 "host: %s. Trying next printer...\n",
407 nt_errstr(nt_status
));
411 } while (!NT_STATUS_IS_OK(nt_status
) && (tries
< MAX_RETRY_CONNECT
));
414 fprintf(stderr
, "ERROR: Unable to connect to CIFS host after (tried %d times)\n", tries
);
419 * Now that we are connected to the server, ignore SIGTERM so that we
420 * can finish out any page data the driver sends (e.g. to eject the
421 * current page... Only ignore SIGTERM if we are printing data from
422 * stdin (otherwise you can't cancel raw jobs...)
426 CatchSignal(SIGTERM
, SIG_IGN
);
433 for (i
= 0; i
< copies
; i
++) {
434 status
= smb_print(cli
, print_title
, fp
);
443 * Return the queue status...
454 * 'get_exit_code()' - Get the backend exit code based on the current error.
458 get_exit_code(NTSTATUS nt_status
)
462 /* List of NTSTATUS errors that are considered
463 * authentication errors
465 static const NTSTATUS auth_errors
[] =
467 NT_STATUS_ACCESS_DENIED
,
468 NT_STATUS_ACCESS_VIOLATION
,
469 NT_STATUS_ACCOUNT_DISABLED
,
470 NT_STATUS_ACCOUNT_LOCKED_OUT
,
471 NT_STATUS_ACCOUNT_RESTRICTION
,
472 NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
,
473 NT_STATUS_INVALID_ACCOUNT_NAME
,
474 NT_STATUS_INVALID_COMPUTER_NAME
,
475 NT_STATUS_INVALID_LOGON_HOURS
,
476 NT_STATUS_INVALID_WORKSTATION
,
477 NT_STATUS_LOGON_FAILURE
,
478 NT_STATUS_NO_SUCH_USER
,
479 NT_STATUS_NO_SUCH_DOMAIN
,
480 NT_STATUS_NO_LOGON_SERVERS
,
481 NT_STATUS_PASSWORD_EXPIRED
,
482 NT_STATUS_PRIVILEGE_NOT_HELD
,
483 NT_STATUS_SHARING_VIOLATION
,
484 NT_STATUS_WRONG_PASSWORD
,
489 "DEBUG: get_exit_code(nt_status=%s [%x])\n",
490 nt_errstr(nt_status
), NT_STATUS_V(nt_status
));
492 for (i
= 0; i
< ARRAY_SIZE(auth_errors
); i
++) {
493 if (!NT_STATUS_EQUAL(nt_status
, auth_errors
[i
])) {
497 fprintf(stderr
, "ATTR: auth-info-required=%s\n", auth_info_required
);
500 * 2 = authentication required...
516 * 'list_devices()' - List the available printers seen on the network...
523 * Eventually, search the local workgroup for available hosts and printers.
526 puts("network smb \"Unknown\" \"Windows Printer via SAMBA\"");
531 smb_complete_connection(struct cli_state
**output_cli
,
535 const char *username
,
536 const char *password
,
537 const char *workgroup
,
540 bool fallback_after_kerberos
)
542 struct cli_state
*cli
; /* New connection */
544 struct cli_credentials
*creds
= NULL
;
546 /* Start the SMB connection */
547 nt_status
= cli_start_connection(&cli
, myname
, server
, NULL
, port
,
548 SMB_SIGNING_DEFAULT
, 0);
549 if (!NT_STATUS_IS_OK(nt_status
)) {
550 fprintf(stderr
, "ERROR: Connection failed: %s\n", nt_errstr(nt_status
));
554 creds
= cli_session_creds_init(cli
,
560 fallback_after_kerberos
,
561 false, /* use_ccache */
562 false); /* password_is_nt_hash */
564 fprintf(stderr
, "ERROR: cli_session_creds_init failed\n");
566 return NT_STATUS_NO_MEMORY
;
569 nt_status
= cli_session_setup_creds(cli
, creds
);
570 if (!NT_STATUS_IS_OK(nt_status
)) {
571 fprintf(stderr
, "ERROR: Session setup failed: %s\n", nt_errstr(nt_status
));
578 nt_status
= cli_tree_connect_creds(cli
, share
, "?????", creds
);
579 if (!NT_STATUS_IS_OK(nt_status
)) {
580 fprintf(stderr
, "ERROR: Tree connect failed (%s)\n",
581 nt_errstr(nt_status
));
592 static bool kerberos_ccache_is_valid(void) {
594 const char *ccache_name
= NULL
;
595 krb5_ccache ccache
= NULL
;
596 krb5_error_code code
;
598 code
= smb_krb5_init_context_common(&ctx
);
600 DBG_ERR("kerberos init context failed (%s)\n",
601 error_message(code
));
605 ccache_name
= krb5_cc_default_name(ctx
);
606 if (ccache_name
== NULL
) {
607 DBG_ERR("Failed to get default ccache name\n");
608 krb5_free_context(ctx
);
612 code
= krb5_cc_resolve(ctx
, ccache_name
, &ccache
);
614 DBG_ERR("Failed to resolve ccache name: %s\n",
616 krb5_free_context(ctx
);
619 krb5_principal default_princ
= NULL
;
620 char *princ_name
= NULL
;
622 code
= krb5_cc_get_principal(ctx
,
626 DBG_ERR("Failed to get default principal from "
629 krb5_cc_close(ctx
, ccache
);
630 krb5_free_context(ctx
);
634 code
= krb5_unparse_name(ctx
,
639 "DEBUG: Try to authenticate as %s\n",
641 krb5_free_unparsed_name(ctx
, princ_name
);
643 krb5_free_principal(ctx
, default_princ
);
645 krb5_cc_close(ctx
, ccache
);
646 krb5_free_context(ctx
);
652 * 'smb_connect()' - Return a connection to a server.
656 smb_connect(struct cli_state
**output_cli
,
657 const char *workgroup
, /* I - Workgroup */
658 const char *server
, /* I - Server */
659 const int port
, /* I - Port */
660 const char *share
, /* I - Printer */
661 const char *username
, /* I - Username */
662 const char *password
, /* I - Password */
663 const char *jobusername
) /* I - User who issued the print job */
665 struct cli_state
*cli
= NULL
; /* New connection */
666 char *myname
= NULL
; /* Client name */
668 bool use_kerberos
= false;
669 bool fallback_after_kerberos
= false;
670 const char *user
= username
;
674 * Get the names and addresses of the client and server...
676 myname
= get_myname(talloc_tos());
678 return NT_STATUS_NO_MEMORY
;
682 if (strcmp(auth_info_required
, "negotiate") == 0) {
683 if (!kerberos_ccache_is_valid()) {
685 "ERROR: No valid Kerberos credential cache found! "
686 "Using smbspool_krb5_wrapper may help.\n");
687 return NT_STATUS_LOGON_FAILURE
;
693 "DEBUG: Try to connect using Kerberos ...\n");
694 } else if (strcmp(auth_info_required
, "username,password") == 0) {
695 if (username
== NULL
) {
696 return NT_STATUS_INVALID_ACCOUNT_NAME
;
699 /* Fallback to NTLM */
700 fallback_after_kerberos
= true;
703 "DEBUG: Try to connect using username/password ...\n");
704 } else if (strcmp(auth_info_required
, "none") == 0) {
706 } else if (strcmp(auth_info_required
, "samba") == 0) {
707 if (username
!= NULL
) {
708 fallback_after_kerberos
= true;
709 } else if (kerberos_ccache_is_valid()) {
710 auth_info_required
= "negotiate";
716 "DEBUG: This backend requires credentials!\n");
717 return NT_STATUS_ACCESS_DENIED
;
720 return NT_STATUS_ACCESS_DENIED
;
723 nt_status
= smb_complete_connection(&cli
,
731 true, /* try kerberos */
732 fallback_after_kerberos
);
733 if (NT_STATUS_IS_OK(nt_status
)) {
734 fprintf(stderr
, "DEBUG: SMB connection established.\n");
741 fprintf(stderr
, "ERROR: SMB connection failed!\n");
745 /* give a chance for a passwordless NTLMSSP session setup */
746 pwd
= getpwuid(geteuid());
748 return NT_STATUS_ACCESS_DENIED
;
751 nt_status
= smb_complete_connection(&cli
,
760 if (NT_STATUS_IS_OK(nt_status
)) {
761 fputs("DEBUG: Connected with NTLMSSP...\n", stderr
);
768 * last try. Use anonymous authentication
772 nt_status
= smb_complete_connection(&cli
,
781 if (NT_STATUS_IS_OK(nt_status
)) {
791 * 'smb_print()' - Queue a job for printing using the SMB protocol.
794 static int /* O - 0 = success, non-0 = failure */
795 smb_print(struct cli_state
* cli
, /* I - SMB connection */
796 const char *print_title
, /* I - Title/job name */
798 { /* I - File to print */
799 uint16_t fnum
; /* File number */
800 int nbytes
, /* Number of bytes read */
801 tbytes
; /* Total bytes read */
802 char buffer
[8192], /* Buffer for copy */
803 *ptr
; /* Pointer into title */
804 char title
[1024] = {0};
810 * Sanitize the title...
812 len
= snprintf(title
, sizeof(title
), "%s", print_title
);
813 if (len
!= strlen(print_title
)) {
817 for (ptr
= title
; *ptr
; ptr
++) {
818 if (!isalnum((int) *ptr
) && !isspace((int) *ptr
)) {
824 * Open the printer device...
827 nt_status
= cli_open(cli
, title
, O_RDWR
| O_CREAT
| O_TRUNC
, DENY_NONE
,
829 if (!NT_STATUS_IS_OK(nt_status
)) {
830 fprintf(stderr
, "ERROR: %s opening remote spool %s\n",
831 nt_errstr(nt_status
), title
);
832 return get_exit_code(nt_status
);
836 * Copy the file to the printer...
844 while ((nbytes
= fread(buffer
, 1, sizeof(buffer
), fp
)) > 0) {
847 status
= cli_writeall(cli
, fnum
, 0, (uint8_t *)buffer
,
848 tbytes
, nbytes
, NULL
);
849 if (!NT_STATUS_IS_OK(status
)) {
850 int ret
= get_exit_code(status
);
851 fprintf(stderr
, "ERROR: Error writing spool: %s\n",
853 fprintf(stderr
, "DEBUG: Returning status %d...\n",
855 cli_close(cli
, fnum
);
862 nt_status
= cli_close(cli
, fnum
);
863 if (!NT_STATUS_IS_OK(nt_status
)) {
864 fprintf(stderr
, "ERROR: %s closing remote spool %s\n",
865 nt_errstr(nt_status
), title
);
866 return get_exit_code(nt_status
);
873 uri_unescape_alloc(const char *uritok
)
877 ret
= (char *) SMB_STRDUP(uritok
);
882 end
= rfc1738_unescape(ret
);