File: ratPwCache.c

package info (click to toggle)
tkrat 1%3A2.2cvs20100105-true-dfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,328 kB
  • ctags: 9,259
  • sloc: ansic: 96,057; tcl: 25,667; makefile: 1,638; sh: 282
file content (402 lines) | stat: -rw-r--r-- 8,387 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * RatPwCache.c --
 *
 *	This file contains password caching routines
 *
 * TkRat software and its included text is Copyright 1996-2004 by
 * Martin Forssn
 *
 * The full text of the legal notice is contained in the file called
 * COPYRIGHT, included with this distribution.
 */

#include <sys/stat.h>
#include <unistd.h>
#include "rat.h"
#include "env_unix.h"

/*
 * For the memory cache
 */
typedef struct CachedPasswd {
    int onDisk;
    char *spec;
    char *passwd;
    struct CachedPasswd *next;
    Tcl_TimerToken token;
} CachedPasswd;
static CachedPasswd *cache = NULL;
static int initialized = 0;
static char *filename = NULL;

/*
 * Local functions
 */
static char *Canonify(const char *spec);
static void ReadDisk(Tcl_Interp *interp);
static void WriteDisk(Tcl_Interp *interp);
static void TouchEntry(Tcl_Interp *interp, CachedPasswd *cp);
static Tcl_TimerProc ErasePasswd;


/*
 *----------------------------------------------------------------------
 *
 * Canonify --
 *
 *      Convert foler specification to canonical form
 *
 * Results:
 *	A pointer to a static buffer containing the canonic form.
 *	This buffer will be rewritten by the next call.
 *
 * Side effects:
 *	None.
 *
 *
 *----------------------------------------------------------------------
 */

static char*
Canonify(const char *spec)
{
    static char *cSpec = NULL;
    static int size = 0;
    char *c;

    if (strlen(spec)+1 > size) {
	size = strlen(spec)+64;
	cSpec = (char*)realloc(cSpec, size);
    }
    strlcpy(cSpec, spec, size);
    if (NULL != (c = strstr(cSpec, "/debug"))) {
	memmove(c, c+6, strlen(c+6)+1);
    }
    if (NULL != (c = strchr(cSpec, '}'))) {
	c[1] = '\0';
    }
    return cSpec;
}

/*
 *----------------------------------------------------------------------
 *
 * ReadDisk --
 *
 *      Read cached passwords from disk
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Updates the local cached list
 *
 *
 *----------------------------------------------------------------------
 */

void
ReadDisk(Tcl_Interp *interp)
{
    CONST84 char **argv, *spec = NULL, *passwd = NULL;
    const char *name;
    CachedPasswd *cp;
    char buf[1024];
    int argc;
    FILE *fp;

    if (NULL == (name = RatGetPathOption(interp, "pwcache_file"))) {
	return;
    }
    filename = cpystr(name);
    initialized = 1;
    if (NULL == (fp = fopen(filename, "r"))) {
	return;
    }
    while (fgets(buf, sizeof(buf), fp) && !feof(fp)) {
	if (TCL_OK != Tcl_SplitList(interp, buf, &argc, &argv)
	    || (argc != 2 && argc != 5)) {
	    continue;
	}	
	if (2 == argc) {
	    /* {spec passwd} */
	    spec = argv[0];
	    passwd = argv[1];
	} else if (5 == argc) {
	    /* {host port user service passwd} */
	    snprintf(buf, sizeof(buf), "{%s:%s/user=%s%s}",
		     argv[0], argv[1], argv[2],
		     (strcmp("imap", argv[3]) ? "/pop3" : ""));
	    spec = buf;
	    passwd = argv[4];
	}
	cp = (CachedPasswd*)ckalloc(sizeof(CachedPasswd)
				    +strlen(spec)+1+strlen(passwd)+1);
	cp->onDisk = 1;
	cp->spec = (char*)cp + sizeof(CachedPasswd);
	strcpy(cp->spec, spec);
	cp->passwd = cp->spec + strlen(cp->spec)+1;
	strcpy(cp->passwd, passwd);
	cp->next = cache;
	cache = cp;
	ckfree(argv);
    }
    fclose(fp);
    return;
}


/*
 *----------------------------------------------------------------------
 *
 * WriteDisk --
 *
 *      Write the cache to disk. Only those entries marked to be stored on
 *	disk are actually output.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Rewrites the disk cache.
 *
 *
 *----------------------------------------------------------------------
 */

void
WriteDisk(Tcl_Interp *interp)
{
    CachedPasswd *cp;
    char c;
    FILE *fp;
    struct stat sbuf;
    int i, fd;
    Tcl_DString ds;

    if (-1 < (fd = open(filename, O_WRONLY))) {
	fstat(fd, &sbuf);
	c = 0;
	for (i=0; i<sbuf.st_size; i++) {
	    if (0 > safe_write(fd, &c, 1)) break;
	}
	close(fd);
	unlink(filename);
    }
    if (NULL == (fp = fopen(filename, "w"))) {
	return;
    }
    fchmod(fileno(fp), 0600);
    Tcl_DStringInit(&ds);
    for (cp = cache; cp; cp = cp->next) {
	if (cp->onDisk) {
	    Tcl_DStringAppendElement(&ds, cp->spec);
	    Tcl_DStringAppendElement(&ds, cp->passwd);
	    fprintf(fp, "%s\n", Tcl_DStringValue(&ds));
	    Tcl_DStringSetLength(&ds, 0);
	}
    }
    fclose(fp);
    Tcl_DStringFree(&ds);
    return;
}


/*
 *----------------------------------------------------------------------
 *
 * TouchEntry --
 *
 *     Touch an entry in the cache. This only affects entries that are
 *	going to timeout.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	May rewrite the cached password file
 *
 *
 *----------------------------------------------------------------------
 */

void
TouchEntry(Tcl_Interp *interp, CachedPasswd *cp)
{
    int timeout;
    Tcl_Obj *oPtr;

    if (cp->onDisk) {
	return;
    }
    Tcl_DeleteTimerHandler(cp->token);
    oPtr = Tcl_GetVar2Ex(interp, "option", "cache_passwd_timeout",
			 TCL_GLOBAL_ONLY);
    Tcl_GetIntFromObj(interp, oPtr, &timeout);
    if (timeout) {
	cp->token =
	    Tcl_CreateTimerHandler(timeout*1000, ErasePasswd, (ClientData)cp);
    }
}


/*
 *----------------------------------------------------------------------
 *
 * ErasePasswd --
 *
 *      Earase a password from the cache
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Modifies the in-memory cache
 *
 *
 *----------------------------------------------------------------------
 */

static void
ErasePasswd(ClientData clientData)
{
    CachedPasswd **tcp, *cp = (CachedPasswd*)clientData;

    Tcl_DeleteTimerHandler(cp->token);
    memset(cp->passwd, 0, strlen(cp->passwd));
    for (tcp = &cache; *tcp != cp; tcp = &(*tcp)->next);
    *tcp = cp->next;
    ckfree(cp);
}


/*
 *----------------------------------------------------------------------
 *
 * RatGetCachedPassword --
 *
 *      get a cached password
 *
 * Results:
 *	Returns a pointer to a static area containing the password,
 *	or NULL if no suitable cached password was found.
 *
 * Side effects:
 *	may read the cached password file
 *
 *
 *----------------------------------------------------------------------
 */

char*
RatGetCachedPassword(Tcl_Interp *interp, const char *spec)
{
    CachedPasswd *cp;
    char *cSpec = Canonify(spec);

    if (0 == initialized) {
	ReadDisk(interp);
    }
    for (cp = cache; cp; cp = cp->next) {
	if (!strcmp(cp->spec, cSpec)) {
	    TouchEntry(interp, cp);
	    return cp->passwd;
	}
    }
    return NULL;
}


/*
 *----------------------------------------------------------------------
 *
 * RatCachePassword --
 *
 *      Cache a password
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	May rewrite the cached password file
 *
 *
 *----------------------------------------------------------------------
 */

void
RatCachePassword(Tcl_Interp *interp, const char *spec, const char *passwd,
		 int store)
{
    CachedPasswd *cp;
    char *cSpec = Canonify(spec);

    if (0 == initialized) {
	ReadDisk(interp);
    }
    cp = (CachedPasswd*)ckalloc(sizeof(CachedPasswd) +
				strlen(cSpec) + 1 + strlen(passwd) + 1);
    cp->onDisk = store;
    cp->spec = (char*)cp + sizeof(CachedPasswd);
    strcpy(cp->spec, cSpec);
    cp->passwd = cp->spec+strlen(cSpec)+1;
    strcpy(cp->passwd, passwd);
    cp->next = cache;
    cp->token = NULL;
    cache = cp;
    if (store) {
	WriteDisk(interp);
    } else {
	TouchEntry(interp, cp);
    }
    return;
}


/*
 *----------------------------------------------------------------------
 *
 * RatPasswdCachePurge --
 *
 *      Purge the password cache. If disk_also is true the both the disk and
 *	memory caches are purged. If disk_also is false, the only the
 *	memory cache is purged.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	May rewrite the cached password file
 *
 *
 *----------------------------------------------------------------------
 */

void
RatPasswdCachePurge(Tcl_Interp *interp, int disk_also)
{
    CachedPasswd *cp, *cpn;

    if (0 == initialized) {
	ReadDisk(interp);
    }
    for (cp = cache; cp; cp = cpn) {
	cpn = cp->next;
	memset(cp->passwd, 0, strlen(cp->passwd));
	Tcl_DeleteTimerHandler(cp->token);
	ckfree(cp);
    }
    cache = NULL;
    if (disk_also) {
	WriteDisk(interp);
    }
    return;
}

int
RatPasswdCachePurgeCmd(ClientData clientData, Tcl_Interp *interp, int objc,
	Tcl_Obj *CONST objv[])
{
    RatPasswdCachePurge(interp, 1);
    return TCL_OK;
}