summaryrefslogtreecommitdiffstats
path: root/fastback.cpp
blob: efe666e0c781007af62128f9820a7612ac655e2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619


/* 
The file is named on the command line.

An option to the command will cause the file to be encrypted before upload, and the output of the command will include the encryption key used.

An option to the command will allow the user to associate a ticket name/number with the file.

$ fastback FILE [ -t TICKET | -n ] [ -e ]

where FILE is the file to be uploaded; where -e indicates that the file should be encrypted before it's uploaded; where -n indicates a new ticket; where TICKET is the name of the ticket

If -t is not specified, -n is assumed.

The name of the uploaded file will be the FILE name, prefixed with the TICKET name, and suffixed with a string of random characters to make the file unique.

The file will be compressed if it is not already compressed.

The configuration file is called fastback.conf.

The program keeps a log of all files uploaded, where they were uploaded to, encryption key used (if any), and a log of the messages from the transfer.
*/



// $ fastback FILE [ -t TICKET | -n ] [ -e ]
//    where FILE is the file to be uploaded
//    where -e indicates that the file should be encrypted
//    where -n indicates a new ticket
//    where TICKET is the name of the ticket
//
//    If -t is not specified, -n is assumed.
//

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <string>
#include <fstream>
#include <sstream>
#include <ext/stdio_filebuf.h>

#include <iostream>

#include <sys/types.h>
#include <regex.h>
#include <stdlib.h>
#include <string.h>
#include <argp.h>
#include <curl/curl.h>

#include <openssl/rand.h>

typedef std::string string;


static const char fastback_name[] = "fastback";

// These are command line options
//    if set they must be malloc'ed memory.
static char* fastback_filename = 0;           // local file to be uploaded
static char* fastback_ticket = 0;         // ticket to upload to
static char* fastback_URLDIR = 0;


static bool  fastback_encrypt = false;    // encrypt file before upload

// '-n' option explicitly set
static bool  fastback_newticket = false;

static bool fastback_verbose = false;
static bool fastback_logging = false;


// if we need to create temporary files
//   first create a temporary directory, stick it's name here
//   and delete the whole tree when we are done
static string fastback_tmpdir;



//static const char* fastback_default_URLDIR = "ftp://dropbox.redhat.com/incoming/";
static const char* fastback_default_URLDIR = "ftp://indus.usersys.redhat.com/incoming";




static size_t 
fastback_read(void *buffer, size_t size, size_t nmemb, void *userp)
{
  return fread(buffer, size, nmemb, (FILE*)userp);
}

static void
show(FILE* file, string url)
{
  if (fastback_URLDIR)
    fprintf(file,"fastback URLDIR: %s\n", fastback_URLDIR);
  else
    fprintf(file,"fastback URLDIR not set\n");

  if (fastback_filename)
    fprintf(file,"fastback file: %s\n", fastback_filename);
    
  if (fastback_ticket)
    fprintf(file,"fastback ticket: %s\n", fastback_ticket);

  else
    {
      if (fastback_newticket)
        fprintf(file,"fastback new ticket explicitly set\n");
      else
        fprintf(file,"fastback new ticket by default\n");
    }
  
  if (fastback_encrypt)
    fprintf(file,"encrypt file\n");
  else
    fprintf(file,"don't encrypt file\n");

  fprintf(file,"fastback URL: %s\n", url.c_str());
}      

static void
check_curl_error(CURLcode err, string url, const char* msg)
{
  if (err)
    {
      show(stderr, url);
      string tmsg = fastback_name;
      tmsg += ": error: ";
      tmsg += msg;
      tmsg += ": ";
      tmsg += curl_easy_strerror(err);
      tmsg += "\n";
      fprintf(stderr,tmsg.c_str());
      exit(4);
    }
}

static error_t
fastback_argp_parser (int key, char *arg, struct argp_state *state)
{
  switch (key)
    {
    case ARGP_KEY_ARG:
      if (fastback_filename)
        argp_error(state,"multiple FILE arguments specified");
      fastback_filename = strdup(arg);
      break;
    case 'e':
      fastback_encrypt = true;
      break;
    case 'n':
      if (fastback_ticket)
        argp_error(state, "invalid options: -n and -t conflict");
      fastback_newticket = true;
      break;
    case 't':
      if (fastback_newticket)
        argp_error(state, "invalid options: -n and -t conflict");
      else
        fastback_ticket = strdup(arg);
      break;
    case 'v': 
      fastback_verbose = true;
      break;
    case 'l': 
      fastback_logging = true;
      break;
    default:
      return ARGP_ERR_UNKNOWN;
    }
  return 0;
}

static struct argp_option fastback_options[] = {
  {"ticket",'t', "TICKET", 0, "the ticket to associate FILE with"}, 
  {0,'n',0,0,"create a new ticket for FILE"},
  {"encrypt",'e',0,0,"encrypt FILE before uploading"},
  {0,'v',0,0,"be verbose"},
  {"logging",'l',0,0,"print logfile to stderr"},
  { 0 }};

static struct argp fastback_argp = { 
  fastback_options, 
  fastback_argp_parser,
  "FILE"
};

static bool
option_parse(const char* filename,
             const char* line, 
             size_t var_start, size_t var_end,
             size_t value_start, size_t value_end)
{
  bool config_file_error = false;
  const char option1[] = "URLDIR";
  size_t option_length = strlen(option1);
  
  if (option_length == (var_end - var_start)
      && memcmp(option1,line+var_start,option_length) == 0)
    {
      int value_length = value_end - value_start;
      fastback_URLDIR = (char*)malloc(value_length + 1);
      memcpy(fastback_URLDIR, line+value_start, value_length);
      fastback_URLDIR[value_length] = 0;
    }

  else
    {
      int var_length = var_end - var_start;
      char* var = (char*)malloc(var_length + 1);
      memcpy(var, line+var_start, var_length);
      var[var_length] = 0;

      fprintf(stderr,"%s: %s\n", filename, line);
      fprintf(stderr, "%s: error: unknown config option: %s\n", 
              fastback_name, var);
      config_file_error = true;
      free(var);
    }

  return config_file_error;
}



static void
error_regerror (int errcode, regex_t *compiled, const char* func)
{
  size_t length = regerror (errcode, compiled, NULL, 0);
  char *buffer = (char*)malloc (length);
  (void) regerror (errcode, compiled, buffer, length);
  fprintf(stderr, "%s: error: %s: %s\n", fastback_name, func, buffer);
  free(buffer);
}

static bool
parse_config(const char* filename)
{
  int config_file_error = 0;
  int err;
  regex_t regexp;
  FILE* file;
  char *line;
  size_t linesize;
  ssize_t readcount;

  const char pattern[] = 
    "^[[:space:]]*"
    "\\(\\|#.*\\|"
    "\\([[:alpha:]][[:alnum:]]*\\)[[:space:]]*="
    "[[:space:]]*\\(\"\\([^\"]*\\)\"\\|\\([^[:space:]]*\\)\\)[[:space:]]*"
    "\\)$";
  const bool debug_pattern = false;

  err = regcomp( &regexp, pattern, 0);
  if (err)
    {
      error_regerror(err, &regexp, "regcomp");
      exit(4);
    }

  size_t matchsize = regexp.re_nsub + 1;
  regmatch_t* matchptr = (regmatch_t*)malloc(sizeof(regmatch_t) * matchsize);

  file = fopen(filename,"r");
  if (!file)
    {
      string msg = fastback_name;
      msg += ": error: could not open: ";
      msg += filename;
      perror(msg.c_str());
      exit(4);
    }

  line = 0;
  linesize = 0;
  readcount = getline(&line, &linesize, file);
  while (readcount != -1)
    {
      /* trim the newline */
      if (readcount != 0 && line[readcount-1] == '\n')
        line[readcount-1] = 0;

      err = regexec( &regexp, line, matchsize, matchptr, 0);
      if (err == 0)
        {
          if (debug_pattern)
            {
              int i;
              fprintf(stderr,"%s: debug: line: %s: %s\n", 
                      fastback_name, filename, line);
              for (i=0; i < matchsize; i++)
                if (matchptr[i].rm_so == -1)
                  fprintf(stderr,"%s: debug: match (%d): no match\n", 
                          fastback_name, i);
                else
                  {
                    int j;
                    char buf[1000];
                    for(j=0; j < (matchptr[i].rm_eo - matchptr[i].rm_so); j++)
                      buf[j] = line[matchptr[i].rm_so + j];
                    buf[j] = 0;
                    if (j >= 1000)
                      {
                        fprintf(stderr,"INTERNAL ERROR: J too large\n");
                        exit(4);
                      }
                    fprintf(stderr, "%s: debug: match (%d): \"%s\"\n", 
                            fastback_name, i, buf);
                  }
            }

          if (matchptr[2].rm_so != -1)
            {
              if (matchptr[4].rm_so != -1)
                config_file_error |=
                  option_parse(filename, line, 
                               matchptr[2].rm_so, matchptr[2].rm_eo, 
                               matchptr[4].rm_so, matchptr[4].rm_eo);
              
              else if (matchptr[5].rm_so != -1)
                config_file_error |=
                  option_parse(filename, line, 
                               matchptr[2].rm_so, matchptr[2].rm_eo, 
                               matchptr[5].rm_so, matchptr[5].rm_eo);
            }
        }      
      else if (err == REG_NOMATCH)
        {
          fprintf(stderr,"%s: %s\n", filename, line);
          fprintf(stderr,"%s: error: invalid config line\n", fastback_name);
          config_file_error = true;
        }
      else
        {
          fprintf(stderr,"%s: %s\n", filename, line);
          error_regerror(err, &regexp, "regexe");
          exit(4);
        }

      readcount = getline(&line, &linesize, file);
    }

  fclose(file);
  return config_file_error;
}

void Error(string func, string msg)
{
  std::cerr << func << msg << std::endl;
  exit(4);
}

void RunCommand(string cmd)
{
    int retcode = system(cmd.c_str());
    if (retcode == -1)
    {
        Error("RunCommand:", "error: could not start subshell: " + cmd);
    }
    if (retcode)
    {
        std::ostringstream msg;
        msg << "error: subshell failed (rc=" << retcode << "):" << cmd;
        Error("RunCommand:", msg.str());
    }        
}

string ReadCommand(string cmd)
{
    FILE* fp = popen(cmd.c_str(),"r");
    if (!fp)
    {
        Error("ReadCommand:", "error: could not start subshell: " + cmd); 
    }

    __gnu_cxx::stdio_filebuf<char> command_output_buffer(fp, std::ios_base::in);
    std::ostringstream output_stream;
    output_stream << &command_output_buffer;

    int retcode = pclose(fp);
    if (retcode)
    {
        std::ostringstream msg;
	msg << "error: subshell failed (rc=" << retcode << "):" << cmd;
	Error("ReadCommand:", msg.str());
    }        

    return output_stream.str();
}

static string rand_base64(int i)
{
  // return a string of random base64 characters of 'i' length
  // 
  // This should probably use the openssl library (or some other 
  // library directly, but it doesn't right now
  std::ostringstream cmd;
  cmd << "openssl rand -base64 " << i;
  string r = ReadCommand(cmd.str()); 
  return r.substr(0,r.length()-1);
}


static string 
randomize_filename(string filename)
{
  // create (max_rand_bytes * 8) random bits encoded as base64
  // add those bits to the filename before the first dot if any.
  const int max_rand_bytes = 3;  // 24 random bits
  string::size_type p = filename.find_first_of('.');
  string r = rand_base64(max_rand_bytes);
  if (p == string::npos)
    return filename + '-' + r;
  else
    return filename.substr(0,p) + '-' + r + filename.substr(p,string::npos);
}

static bool
is_compressed(const string& filename)
{
  string cmd = string("file ") + filename;
  string output = ReadCommand(cmd);

  return output.find("compressed") != string::npos;
}


static void
compress(const string& in_filename, const string& out_filename)
{
  // compress file 'in_filename' into 'out_filename'
  //  again this should probably use a library, but doesn't yet
  string cmd = string("gzip <") + in_filename + " >" + out_filename;
  ReadCommand(cmd);
}

static bool
readable(string filename)
{
  // is 'filename' readable by the running user
  FILE* file = fopen(filename.c_str(),"r");
  if (file)
    {
      fclose(file);
      return true;
    }
  return false;
}

static string 
filename_basename(const string& filename)
{
  return basename(filename.c_str());
}

static string
create_temporary_file(string filename)
{
  // create a filename in a temporary directory
  //   for now you must make sure that the name is unique to this run
  if (fastback_tmpdir == "")
    {
      char TEMPLATE[] = "/tmp/fastbackXXXXXX";
      fastback_tmpdir = mkdtemp(TEMPLATE);
    }
  return fastback_tmpdir + '/' + filename;
}

static void
cleanup()
{
  free(fastback_filename);
  free(fastback_ticket);
  free(fastback_URLDIR);

  if (fastback_tmpdir != "")
    RunCommand(string("rm -rf " + fastback_tmpdir));
}


int
main(int argc, char** argv)
{
  error_t err;

  err = argp_parse( &fastback_argp, argc, argv, 0, 0, 0);
  if (err)
    {
      if (errno == err)
        {
          string msg = fastback_name;
          msg += ": error: argp_parse";
          perror(msg.c_str());
        }
      else
        fprintf(stderr, "%s: error from argp_parse: error code %d\n", 
                fastback_name, err);
      cleanup();
      exit(2);
    }

  if (!fastback_filename)
    {
      fprintf(stderr, "%s: error: no FILE given on command line\n", 
              fastback_name);
      fprintf(stderr, 
              "Try `%s --help' or `fastback --usage' for more information.\n", 
              fastback_name);
      exit(2);
    }

  if (parse_config("fastback.conf"))
    exit(2);

  if (!fastback_URLDIR)
    fastback_URLDIR = strdup(fastback_default_URLDIR);

  if (!readable(fastback_filename))
    {
      string msg = fastback_name;
      msg += ": error: could not open: ";
      msg += fastback_filename;
      perror(msg.c_str());
      exit(3);
    }
    
  string final_filename = fastback_filename;

  if (!is_compressed(final_filename))
    {
      string compressed_filename = 
        create_temporary_file(filename_basename(final_filename) + ".gz");
      compress(final_filename,compressed_filename);
      final_filename = compressed_filename;
    }

  CURL* handle;
  CURLcode curl_err;
  FILE* file;
  string url;

  file = fopen(final_filename.c_str(),"r");
  if (!file)
    {
      string msg = fastback_name;
      msg += ": error: could not open: ";
      msg += final_filename;
      perror(msg.c_str());
      exit(3);
    }

  url = fastback_URLDIR;
  if (url[url.length()-1] != '/')
    url += '/';
  url += randomize_filename(filename_basename(final_filename.c_str()));

  if (fastback_verbose)
    show(stdout,url);

  if (fastback_logging)
    show(stderr,url);

  if (curl_global_init(CURL_GLOBAL_ALL))
    {
      fprintf(stderr,"%s: error: curl_global_init: could not initialze curl\n", 
              fastback_name);
      exit(3);
    }

  handle = curl_easy_init();
  if (!handle)
    {
      fprintf(stderr,"%s: error: curl_easy_init: could not initialize curl\n", fastback_name);
      exit(3);
    }

  // note that it is the fastback logging option which invokes curl verbose
  curl_err = curl_easy_setopt(handle, CURLOPT_VERBOSE, fastback_logging);
  check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_VERBOSE)");
  
  curl_err = curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
  check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_URL)");

  curl_err = curl_easy_setopt(handle, CURLOPT_READFUNCTION, fastback_read); 
  check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_READFUNCTION)");

  curl_err = curl_easy_setopt(handle, CURLOPT_READDATA, (void*)file); 
  check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_READDATA)");

  curl_err = curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L);
  check_curl_error(curl_err, url, "curl_easy_setopt(CURLOPT_READDATA)");

  curl_err = curl_easy_perform(handle); 
  check_curl_error(curl_err, url, "curl_easy_perform");

  curl_easy_cleanup(handle);
  fclose(file);

  if (fastback_ticket)
    printf("Please paste this into %s:\n", fastback_ticket);
  else
    printf("Please create a new ticket, and paste this into it:\n");
  printf("FASTBACK:\n");
  if (fastback_ticket)
    printf("TICKET: %s\n", fastback_ticket);
  printf("UPLOAD: %s\n", url.c_str());
  printf("END:\n");

  cleanup();
  return 0;
}