summaryrefslogtreecommitdiffstats
path: root/sqwebmail/sqconfig.c
blob: 910cf0897a1a44fc5227064bdc919ab65c23156f (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
/*
** Copyright 1998 - 1999 Double Precision, Inc.  See COPYING for
** distribution information.
*/


/*
*/
#include	"sqwebmail.h"
#include	"sqconfig.h"
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif
#include	<sys/types.h>
#include	<sys/stat.h>
#include	"maildir/maildircreate.h"

/* Assume all configuration data fits in 256 char buffer. */

static char linebuf[256];

const char *read_sqconfig(const char *dir, const char *configfile, time_t *mtime)
{
char *p=malloc(strlen(dir) + strlen(configfile) + 2);
struct stat stat_buf;
FILE	*f;

	if (!p)	enomem();
	strcat(strcat(strcpy(p, dir), "/"), configfile);
	f=fopen(p, "r");
	free(p);
	if (!f)	return (0);
	if (fstat(fileno(f), &stat_buf) != 0 ||
		!fgets(linebuf, sizeof(linebuf), f))
	{
		fclose(f);
		return (0);
	}
	fclose(f);
	if (mtime)	*mtime=stat_buf.st_mtime;

	linebuf[sizeof(linebuf)-1]=0;
	if ((p=strchr(linebuf, '\n')) != 0)	*p=0;
	return (linebuf);
}

void write_sqconfig(const char *dir, const char *configfile, const char *val)
{
	char *p=malloc(strlen(dir) + strlen(configfile) + 2);

	struct maildir_tmpcreate_info createInfo;
	FILE *fp;

	if (!p)	enomem();

	strcat(strcat(strcpy(p, dir), "/"), configfile);
	if (!val)
	{
		unlink(p);
		free(p);
		return;
	}

	maildir_tmpcreate_init(&createInfo);

	createInfo.maildir=dir;
	createInfo.uniq="config";
	createInfo.doordie=1;

	fp=maildir_tmpcreate_fp(&createInfo);

	if (!fp)
		enomem();


	free(createInfo.newname);
	createInfo.newname=p;

	fprintf(fp, "%s\n", val);
	fflush(fp);
	if (ferror(fp))	eio("Error after write:",p);
	fclose(fp);

	/* Note - umask should already turn off the 077 bits, but
	** just in case someone screwed up previously, I'll fix it
	** myself */

	chmod(createInfo.tmpname, 0600);
	rename(createInfo.tmpname, createInfo.newname);
	maildir_tmpcreate_free(&createInfo);
}