-
Notifications
You must be signed in to change notification settings - Fork 0
/
spool_controller.cpp
109 lines (98 loc) · 3.02 KB
/
spool_controller.cpp
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
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <errno.h>
#include <fstream>
#include <sstream>
#include <sys/stat.h>
#include <cstdio>
#include <unistd.h>
#include "spool_controller.hpp"
// Static globals
static std::string SPOOL_DIR = "/var/spool/printer";
static std::string SPOOL_INFO = "spool_info.txt";
spool_controller::spool_controller() : info(SPOOL_DIR + "/" + SPOOL_INFO) {
current_uid = getuid();
euid = geteuid();
// Check if the spool directory exists
std::ifstream spool_dir(SPOOL_DIR);
// If not, create it
if (!spool_dir) {
if (errno == ENOENT)
mkdir(SPOOL_DIR.c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
else {
perror("Error");
exit(1);
}
} else
spool_dir.close();
seteuid(current_uid);
// Check permissions
struct stat statbuf;
if (stat(SPOOL_DIR.c_str(), &statbuf) == -1) {
std::cout << "Error getting permissions for the spool directory\n";
exit(1);
} else if ((statbuf.st_mode & S_IFDIR) == 0) {
std::cout << "Spool directory is actually a file.";
exit(1);
} else if ((statbuf.st_mode & S_IRUSR) == 0) {
std::cout << "Spool directory is not readable.";
exit(1);
} else if ((statbuf.st_mode & S_IWUSR) == 0) {
std::cout << "Spool directory is not writable.";
exit(1);
} else if ((statbuf.st_mode & S_IRWXO & S_IRWXG) != 0) {
std::cout << "Spool directory has incorrect permissions";
exit(1);
}
}
void spool_controller::add_files(std::vector<std::string> files) {
for (auto fname : files) {
std::ifstream file(fname);
if (!file) {
if (errno == EACCES)
std::cout << fname << ": X Permission denied\n";
else if (errno == ENOENT)
std::cout << fname << ": X File not found\n";
continue;
}
seteuid(euid);
// Get the destination file name from the spool_info object
auto result = info.add_file(fname, current_uid);
auto destination_filename = result.second;
auto id = result.first;
std::ofstream outfile(SPOOL_DIR + "/" + destination_filename);
seteuid(current_uid);
// Do the transfer
outfile << file.rdbuf();
outfile.close();
file.close();
std::cout << fname << ": Y " << id << std::endl;
}
}
void spool_controller::rm_files(std::vector<std::string> ids) {
for (auto id : ids) {
// Remove the file associated with the id from the spool info, and get its
// name
auto fname = info.rm_file(id, current_uid);
// If such a file exists, remove it from the spool directory.
if (!fname.empty()) {
seteuid(euid);
remove((SPOOL_DIR + "/" + fname).c_str());
seteuid(current_uid);
std::cout << id << ": Y\n";
} else {
if (errno == EACCES)
std::cout << id << ": X Permission denied\n";
else if (errno == ENOENT)
std::cout << id << ": X File not found\n";
}
}
}
void spool_controller::ls_files() {
// Listing files requires privilege,
// since we need to take a stat on spooled files.
seteuid(euid);
std::cout << info.ls_files(SPOOL_DIR);
seteuid(current_uid);
}