selfpaste.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /**
  2. * This program is free software: you can redistribute it and/or modify
  3. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  4. * You should have received a copy of the
  5. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  6. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  7. *
  8. * 2019 - 2020 https://://www.bananas-playground.net/projekt/selfpaste
  9. */
  10. /**
  11. * !WARNING!
  12. * This is a very simple, with limited experience written, c program.
  13. * Use at own risk and feel free to improve
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <argp.h>
  19. #include <unistd.h>
  20. #include <pwd.h>
  21. #include <time.h>
  22. /* https://curl.haxx.se */
  23. #include <curl/curl.h>
  24. /* https://github.com/json-c/json-c */
  25. #include <json-c/json.h>
  26. /**
  27. * Commandline arguments
  28. * see: https://www.gnu.org/software/libc/manual/html_node/Argp-Example-3.html#Argp-Example-3
  29. */
  30. const char *argp_program_version = "1.1";
  31. const char *argp_program_bug_address = "https://://www.bananas-playground.net/projekt/selfpaste";
  32. static char doc[] = "selfpaste. Upload given file to your selfpaste installation.";
  33. static char args_doc[] = "file";
  34. /* The options we understand. */
  35. static struct argp_option options[] = {
  36. {"verbose",'v', 0, 0, "Produce verbose output" },
  37. {"quiet", 'q', 0, 0, "Don't produce any output" },
  38. {"create-config-file", 'c', 0, 0, "Create default config file" },
  39. { 0 }
  40. };
  41. struct cmdArguments {
  42. char *args[1];
  43. int quiet, verbose, create_config_file;
  44. };
  45. /* Parse a single option. */
  46. static error_t
  47. parse_opt (int key, char *arg, struct argp_state *state) {
  48. struct cmdArguments *arguments = state->input;
  49. switch (key) {
  50. case 'q':
  51. arguments->quiet = 1;
  52. break;
  53. case 'v':
  54. arguments->verbose = 1;
  55. break;
  56. case 'c':
  57. arguments->create_config_file = 1;
  58. break;
  59. case ARGP_KEY_ARG:
  60. if (state->arg_num >= 1)
  61. // Too many arguments.
  62. argp_usage (state);
  63. arguments->args[state->arg_num] = arg;
  64. break;
  65. case ARGP_KEY_END:
  66. if (state->arg_num < 1 && arguments->create_config_file == 0)
  67. /* Not enough arguments. */
  68. argp_usage (state);
  69. break;
  70. default:
  71. return ARGP_ERR_UNKNOWN;
  72. }
  73. return 0;
  74. }
  75. static struct argp argp = { options, parse_opt, args_doc, doc };
  76. /**
  77. * Simple random string generation
  78. */
  79. const char availableChars[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
  80. int intN(int n) { return rand() % n; }
  81. char *randomString(int len) {
  82. char *rstr = malloc((len + 1) * sizeof(char));
  83. int i;
  84. for (i = 0; i < len; i++) {
  85. rstr[i] = availableChars[intN(strlen(availableChars))];
  86. }
  87. rstr[len] = '\0';
  88. return rstr;
  89. }
  90. /**
  91. * struct to hold the config options loaded from config file
  92. * Extend if the options file changes.
  93. */
  94. struct configOptions {
  95. char *secret;
  96. char *endpoint;
  97. };
  98. /**
  99. * struct to hold the returned data from the http post call
  100. * done with curl
  101. * see: https://curl.haxx.se/libcurl/c/getinmemory.html
  102. */
  103. struct MemoryStruct {
  104. char *memory;
  105. size_t size;
  106. };
  107. /**
  108. * callback function from the curl call
  109. * see: https://curl.haxx.se/libcurl/c/getinmemory.html
  110. */
  111. static size_t
  112. WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {
  113. struct MemoryStruct *mem = (struct MemoryStruct *)userp;
  114. size_t realsize = size * nmemb;
  115. char *ptr = realloc(mem->memory, mem->size + realsize + 1);
  116. if(ptr == NULL) {
  117. /* out of memory! */
  118. printf("not enough memory (realloc returned NULL)\n");
  119. return 0;
  120. }
  121. mem->memory = ptr;
  122. memcpy(&(mem->memory[mem->size]), contents, realsize);
  123. mem->size += realsize;
  124. mem->memory[mem->size] = 0;
  125. return realsize;
  126. }
  127. /**
  128. * make a post curl call to upload the given file
  129. * and receive the URL as a answer
  130. * see: https://curl.haxx.se/libcurl/c/getinmemory.html
  131. */
  132. int uploadCall(struct configOptions cfgo, struct cmdArguments arguments) {
  133. CURL *curl_handle;
  134. CURLcode res;
  135. struct MemoryStruct chunk;
  136. chunk.memory = malloc(1); /* will be grown as needed by the realloc above */
  137. chunk.size = 0; /* no data at this point */
  138. curl_global_init(CURL_GLOBAL_ALL);
  139. /* init the curl session */
  140. curl_handle = curl_easy_init();
  141. /* specify URL to get */
  142. curl_easy_setopt(curl_handle, CURLOPT_URL, cfgo.endpoint);
  143. /* send all data to this function */
  144. curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  145. /* we pass our 'chunk' struct to the callback function */
  146. curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
  147. /* some servers don't like requests that are made without a user-agent */
  148. /* field, so we provide one */
  149. curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "selfpaseCurlAgent/1.0");
  150. /* add the POST data */
  151. /* https://curl.haxx.se/libcurl/c/postit2.html */
  152. curl_mime *form = NULL;
  153. curl_mimepart *field = NULL;
  154. form = curl_mime_init(curl_handle);
  155. field = curl_mime_addpart(form);
  156. curl_mime_name(field, "pasty");
  157. curl_mime_filedata(field, arguments.args[0]);
  158. field = curl_mime_addpart(form);
  159. curl_mime_name(field, "dl");
  160. curl_mime_data(field, cfgo.secret, CURL_ZERO_TERMINATED);
  161. curl_easy_setopt(curl_handle, CURLOPT_MIMEPOST, form);
  162. /* execute it! */
  163. res = curl_easy_perform(curl_handle);
  164. /* check for errors */
  165. if(res != CURLE_OK || chunk.size < 1) {
  166. printf("ERROR: curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  167. exit(1);
  168. }
  169. json_object *json, *jsonWork;
  170. enum json_tokener_error jerr = json_tokener_success;
  171. if (chunk.memory != NULL) {
  172. if(arguments.verbose) printf("%lu bytes retrieved\n", (unsigned long)chunk.size);
  173. if(arguments.verbose) printf("CURL returned:\n%s\n", chunk.memory);
  174. /* https://gist.github.com/leprechau/e6b8fef41a153218e1f4 */
  175. json = json_tokener_parse_verbose(chunk.memory, &jerr);
  176. if (jerr == json_tokener_success) {
  177. jsonWork = json_object_object_get(json, "status");
  178. printf("Status: %s\n", json_object_get_string(jsonWork));
  179. jsonWork = json_object_object_get(json, "message");
  180. printf("selfpastelink: %s\n", json_object_get_string(jsonWork));
  181. }
  182. else {
  183. printf("ERROR: Invalid payload returned. Check your config:\n%s\n", chunk.memory);
  184. }
  185. }
  186. /* cleanup curl stuff */
  187. curl_easy_cleanup(curl_handle);
  188. curl_mime_free(form);
  189. free(chunk.memory);
  190. curl_global_cleanup();
  191. return 0;
  192. }
  193. /**
  194. * main routine
  195. */
  196. int main(int argc, char *argv[]) {
  197. srand(time(NULL));
  198. /**
  199. * command line argument parsing and default values
  200. */
  201. struct cmdArguments arguments;
  202. arguments.quiet = 0;
  203. arguments.verbose = 0;
  204. arguments.create_config_file = 0;
  205. argp_parse (&argp, argc, argv, 0, 0, &arguments);
  206. if(arguments.verbose) {
  207. printf ("File = %s\n"
  208. "Verbose = %s\n"
  209. "Quiet = %s\n"
  210. "Create config file = %s\n",
  211. arguments.args[0],
  212. arguments.verbose ? "yes" : "no",
  213. arguments.quiet ? "yes" : "no",
  214. arguments.create_config_file ? "yes" : "no"
  215. );
  216. }
  217. /**
  218. * Config file check.
  219. * Also create if non is available and command line option
  220. * to create it is set.
  221. */
  222. char* homedir = getenv("HOME");
  223. if ( homedir == NULL ) {
  224. homedir = getpwuid(getuid())->pw_dir;
  225. }
  226. if(homedir[0] == '\0') {
  227. printf("ERROR: $HOME directory not found?\n");
  228. return(1);
  229. }
  230. if(arguments.verbose) printf("Homedir: %s\n", homedir);
  231. char configFileName[16] = "/.selfpaste.cfg";
  232. if(arguments.verbose) printf("Config file name: '%s'\n", configFileName);
  233. char configFilePath[strlen(homedir) + strlen(configFileName)];
  234. strcpy(configFilePath, homedir);
  235. strcat(configFilePath, configFileName);
  236. if(arguments.verbose) printf("Configfilepath: '%s'\n", configFilePath);
  237. if(access(configFilePath, F_OK) != -1) {
  238. if(arguments.verbose) printf("Using configfile: '%s'\n", configFilePath);
  239. if(arguments.create_config_file == 1) {
  240. printf("INFO: Re creating configfile by manually deleting it.\n");
  241. return(1);
  242. }
  243. } else {
  244. printf("ERROR: Configfile '%s' not found.\n",configFilePath);
  245. if(arguments.create_config_file == 1) {
  246. printf("Creating configfile: '%s'\n", configFilePath);
  247. FILE *fp = fopen(configFilePath, "w");
  248. if (fp) {
  249. fputs("# selfpaste config file.\n", fp);
  250. fprintf(fp, "# See %s for more details.\n", argp_program_bug_address);
  251. fprintf(fp, "# Version: %s\n", argp_program_version);
  252. fprintf(fp, "SELFPASTE_UPLOAD_SECRET=%s\n", randomString(50));
  253. fputs("ENDPOINT=http://you-seflpaste-endpoi.nt\n", fp);
  254. fclose(fp);
  255. printf("Config file '%s' created.\nPlease update your settings!\n", configFilePath);
  256. return(0);
  257. }
  258. else {
  259. printf("ERROR: Configfile '%s' could not be written.\n",configFilePath);
  260. }
  261. }
  262. exit(1);
  263. }
  264. /**
  265. * Reading and parsing the config file.
  266. * populate configOptions struct
  267. *
  268. * https://rosettacode.org/wiki/Read_a_configuration_file#C
  269. * https://github.com/welljsjs/Config-Parser-C
  270. * https://hyperrealm.github.io/libconfig/
  271. * https://www.gnu.org/software/libc/manual/html_node/Finding-Tokens-in-a-String.html
  272. */
  273. struct configOptions configOptions;
  274. FILE* fp;
  275. if ((fp = fopen(configFilePath, "r")) == NULL) {
  276. printf("ERROR: Configfile '%s' could not be opened.\n",configFilePath);
  277. return(1);
  278. }
  279. if(arguments.verbose) printf("Reading configfile: '%s'\n", configFilePath);
  280. char line[128];
  281. char *optKey,*optValue, *workwith;
  282. while (fgets(line, sizeof line, fp) != NULL ) {
  283. if(arguments.verbose) printf("- Line: %s", line);
  284. if (line[0] == '#') continue;
  285. /* important. strok modifies the string it works with */
  286. workwith = strdup(line);
  287. optKey = strtok(workwith, "=");
  288. if(arguments.verbose) printf("Option: %s\n", optKey);
  289. optValue = strtok(NULL, "\n\r");
  290. if(arguments.verbose) printf("Value: %s\n", optValue);
  291. if(strcmp("ENDPOINT",optKey) == 0) {
  292. configOptions.endpoint = optValue;
  293. }
  294. if(strcmp("SELFPASTE_UPLOAD_SECRET",optKey) == 0) {
  295. configOptions.secret = optValue;
  296. }
  297. }
  298. fclose(fp);
  299. if(arguments.verbose) {
  300. printf("Using\n- Secret: %s\n- Endpoint: %s\n- File: %s\n",
  301. configOptions.secret,configOptions.endpoint,
  302. arguments.args[0]);
  303. }
  304. /* do the upload */
  305. uploadCall(configOptions, arguments);
  306. return(0);
  307. }