summaryrefslogtreecommitdiffstats
path: root/comm/third_party/libotr/src/instag.c
blob: cccd94fb6c04e5434c0d65b0825ef5dc9efc30bc (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
/*
 *  Off-the-Record Messaging library
 *  Copyright (C) 2004-2015  Ian Goldberg, Rob Smits, Chris Alexander,
 *  			      Willy Lew, Lisa Du, Nikita Borisov
 *                           <otr@cypherpunks.ca>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of version 2.1 of the GNU Lesser General
 *  Public License as published by the Free Software Foundation.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/* system headers */
#include <stdio.h>
#include <stdlib.h>

/* libgcrypt headers */
#include <gcrypt.h>

/* libotr headers */
#include "instag.h"
#include "userstate.h"

/* Forget the given instag. */
void otrl_instag_forget(OtrlInsTag* instag) {
    if (!instag) return;

    if (instag->accountname) free(instag->accountname);
    if (instag->protocol) free(instag->protocol);

    /* Re-link the list */
    *(instag->tous) = instag->next;
    if (instag->next) {
	instag->next->tous = instag->tous;
    }

    free(instag);
}

/* Forget all instags in a given OtrlUserState. */
void otrl_instag_forget_all(OtrlUserState us) {
    while(us->instag_root) {
	otrl_instag_forget(us->instag_root);
    }
}

/* Fetch the instance tag from the given OtrlUserState associated with
 * the given account */
OtrlInsTag * otrl_instag_find(OtrlUserState us, const char *accountname,
	const char *protocol)
{
    OtrlInsTag *p;

    for(p=us->instag_root; p; p=p->next) {
	if (!strcmp(p->accountname, accountname) &&
		!strcmp(p->protocol, protocol)) {
	    return p;
	}
    }
    return NULL;
}

/* Read our instance tag from a file on disk into the given
 * OtrlUserState. */
gcry_error_t otrl_instag_read(OtrlUserState us, const char *filename)
{
    gcry_error_t err;
    FILE *instf;

    /* Open the instance tag file. */
    instf = fopen(filename, "rb");
    if (!instf) {
	return gcry_error_from_errno(errno);
    }

    err = otrl_instag_read_FILEp(us, instf);
    fclose(instf);
    return err;
}

/* Read our instance tag from a file on disk into the given
 * OtrlUserState. The FILE* must be open for reading. */
gcry_error_t otrl_instag_read_FILEp(OtrlUserState us, FILE *instf)
{
    if (!instf) return gcry_error(GPG_ERR_NO_ERROR);

    OtrlInsTag *p;
    char storeline[1000];
    size_t maxsize = sizeof(storeline);

    while(fgets(storeline, maxsize, instf)) {
	char *prevpos;
	char *pos;
	unsigned int instag = 0;

	p = malloc(sizeof(*p));
	if (!p) {
	    return gcry_error(GPG_ERR_ENOMEM);
	}

	/* Parse the line, which should be of the form:
	 * accountname\tprotocol\t40_hex_nybbles\n          */
	prevpos = storeline;
	pos = strchr(prevpos, '\t');
	if (!pos) {
	    free(p);
	    continue;
	}
	*pos = '\0';
	pos++;
	p->accountname = malloc(pos - prevpos);
	if (!(p->accountname)) {
	    free(p);
	    return gcry_error(GPG_ERR_ENOMEM);
	}
	memmove(p->accountname, prevpos, pos - prevpos);

	prevpos = pos;
	pos = strchr(prevpos, '\t');
	if (!pos) {
	    free(p->accountname);
	    free(p);
	    continue;
	}
	*pos = '\0';
	pos++;
	p->protocol = malloc(pos - prevpos);
	if (!(p->protocol)) {
	    free(p->accountname);
	    free(p);
	    return gcry_error(GPG_ERR_ENOMEM);
	}
	memmove(p->protocol, prevpos, pos - prevpos);

	prevpos = pos;
	pos = strchr(prevpos, '\r');
	if (!pos) pos = strchr(prevpos, '\n');
	if (!pos) {
	    free(p->accountname);
	    free(p->protocol);
	    free(p);
	    continue;
	}
	*pos = '\0';
	pos++;
	/* hex str of length 8 */
	if (strlen(prevpos) != 8) {
	    free(p->accountname);
	    free(p->protocol);
	    free(p);
	    continue;
	}

	sscanf(prevpos, "%08x", &instag);

	if (instag < OTRL_MIN_VALID_INSTAG) {
	    free(p->accountname);
	    free(p->protocol);
	    free(p);
	    continue;
	}
	p->instag = instag;

	/* Link it up */
	p->next = us->instag_root;
	if (p->next) {
	    p->next->tous = &(p->next);
	}
	p->tous = &(us->instag_root);
	us->instag_root = p;
    }

    return gcry_error(GPG_ERR_NO_ERROR);
}

/* Generate a new instance tag for the given account and write to file */
gcry_error_t otrl_instag_generate(OtrlUserState us, const char *filename,
	const char *accountname, const char *protocol)
{
    gcry_error_t err;
    FILE *instf;

    /* Open the instance tag file. */
    instf = fopen(filename, "wb");
    if (!instf) {
	return gcry_error_from_errno(errno);
    }

    err = otrl_instag_generate_FILEp(us, instf, accountname, protocol);
    fclose(instf);
    return err;
}

/* Return a new valid instance tag */
otrl_instag_t otrl_instag_get_new()
{
    otrl_instag_t result = 0;

    while(result < OTRL_MIN_VALID_INSTAG) {
	otrl_instag_t * instag = (otrl_instag_t *)gcry_random_bytes(
		sizeof(otrl_instag_t), GCRY_STRONG_RANDOM);
	result = *instag;
	gcry_free(instag);
    }

    return result;
}

/* Generate a new instance tag for the given account and write to file
 * The FILE* must be open for writing. */
gcry_error_t otrl_instag_generate_FILEp(OtrlUserState us, FILE *instf,
	const char *accountname, const char *protocol)
{
    OtrlInsTag *p;
    if (!accountname || !protocol) return gcry_error(GPG_ERR_NO_ERROR);

    p = (OtrlInsTag *)malloc(sizeof(OtrlInsTag));
    p->accountname = strdup(accountname);
    p->protocol = strdup(protocol);

    p->instag = otrl_instag_get_new();

    /* Add to our list in OtrlUserState */
    p->next = us->instag_root;
    if (p->next) {
	p->next->tous = &(p->next);
    }
    p->tous = &(us->instag_root);
    us->instag_root = p;

    otrl_instag_write_FILEp(us, instf);

    return gcry_error(GPG_ERR_NO_ERROR);
}

/* Write our instance tags to a file on disk. */
gcry_error_t otrl_instag_write(OtrlUserState us, const char *filename)
{
    gcry_error_t err;
    FILE *instf;

    /* Open the instance tag file. */
    instf = fopen(filename, "wb");
    if (!instf) {
	return gcry_error_from_errno(errno);
    }

    err = otrl_instag_write_FILEp(us, instf);
    fclose(instf);
    return err;
}

/* Write our instance tags to a file on disk.
 * The FILE* must be open for writing. */
gcry_error_t otrl_instag_write_FILEp(OtrlUserState us, FILE *instf)
{
    OtrlInsTag *p;
    /* This line should be ignored when read back in, since there are no
    tabs. */
    fprintf(instf, "# WARNING! You shouldn't copy this file to another"
    " computer. It is unnecessary and can cause problems.\n");
    for(p=us->instag_root; p; p=p->next) {
	fprintf(instf, "%s\t%s\t%08x\n", p->accountname, p->protocol,
		p->instag);
    }

    return gcry_error(GPG_ERR_NO_ERROR);
}