aboutsummaryrefslogtreecommitdiff
path: root/eperd/condmv.c
blob: 78bc9be45a1868c2ea7daefcb192917630738634 (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
/*
 * Copyright (c) 2013 RIPE NCC <atlas@ripe.net>
 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
 * condmv.c -- move a file only if the destination doesn't exist
 */

#include "libbb.h"
#include "eperd.h"

#define SAFE_PREFIX_FROM ATLAS_DATA_NEW
#define SAFE_PREFIX_TO ATLAS_DATA_OUT

#define A_FLAG	(1 << 0)
#define F_FLAG	(1 << 1)

#define DEFAULT_INTERVAL	60

struct condmvstate
{
	char *from;
	char *to;
	char *atlas;
	int force;
	int interval;
};

static void *condmv_init(int argc, char *argv[],
	void (*done)(void *state) UNUSED_PARAM)
{
	char *opt_add, *opt_interval, *from, *to, *check;
	int interval;
	uint32_t opt;
	struct condmvstate *state;

	opt_add= NULL;
	opt_interval= NULL;
	opt_complementary= NULL;	/* For when we are called by crond */
	opt= getopt32(argv, "!A:fi:", &opt_add, &opt_interval);
	if (opt == (uint32_t)-1)
		return NULL;

	if (argc != optind + 2)
	{
		crondlog(LVL8 "too many or too few arguments (required 2)"); 
		return NULL;
	}

	if (opt_interval)
	{
		interval= strtoul(opt_interval, &check, 0);
		if (interval <= 0)
		{
			crondlog(LVL8 "unable to parse interval '%s'",
				opt_interval); 
			return NULL;
		}
	}
	else
		interval= DEFAULT_INTERVAL;

	from= argv[optind];
	to= argv[optind+1];

	if (!validate_filename(from, SAFE_PREFIX_FROM))
	{
		fprintf(stderr, "insecure from file '%s'\n", from);
		return NULL;
	}
	if (!validate_filename(to, SAFE_PREFIX_TO))
	{
		fprintf(stderr, "insecure to file '%s'\n", to);
		return NULL;
	}

	state= malloc(sizeof(*state));
	state->from= strdup(from);
	state->to= strdup(to);
	state->atlas= opt_add ? strdup(opt_add) : NULL;
	state->force= !!(opt & F_FLAG);
	state->interval= interval;

	return state;
}

static void condmv_start(void *state)
{
	size_t len;
	time_t mytime;
	char *to;
	FILE *file;
	struct condmvstate *condmvstate;
	struct stat sb;

	condmvstate= state;

	len= strlen(condmvstate->to) + 20;
	to= malloc(len);
	snprintf(to, len, "%s.%ld", condmvstate->to,
		(long)time(NULL)/condmvstate->interval);

	crondlog(LVL7 "condmv_start: destination '%s'\n", to);

	if (stat(to, &sb) == 0 && !condmvstate->force)
	{
		free(to);
		return;
	}

	if (condmvstate->atlas)
	{
		mytime = time(NULL);
		/* We have to add something to the existing file before moving
		 * to.
		 */
		file= fopen(condmvstate->from, "a");
		if (file == NULL)
		{
			crondlog(LVL9 "condmv: unable to append to '%s': %s\n",
				condmvstate->from, strerror(errno));
			free(to);
			return;
		}
		if (fprintf(file, "%s %lu %s\n", condmvstate->atlas, mytime,
			condmvstate->from) < 0)
		{
			crondlog(LVL9 "condmv: unable to append to '%s': %s\n",
				condmvstate->from, strerror(errno));
			fclose(file);
			free(to);
			return;
		}
		if (fclose(file) != 0)
		{
			crondlog(LVL9 "condmv: unable to close '%s': %s\n",
				condmvstate->from, strerror(errno));
			free(to);
			return;
		}
	}
	if (rename(condmvstate->from, to) != 0)
	{
		crondlog(LVL9 "condmv: unable to rename '%s' to '%s': %s\n",
			condmvstate->from, to, strerror(errno));
	}	
	free(to);
}

static int condmv_delete(void *state)
{
	struct condmvstate *condmvstate;

	condmvstate= state;
	free(condmvstate->from);
	condmvstate->from= NULL;
	free(condmvstate->to);
	condmvstate->to= NULL;
	free(condmvstate->atlas);
	condmvstate->atlas= NULL;

	free(condmvstate);
	
	return 1;
}

struct testops condmv_ops = { condmv_init, condmv_start, condmv_delete };