File: submit.c

package info (click to toggle)
certmonger 0.79.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,428 kB
  • sloc: ansic: 59,314; sh: 6,767; xml: 2,486; makefile: 919; python: 564
file content (182 lines) | stat: -rw-r--r-- 4,580 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (C) 2009,2011 Red Hat, Inc.
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "../../src/config.h"

#include <sys/types.h>
#include <sys/select.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <krb5.h>

#include <talloc.h>

#include "../../src/log.h"
#include "../../src/store-int.h"
#include "../../src/store.h"
#include "../../src/submit.h"
#include "../../src/submit-e.h"
#include "../../src/submit-u.h"
#include "tools.h"

static void
wait_to_read(int fd)
{
	fd_set rfds;
	struct timeval tv;
	FD_ZERO(&rfds);
	FD_SET(fd, &rfds);
	tv.tv_sec = 1;
	tv.tv_usec = 0;
	select(fd + 1, &rfds, NULL, NULL, &tv);
}

int
main(int argc, char **argv)
{
	struct cm_submit_state *state;
	struct cm_store_ca *ca;
	struct cm_store_entry *entry;
	int fd, ret, i;
	void *parent;
	char *p;

#ifdef HAVE_UUID
	cm_submit_uuid_fixed_for_testing = 1; /* use fixed UUIDs */
#endif
	cm_log_set_method(cm_log_stderr);
	cm_log_set_level(3);
	cm_set_fips_from_env();
	parent = talloc_new(NULL);
	if (argc > 2) {
		ca = cm_store_files_ca_read(parent, argv[1]);
		if (ca == NULL) {
			printf("Error reading %s: %s.\n", argv[1],
			       strerror(errno));
			return -1;
		}
		entry = cm_store_files_entry_read(parent, argv[2]);
		if (entry == NULL) {
			printf("Error reading %s: %s.\n", argv[2],
			       strerror(errno));
			return -1;
		}
	} else {
		printf("Specify a CA file and an entry file as the two "
		       "arguments.\n");
		return -1;
	}
	state = cm_submit_start(ca, entry);
	if (state != NULL) {
		for (;;) {
			fd = cm_submit_get_fd(state);
			if (fd != -1) {
				wait_to_read(fd);
			} else {
				sleep(1);
			}
			if (cm_submit_ready(state) == 0) {
				break;
			}
		}
		if (cm_submit_issued(state) == 0) {
			while (strlen(entry->cm_cert) > 0) {
				i = strlen(entry->cm_cert) - 1;
				if (entry->cm_cert[i] == '\n') {
					entry->cm_cert[i] = '\0';
				} else {
					break;
				}
			}
			p = talloc_asprintf(entry, "%s\n", entry->cm_cert);
			talloc_free(entry->cm_cert);
			entry->cm_cert = p;
			printf("%s", entry->cm_cert);
			ret = CM_SUBMIT_STATUS_ISSUED;
		} else
		if (cm_submit_save_ca_cookie(state) == 0) {
			printf("Certificate not issued, saved a cookie.\n");
			ret = CM_SUBMIT_STATUS_WAIT;
		} else
		if (cm_submit_rejected(state) == 0) {
			if (entry->cm_ca_error != NULL) {
				printf("Request rejected: %s.\n",
				       entry->cm_ca_error);
			} else {
				printf("Request rejected.\n");
			}
			ret = CM_SUBMIT_STATUS_REJECTED;
		} else
		if (cm_submit_unreachable(state) == 0) {
			if (entry->cm_ca_error != NULL) {
				printf("CA was unreachable: %s.\n",
				       entry->cm_ca_error);
			} else {
				printf("CA was unreachable.\n");
			}
			ret = CM_SUBMIT_STATUS_UNREACHABLE;
		} else
		if (cm_submit_unconfigured(state) == 0) {
			if (entry->cm_ca_error != NULL) {
				printf("CA helper was un- or "
				       "under-configured: %s.\n",
				       entry->cm_ca_error);
			} else {
				printf("CA helper was un- or "
				       "under-configured.\n");
			}
			ret = CM_SUBMIT_STATUS_UNCONFIGURED;
		} else
		if (cm_submit_need_scep_messages(state) == 0) {
			if (entry->cm_ca_error != NULL) {
				printf("CA helper needs SCEP "
				       "messages: %s.\n",
				       entry->cm_ca_error);
			} else {
				printf("CA helper needs SCEP "
				       "messages.\n");
			}
			ret = CM_SUBMIT_STATUS_NEED_SCEP_MESSAGES;
		} else
		if (cm_submit_need_rekey(state) == 0) {
			if (entry->cm_ca_error != NULL) {
				printf("CA helper says we need to "
				       "rekey: %s.\n",
				       entry->cm_ca_error);
			} else {
				printf("CA helper says we need to "
				       "rekey.\n");
			}
			ret = CM_SUBMIT_STATUS_NEED_REKEY;
		} else {
			printf("Can't explain what happened.\n");
			ret = -1;
		}
		cm_submit_done(state);
	} else {
		printf("Failed to start.\n");
		ret = -1;
	}
	cm_store_entry_save(entry);
	cm_store_ca_save(ca);
	talloc_free(parent);
	return ret;
}