s3: smbclient: Move cmd_setmode out of clitar.c and back into client.c
[sfrench/samba-autobuild/.git] / source3 / client / smbspool.c
index e567ef8037aa8590048fa60696e69accf2869852..7161e86468ec2878d7f281805e1565f22bddcb99 100644 (file)
@@ -23,6 +23,9 @@
 */
 
 #include "includes.h"
+#include "system/filesys.h"
+#include "system/passwd.h"
+#include "libsmb/libsmb.h"
 
 /*
  * Starting with CUPS 1.3, Kerberos support is provided by cupsd including
@@ -148,7 +151,14 @@ main(int argc,                     /* I - Number of command-line arguments */
                perror("ERROR: Unable to open print file");
                goto done;
        } else {
-               copies = atoi(argv[4]);
+               char *p = argv[4];
+               char *endp;
+
+               copies = strtol(p, &endp, 10);
+               if (p == endp) {
+                       perror("ERROR: Unable to determine number of copies");
+                       goto done;
+               }
        }
 
        /*
@@ -233,20 +243,18 @@ main(int argc,                    /* I - Number of command-line arguments */
 
                port = atoi(sep);
        } else {
-               port = 445;
+               port = 0;
        }
 
        /*
          * Setup the SAMBA server state...
          */
 
-       setup_logging("smbspool", True);
+       setup_logging("smbspool", DEBUG_STDOUT);
 
-       lp_set_in_client(True); /* Make sure that we tell lp_load we are */
+       smb_init_locale();
 
-       load_case_tables();
-
-       if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, True)) {
+       if (!lp_load_client(get_dyn_CONFIGFILE())) {
                fprintf(stderr, "ERROR: Can't load %s - run testparm to debug it\n", get_dyn_CONFIGFILE());
                goto done;
        }
@@ -347,7 +355,7 @@ get_exit_code(struct cli_state * cli,
                }
 
                if (cli) {
-                       if (cli->use_kerberos || (cli->capabilities & CAP_EXTENDED_SECURITY))
+                       if (cli->use_kerberos && cli->got_kerberos_mechanism)
                                fputs("ATTR: auth-info-required=negotiate\n", stderr);
                        else
                                fputs("ATTR: auth-info-required=username,password\n", stderr);
@@ -401,7 +409,7 @@ smb_complete_connection(const char *myname,
        /* Start the SMB connection */
        *need_auth = false;
        nt_status = cli_start_connection(&cli, myname, server, NULL, port,
-                                        Undefined, flags, NULL);
+                                        SMB_SIGNING_DEFAULT, flags);
        if (!NT_STATUS_IS_OK(nt_status)) {
                fprintf(stderr, "ERROR: Connection failed: %s\n", nt_errstr(nt_status));
                return NULL;
@@ -432,10 +440,13 @@ smb_complete_connection(const char *myname,
                return NULL;
        }
 
-       if (!cli_send_tconX(cli, share, "?????", password, strlen(password) + 1)) {
-               fprintf(stderr, "ERROR: Tree connect failed (%s)\n", cli_errstr(cli));
+       nt_status = cli_tree_connect(cli, share, "?????", password,
+                                    strlen(password) + 1);
+       if (!NT_STATUS_IS_OK(nt_status)) {
+               fprintf(stderr, "ERROR: Tree connect failed (%s)\n",
+                       nt_errstr(nt_status));
 
-               if (get_exit_code(cli, cli_nt_error(cli)) == 2) {
+               if (get_exit_code(cli, nt_status) == 2) {
                        *need_auth = true;
                }
 
@@ -550,11 +561,12 @@ smb_print(struct cli_state * cli, /* I - SMB connection */
          char *title,          /* I - Title/job name */
          FILE * fp)
 {                              /* I - File to print */
-       int             fnum;   /* File number */
+       uint16_t             fnum;      /* File number */
        int             nbytes, /* Number of bytes read */
                        tbytes; /* Total bytes read */
        char            buffer[8192],   /* Buffer for copy */
                       *ptr;    /* Pointer into title */
+       NTSTATUS nt_status;
 
 
        /*
@@ -571,11 +583,12 @@ smb_print(struct cli_state * cli, /* I - SMB connection */
          * Open the printer device...
          */
 
-       fnum = cli_open(cli, title, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE);
-       if (fnum == -1) {
+       nt_status = cli_open(cli, title, O_RDWR | O_CREAT | O_TRUNC, DENY_NONE,
+                         &fnum);
+       if (!NT_STATUS_IS_OK(nt_status)) {
                fprintf(stderr, "ERROR: %s opening remote spool %s\n",
-                       cli_errstr(cli), title);
-               return (get_exit_code(cli, cli_nt_error(cli)));
+                       nt_errstr(nt_status), title);
+               return get_exit_code(cli, nt_status);
        }
 
        /*
@@ -588,22 +601,28 @@ smb_print(struct cli_state * cli, /* I - SMB connection */
        tbytes = 0;
 
        while ((nbytes = fread(buffer, 1, sizeof(buffer), fp)) > 0) {
-               if (cli_write(cli, fnum, 0, buffer, tbytes, nbytes) != nbytes) {
-                       int status = get_exit_code(cli, cli_nt_error(cli));
-
-                       fprintf(stderr, "ERROR: Error writing spool: %s\n", cli_errstr(cli));
-                       fprintf(stderr, "DEBUG: Returning status %d...\n", status);
+               NTSTATUS status;
+
+               status = cli_writeall(cli, fnum, 0, (uint8_t *)buffer,
+                                     tbytes, nbytes, NULL);
+               if (!NT_STATUS_IS_OK(status)) {
+                       int ret = get_exit_code(cli, status);
+                       fprintf(stderr, "ERROR: Error writing spool: %s\n",
+                               nt_errstr(status));
+                       fprintf(stderr, "DEBUG: Returning status %d...\n",
+                               ret);
                        cli_close(cli, fnum);
 
-                       return (status);
+                       return (ret);
                }
                tbytes += nbytes;
        }
 
-       if (!cli_close(cli, fnum)) {
+       nt_status = cli_close(cli, fnum);
+       if (!NT_STATUS_IS_OK(nt_status)) {
                fprintf(stderr, "ERROR: %s closing remote spool %s\n",
-                       cli_errstr(cli), title);
-               return (get_exit_code(cli, cli_nt_error(cli)));
+                       nt_errstr(nt_status), title);
+               return get_exit_code(cli, nt_status);
        } else {
                return (0);
        }