- int /* O - Exit status */
- main(int argc, /* I - Number of command-line arguments */
- char *argv[]) /* I - Command-line arguments */
-{
- int i; /* Looping var */
- int copies; /* Number of copies */
- int port; /* Port number */
- char uri[1024], /* URI */
- *sep, /* Pointer to separator */
- *password; /* Password */
- const char *username, /* Username */
- *server, /* Server name */
- *printer; /* Printer name */
- const char *workgroup; /* Workgroup */
- FILE *fp; /* File to print */
- int status=0; /* Status of LPD job */
- struct cli_state *cli; /* SMB interface */
- char null_str[1];
- int tries = 0;
-
- null_str[0] = '\0';
-
- /* we expect the URI in argv[0]. Detect the case where it is in argv[1] and cope */
- if (argc > 2 && strncmp(argv[0],"smb://", 6) && !strncmp(argv[1],"smb://", 6)) {
- argv++;
- argc--;
- }
-
- if (argc == 1)
- {
- /*
- * NEW! In CUPS 1.1 the backends are run with no arguments to list the
- * available devices. These can be devices served by this backend
- * or any other backends (i.e. you can have an SNMP backend that
- * is only used to enumerate the available network printers... :)
- */
-
- list_devices();
- return (0);
- }
-
- if (argc < 6 || argc > 7)
- {
- fprintf(stderr, "Usage: %s [DEVICE_URI] job-id user title copies options [file]\n",
- argv[0]);
- fputs(" The DEVICE_URI environment variable can also contain the\n", stderr);
- fputs(" destination printer:\n", stderr);
- fputs("\n", stderr);
- fputs(" smb://[username:password@][workgroup/]server[:port]/printer\n", stderr);
- return (1);
- }
-
- /*
- * If we have 7 arguments, print the file named on the command-line.
- * Otherwise, print data from stdin...
- */
-
-
- if (argc == 6)
- {
- /*
- * Print from Copy stdin to a temporary file...
- */
-
- fp = stdin;
- copies = 1;
- }
- else if ((fp = fopen(argv[6], "rb")) == NULL)
- {
- perror("ERROR: Unable to open print file");
- return (1);
- }
- else
- copies = atoi(argv[4]);
-
- /*
- * Find the URI...
- */
-
- if (getenv("DEVICE_URI") != NULL)
- strncpy(uri, getenv("DEVICE_URI"), sizeof(uri) - 1);
- else if (strncmp(argv[0], "smb://", 6) == 0)
- strncpy(uri, argv[0], sizeof(uri) - 1);
- else
- {
- fputs("ERROR: No device URI found in DEVICE_URI environment variable or argv[0] !\n", stderr);
- return (1);
- }
-
- uri[sizeof(uri) - 1] = '\0';
-
- /*
- * Extract the destination from the URI...
- */
-
- if ((sep = strrchr_m(uri, '@')) != NULL)
- {
- username = uri + 6;
- *sep++ = '\0';
-
- server = sep;
-
- /*
- * Extract password as needed...
- */
-
- if ((password = strchr_m(username, ':')) != NULL)
- *password++ = '\0';
- else
- password = null_str;
- }
- else
- {
- username = null_str;
- password = null_str;
- server = uri + 6;
- }
-
- if ((sep = strchr_m(server, '/')) == NULL)
- {
- fputs("ERROR: Bad URI - need printer name!\n", stderr);
- return (1);
- }
-
- *sep++ = '\0';
- printer = sep;
-
- if ((sep = strchr_m(printer, '/')) != NULL)
- {
- /*
- * Convert to smb://[username:password@]workgroup/server/printer...
- */
-
- *sep++ = '\0';
-
- workgroup = server;
- server = printer;
- printer = sep;
- }
- else
- workgroup = NULL;
-
- if ((sep = strrchr_m(server, ':')) != NULL)
- {
- *sep++ = '\0';
-
- port=atoi(sep);
- }
- else
- port=0;
-
-
- /*
- * Setup the SAMBA server state...
- */
-
- setup_logging("smbspool", True);
-
- in_client = True; /* Make sure that we tell lp_load we are */
-
- if (!lp_load(dyn_CONFIGFILE, True, False, False, True))
- {
- fprintf(stderr, "ERROR: Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
- return (1);
- }
-
- if (workgroup == NULL)
- workgroup = lp_workgroup();
-
- load_interfaces();
-
- do
- {
- if ((cli = smb_connect(workgroup, server, port, printer, username, password, argv[2])) == NULL)
- {
- if (getenv("CLASS") == NULL)
- {
- fprintf(stderr, "ERROR: Unable to connect to CIFS host, will retry in 60 seconds...\n");
- sleep (60); /* should just waiting and retrying fix authentication ??? */
- tries++;
- }
- else
- {
- fprintf(stderr, "ERROR: Unable to connect to CIFS host, trying next printer...\n");
- return (1);
- }
- }
- }
- while ((cli == NULL) && (tries < MAX_RETRY_CONNECT));
-
- if (cli == NULL) {
- fprintf(stderr, "ERROR: Unable to connect to CIFS host after (tried %d times)\n", tries);
- return (1);
- }
-
- /*
- * Now that we are connected to the server, ignore SIGTERM so that we
- * can finish out any page data the driver sends (e.g. to eject the
- * current page... Only ignore SIGTERM if we are printing data from
- * stdin (otherwise you can't cancel raw jobs...)
- */
-
- if (argc < 7)
- CatchSignal(SIGTERM, SIG_IGN);
-
- /*
- * Queue the job...
- */
-
- for (i = 0; i < copies; i ++)
- if ((status = smb_print(cli, argv[3] /* title */, fp)) != 0)
- break;
-
- cli_shutdown(cli);
-
- /*
- * Return the queue status...
- */
-
- return (status);
+int /* O - Exit status */
+main(int argc, /* I - Number of command-line arguments */
+ char *argv[])
+{ /* I - Command-line arguments */
+ int i; /* Looping var */
+ int copies; /* Number of copies */
+ int port; /* Port number */
+ char uri[1024], /* URI */
+ *sep, /* Pointer to separator */
+ *tmp, *tmp2, /* Temp pointers to do escaping */
+ *password; /* Password */
+ char *username, /* Username */
+ *server, /* Server name */
+ *printer;/* Printer name */
+ const char *workgroup; /* Workgroup */
+ FILE *fp; /* File to print */
+ int status = 1; /* Status of LPD job */
+ struct cli_state *cli; /* SMB interface */
+ char null_str[1];
+ int tries = 0;
+ bool need_auth = true;
+ const char *dev_uri;
+ TALLOC_CTX *frame = talloc_stackframe();
+
+ null_str[0] = '\0';
+
+ /*
+ * we expect the URI in argv[0]. Detect the case where it is in
+ * argv[1] and cope
+ */
+ if (argc > 2 && strncmp(argv[0], "smb://", 6) &&
+ strncmp(argv[1], "smb://", 6) == 0) {
+ argv++;
+ argc--;
+ }
+
+ if (argc == 1) {
+ /*
+ * NEW! In CUPS 1.1 the backends are run with no arguments
+ * to list the available devices. These can be devices
+ * served by this backend or any other backends (i.e. you
+ * can have an SNMP backend that is only used to enumerate
+ * the available network printers... :)
+ */
+
+ list_devices();
+ status = 0;
+ goto done;
+ }
+
+ if (argc < 6 || argc > 7) {
+ fprintf(stderr,
+"Usage: %s [DEVICE_URI] job-id user title copies options [file]\n"
+" The DEVICE_URI environment variable can also contain the\n"
+" destination printer:\n"
+"\n"
+" smb://[username:password@][workgroup/]server[:port]/printer\n",
+ argv[0]);
+ goto done;
+ }
+
+ /*
+ * If we have 7 arguments, print the file named on the command-line.
+ * Otherwise, print data from stdin...
+ */
+
+ if (argc == 6) {
+ /*
+ * Print from Copy stdin to a temporary file...
+ */
+
+ fp = stdin;
+ copies = 1;
+ } else if ((fp = fopen(argv[6], "rb")) == NULL) {
+ perror("ERROR: Unable to open print file");
+ goto done;
+ } else {
+ copies = atoi(argv[4]);
+ }
+
+ /*
+ * Find the URI...
+ */
+
+ dev_uri = getenv("DEVICE_URI");
+ if (dev_uri) {
+ strncpy(uri, dev_uri, sizeof(uri) - 1);
+ } else if (strncmp(argv[0], "smb://", 6) == 0) {
+ strncpy(uri, argv[0], sizeof(uri) - 1);
+ } else {
+ fputs("ERROR: No device URI found in DEVICE_URI environment variable or argv[0] !\n", stderr);
+ goto done;
+ }
+
+ uri[sizeof(uri) - 1] = '\0';
+
+ /*
+ * Extract the destination from the URI...
+ */
+
+ if ((sep = strrchr_m(uri, '@')) != NULL) {
+ tmp = uri + 6;
+ *sep++ = '\0';
+
+ /* username is in tmp */
+
+ server = sep;
+
+ /*
+ * Extract password as needed...
+ */
+
+ if ((tmp2 = strchr_m(tmp, ':')) != NULL) {
+ *tmp2++ = '\0';
+ password = uri_unescape_alloc(tmp2);
+ } else {
+ password = null_str;
+ }
+ username = uri_unescape_alloc(tmp);
+ } else {
+ if ((username = getenv("AUTH_USERNAME")) == NULL) {
+ username = null_str;
+ }
+
+ if ((password = getenv("AUTH_PASSWORD")) == NULL) {
+ password = null_str;
+ }
+
+ server = uri + 6;
+ }
+
+ tmp = server;
+
+ if ((sep = strchr_m(tmp, '/')) == NULL) {
+ fputs("ERROR: Bad URI - need printer name!\n", stderr);
+ goto done;
+ }
+
+ *sep++ = '\0';
+ tmp2 = sep;
+
+ if ((sep = strchr_m(tmp2, '/')) != NULL) {
+ /*
+ * Convert to smb://[username:password@]workgroup/server/printer...
+ */
+
+ *sep++ = '\0';
+
+ workgroup = uri_unescape_alloc(tmp);
+ server = uri_unescape_alloc(tmp2);
+ printer = uri_unescape_alloc(sep);
+ } else {
+ workgroup = NULL;
+ server = uri_unescape_alloc(tmp);
+ printer = uri_unescape_alloc(tmp2);
+ }
+
+ if ((sep = strrchr_m(server, ':')) != NULL) {
+ *sep++ = '\0';
+
+ port = atoi(sep);
+ } else {
+ port = 445;
+ }
+
+ /*
+ * Setup the SAMBA server state...
+ */
+
+ setup_logging("smbspool", True);
+
+ lp_set_in_client(True); /* Make sure that we tell lp_load we are */
+
+ load_case_tables();
+
+ if (!lp_load(get_dyn_CONFIGFILE(), True, False, False, True)) {
+ fprintf(stderr, "ERROR: Can't load %s - run testparm to debug it\n", get_dyn_CONFIGFILE());
+ goto done;
+ }
+
+ if (workgroup == NULL) {
+ workgroup = lp_workgroup();
+ }
+
+ load_interfaces();
+
+ do {
+ cli = smb_connect(workgroup, server, port, printer,
+ username, password, argv[2], &need_auth);
+ if (cli == NULL) {
+ if (need_auth) {
+ exit(2);
+ } else if (getenv("CLASS") == NULL) {
+ fprintf(stderr, "ERROR: Unable to connect to CIFS host, will retry in 60 seconds...\n");
+ sleep(60);
+ tries++;
+ } else {
+ fprintf(stderr, "ERROR: Unable to connect to CIFS host, trying next printer...\n");
+ goto done;
+ }
+ }
+ } while ((cli == NULL) && (tries < MAX_RETRY_CONNECT));
+
+ if (cli == NULL) {
+ fprintf(stderr, "ERROR: Unable to connect to CIFS host after (tried %d times)\n", tries);
+ goto done;
+ }
+
+ /*
+ * Now that we are connected to the server, ignore SIGTERM so that we
+ * can finish out any page data the driver sends (e.g. to eject the
+ * current page... Only ignore SIGTERM if we are printing data from
+ * stdin (otherwise you can't cancel raw jobs...)
+ */
+
+ if (argc < 7) {
+ CatchSignal(SIGTERM, SIG_IGN);
+ }
+
+ /*
+ * Queue the job...
+ */
+
+ for (i = 0; i < copies; i++) {
+ status = smb_print(cli, argv[3] /* title */ , fp);
+ if (status != 0) {
+ break;
+ }
+ }
+
+ cli_shutdown(cli);
+
+ /*
+ * Return the queue status...
+ */
+
+done:
+
+ TALLOC_FREE(frame);
+ return (status);