OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <cups/backend.h> | |
6 | |
7 #include "base/at_exit.h" | |
8 #include "base/base_paths.h" | |
9 #include "base/command_line.h" | |
10 #include "base/file_path.h" | |
11 #include "base/logging.h" | |
12 #include "base/path_service.h" | |
13 | |
14 #include "cloud_print/virtual_driver/posix/printer_driver_util_posix.h" | |
15 | |
16 // Main function for backend. | |
17 int main(int argc, const char* argv[]) { | |
18 // With no arguments, send identification string as required by CUPS. | |
19 if (argc == 1) { | |
20 printf("file GCP-driver:/ \"GCP Virtual Driver\" \"GCP Virtual Driver\" " | |
21 "\"MFG:Google Inc.;MDL:GCP Virtual Driver;DES:GCP Virtual Driver;" | |
22 "CLS:PRINTER;CMD:POSTSCRIPT;\"\n"); | |
23 return 0; | |
24 } | |
25 | |
26 if (argc < 6 || argc > 7) { | |
27 fprintf(stderr, "Usage: GCP-Driver job-id user " | |
28 "title copies options [file]\n"); | |
29 return 0; | |
30 } | |
31 | |
32 // We can run the backend as root or unpriveliged user lp | |
33 // Since we want to launch the printer dialog as the user | |
34 // that initiated the print job, we run the backend as root | |
35 // and then setuid to the user that started the print job | |
36 printer_driver_util::SetUser(argv[2]); | |
37 | |
38 // AtExitManager is necessary to use the path service. | |
39 base::AtExitManager aemanager; | |
40 // CommandLine is only setup to enable logging, never used subseqeuently. | |
41 CommandLine::Init(argc, argv); | |
42 // Location for the log file. | |
43 std::string log_location = "/var/log/GCP-Driver.log"; | |
44 | |
45 // Set up logging. | |
46 logging::InitLogging(log_location.c_str(), | |
47 logging::LOG_ONLY_TO_FILE, | |
48 logging::LOCK_LOG_FILE, | |
49 logging::APPEND_TO_OLD_LOG_FILE, | |
50 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); | |
51 | |
52 // Temporary directory to hold the output file. | |
53 FilePath temp_dir; | |
54 // Strings to hold details of current job. | |
55 std::string current_user; | |
56 std::string set_var; | |
57 std::string job_title; | |
58 std::string job_id; | |
59 std::string file_name; | |
60 std::string print_ticket; | |
61 | |
62 // Get temp directory to hold spool file. | |
63 if (!PathService::Get(base::DIR_TEMP, &temp_dir)) { | |
64 LOG(ERROR) << "Unable to get DIR_TEMP"; | |
65 return CUPS_BACKEND_CANCEL; | |
66 } | |
67 // Get details from command line and set spool path. | |
68 current_user = argv[2]; | |
69 job_title = argv[3]; | |
70 job_id = argv[1]; | |
71 printer_driver_util::GetOptions(argv[5], &print_ticket); | |
72 file_name = current_user + "-" + job_title + "-" + job_id; | |
73 FilePath output_path = temp_dir.Append(file_name); | |
74 | |
75 // However, the input file can only be read as root. | |
76 if (!setuid(0)) { | |
77 PLOG(ERROR) << "Unable to setuid back to 0"; | |
78 } | |
79 | |
80 if (argc == 7) { | |
81 // Read from file if specified. | |
82 FILE* input_pdf = fopen(argv[6], "r"); | |
83 if (input_pdf == NULL) { | |
84 LOG(ERROR) << "Unable to read input PDF"; | |
85 return CUPS_BACKEND_CANCEL; | |
86 } | |
87 // File is opened. | |
88 printer_driver_util::WriteToTemp(input_pdf, output_path); | |
89 } else { | |
90 // Otherwise, read from stdin. | |
91 printer_driver_util::WriteToTemp(stdin, output_path); | |
92 } | |
93 | |
94 // Change back to user to launch Chrome. | |
95 printer_driver_util::SetUser(argv[2]); | |
96 PLOG(ERROR) << print_ticket; | |
97 // Launch Chrome and pass the print job onto Cloud Print. | |
98 printer_driver_util::LaunchPrintDialog(output_path.value(), job_title, | |
99 current_user, print_ticket); | |
100 | |
101 return 0; | |
102 } | |
OLD | NEW |