s3:client: Fix smbspool device uri handling
[samba.git] / source3 / client / smbspool.c
index a9cea089da744ca4177fa97708ef55498a93af75..5efaa091913e45a9ee387df8d8eeadff1542de53 100644 (file)
@@ -100,10 +100,12 @@ main(int argc,                    /* I - Number of command-line arguments */
        char            empty_str[] = "";
        int             tries = 0;
        bool            need_auth = true;
-       const char     *dev_uri;
+       const char     *dev_uri = NULL;
+       const char     *env = NULL;
        const char     *config_file = NULL;
        TALLOC_CTX     *frame = talloc_stackframe();
-       bool device_uri_cmdline = false;
+       const char *print_user = NULL;
+       const char *print_title = NULL;
        const char *print_file = NULL;
        const char *print_copies = NULL;
        int cmp;
@@ -140,21 +142,81 @@ main(int argc,                    /* I - Number of command-line arguments */
        }
 
        /*
-        * If we have 6 arguments find out if we have the device_uri from the
-        * command line or the print data
+        * Find out if we have the device_uri in the command line.
+        *
+        * If we are started as a CUPS backend argv[0] is normally the
+        * device_uri!
         */
-       if (argc == 7) {
-               cmp = strncmp(argv[1], "smb://", 6);
-               if (cmp == 0) {
-                       device_uri_cmdline = true;
+       if (argc == 8) {
+               /*
+                * smbspool <uri> <job> <user> <title> <copies> <options> <file>
+                * 0        1     2     3      4       5        6         7
+                */
+
+               dev_uri = argv[1];
+
+               print_user = argv[3];
+               print_title = argv[4];
+               print_copies = argv[5];
+               print_file = argv[7];
+       } else if (argc == 7) {
+               int cmp1;
+               int cmp2;
+
+               /*
+                * <uri>    <job> <user> <title> <copies> <options> <file>
+                * smbspool <uri> <job>  <user>  <title>  <copies>  <options>
+                * smbspool <job> <user> <title> <copies> <options> <file> | DEVICE_URI
+                */
+               cmp1 = strncmp(argv[0], "smb://", 6);
+               cmp2 = strncmp(argv[1], "smb://", 6);
+
+               if (cmp1 == 0) {
+                       /*
+                        * <uri>    <job> <user> <title> <copies> <options> <file>
+                        * 0        1     2      3       4        5         6
+                        */
+                       dev_uri = argv[0];
+
+                       print_user = argv[2];
+                       print_title = argv[3];
+                       print_copies = argv[4];
+                       print_file = argv[6];
+               } else if (cmp2 == 0) {
+                       /*
+                        * smbspool <uri> <job>  <user>  <title>  <copies>  <options>
+                        * 0        1     2      3       4        5         6
+                        */
+                       dev_uri = argv[1];
+
+                       print_user = argv[3];
+                       print_title = argv[4];
+                       print_copies = argv[5];
+                       print_file = NULL;
                } else {
+                       /*
+                        * smbspool <job> <user> <title> <copies> <options> <file> | DEVICE_URI
+                        * 0        1     2      3       4        5         6
+                        */
+                       print_user = argv[2];
+                       print_title = argv[3];
                        print_copies = argv[4];
                        print_file = argv[6];
                }
-       } else if (argc == 8) {
-               device_uri_cmdline = true;
-               print_copies = argv[5];
-               print_file = argv[7];
+       } else if (argc == 6) {
+               /*
+                * <uri>    <job> <user> <title> <copies> <options>
+                * smbspool <job> <user> <title> <copies> <options> | DEVICE_URI
+                * 0        1     2      3       4        5
+                */
+               cmp = strncmp(argv[0], "smb://", 6);
+               if (cmp == 0) {
+                       dev_uri = argv[0];
+               }
+
+               print_user = argv[2];
+               print_title = argv[3];
+               print_copies = argv[4];
        }
 
        if (print_file != NULL) {
@@ -179,18 +241,17 @@ main(int argc,                    /* I - Number of command-line arguments */
        /*
         * Find the URI ...
         */
-       if (device_uri_cmdline) {
-               dev_uri = argv[1];
-       } else {
-               dev_uri = getenv("DEVICE_URI");
-               if (dev_uri == NULL || strlen(dev_uri) == 0) {
-                       dev_uri = "";
+       if (dev_uri == NULL) {
+               env = getenv("DEVICE_URI");
+               if (env != NULL && env[0] != '\0') {
+                       dev_uri = env;
                }
        }
 
-       auth_info_required = getenv("AUTH_INFO_REQUIRED");
-       if (auth_info_required == NULL) {
-               auth_info_required = "none";
+       if (dev_uri == NULL) {
+               fprintf(stderr,
+                       "ERROR: No valid device URI has been specified\n");
+               goto done;
        }
 
        cmp = strncmp(dev_uri, "smb://", 6);
@@ -206,6 +267,11 @@ main(int argc,                     /* I - Number of command-line arguments */
                goto done;
        }
 
+       auth_info_required = getenv("AUTH_INFO_REQUIRED");
+       if (auth_info_required == NULL) {
+               auth_info_required = "none";
+       }
+
        /*
          * Extract the destination from the URI...
          */
@@ -302,8 +368,14 @@ main(int argc,                     /* I - Number of command-line arguments */
        load_interfaces();
 
        do {
-               cli = smb_connect(workgroup, server, port, printer,
-                       username, password, argv[3], &need_auth);
+               cli = smb_connect(workgroup,
+                                 server,
+                                 port,
+                                 printer,
+                                 username,
+                                 password,
+                                 print_user,
+                                 &need_auth);
                if (cli == NULL) {
                        if (need_auth) {
                                exit(2);
@@ -339,7 +411,7 @@ main(int argc,                      /* I - Number of command-line arguments */
          */
 
        for (i = 0; i < copies; i++) {
-               status = smb_print(cli, argv[4] /* title */ , fp);
+               status = smb_print(cli, print_title, fp);
                if (status != 0) {
                        break;
                }