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