| 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 #include <sys/stat.h> | |
| 8 #include <sys/types.h> | |
| 9 #include <sys/wait.h> | |
| 10 | |
| 11 #include <cups/backend.h> | |
| 12 | |
| 13 #include "base/json/json_writer.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/string_tokenizer.h" | |
| 16 #include "base/values.h" | |
| 17 | |
| 18 #include "cloud_print/virtual_driver/posix/printer_driver_util_posix.h" | |
| 19 | |
| 20 namespace printer_driver_util { | |
| 21 | |
| 22 void WriteToTemp(FILE* input_pdf, FilePath output_path) { | |
| 23 FILE* output_pdf; | |
| 24 char buffer[128]; | |
| 25 output_pdf = fopen(output_path.value().c_str(), "w"); | |
| 26 if (output_pdf == NULL) { | |
| 27 LOG(ERROR) << "Unable to open file handle for writing output file"; | |
| 28 exit(CUPS_BACKEND_CANCEL); | |
| 29 } | |
| 30 // Read from input file or stdin and write to output file. | |
| 31 while (fgets(buffer, sizeof(buffer), input_pdf) != NULL) { | |
| 32 fputs(buffer, output_pdf); | |
| 33 } | |
| 34 | |
| 35 LOG(INFO) << "Successfully wrote output file"; | |
| 36 // ensure everything is written, then close the files. | |
| 37 fflush(output_pdf); | |
| 38 fclose(input_pdf); | |
| 39 fclose(output_pdf); | |
| 40 } | |
| 41 | |
| 42 // Sets the UID of the process to that of the username. | |
| 43 void SetUser(const char* user) { | |
| 44 struct passwd* calling_user = NULL; | |
| 45 calling_user = getpwnam(user); | |
| 46 if (calling_user == NULL) { | |
| 47 LOG(ERROR) << "Unable to get calling user"; | |
| 48 exit(CUPS_BACKEND_CANCEL); | |
| 49 } | |
| 50 if (!setuid(calling_user->pw_uid) == -1) { | |
| 51 LOG(ERROR) << "Unable to set UID"; | |
| 52 exit(CUPS_BACKEND_CANCEL); | |
| 53 } | |
| 54 | |
| 55 LOG(INFO) << "Successfully set user and group ID"; | |
| 56 } | |
| 57 | |
| 58 // Parses the options passed in on the command line to key value | |
| 59 // JSON pairs. Assumes that the input options string is of the | |
| 60 // format KEY=VALUE, with expressions being separated by spaces. | |
| 61 // Fails if print_ticket cannot be written to. | |
| 62 void GetOptions(const char* options, std::string* print_ticket) { | |
| 63 if (options == NULL) { | |
| 64 *(print_ticket) = "{}"; | |
| 65 return; | |
| 66 } | |
| 67 CStringTokenizer t(options, options + strlen(options), " "); | |
| 68 DictionaryValue* json_options = new DictionaryValue; | |
| 69 | |
| 70 while (t.GetNext()) { | |
| 71 std::string token = t.token(); | |
| 72 // If the token ends with a slash, that indicates | |
| 73 // that the next space is actually escaped | |
| 74 // So we append the next token onto this token | |
| 75 // if possible. We also replace the slash with a space | |
| 76 // since the JSON will expect an unescaped string. | |
| 77 while (token.at(token.length()-1) == '\\') { | |
| 78 if (t.GetNext()) { | |
| 79 token.replace(token.length()-1, 1, " "); | |
| 80 token.append(t.token()); | |
| 81 } else { | |
| 82 break; | |
| 83 } | |
| 84 } | |
| 85 size_t pos = token.find("="); | |
| 86 if (pos == std::string::npos) { | |
| 87 continue; | |
| 88 } | |
| 89 std::string option_name = token.substr(0, pos); | |
| 90 std::string option_value = token.substr(pos+1); | |
| 91 base::StringValue* val= base::Value::CreateStringValue(option_value); | |
| 92 json_options->SetWithoutPathExpansion(option_name, val); | |
| 93 } | |
| 94 base::JSONWriter::Write(json_options, /*pretty_print=*/false, print_ticket); | |
| 95 delete json_options; | |
| 96 } | |
| 97 | |
| 98 } // namespace printer_driver_util | |
| 99 | |
| OLD | NEW |