File: first_run.c

package info (click to toggle)
apf 0.8.4-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 1,724 kB
  • ctags: 1,013
  • sloc: ansic: 15,777; sh: 3,591; makefile: 89
file content (403 lines) | stat: -rw-r--r-- 9,989 bytes parent folder | download
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
/*
 * active port forwarder - software for secure forwarding
 * Copyright (C) 2003-2007 jeremian <jeremian [at] poczta.fm>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <pwd.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <assert.h>

static char* home_dir = NULL;
static char* home_dir_store = NULL;
static char* home_dir_key = NULL;
static char* home_dir_cer = NULL;

typedef struct entry
{ 
  char *key;
  unsigned char *value;
} entryT;

static 
entryT entries[6] = {
  {"countryName", (unsigned char*) "PL"},
  {"stateOrProvinceName", (unsigned char*) "War-Maz"},
  {"localityName", (unsigned char*) "Olsztyn"},
  {"organizationName", (unsigned char*) "gray-world.net"},
  {"organizationalUnitName", (unsigned char*) "APF team"},
  {"commonName", (unsigned char*) "Jeremian <jeremian [at] poczta [dot] fm>"},
};  

/*
 * Function name: callback
 * Description: Prints the info about rsa key generation events.
 * Arguments: i, j, k - described in the manual page about RSA_generate_key
 */

static void
callback(int i, int j, void* k)
{
  if (k == NULL) {
    printf("%d", i);
    fflush(stdout);
  }
}

/*
 * Function name: create_apf_dir
 * Description: Creates .apf directory in ~/ or apf directory locally.
 * Arguments: type - type of the directory to create:
 *                     0 - .apf in ~/
 *                     1 - apf in current dir
 * Returns: 0 - success,
 *          1 - problems with fetching user info,
 *          2 - home directory is not set,
 *          3 - calloc failure,
 *          4 - directory creation failure.
 */

int
create_apf_dir(char type)
{
  int length;
  struct stat buf;
  struct passwd *user = getpwuid(getuid());
  if (type == 0) {
    if (user == NULL) {
      return 1; /* some problems with fetching user info*/
    }
    if (user->pw_dir == NULL) {
      return 2; /* home directory is not set? */
    }
    if (home_dir) {
      free(home_dir);
      home_dir = NULL;
    }
    length = strlen(user->pw_dir);
    home_dir = calloc(1, length + 6);
    if (home_dir == NULL) {
      return 3; /* calloc failed */
    }
    strcpy(home_dir, user->pw_dir);
    if (home_dir[length] == '/') {
      strcpy(&home_dir[length], ".apf");
    }
    else {
      strcpy(&home_dir[length], "/.apf");
    }
    if (stat(home_dir, &buf)) {
      if (mkdir(home_dir, 0700)) {
        return 4; /* creating directory failed */
      }
    }
  }
  else {
    if (home_dir) {
      free(home_dir);
      home_dir = NULL;
    }
    home_dir = calloc(1, 4);
    if (home_dir == NULL) {
      return 3; /* calloc failed */
    }
    strcpy(home_dir, "apf");
    if (stat(home_dir, &buf)) {
      if (mkdir(home_dir, 0700)) {
        return 4; /* creating directory failed */
      }
    }
  }
  return 0;
}

/*
 * Function name: create_publickey_store
 * Description: Creates the file to store information about public keys.
 * Arguments: storefile - the pointer to filename
 * Returns: 0 - success,
 *          >0 - failure.
 */

int
create_publickey_store(char** storefile)
{
  int store_length, home_length;
  struct stat buf;
  FILE* store_file;
  assert(storefile != NULL);
  assert((*storefile) != NULL);
  /* check in local directory first */
  if (stat(*storefile, &buf) == 0) {
    return 0;
  }
  /* check in home_dir */
  store_length = strlen(*storefile);
  home_length = strlen(home_dir);
  if (home_dir_store) {
    free(home_dir_store);
    home_dir_store = NULL;
  }
  home_dir_store = calloc(1, home_length + store_length + 2);
  if (home_dir_store == NULL) {
    return 1; /* calloc failed */
  }
  strcpy(home_dir_store, home_dir);
  home_dir_store[home_length] = '/';
  strcpy(&home_dir_store[home_length+1], *storefile);
  *storefile = home_dir_store;
  store_file = fopen(home_dir_store, "a");
  if (store_file == NULL) {
    return 1;
  }
  fclose(store_file);
  if (stat(home_dir_store, &buf) == 0) {
    return 0;
  }
  return 2;
}

/*
 * Function name: generate_rsa_key
 * Description: Generates the RSA key.
 * Arguments: keyfile - the pointer to filename
 * Returns: 0 - success,
 *          >0 - failure.
 */

int
generate_rsa_key(char** keyfile)
{
  int key_length, home_length;
  RSA* rsa_key;
  FILE* rsa_file;
  struct stat buf;
  assert(keyfile != NULL);
  assert((*keyfile) != NULL);
  /* check in local directory first */
  if (stat(*keyfile, &buf) == 0) {
    return 0;
  }
  /* check in home_dir */
  key_length = strlen(*keyfile);
  home_length = strlen(home_dir);
  if (home_dir_key) {
    free(home_dir_key);
    home_dir_key = NULL;
  }
  home_dir_key = calloc(1, home_length + key_length + 2);
  if (home_dir_key == NULL) {
    return 1; /* calloc failed */
  }
  strcpy(home_dir_key, home_dir);
  home_dir_key[home_length] = '/';
  strcpy(&home_dir_key[home_length+1], *keyfile);
  *keyfile = home_dir_key;
  if (stat(home_dir_key, &buf) == 0) {
    return 0;
  }
  /* have to generate the key */
  printf("generating rsa key: 2048 bits\n");
  rsa_key = RSA_generate_key(2048, 65537, callback, NULL);
  if (RSA_check_key(rsa_key)==1) {
    printf("   OK!\n");
  }
  else {
    printf("   FAILED!\n");
    return 1;
  }

  rsa_file = fopen(home_dir_key, "a");
  PEM_write_RSAPrivateKey(rsa_file, rsa_key, NULL, NULL, 0, NULL, NULL);
  fclose(rsa_file);
  return 0;
}

/*
 * Function name: generate_certificate
 * Description: Generates X509 certificate.
 * Arguments: cerfile - the pointer to filename
 *            keyfile - the name of the file with key
 * Returns: 0 - success,
 *          >0 - failure.
 */

int
generate_certificate(char** cerfile, char* keyfile)
{
  int cer_length, home_length, i;
  struct stat buf;
  X509* cert;
  X509_REQ* req;
  X509_NAME* subj;
  RSA* rsa_key;
  EVP_PKEY* pkey;
  const EVP_MD *digest;
  FILE* fp;
  assert(cerfile != NULL);
  assert((*cerfile) != NULL);
  assert(keyfile != NULL);
  /* check in local directory first */
  if (stat(*cerfile, &buf) == 0) {
    return 0;
  }
  /* check in home_dir */
  cer_length = strlen(*cerfile);
  home_length = strlen(home_dir);
  if (home_dir_cer) {
    free(home_dir_cer);
    home_dir_cer = NULL;
  }
  home_dir_cer = calloc(1, home_length + cer_length + 2);
  if (home_dir_cer == NULL) {
    return 1; /* calloc failed */
  }
  strcpy(home_dir_cer, home_dir);
  home_dir_cer[home_length] = '/';
  strcpy(&home_dir_cer[home_length+1], *cerfile);
  *cerfile = home_dir_cer;
  if (stat(home_dir_cer, &buf) == 0) {
    return 0;
  }
  /* have to generate the certificate */
  printf("generating self signed certificate\n");
  fp = fopen(keyfile, "r");
  if (fp == NULL) {
    return 2; /* can't open keyfile */
  }
  rsa_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
  fclose(fp);
  if (rsa_key == NULL) {
    return 3; /* can't read RSAPrivateKey */
  }
  pkey = EVP_PKEY_new();
  if (pkey == NULL) {
    return 4; /* creating new pkey failed */
  }
  if (EVP_PKEY_set1_RSA(pkey, rsa_key) == 0) {
    return 5; /* setting rsa key failed */
  }
  req = X509_REQ_new();
  if (req == NULL) {
    return 6; /* creating new request failed */
  }
  X509_REQ_set_pubkey(req, pkey);
  subj = X509_NAME_new();
  if (subj == NULL) {
    return 7; /* creating new subject name failed */
  }

  for (i = 0; i < 6; i++)
  {
    int nid;
    X509_NAME_ENTRY *ent;

    if ((nid = OBJ_txt2nid(entries[i].key)) == NID_undef)
    {
      return 8; /* finding NID for a key failed */
    }
    ent = X509_NAME_ENTRY_create_by_NID(NULL, nid, MBSTRING_ASC,entries[i].value, -1);
    if (ent == NULL) {
      return 9; /* creating name entry from NID failed */
    }
    if (X509_NAME_add_entry(subj, ent, -1, 0) == 0) {
      return 10; /* adding entry to name failed */
    }
  }
  if (X509_REQ_set_subject_name(req, subj) == 0) {
    return 11; /* adding subject to request failed */
  }

  digest = EVP_sha1();

  if (X509_REQ_sign(req, pkey, digest) == 0) {
    return 12; /* signing request failed */
  }

  cert = X509_REQ_to_X509(req, 1000, pkey);
  
  if (X509_set_version(cert, 2L) == 0) {
    return 13; /* setting certificate version failed */
  }
  ASN1_INTEGER_set(X509_get_serialNumber(cert), 1);
  
  if (cert == NULL) {
    return 14; /* creating certificate failed */
  }

  if (X509_sign(cert, pkey, digest) == 0) {
    return 15; /* signing failed */
  }
  
  fp = fopen(home_dir_cer, "w");
  if (fp == NULL) {
    return 16; /* writing certificate failed */
  }
  PEM_write_X509(fp, cert);
  fclose(fp);
  
  EVP_PKEY_free(pkey);
  X509_REQ_free(req);
  X509_free(cert);
  return 0;
}

/*
 * Function name: get_store_filename
 * Description: Returns the name of the file for storing information about public keys.
 * Returns: The name of the file for storing information about public keys.
 */

char*
get_store_filename()
{
  return home_dir_store;
}

/*
 * Function name: get_key_filename
 * Description: Returns the name of the file with key.
 * Returns: The name of the file with key.
 */

char*
get_key_filename()
{
  return home_dir_key;
}

/*
 * Function name: get_cer_filename
 * Description: Returns the name of the file with certificate.
 * Returns: The name of the file with certificate.
 */

char*
get_cer_filename()
{
  return home_dir_cer;
}