[go: up one dir, main page]

File: chirp_ticket.c

package info (click to toggle)
cctools 7.1.2-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 43,744 kB
  • sloc: ansic: 187,336; cpp: 20,196; python: 18,633; sh: 11,159; xml: 3,688; perl: 2,817; makefile: 1,105
file content (238 lines) | stat: -rw-r--r-- 6,682 bytes parent folder | download | duplicates (3)
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
/*
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
Copyright (C) 2005- The University of Notre Dame
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/

#include "chirp_ticket.h"

#include "buffer.h"
#include "md5.h"
#include "xxmalloc.h"

#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>

#define DIGEST_SCAN  "%n%32[0123456789abcdefABCDEF]%n"
#define DIGEST_FORMAT  "%32s"
#define TICKET_PREFIX  ".__"
#define TICKET_PREFIX_LENGTH  (sizeof(TICKET_PREFIX)/sizeof(char))
#define TICKET_FILENAME_FORMAT TICKET_PREFIX "ticket." DIGEST_FORMAT
#define TICKET_FILENAME_SCAN TICKET_PREFIX "ticket." DIGEST_SCAN
#define TICKET_SUBJECT_FORMAT "ticket:" DIGEST_FORMAT
#define TICKET_SUBJECT_SCAN "ticket:" DIGEST_SCAN
#define TICKET_DIGEST_LENGTH  (MD5_DIGEST_LENGTH_HEX)

#define unsigned_isspace(c) isspace((unsigned char) c)

static int readquote(const char **buffer, const char **s, size_t * l)
{
	while(unsigned_isspace(**buffer))
		(*buffer)++;
	if(**buffer != '"')
		return 0;
	(*buffer)++;
	*l = 0;
	*s = *buffer;
	while(**buffer != '"' && **buffer != '\0') {
		(*buffer)++;
		(*l)++;
	}
	if(**buffer != '"')
		return 0;
	(*buffer)++;
	return 1;
}

void chirp_ticket_free(struct chirp_ticket *ct)
{
	size_t n = 0;
	free(ct->subject);
	free(ct->ticket);
	while(n < ct->nrights) {
		free(ct->rights[n].directory);
		free(ct->rights[n].acl);
		n++;
	}
	free(ct->rights);
}

char *chirp_ticket_tostring(struct chirp_ticket *ct)
{
	size_t n;
	char *result;
	buffer_t B[1];

	buffer_init(B);
	buffer_abortonfailure(B, 1);

	buffer_putfstring(B, "subject \"%s\"\n", ct->subject);
	buffer_putfstring(B, "ticket \"%s\"\n", ct->ticket);
	buffer_putfstring(B, "expiration \"%lu\"\n", (unsigned long) ct->expiration);
	for(n = 0; n < ct->nrights; n++) {
		buffer_putfstring(B, "rights \"%s\" \"%s\"\n", ct->rights[n].directory, ct->rights[n].acl);
	}

	buffer_dup(B, &result);
	buffer_free(B);

	return result;
}

int chirp_ticket_read(const char *ticket, struct chirp_ticket *ct)
{
	int status = 0;

	const char *b = ticket;
	size_t l = strlen(ticket);

	/* Ticket format (quoted strings may span multiple lines):
	 * subject "<subject>"
	 * ticket "<ticket>"
	 * expiration "<UTC seconds since Epoch>"
	 * rights "<directory>" "<acl>"
	 * rights "<directory>" "<acl>"
	 * ...
	 */
	size_t n;
	const char *s;
	const char *buffer = b;
	time_t now = time(NULL);
	ct->subject = NULL;
	ct->ticket = NULL;
	ct->expiration = now;	/* default expire now... */
	ct->expired = 1;	/* default is expired */
	ct->nrights = 0;
	ct->rights = NULL;
	while(1) {
		assert(buffer >= b && b + l >= buffer);
		while(unsigned_isspace(*buffer))
			buffer++;
		assert(buffer >= b && b + l >= buffer);
		if(strncmp(buffer, "subject", strlen("subject")) == 0) {
			buffer += strlen("subject");
			if(!readquote(&buffer, &s, &n))
				break;
			ct->subject = xxrealloc(ct->subject, n + 1);
			memcpy(ct->subject, s, n);
			ct->subject[n] = '\0';
		} else if(strncmp(buffer, "ticket", strlen("ticket")) == 0) {
			buffer += strlen("ticket");
			if(!readquote(&buffer, &s, &n))
				break;
			ct->ticket = xxrealloc(ct->ticket, n + 1);
			memcpy(ct->ticket, s, n);
			ct->ticket[n] = '\0';
		} else if(strncmp(buffer, "expiration", strlen("expiration")) == 0) {
			buffer += strlen("expiration");
			if(!readquote(&buffer, &s, &n))
				break;
			char *stime = xxmalloc(n + 1);
			memcpy(stime, s, n);
			stime[n] = '\0';
			ct->expiration = (time_t) strtoul(stime, NULL, 10);
			ct->expired = (ct->expiration <= now);
			free(stime);
		} else if(strncmp(buffer, "rights", strlen("rights")) == 0) {
			buffer += strlen("rights");
			if(!readquote(&buffer, &s, &n))
				break;
			ct->rights = xxrealloc(ct->rights, sizeof(*ct->rights) * (++ct->nrights) + 1);
			ct->rights[ct->nrights - 1].directory = xxmalloc(n + 1);
			memcpy(ct->rights[ct->nrights - 1].directory, s, n);
			ct->rights[ct->nrights - 1].directory[n] = '\0';
			if(!readquote(&buffer, &s, &n))
				break;
			char *acl = xxmalloc(n + 1);
			memcpy(acl, s, n);
			acl[n] = '\0';
			ct->rights[ct->nrights - 1].acl = xxstrdup(acl);
			free(acl);
		} else if(*buffer == '\0') {
			if(ct->subject && ct->ticket && ct->nrights > 0) {
				status = 1;
			}
			break;
		} else {
			break;
		}
	}
	if(ct->rights == NULL) {
		assert(ct->nrights == 0);
		ct->rights = xxrealloc(ct->rights, sizeof(*ct->rights) * (++ct->nrights) + 1);
		ct->rights[ct->nrights - 1].directory = xxstrdup("/");
		ct->rights[ct->nrights - 1].acl = xxstrdup("n");
		ct->nrights = 1;
	}
	return status && !ct->expired;
}

void chirp_ticket_name(const char *pk, char *ticket_subject, char *ticket_filename)
{
	unsigned char digest[TICKET_DIGEST_LENGTH];
	md5_context_t context;
	md5_init(&context);
	md5_update(&context, (const unsigned char *) pk, strlen(pk));
	md5_final(digest, &context);
	sprintf(ticket_subject, TICKET_SUBJECT_FORMAT, md5_string(digest));
	sprintf(ticket_filename, "/" TICKET_FILENAME_FORMAT, md5_string(digest));
}

void chirp_ticket_filename(char *ticket_filename, const char *ticket_subject, const char *digest)
{
	if(digest == NULL) {
		assert(ticket_subject);
		int result = chirp_ticket_isticketsubject(ticket_subject, &digest);
		assert(result);
	}
	sprintf(ticket_filename, "/" TICKET_FILENAME_FORMAT, digest);
}

void chirp_ticket_subject(char *ticket_subject, const char *ticket_filename)
{
	const char *digest;
	int result = chirp_ticket_isticketfilename(ticket_filename, &digest);
	assert(result);
	sprintf(ticket_subject, TICKET_SUBJECT_FORMAT, digest);
}

int chirp_ticket_isticketfilename(const char *ticket_filename, const char **digest)
{
	int n1, n2;
	char buffer[TICKET_DIGEST_LENGTH + 1];
	if((sscanf(ticket_filename, TICKET_FILENAME_SCAN, &n1, buffer, &n2) == 1) && ((n2 - n1) == TICKET_DIGEST_LENGTH) && (strlen(ticket_filename) == (size_t) n2)) {
		assert(n1 > 0);
		*digest = ticket_filename + n1;
		return 1;
	} else {
		return 0;
	}
}

int chirp_ticket_isticketsubject(const char *ticket_subject, const char **digest)
{
	int n1, n2;
	char buffer[TICKET_DIGEST_LENGTH + 1];
	if((sscanf(ticket_subject, TICKET_SUBJECT_SCAN, &n1, buffer, &n2) == 1) && ((n2 - n1) == TICKET_DIGEST_LENGTH) && (strlen(ticket_subject) == (size_t) n2)) {
		assert(n1 > 0);
		*digest = ticket_subject + n1;
		return 1;
	} else {
		return 0;
	}
}

const char *chirp_ticket_digest(const char *pk)
{
	unsigned char digest[TICKET_DIGEST_LENGTH];
	md5_context_t context;
	md5_init(&context);
	md5_update(&context, (const unsigned char *) pk, strlen(pk));
	md5_final(digest, &context);
	return md5_string(digest);	/* static memory */
}

/* vim: set noexpandtab tabstop=4: */