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 <pwd.h> |
| 6 #include <stdio.h> |
| 7 |
| 8 #include <cups/backend.h> |
| 9 |
| 10 #include <sys/stat.h> |
| 11 #include <sys/types.h> |
| 12 #include <sys/wait.h> |
| 13 |
| 14 #include "base/at_exit.h" |
| 15 #include "base/base_paths.h" |
| 16 #include "base/command_line.h" |
| 17 #include "base/file_path.h" |
| 18 #include "base/logging.h" |
| 19 #include "base/path_service.h" |
| 20 |
| 21 #include "cloud_print/virtual_driver/posix/printer_driver_util_posix.h" |
| 22 |
| 23 using namespace std; |
| 24 |
| 25 int WriteToTemp(FILE* input_pdf, FilePath output_path) { |
| 26 FILE* output_pdf; |
| 27 char buffer[128]; |
| 28 output_pdf = fopen(output_path.value().c_str(),"w"); |
| 29 if(output_pdf == NULL) { |
| 30 LOG(ERROR) <<"Unable to open file handle for writing output file"; |
| 31 exit(CUPS_BACKEND_CANCEL); |
| 32 } |
| 33 // Read from input file or stdin and write to output file. |
| 34 while(fgets(buffer,128,input_pdf) != NULL){ |
| 35 fputs(buffer, output_pdf); |
| 36 } |
| 37 |
| 38 LOG(INFO) << "Successfully wrote output file"; |
| 39 // ensure everything is written, then close the files. |
| 40 fflush(output_pdf); |
| 41 fclose(input_pdf); |
| 42 fclose(output_pdf); |
| 43 } |
| 44 |
| 45 void SetUser(const char* user) { |
| 46 struct passwd* calling_user=NULL; |
| 47 calling_user=getpwnam(user); |
| 48 if(calling_user == NULL) { |
| 49 LOG(ERROR) << "Unable to get calling user"; |
| 50 exit(CUPS_BACKEND_CANCEL); |
| 51 } |
| 52 if(setgid(calling_user->pw_gid)==-1) { |
| 53 LOG(ERROR) << "Unable to set group ID"; |
| 54 exit(CUPS_BACKEND_CANCEL); |
| 55 } |
| 56 if(!setuid(calling_user->pw_uid)==-1) { |
| 57 LOG(ERROR) << "Unable to set UID"; |
| 58 exit(CUPS_BACKEND_CANCEL); |
| 59 } |
| 60 |
| 61 LOG(INFO) << "Successfully set user and group ID"; |
| 62 } |
| 63 |
| 64 |
| 65 |
| 66 // Main function for backend. |
| 67 int main(int argc, const char* argv[]) { |
| 68 // With no arguments, send identification string as required by CUPS. |
| 69 if (argc==1) { |
| 70 printf("file GCP-driver:/ \"GCP Virtual Driver\" \"GCP Virtual Driver\" " |
| 71 "\"MFG:Google Inc.;MDL:GCP Virtual Driver;DES:GCP Virtual Driver;" |
| 72 "CLS:PRINTER;CMD:POSTSCRIPT;\"\n"); |
| 73 return 0; |
| 74 } |
| 75 |
| 76 if (argc<6 || argc>7) { |
| 77 fprintf(stderr,"Usage: GCP-Driver job-id user" |
| 78 "title copies options [file]\n"); |
| 79 return 0; |
| 80 } |
| 81 |
| 82 //AtExitManager is necessary to use the path service. |
| 83 base::AtExitManager aemanager; |
| 84 // File handler for the temporary PDF we pass onto Chrome. |
| 85 FILE* output_pdf; |
| 86 //CommandLine is only setup to enable logging, never used subseqeuently. |
| 87 CommandLine::Init(argc,argv); |
| 88 // Location for the log file. |
| 89 std::string log_location="/var/log/GCP-Driver.log"; |
| 90 |
| 91 // Set up logging. |
| 92 logging::InitLogging(log_location.c_str(), |
| 93 logging::LOG_ONLY_TO_FILE, |
| 94 logging::LOCK_LOG_FILE, |
| 95 logging::APPEND_TO_OLD_LOG_FILE, |
| 96 logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); |
| 97 |
| 98 // Temporary directory to hold the output file. |
| 99 FilePath temp_dir; |
| 100 // Strings to hold details of current job. |
| 101 std::string current_user; |
| 102 std::string set_var; |
| 103 std::string job_title; |
| 104 std::string job_id; |
| 105 std::string file_name; |
| 106 |
| 107 // Get temp directory to hold spool file. |
| 108 if(!PathService::Get(base::DIR_TEMP, &temp_dir)) { |
| 109 LOG(ERROR) << "Unable to get DIR_TEMP"; |
| 110 return CUPS_BACKEND_CANCEL; |
| 111 } |
| 112 // Get details from command line and set spool path. |
| 113 current_user = argv[2]; |
| 114 job_title = argv[3]; |
| 115 job_id = argv[1]; |
| 116 file_name = current_user + "-" + job_title + "-" + job_id; |
| 117 FilePath output_path = temp_dir.Append(file_name); |
| 118 |
| 119 // Read from file if specified. |
| 120 if(argc==7){ |
| 121 FILE* input_pdf=fopen(argv[6],"r"); |
| 122 if(input_pdf == NULL) { |
| 123 LOG(ERROR) << "Unable to read input PDF"; |
| 124 return CUPS_BACKEND_CANCEL; |
| 125 } |
| 126 // File is opened. |
| 127 WriteToTemp(input_pdf,output_path); |
| 128 } |
| 129 // Otherwise, read from stdin. |
| 130 else{ |
| 131 WriteToTemp(stdin, output_path); |
| 132 } |
| 133 |
| 134 // Set File permissions to allow non-sudo user to read spool file. |
| 135 if(chmod(output_path.value().c_str(),S_IRUSR|S_IWUSR|S_IROTH|S_IWOTH)!=0) { |
| 136 LOG(ERROR)<<"Unable to change file permissions"; |
| 137 return CUPS_BACKEND_CANCEL; |
| 138 } |
| 139 |
| 140 pid_t pid=fork(); |
| 141 |
| 142 if(!pid) { |
| 143 // In child process. |
| 144 |
| 145 // Set the user to the one that initiated print job. |
| 146 SetUser(argv[2]); |
| 147 |
| 148 // Launch Chrome and pass the print job onto Cloud Print. |
| 149 LaunchPrintDialog(output_path.value(), job_title, current_user); |
| 150 |
| 151 return 0; |
| 152 }// back in parent process. |
| 153 // wait for child, then terminate. |
| 154 waitpid(pid,NULL,0); |
| 155 return 0; |
| 156 }// main |
OLD | NEW |