summaryrefslogtreecommitdiffstats
path: root/fastback.cpp
blob: 4c464bf239b15a09e1e1dd0c47700614b269eaca (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


/* 
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 <sys/types.h>
#include <regex.h>
#include <stdlib.h>
#include <string.h>
#include <argp.h>

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

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

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

static error_t
fastback_argp_parser (int key, char *arg, struct argp_state *state)
{
  switch (key)
    {
    case ARGP_KEY_ARG:
      if (fastback_file)
        argp_error(state,"multiple FILE arguments specified");
      fastback_file = 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;
    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 }};

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


static char* fastback_URL;

static void
option_parse(const char* line, 
             size_t var_start, size_t var_end,
             size_t value_start, size_t value_end)
{
  const char option1[] = "URL";
  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_URL = (char*)malloc(value_length + 1);
      memcpy(fastback_URL, line+value_start, value_length);
      fastback_URL[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, "error: unknown config option: %s\n", var);
      free(var);
    }
}



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

static int
parse_config(const char* filename)
{
  int syntax_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 int debug_pattern = 0;

  err = regcomp( &regexp, pattern, 0);
  if (err)
    {
      error_regerror(err, &regexp, "error from 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)
    {
      perror("error opening file");
      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,"line: %s\n", line);
              for (i=0; i < matchsize; i++)
                if (matchptr[i].rm_so == -1)
                  fprintf(stderr,"match (%d): no match\n", 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,"J too large\n");
                        exit(4);
                      }
                    printf("match (%d): \"%s\"\n", i, buf);
                  }
            }

          if (matchptr[2].rm_so != -1)
            {
              if (matchptr[4].rm_so != -1)
                option_parse(line, 
                             matchptr[2].rm_so, matchptr[2].rm_eo, 
                             matchptr[4].rm_so, matchptr[4].rm_eo);
              
              else if (matchptr[5].rm_so != -1)
                option_parse(line, 
                             matchptr[2].rm_so, matchptr[2].rm_eo, 
                             matchptr[5].rm_so, matchptr[5].rm_eo);
              else
                fprintf(stderr,"config var set to nothing\n");
            }
        }      
      else if (err == REG_NOMATCH)
        {
          fprintf(stderr,"error invalid config line: %s\n", line);
          syntax_error = 1;
        }
      else
        {
          error_regerror(err, &regexp, "error from regcomp");
          exit(4);
        }

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

  fclose(file);
  return syntax_error;
}




static void
cleanup()
{
  free(fastback_file);
  free(fastback_ticket);
  free(fastback_URL);
}


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

  err = argp_parse( &fastback_argp, argc, argv, 0, 0, 0);
  if (err)
    {
      fprintf(stderr, "error from argp_parse: %d (errno: %d)\n", err, errno);
      perror("fastback:");
      cleanup();
      exit(2);
    }

  if (!fastback_file)
    {
      printf("error: fastback file not set\n");
      exit(2);
    }

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

  if (fastback_URL)
    printf("fastback URL: %s\n", fastback_URL);
  else
    printf("fastback URL not set\n");


  if (fastback_file)
    printf("fastback file: %s\n", fastback_file);
    
  if (fastback_ticket)
      printf("fastback ticket: %s\n", fastback_ticket);

  else
    {
      if (fastback_newticket)
        printf("fastback new ticket explicitly set\n");
      else
        printf("fastback new ticket by default\n");
    }

  if (fastback_encrypt)
    printf("encrypt file\n");
  else
    printf("don't encrypt file\n");

  cleanup();
  return 0;
}