/
dpkg.ecpp
114 lines (99 loc) · 2.51 KB
/
dpkg.ecpp
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
<%pre>
#include <stdio.h>
#include <string>
#include <fstream>
#include <fcntl.h> // for open()
#include <cerrno> // for errno
#include <cstdio> // for perror()
using namespace std;
</%pre><%args>
command;
package;
</%args><%shared>
bool isFileLocked(const std::string& fileName)
{
int fd;
fd=open(fileName.c_str(), O_RDWR | O_CREAT | O_NOFOLLOW, 0640);
if (fd < 0)
{
close(fd);
return true;
}
// Aquire a write lock
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
if (fcntl(fd, F_SETLK, &fl) == -1)
{
close(fd);
return true;
}
// if we got here, we own the lock
close(fd);
return false;
}
</%shared><%cpp>
reply.setHeader ("Cache-Control", "no-cache", false);
//restrict commands
if (command == "install" ||
command == "update" ||
command == "remove" ||
command == "autoremove" ||
command == "clean")
{
string cmd = "";
bool lockNeeded = true;
if (command == "install")
cmd = "apt-get -y install "+ package;
else if (command == "remove")
cmd = "apt-get -y remove "+ package;
else if (command == "update")
{
cmd = "apt-get update";
lockNeeded = false;
}
else if (command == "autoremove")
cmd = "apt-get -y autoremove";
else if (command == "clean")
cmd = "apt-get -y clean";
cmd += " 2>&1";
reply.setDirectMode(200, "OK");
reply.out() << "<html><head>"
<< "<link rel=\"stylesheet\" type=\"text/css\" href=\"/static/stylesheets/dpkg.css\" />"
<< "<body>\n";
reply.out().flush();
if (!lockNeeded || !isFileLocked("/var/cache/apt/archives/lock")) {
if (FILE *stream = popen(cmd.c_str(), "r"))
{
char buffer[1024];
while (!feof(stream))
{
if (fgets(buffer, sizeof(buffer), stream) != NULL)
{
reply.out() << string(buffer).substr(0, strlen(buffer) - 1 ); //we must escape special chars via sout (xss)
reply.out() << "<br />\n";
reply.out().flush();
}
}
reply.out() << "<br />\n";
reply.out() << "--------------------<br />\n";
reply.out() << "- finished -<br />\n";
reply.out() << "--------------------<br />\n";
pclose(stream);
}
}
else
{
reply.out() << "can't get lock.";
}
reply.out() << "</body></html>\n";
reply.out().flush();
}
else if (command == "locked")
{
}
else
reply.out() << "Illegal command.";
</%cpp>