-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsnebu-permissions.c
143 lines (138 loc) · 4.29 KB
/
snebu-permissions.c
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
/* Copyright 2009 - 2021 Derek Pressnall
*
* This file is part of Snebu, the Simple Network Encrypting Backup Utility
*
* Snebu is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 3
* as published by the Free Software Foundation.
*
* Snebu is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Snebu. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <utime.h>
#include <time.h>
#include <getopt.h>
#include <sqlite3.h>
#include <errno.h>
int restore(int argc, char **argv);
int help(char *topic);
void usage();
extern sqlite3 *bkcatalog;
int permissions(int argc, char **argv);
int permissions(int argc, char **argv)
{
int optc;
char *sqlerr;
sqlite3_stmt *sqlres;
char *sqlstmt = 0;
char *sqlstmt2 = 0;
char *sqlstmt3 = 0;
char *sqlstmt4 = 0;
int foundopts = 0;
char *action = "";
char user[128];
char bkname[128];
char command[128];
struct option longopts[] = {
{ "list", no_argument, NULL, 'l' },
{ "add", no_argument, NULL, 'a' },
{ "remove", no_argument, NULL, 'r' },
{ "command", required_argument, NULL, 'c' },
{ "name", required_argument, NULL, 'n' },
{ "user", required_argument, NULL, 'u' },
{ NULL, no_argument, NULL, 0 }
};
int longoptidx;
int sargs = 0;
*command = *bkname = *user = '\0';
while ((optc = getopt_long(argc, argv, "larc:n:u:", longopts, &longoptidx)) >= 0) {
switch (optc) {
case 'l':
action = "list";
foundopts |= 1;
break;
case 'a':
action = "add";
foundopts |= 2;
break;
case 'r':
action = "remove";
foundopts |= 4;
break;
case 'c':
strncpy(command, optarg, 127);
command[127] = 0;
foundopts |= 8;
break;
case 'n':
strncpy(bkname, optarg, 127);
bkname[127] = 0;
foundopts |= 16;
break;
case 'u':
strncpy(user, optarg, 127);
user[127] = 0;
foundopts |= 32;
break;
default:
usage();
exit(1);
}
}
if (strcmp(action, "list") == 0) {
sqlstmt2 = strlen(user) > 0 ? sargs++, sqlite3_mprintf(" where username = '%q'", user) : sqlite3_mprintf("");
sqlstmt3 = strlen(command) > 0 ? sargs++, sqlite3_mprintf(" %s command = '%q'", (sargs < 2 ? "where" : "and"), command) : sqlite3_mprintf("");
sqlstmt4 = strlen(bkname) > 0 ? sargs++, sqlite3_mprintf(" %s backupname = '%q'", (sargs < 2 ? "where" : "and"), bkname) : sqlite3_mprintf("");
sqlite3_prepare_v2(bkcatalog, (sqlstmt = sqlite3_mprintf(
"select username, command, backupname from userpermissions%s%s%s",
sqlstmt2, sqlstmt3, sqlstmt4)), -1, &sqlres, 0);
while (sqlite3_step(sqlres) == SQLITE_ROW) {
printf("%s %s %s\n",
sqlite3_column_text(sqlres, 0),
sqlite3_column_text(sqlres, 1),
sqlite3_column_text(sqlres, 2));
}
if (sqlstmt4 != NULL)
sqlite3_free(sqlstmt4);
if (sqlstmt3 != NULL)
sqlite3_free(sqlstmt3);
if (sqlstmt2 != NULL)
sqlite3_free(sqlstmt2);
sqlite3_free(sqlstmt);
}
if (strcmp(action, "add") == 0) {
sqlite3_exec(bkcatalog, (sqlstmt = sqlite3_mprintf(
"insert into userpermissions (username, command, backupname) "
"values ('%q', '%q', '%q')", user, command, bkname)), 0, 0, &sqlerr);
if (sqlerr != 0) {
fprintf(stderr, "%s\n%s\n\n", sqlerr, sqlstmt);
sqlite3_free(sqlerr);
}
sqlite3_free(sqlstmt);
}
if (strcmp(action, "remove") == 0) {
sqlite3_exec(bkcatalog, (sqlstmt = sqlite3_mprintf(
"delete from userpermissions where username = '%q' and "
"command = '%q' and backupname = '%q'",
user, command, bkname)), 0, 0, &sqlerr);
if (sqlerr != 0) {
fprintf(stderr, "%s\n%s\n\n", sqlerr, sqlstmt);
sqlite3_free(sqlerr);
}
sqlite3_free(sqlstmt);
}
return(0);
}