File: maintenance.cc

package info (click to toggle)
apt-cacher-ng 3.3.1-2~bpo10+1
  • links: PTS, VCS
  • area: main
  • in suites: buster-backports
  • size: 2,040 kB
  • sloc: cpp: 17,564; sh: 553; ansic: 401; perl: 377; makefile: 126
file content (342 lines) | stat: -rw-r--r-- 8,788 bytes parent folder | download | duplicates (6)
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


#include "meta.h"
#include "lockable.h"

#include "maintenance.h"
#include "expiration.h"
#include "pkgimport.h"
#include "showinfo.h"
#include "mirror.h"
#include "aclogger.h"
#include "filereader.h"
#include "acfg.h"
#include "acbuf.h"
#include "sockio.h"
#include "caddrinfo.h"

#include "debug.h"

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <unistd.h>
#include <signal.h>

using namespace std;

#define MAINT_HTML_DECO "maint.html" 

namespace acng {

tSpecialRequest::tSpecialRequest(const tRunParms& parms) :
		m_parms(parms)
{
}

tSpecialRequest::~tSpecialRequest()
{
	if(m_bChunkHeaderSent)
		SendRawData(WITHLEN("0\r\n\r\n"), MSG_NOSIGNAL);
}

bool tSpecialRequest::SendRawData(const char *data, size_t len, int flags)
{
	if(m_parms.fd<3) // nothing raw to send for stdout
		return true;

	while(len>0)
	{
		int r=send(m_parms.fd, data, len, flags);
		if(r<0)
		{
			if(errno==EINTR || errno==EAGAIN)
				r=0;
			else
				return false;
		}
		
		data+=r;
		len-=r;
	}
	return true;
}

void tSpecialRequest::SendChunkRemoteOnly(const char *data, size_t len)
{
	if(!data || !len || m_parms.fd<0)
		return;

	if(m_parms.fd<3)
	{
		ignore_value(::write(m_parms.fd, data, len));
		return;
	}
	// send HTTP chunk header
	char buf[23];
	int l = sprintf(buf, "%x\r\n", (uint) len);
	SendRawData(buf, l, MSG_MORE | MSG_NOSIGNAL);
	SendRawData(data, len, MSG_MORE | MSG_NOSIGNAL);
	SendRawData("\r\n", 2, MSG_NOSIGNAL);
}

void tSpecialRequest::SendChunk(const char *data, size_t len)
{
	SendChunkRemoteOnly(data, len);
	SendChunkLocalOnly(data, len);
}

void tSpecialRequest::SendChunkedPageHeader(const char *httpstatus, const char *mimetype)
{
	tSS s(100);
	s <<  "HTTP/1.1 " << httpstatus << "\r\n"
			"Connection: close\r\n"
			"Transfer-Encoding: chunked\r\n"
			"Content-Type: " << mimetype << "\r\n\r\n";
	SendRawData(s.data(), s.length(), MSG_MORE);
	m_bChunkHeaderSent = true;
}

class tAuthRequest : public tSpecialRequest
{
public:

	// XXX: c++11 using tSpecialRequest::tSpecialRequest;
	inline tAuthRequest(const tSpecialRequest::tRunParms& parms)
	: tSpecialRequest(parms) {};

	void Run() override
	{
		const char authmsg[] = "HTTP/1.1 401 Not Authorized\r\nWWW-Authenticate: "
        "Basic realm=\"For login data, see AdminAuth in Apt-Cacher NG config files\"\r\n"
        "Connection: Close\r\n"
        "Content-Type: text/plain\r\nContent-Length:81\r\n\r\n"
        "Not Authorized. Please contact Apt-Cacher NG administrator for further questions.<br>"
        "<br>"
        "For Admin: Check the AdminAuth option in one of the *.conf files in Apt-Cacher NG "
        "configuration directory, probably " CFGDIR  ;
		SendRawData(authmsg, sizeof(authmsg)-1, 0);
	}
};

class authbounce : public tSpecialRequest
{
public:

	// XXX: c++11 using tSpecialRequest::tSpecialRequest;
	inline authbounce(const tSpecialRequest::tRunParms& parms)
	: tSpecialRequest(parms) {};


	void Run() override
	{
		const char authmsg[] = "HTTP/1.1 200 Not Authorized\r\n"
        "Connection: Close\r\n"
        "Content-Type: text/plain\r\nContent-Length: 102\r\n\r\n"
        "Not Authorized. To start this action, an administrator password must be set and "
        "you must be logged in.";
		SendRawData(authmsg, sizeof(authmsg)-1, 0);
	}
};

string & tSpecialRequest::GetHostname()
{
	if (m_sHostname.empty())
	{
		struct sockaddr_storage ss;
		socklen_t slen = sizeof(ss);
		char hbuf[NI_MAXHOST];

		if (0==getsockname(m_parms.fd, (struct sockaddr *)&ss, &slen) && 0
				==getnameinfo((struct sockaddr*) &ss, sizeof(ss), hbuf,
						sizeof(hbuf),
						nullptr, 0, NI_NUMERICHOST))
		{
			const char *p=hbuf;
			bool bAddBrs(false);
			if(0==strncmp(hbuf, "::ffff:", 7) && strpbrk(p, "0123456789."))
				p+=7; // no more colons there, looks like v4 IP in v6 space -> crop it
			else if(strchr(p, (int) ':'))
				bAddBrs=true; // full v6 address for sure, add brackets

			if(bAddBrs)
				m_sHostname="[";
			m_sHostname+=p;
			if(bAddBrs)
				m_sHostname+="]";
		}
		else
			m_sHostname="IP-of-this-cache-server";
	}
	return m_sHostname;
}

LPCSTR tSpecialRequest::GetTaskName()
{
	switch(m_parms.type)
	{
	case workNotSpecial: return "ALARM";
	case workExExpire: return "Expiration";
	case workExList: return "Expired Files Listing";
	case workExPurge: return "Expired Files Purging";
	case workExListDamaged: return "Listing Damaged Files";
	case workExPurgeDamaged: return "Truncating Damaged Files";
	case workExTruncDamaged: return "Truncating damaged files to zero size";
	//case workRAWDUMP: /*fall-through*/
	//case workBGTEST: return "42";
	case workUSERINFO: return "General Configuration Information";
	case workTraceStart:
	case workTraceEnd:
	case workMAINTREPORT: return "Status Report and Maintenance Tasks Overview";
	case workAUTHREQUEST: return "Authentication Required";
	case workAUTHREJECT: return "Authentication Denied";
	case workIMPORT: return "Data Import";
	case workMIRROR: return "Archive Mirroring";
	case workDELETE: return "Manual File Deletion";
	case workDELETECONFIRM: return "Manual File Deletion (Confirmed)";
	case workTRUNCATE: return "Manual File Truncation";
	case workTRUNCATECONFIRM: return "Manual File Truncation (Confirmed)";
	case workCOUNTSTATS: return "Status Report With Statistics";
	case workSTYLESHEET: return "CSS";
	// case workJStats: return "Stats";
	}
	return "SpecialOperation";
}

tSpecialRequest::eMaintWorkType tSpecialRequest::DispatchMaintWork(cmstring& cmd, const char* auth)
{
	LOGSTARTs("DispatchMaintWork");
	LOG("cmd: " << cmd);

#if 0 // defined(DEBUG)
	if(cmd.find("tickTack")!=stmiss)
	{
		tBgTester(conFD).Run(cmd);
		return;
	}
#endif

	auto epos=cmd.find('?');
	if(epos == stmiss)
		epos=cmd.length();
	auto spos=cmd.find_first_not_of('/');
	auto wlen=epos-spos;

	static string cssString("style.css");
	if(wlen==cssString.length() && 0 == (cmd.compare(spos, wlen, cssString)))
		return workSTYLESHEET;

	// not starting like the maint page?
	if(cmd.compare(spos, wlen, cfg::reportpage))
		return workNotSpecial;

	// ok, filename identical, also the end, or having a parameter string?
	if(epos == cmd.length())
		return workMAINTREPORT;

	// not smaller, was already compared, can be only longer, means having parameters,
	// means needs authorization

	// all of the following need authorization if configured, enforce it
	switch(cfg::CheckAdminAuth(auth))
	{
     case 0:
#ifdef HAVE_CHECKSUM
        break; // auth is ok or no passwort is set
#else
        // most data modifying tasks cannot be run safely without checksumming support 
        return workAUTHREJECT;
#endif
     case 1: return workAUTHREQUEST;
     default: return workAUTHREJECT;
	}

	struct { LPCSTR trigger; tSpecialRequest::eMaintWorkType type; } matches [] =
	{
			{"doExpire=", workExExpire},
			{"justShow=", workExList},
			{"justRemove=", workExPurge},
			{"justShowDamaged=", workExListDamaged},
			{"justRemoveDamaged=", workExPurgeDamaged},
			{"justTruncDamaged=", workExTruncDamaged},
			{"doImport=", workIMPORT},
			{"doMirror=", workMIRROR},
			{"doDelete=", workDELETECONFIRM},
			{"doDeleteYes=", workDELETE},
			{"doTruncate=", workTRUNCATECONFIRM},
			{"doTruncateYes=", workTRUNCATE},
			{"doCount=", workCOUNTSTATS},
			{"doTraceStart=", workTraceStart},
			{"doTraceEnd=", workTraceEnd},
//			{"doJStats", workJStats}
	};
	for(auto& needle: matches)
		if(StrHasFrom(cmd, needle.trigger, epos))
			return needle.type;

	// something weird, go to the maint page
	return workMAINTREPORT;
}

tSpecialRequest* tSpecialRequest::MakeMaintWorker(const tRunParms& parms)
{
	switch (parms.type)
	{
	case workNotSpecial:
		return nullptr;
	case workExExpire:
	case workExList:
	case workExPurge:
	case workExListDamaged:
	case workExPurgeDamaged:
	case workExTruncDamaged:
		return new expiration(parms);
	case workUSERINFO:
		return new tShowInfo(parms);
	case workMAINTREPORT:
	case workCOUNTSTATS:
	case workTraceStart:
	case workTraceEnd:
		return new tMaintPage(parms);
	case workAUTHREQUEST:
		return new tAuthRequest(parms);
	case workAUTHREJECT:
		return new authbounce(parms);
	case workIMPORT:
		return new pkgimport(parms);
	case workMIRROR:
		return new pkgmirror(parms);
	case workDELETE:
	case workDELETECONFIRM:
		return new tDeleter(parms, "Delet");
	case workTRUNCATE:
	case workTRUNCATECONFIRM:
		return new tDeleter(parms, "Truncat");
	case workSTYLESHEET:
		return new tStyleCss(parms);
#if 0
	case workJStats:
		return new jsonstats(parms);
#endif
	}
	return nullptr;
}

void tSpecialRequest::RunMaintWork(eMaintWorkType jobType, cmstring& cmd, int fd)
{
	if(cfg::DegradedMode() && jobType != workSTYLESHEET)
		jobType = workUSERINFO;

	try {
		SHARED_PTR<tSpecialRequest> p;
		p.reset(MakeMaintWorker({fd, jobType, cmd}));
		if(p)
			p->Run();
	}
	catch(...)
	{
	}
}

}