File: gen_crq.cpp

package info (click to toggle)
tqsllib 2.0-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,440 kB
  • ctags: 1,081
  • sloc: sh: 8,309; cpp: 7,978; xml: 4,065; makefile: 100
file content (132 lines) | stat: -rw-r--r-- 3,501 bytes parent folder | download | duplicates (5)
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
/***************************************************************************
                          gen_crq.cpp  -  description
                             -------------------
    begin                : Sat Dec 14 2002
    copyright            : (C) 2002 by ARRL
    author               : Jon Bloom
    email                : jbloom@arrl.org
    revision             : $Id: gen_crq.cpp,v 1.2 2003/08/18 17:06:33 ke3z Exp $

 Generates a set of certificate-request files.

 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "sysconfig.h"
#endif

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <unistd.h>
#include <exception>
#ifdef HAVE_GETOPT_H
	#include <getopt.h>
#endif
#include "tqsllib.h"
#include "tqslexc.h"

using namespace std;

int
usage() {
	cerr << "Usage: -e email -d dxcc [-c sign_call] [-x sign_dxcc] call1 [call2 ...]" << endl;
	exit(EXIT_FAILURE);
}

int
main(int argc, char *argv[]) {
	string sign_call, email_addr;
	int dxcc = 0, sign_dxcc = 0;
	tQSL_Cert sign_cert = 0;

	try {
		if (tqsl_init())
			throw tqslexc();
		int c;
		while ((c = getopt(argc, argv, "c:x:e:d:")) != -1) {
			switch (c) {
				case 'c':
					sign_call = optarg;
					break;
				case 'x':
					sign_dxcc = atoi(optarg);
					break;
				case 'd':
					dxcc = atoi(optarg);
					break;
				case 'e':
					email_addr = optarg;
					break;
				default:
					usage();
			}
		}
		if (optind >= argc || email_addr == "" || dxcc == 0)
			usage();
		if (sign_call != "") {
//			if (sign_dxcc == 0)
//				usage();
			tQSL_Cert *list;
			int ncerts;
			if (tqsl_selectCertificates(&list, &ncerts, sign_call.c_str(), sign_dxcc, 0, 0, 1))
				throw tqslexc();
			if (ncerts < 1) {
				string erm = "No signing certificate found for " + sign_call;
				if (sign_dxcc) {
					char buf[30];
					sprintf(buf, " with DXCC=%d", sign_dxcc);
					erm += buf;
				}
				throw myexc(erm);
			}
			sign_cert = *list;
		} else if (sign_dxcc != 0)
			usage();
		if (sign_cert) {
			char buf[512];
			long serial;
			int cdxcc;
			if (tqsl_getCertificateIssuer(sign_cert, buf, sizeof buf))
				throw tqslexc();
			if (tqsl_getCertificateSerial(sign_cert, &serial))
				throw tqslexc();
			if (tqsl_getCertificateDXCCEntity(sign_cert, &cdxcc))
				throw tqslexc();
			cout << "Signing certificate issuer: " << buf << endl;
			cout << "Signing certificate serial: " << serial << endl;
			cout << "  Signing certificate DXCC: " << cdxcc << endl;
			if (tqsl_beginSigning(sign_cert, "", 0, 0))
				throw tqslexc();
		}
		TQSL_CERT_REQ crq;
		memset(&crq, 0, sizeof crq);
		strcpy(crq.name, "Ish Kabibble");
		strcpy(crq.address1, "1 No Place");
		strcpy(crq.city, "City");
		strcpy(crq.state, "ST");
		strcpy(crq.country, "USA");
		strcpy(crq.emailAddress, email_addr.c_str());
		crq.dxccEntity = dxcc;
		tqsl_initDate(&crq.qsoNotBefore, "1945-11-15");
		crq.signer = sign_cert;
		for (; optind < argc; optind++) {
			string call = argv[optind];
			strcpy(crq.callSign, call.c_str());
			for (char *cp = argv[optind]; *cp; cp++) {
				if (*cp == '/')
					*cp = '_';
			}
			string filename = string(argv[optind]) + ".tq5";
			cout << "Creating CRQ for " << crq.callSign << " DXCC=" << crq.dxccEntity << endl;
			if (tqsl_createCertRequest(filename.c_str(), &crq, 0, 0))
				throw tqslexc();
		}
		return EXIT_SUCCESS;
	} catch (exception& x) {
		cerr << "Aborting: " << x.what() << endl;
		return EXIT_FAILURE;
	}
	return EXIT_SUCCESS;
}