context.c 16.5 KB
Newer Older
cypherpunk's avatar
cypherpunk committed
1
2
/*
 *  Off-the-Record Messaging library
Rob Smits's avatar
Rob Smits committed
3
4
 *  Copyright (C) 2004-2012  Ian Goldberg, Rob Smits, Chris Alexander,
 *  			      Willy Lew, Lisa Du, Nikita Borisov
cypherpunk's avatar
cypherpunk committed
5
6
7
8
9
10
11
12
13
14
15
16
17
 *                           <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
Rob Smits's avatar
Rob Smits committed
18
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cypherpunk's avatar
cypherpunk committed
19
20
21
22
23
24
25
26
27
28
29
 */

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

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

/* libotr headers */
#include "context.h"
Rob Smits's avatar
Rob Smits committed
30
31
#include "instag.h"

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
#if OTRL_DEBUGGING
#include <stdio.h>

void otrl_auth_dump(FILE *f, const OtrlAuthInfo *auth);
void otrl_sm_dump(FILE *f, const OtrlSMState *sm);

/* Dump the contents of a context to the FILE *f. */
void otrl_context_dump(FILE *f, const ConnContext *context)
{
    const Fingerprint *fing;

    fprintf(f, "Context %p:\n\n", context);

    fprintf(f, "  Username: %s\n", context->username);
    fprintf(f, "  Accountname: %s\n", context->accountname);
    fprintf(f, "  Protocol: %s\n\n", context->protocol);
    fprintf(f, "  Master context: %p%s\n", context->m_context,
	    context->m_context == context ? " IS MASTER" : "");
    fprintf(f, "  Recent recv child: %p\n", context->recent_rcvd_child);
    fprintf(f, "  Recent sent child: %p\n", context->recent_sent_child);
    fprintf(f, "  Recent child: %p\n\n", context->recent_child);
    fprintf(f, "  Our instance:   %08x\n", context->our_instance);
    fprintf(f, "  Their instance: %08x\n\n", context->their_instance);
    fprintf(f, "  Msgstate: %d (%s)\n\n", context->msgstate,
	context->msgstate == OTRL_MSGSTATE_PLAINTEXT ? "PLAINTEXT" :
	context->msgstate == OTRL_MSGSTATE_ENCRYPTED ? "ENCRYPTED" :
	context->msgstate == OTRL_MSGSTATE_FINISHED ? "FINISHED" :
	"INVALID");
    otrl_auth_dump(f, &context->auth);
    fprintf(f, "\n  Fingerprints:\n");
    for (fing = context->fingerprint_root.next; fing; fing = fing->next) {
	fprintf(f, "    %p ", fing);
	if (fing->fingerprint == NULL) {
	    fprintf(f, "(null)");
	} else {
	    int i;
	    for (i=0;i<20;++i) {
		fprintf(f, "%02x", fing->fingerprint[i]);
	    }
	}
	fprintf(f, " %p", fing->context);
	if (fing->trust && fing->trust[0]) {
	    fprintf(f, " %s", fing->trust);
	}
	fprintf(f, "\n");
    }
    fprintf(f, "\n  Active fingerprint: %p\n\n", context->active_fingerprint);
    fprintf(f, "  Protocol version: %d\n", context->protocol_version);
    fprintf(f, "  OTR offer: %d (%s)\n\n", context->otr_offer,
	context->otr_offer == OFFER_NOT ? "NOT" :
	context->otr_offer == OFFER_SENT ? "SENT" :
	context->otr_offer == OFFER_REJECTED ? "REJECTED" :
	context->otr_offer == OFFER_ACCEPTED ? "ACCEPTED" :
	"INVALID");

    fprintf(f, "  Application data: %p\n", context->app_data);
    if (context->smstate == NULL) {
	fprintf(f, "  SM state: NULL\n");
    } else {
	otrl_sm_dump(f, context->smstate);
    }
    fprintf(f, "\n");
}

/* Dump the master context of this context, and all of its children. */
void otrl_context_siblings_dump(FILE *f, const ConnContext *context)
{
    const ConnContext *citer;
    for (citer = context->m_context;
	    citer && citer->m_context == context->m_context;
	    citer = citer->next) {
	if (citer == context) {
	    fprintf(f, "*** ");
	}
	otrl_context_dump(f, citer);
    }
}
109
110
111
112
113
114
115
116
117
118
119

/* Dump all contexts. */
void otrl_context_all_dump(FILE *f, OtrlUserState us)
{
    const ConnContext *citer;
    unsigned int ctxnum = 1;
    for (citer = us->context_root; citer; citer = citer->next, ++ctxnum) {
	fprintf(f, "%u. ", ctxnum);
	otrl_context_dump(f, citer);
    }
}
120
121
#endif

cypherpunk's avatar
cypherpunk committed
122
123
124
125
126
/* Create a new connection context. */
static ConnContext * new_context(const char * user, const char * accountname,
	const char * protocol)
{
    ConnContext * context;
cypherpunk's avatar
cypherpunk committed
127
    OtrlSMState *smstate;
Rob Smits's avatar
Rob Smits committed
128
129

    context = malloc(sizeof(ConnContext));
cypherpunk's avatar
cypherpunk committed
130
    assert(context != NULL);
Rob Smits's avatar
Rob Smits committed
131

cypherpunk's avatar
cypherpunk committed
132
133
134
    context->username = strdup(user);
    context->accountname = strdup(accountname);
    context->protocol = strdup(protocol);
Rob Smits's avatar
Rob Smits committed
135

136
    context->msgstate = OTRL_MSGSTATE_PLAINTEXT;
Rob Smits's avatar
Rob Smits committed
137
    otrl_auth_new(context);
cypherpunk's avatar
cypherpunk committed
138
139
140
141
142
143

    smstate = malloc(sizeof(OtrlSMState));
    assert(smstate != NULL);
    otrl_sm_state_new(smstate);
    context->smstate = smstate;

Rob Smits's avatar
Rob Smits committed
144
145
    context->our_instance = 0;
    context->their_instance = OTRL_INSTAG_MASTER;
cypherpunk's avatar
cypherpunk committed
146
147
148
149
150
    context->fingerprint_root.fingerprint = NULL;
    context->fingerprint_root.context = context;
    context->fingerprint_root.next = NULL;
    context->fingerprint_root.tous = NULL;
    context->active_fingerprint = NULL;
cypherpunk's avatar
   
cypherpunk committed
151
    memset(context->sessionid, 0, 20);
152
    context->sessionid_len = 0;
cypherpunk's avatar
   
cypherpunk committed
153
    context->protocol_version = 0;
cypherpunk's avatar
cypherpunk committed
154
155
156
    context->otr_offer = OFFER_NOT;
    context->app_data = NULL;
    context->app_data_free = NULL;
157
    context->context_priv = otrl_context_priv_new();
cypherpunk's avatar
   
cypherpunk committed
158
    assert(context->context_priv != NULL);
cypherpunk's avatar
cypherpunk committed
159
    context->next = NULL;
Rob Smits's avatar
Rob Smits committed
160
161
162
163
164
    context->m_context = context;
    context->recent_rcvd_child = NULL;
    context->recent_sent_child = NULL;
    context->recent_child = NULL;

cypherpunk's avatar
cypherpunk committed
165
166
167
    return context;
}

Rob Smits's avatar
Rob Smits committed
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
ConnContext * otrl_context_find_recent_instance(ConnContext * context,
	otrl_instag_t recent_instag) {
    ConnContext * m_context;

    if (!context) return NULL;

    m_context = context->m_context;

    if (!m_context) return NULL;

    switch(recent_instag) {
	case OTRL_INSTAG_RECENT:
	    return m_context->recent_child;
	case OTRL_INSTAG_RECENT_RECEIVED:
	    return m_context->recent_rcvd_child;
	case OTRL_INSTAG_RECENT_SENT:
	    return m_context->recent_sent_child;
	default:
	    return NULL;
    }
}

/* Find the instance of this context that has the best security level,
   and for which we have most recently received a message from. Note that most
   recent in this case is limited to a one-second resolution. */
ConnContext * otrl_context_find_recent_secure_instance(ConnContext * context)
{
    ConnContext *curp; /* for iteration */
    ConnContext *m_context; /* master */
    ConnContext *cresult = context;  /* best so far */

    if (!context) {
	return cresult;
    }

    m_context = context->m_context;

    for (curp = m_context; curp && curp->m_context == m_context;
	    curp = curp->next) {
	int msgstate_improved = 0; /* 0 == same, 1 == improved   */
	int trust_improved = 0;    /* (will immediately 'continue' if worse
				    * than) */

	if (cresult->msgstate == curp->msgstate) {
	    msgstate_improved = 0;
213
214
215
	} else if (curp->msgstate == OTRL_MSGSTATE_ENCRYPTED ||
		(cresult->msgstate == OTRL_MSGSTATE_PLAINTEXT &&
		curp->msgstate == OTRL_MSGSTATE_FINISHED)) {
Rob Smits's avatar
Rob Smits committed
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
	    msgstate_improved = 1;
	} else {
	    continue;
	}


	if (otrl_context_is_fingerprint_trusted(cresult->active_fingerprint) ==
		otrl_context_is_fingerprint_trusted(curp->active_fingerprint)) {

	    trust_improved = 0;
	} else if
		(otrl_context_is_fingerprint_trusted(curp->active_fingerprint)){

	    trust_improved = 1;
	} else {
	    continue;
	}

	if (msgstate_improved || trust_improved ||
		(!msgstate_improved && !trust_improved &&
		curp->context_priv->lastrecv >=
		cresult->context_priv->lastrecv)) {
	    cresult = curp;
	}
    }

    return cresult;
}

/* Look up a connection context by name/account/protocol/instag from the given
cypherpunk's avatar
cypherpunk committed
246
247
248
 * OtrlUserState.  If add_if_missing is true, allocate and return a new
 * context if one does not currently exist.  In that event, call
 * add_app_data(data, context) so that app_data and app_data_free can be
Rob Smits's avatar
Rob Smits committed
249
250
 * filled in by the application, and set *addedp to 1.
 * In the 'their_instance' field note that you can also specify a 'meta-
251
 * instance' value such as OTRL_INSTAG_MASTER, OTRL_INSTAG_RECENT,
Rob Smits's avatar
Rob Smits committed
252
 * OTRL_INSTAG_RECENT_RECEIVED and OTRL_INSTAG_RECENT_SENT. */
cypherpunk's avatar
cypherpunk committed
253
ConnContext * otrl_context_find(OtrlUserState us, const char *user,
Rob Smits's avatar
Rob Smits committed
254
255
	const char *accountname, const char *protocol,
	otrl_instag_t their_instance, int add_if_missing, int *addedp,
cypherpunk's avatar
cypherpunk committed
256
257
258
259
260
261
	void (*add_app_data)(void *data, ConnContext *context), void *data)
{
    ConnContext ** curp;
    int usercmp = 1, acctcmp = 1, protocmp = 1;
    if (addedp) *addedp = 0;
    if (!user || !accountname || !protocol) return NULL;
Rob Smits's avatar
Rob Smits committed
262

cypherpunk's avatar
cypherpunk committed
263
    for (curp = &(us->context_root); *curp; curp = &((*curp)->next)) {
Rob Smits's avatar
Rob Smits committed
264
	if ((usercmp = strcmp((*curp)->username, user)) > 0 ||
cypherpunk's avatar
cypherpunk committed
265
		(usercmp == 0 &&
Rob Smits's avatar
Rob Smits committed
266
		(acctcmp = strcmp((*curp)->accountname, accountname)) > 0) ||
cypherpunk's avatar
cypherpunk committed
267
		(usercmp == 0 && acctcmp == 0 &&
268
		(protocmp = strcmp((*curp)->protocol, protocol)) > 0) ||
269
		(usercmp == 0 && acctcmp == 0 && protocmp == 0
270
		&& (their_instance < OTRL_MIN_VALID_INSTAG ||
271
		    ((*curp)->their_instance >= their_instance))))
cypherpunk's avatar
cypherpunk committed
272
273
274
275
	    /* We're at the right place in the list.  We've either found
	     * it, or gone too far. */
	    break;
    }
Rob Smits's avatar
Rob Smits committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296

    if (usercmp == 0 && acctcmp == 0 && protocmp == 0 && *curp &&
	    (their_instance < OTRL_MIN_VALID_INSTAG ||
	    (their_instance == (*curp)->their_instance))) {
	/* Found one! */
	if (their_instance >= OTRL_MIN_VALID_INSTAG ||
		their_instance == OTRL_INSTAG_MASTER) {
	    return *curp;
	}

	/* We need to go back and check more values in the context */
	switch(their_instance) {
	    case OTRL_INSTAG_BEST:
		return otrl_context_find_recent_secure_instance(*curp);
	    case OTRL_INSTAG_RECENT:
	    case OTRL_INSTAG_RECENT_RECEIVED:
	    case OTRL_INSTAG_RECENT_SENT:
		return otrl_context_find_recent_instance(*curp, their_instance);
	    default:
		return NULL;
	}
cypherpunk's avatar
cypherpunk committed
297
    }
Rob Smits's avatar
Rob Smits committed
298

cypherpunk's avatar
cypherpunk committed
299
300
    if (add_if_missing) {
	ConnContext *newctx;
Rob Smits's avatar
Rob Smits committed
301
302
303
	OtrlInsTag *our_instag = (OtrlInsTag *)otrl_instag_find(us, accountname,
		protocol);

cypherpunk's avatar
cypherpunk committed
304
305
306
307
308
309
310
311
312
313
314
	if (addedp) *addedp = 1;
	newctx = new_context(user, accountname, protocol);
	newctx->next = *curp;
	if (*curp) {
	    (*curp)->tous = &(newctx->next);
	}
	*curp = newctx;
	newctx->tous = curp;
	if (add_app_data) {
	    add_app_data(data, *curp);
	}
Rob Smits's avatar
Rob Smits committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338

	/* Initialize specified instance tags */
	if (our_instag) {
	    newctx->our_instance = our_instag->instag;
	}

	if (their_instance >= OTRL_MIN_VALID_INSTAG ||
		their_instance == OTRL_INSTAG_MASTER) {
	    newctx->their_instance = their_instance;
	}

	if (their_instance >= OTRL_MIN_VALID_INSTAG) {
	    newctx->m_context = otrl_context_find(us, user, accountname,
		protocol, OTRL_INSTAG_MASTER, 1, NULL, add_app_data, data);
	}

	if (their_instance == OTRL_INSTAG_MASTER) {
	    /* if we're adding a master, there are no children, so the most
	     * recent context is the one we add. */
	    newctx->recent_child = newctx;
	    newctx->recent_rcvd_child = newctx;
	    newctx->recent_sent_child = newctx;
	}

cypherpunk's avatar
cypherpunk committed
339
340
341
342
343
	return *curp;
    }
    return NULL;
}

344
/* Return true iff the given fingerprint is marked as trusted. */
Rob Smits's avatar
Rob Smits committed
345
346
347
348
int otrl_context_is_fingerprint_trusted(Fingerprint *fprint) {
    return fprint && fprint->trust && fprint->trust[0] != '\0';
}

349
350
/* This method gets called after sending or receiving a message, to
 * update the master context's "recent context" pointers. */
Rob Smits's avatar
Rob Smits committed
351
352
353
354
355
356
357
358
359
360
361
362
363
364
void otrl_context_update_recent_child(ConnContext *context,
	unsigned int sent_msg) {
    ConnContext *m_context = context->m_context;

    if (sent_msg) {
	m_context->recent_sent_child = context;
    } else {
	m_context->recent_rcvd_child = context;
    }

    m_context->recent_child = context;

}

cypherpunk's avatar
cypherpunk committed
365
366
367
368
369
/* Find a fingerprint in a given context, perhaps adding it if not
 * present. */
Fingerprint *otrl_context_find_fingerprint(ConnContext *context,
	unsigned char fingerprint[20], int add_if_missing, int *addedp)
{
Rob Smits's avatar
Rob Smits committed
370
    Fingerprint *f;
cypherpunk's avatar
cypherpunk committed
371
    if (addedp) *addedp = 0;
Rob Smits's avatar
Rob Smits committed
372
373
374
375
376
377

    if (!context || !context->m_context) return NULL;

    context = context->m_context;

    f = context->fingerprint_root.next;
cypherpunk's avatar
cypherpunk committed
378
379
380
381
    while(f) {
	if (!memcmp(f->fingerprint, fingerprint, 20)) return f;
	f = f->next;
    }
Rob Smits's avatar
Rob Smits committed
382

cypherpunk's avatar
cypherpunk committed
383
384
385
386
387
388
389
390
391
    /* Didn't find it. */
    if (add_if_missing) {
	if (addedp) *addedp = 1;
	f = malloc(sizeof(*f));
	assert(f != NULL);
	f->fingerprint = malloc(20);
	assert(f->fingerprint != NULL);
	memmove(f->fingerprint, fingerprint, 20);
	f->context = context;
cypherpunk's avatar
   
cypherpunk committed
392
	f->trust = NULL;
cypherpunk's avatar
cypherpunk committed
393
394
395
396
397
398
399
400
401
402
403
	f->next = context->fingerprint_root.next;
	if (f->next) {
	    f->next->tous = &(f->next);
	}
	context->fingerprint_root.next = f;
	f->tous = &(context->fingerprint_root.next);
	return f;
    }
    return NULL;
}

cypherpunk's avatar
   
cypherpunk committed
404
405
406
407
408
409
410
411
412
/* Set the trust level for a given fingerprint */
void otrl_context_set_trust(Fingerprint *fprint, const char *trust)
{
    if (fprint == NULL) return;

    free(fprint->trust);
    fprint->trust = trust ? strdup(trust) : NULL;
}

413
414
/* Force a context into the OTRL_MSGSTATE_FINISHED state. */
void otrl_context_force_finished(ConnContext *context)
cypherpunk's avatar
cypherpunk committed
415
{
416
417
    context->msgstate = OTRL_MSGSTATE_FINISHED;
    otrl_auth_clear(&(context->auth));
cypherpunk's avatar
cypherpunk committed
418
    context->active_fingerprint = NULL;
cypherpunk's avatar
   
cypherpunk committed
419
    memset(context->sessionid, 0, 20);
420
    context->sessionid_len = 0;
cypherpunk's avatar
   
cypherpunk committed
421
    context->protocol_version = 0;
cypherpunk's avatar
cypherpunk committed
422
    otrl_sm_state_free(context->smstate);
423
    otrl_context_priv_force_finished(context->context_priv);
cypherpunk's avatar
cypherpunk committed
424
425
}

426
427
/* Force a context into the OTRL_MSGSTATE_PLAINTEXT state. */
void otrl_context_force_plaintext(ConnContext *context)
cypherpunk's avatar
cypherpunk committed
428
{
429
430
    /* First clean up everything we'd need to do for the FINISHED state */
    otrl_context_force_finished(context);
cypherpunk's avatar
cypherpunk committed
431

432
433
    /* And just set the state properly */
    context->msgstate = OTRL_MSGSTATE_PLAINTEXT;
cypherpunk's avatar
cypherpunk committed
434
435
436
437
}

/* Forget a fingerprint (so long as it's not the active one.  If it's a
 * fingerprint_root, forget the whole context (as long as
438
 * and_maybe_context is set, and it's PLAINTEXT).  Also, if it's not
cypherpunk's avatar
cypherpunk committed
439
 * the fingerprint_root, but it's the only fingerprint, and we're
440
 * PLAINTEXT, forget the whole context if and_maybe_context is set. */
cypherpunk's avatar
cypherpunk committed
441
442
443
444
445
void otrl_context_forget_fingerprint(Fingerprint *fprint,
	int and_maybe_context)
{
    ConnContext *context = fprint->context;
    if (fprint == &(context->fingerprint_root)) {
446
447
	if (context->msgstate == OTRL_MSGSTATE_PLAINTEXT &&
		and_maybe_context) {
cypherpunk's avatar
cypherpunk committed
448
449
450
	    otrl_context_forget(context);
	}
    } else {
451
	if (context->msgstate != OTRL_MSGSTATE_PLAINTEXT ||
cypherpunk's avatar
cypherpunk committed
452
		context->active_fingerprint != fprint) {
Rob Smits's avatar
Rob Smits committed
453

cypherpunk's avatar
cypherpunk committed
454
	    free(fprint->fingerprint);
cypherpunk's avatar
   
cypherpunk committed
455
	    free(fprint->trust);
cypherpunk's avatar
cypherpunk committed
456
457
458
459
460
	    *(fprint->tous) = fprint->next;
	    if (fprint->next) {
		fprint->next->tous = fprint->tous;
	    }
	    free(fprint);
461
	    if (context->msgstate == OTRL_MSGSTATE_PLAINTEXT &&
cypherpunk's avatar
cypherpunk committed
462
463
464
465
466
467
468
469
470
471
		    context->fingerprint_root.next == NULL &&
		    and_maybe_context) {
		/* We just deleted the only fingerprint.  Forget the
		 * whole thing. */
		otrl_context_forget(context);
	    }
	}
    }
}

Rob Smits's avatar
Rob Smits committed
472
473
474
475
476
/* Forget a whole context, so long as it's PLAINTEXT. If a context has child
 * instances, don't remove this instance unless children are also all in
 * PLAINTEXT state. In this case, the children will also be removed.
 * Returns 0 on success, 1 on failure. */
int otrl_context_forget(ConnContext *context)
cypherpunk's avatar
cypherpunk committed
477
{
Rob Smits's avatar
Rob Smits committed
478
479
480
481
482
483
484
485
    if (context->msgstate != OTRL_MSGSTATE_PLAINTEXT) return 1;

    if (context->their_instance == OTRL_INSTAG_MASTER) {
	ConnContext *c_iter;

	for (c_iter = context; c_iter &&
		c_iter->m_context == context->m_context;
		c_iter = c_iter->next) {
486
	    if (c_iter->msgstate != OTRL_MSGSTATE_PLAINTEXT) return 1;
Rob Smits's avatar
Rob Smits committed
487
488
489
490
491
492
493
494
495
496
497
498
	}

	c_iter = context->next;
	while (c_iter && c_iter->m_context == context->m_context) {
	    if (!otrl_context_forget(c_iter)) {
		c_iter = context->next;
	    } else {
		return 1;
	    }
	}

    }
cypherpunk's avatar
cypherpunk committed
499

500
    /* Just to be safe, force to plaintext.  This also frees any
cypherpunk's avatar
cypherpunk committed
501
     * extraneous data lying around. */
502
    otrl_context_force_plaintext(context);
cypherpunk's avatar
cypherpunk committed
503
504
505
506
507
508
509
510
511

    /* First free all the Fingerprints */
    while(context->fingerprint_root.next) {
	otrl_context_forget_fingerprint(context->fingerprint_root.next, 0);
    }
    /* Now free all the dynamic info here */
    free(context->username);
    free(context->accountname);
    free(context->protocol);
cypherpunk's avatar
cypherpunk committed
512
    free(context->smstate);
cypherpunk's avatar
cypherpunk committed
513
514
515
    context->username = NULL;
    context->accountname = NULL;
    context->protocol = NULL;
cypherpunk's avatar
cypherpunk committed
516
    context->smstate = NULL;
cypherpunk's avatar
cypherpunk committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530

    /* Free the application data, if it exists */
    if (context->app_data && context->app_data_free) {
	(context->app_data_free)(context->app_data);
	context->app_data = NULL;
    }

    /* Fix the list linkages */
    *(context->tous) = context->next;
    if (context->next) {
	context->next->tous = context->tous;
    }

    free(context);
Rob Smits's avatar
Rob Smits committed
531
    return 0;
cypherpunk's avatar
cypherpunk committed
532
533
}

534
/* Forget all the contexts in a given OtrlUserState. */
cypherpunk's avatar
cypherpunk committed
535
536
void otrl_context_forget_all(OtrlUserState us)
{
Rob Smits's avatar
Rob Smits committed
537
538
539
540
541
542
    ConnContext *c_iter;

    for (c_iter = us->context_root; c_iter; c_iter = c_iter->next) {
	otrl_context_force_plaintext(c_iter);
    }

cypherpunk's avatar
cypherpunk committed
543
544
545
546
    while (us->context_root) {
	otrl_context_forget(us->context_root);
    }
}