Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
mxq
Public
Notifications
You must be signed in to change notification settings
Fork
3
Star
3
Code
Issues
21
Pull requests
3
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
0be33f7
helper
manpages
mysql
web
.gitignore
.vimrc
Doxyfile
LICENSE
Makefile
README.md
keywordset.c
keywordset.h
mx_flock.c
mx_flock.h
mx_getopt.c
mx_getopt.h
mx_log.c
mx_log.h
mx_mysql.c
mx_mysql.h
mx_proc.c
mx_proc.h
mx_util.c
mx_util.h
mxq.h
mxq_daemon.c
mxq_daemon.h
mxq_group.c
mxq_group.h
mxq_job.c
mxq_job.h
mxq_log.c
mxqadmin.c
mxqd.c
mxqd.h
mxqd_control.c
mxqd_control.h
mxqdctl-hostconfig.sh
mxqdump.c
mxqkill.c
mxqps.c
mxqset.c
mxqsub.c
os-release
parser.y
ppidcache.c
ppidcache.h
test_keywordset.c
test_mx_log.c
test_mx_mysql.c
test_mx_util.c
test_mxqd_control.c
test_parser.c
xmalloc.h
Breadcrumbs
mxq
/
mxqset.c
Blame
Blame
Latest commit
History
History
245 lines (212 loc) · 7.69 KB
Breadcrumbs
mxq
/
mxqset.c
Top
File metadata and controls
Code
Blame
245 lines (212 loc) · 7.69 KB
Raw
#define _GNU_SOURCE #include <argp.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include <sysexits.h> #include "mx_mysql.h" #include "mxq.h" #include "mxq_group.h" #include "keywordset.h" #include "stdarg.h" static __attribute__ ((noreturn)) void die(char *msg, ...) { va_list ap; va_start(ap, msg); fprintf(stderr, "%s: ",program_invocation_short_name); vfprintf(stderr, msg, ap); va_end(ap); exit(1); } static void verify_group_permission(struct mx_mysql *mysql , long unsigned groupid, uid_t client_uid) { struct mx_mysql_stmt *stmt = NULL; unsigned long long num_rows; unsigned uid; stmt = mx_mysql_statement_prepare(mysql,"SELECT user_uid FROM mxq_group WHERE group_id = ? LIMIT 1"); if (!stmt) die("%m\n"); mx_mysql_statement_param_bind(stmt, 0, uint64, &(groupid)); if (mx_mysql_statement_execute(stmt, &num_rows) < 0) die("%m\n"); if (num_rows < 1) die("no such group %ld\n", groupid); mx_mysql_statement_result_bind(stmt, 0, uint32, &uid); if (mx_mysql_statement_fetch(stmt) < 0) die("%m\n"); if ( client_uid != 0 && client_uid != uid ) die("no permission to access this group\n"); mx_mysql_statement_close(&stmt); } enum OPEN_CLOSED { OPEN_CLOSED_UNSET = 0, OPEN_CLOSED_CLOSED, OPEN_CLOSED_OPEN, }; static void update_closed_flag(struct mx_mysql *mysql, long unsigned groupid, enum OPEN_CLOSED open_closed) { struct mx_mysql_stmt *stmt = NULL; unsigned long long num_rows; uint64_t mask = ~MXQ_GROUP_FLAG_CLOSED; uint64_t value = open_closed == OPEN_CLOSED_CLOSED ? MXQ_GROUP_FLAG_CLOSED : 0; stmt = mx_mysql_statement_prepare(mysql, "UPDATE mxq_group SET group_flags = ( group_flags & ?) | ? WHERE group_id = ?"); if (!stmt) die("%m\n"); mx_mysql_statement_param_bind(stmt, 0, uint64, &(mask)); mx_mysql_statement_param_bind(stmt, 1, uint64, &(value)); mx_mysql_statement_param_bind(stmt, 2, uint64, &(groupid)); if (mx_mysql_statement_execute(stmt, &num_rows) < 0) die("%m\n"); if (num_rows < 1) die("no such group %ld\n", groupid); mx_mysql_statement_close(&stmt); } static void update_blacklist(struct mx_mysql *mysql, long unsigned groupid, char *update_blacklist) { struct mx_mysql_stmt *stmt = NULL; unsigned long long num_rows; char *group_blacklist = NULL; struct keywordset *blacklist; if (strcmp(update_blacklist, "") !=0 ) { stmt = mx_mysql_statement_prepare(mysql, "SELECT group_blacklist FROM mxq_group WHERE group_id = ? LIMIT 1"); if (!stmt) die("%m\n"); mx_mysql_statement_param_bind(stmt, 0, uint64, &groupid); if (mx_mysql_statement_execute(stmt, &num_rows) < 0) die("%m\n"); if (num_rows < 1) die("no such group %ld\n", groupid); mx_mysql_statement_result_bind(stmt, 0, string, &group_blacklist); if (mx_mysql_statement_fetch(stmt) < 0) die("%m\n"); mx_mysql_statement_close(&stmt); blacklist = keywordset_new(group_blacklist); free(group_blacklist); } else { blacklist = keywordset_new(NULL); } keywordset_update(blacklist, update_blacklist); group_blacklist = keywordset_get(blacklist); stmt = mx_mysql_statement_prepare(mysql, "UPDATE mxq_group SET group_blacklist = ? WHERE group_id = ?"); if (!stmt) die("%m\n"); mx_mysql_statement_param_bind(stmt, 0, string, &group_blacklist); mx_mysql_statement_param_bind(stmt, 1, uint64, &groupid); if (mx_mysql_statement_execute(stmt, &num_rows) < 0) die("%m\n"); if (num_rows < 1) die("no such group %ld\n", groupid); mx_mysql_statement_close(&stmt); free(group_blacklist); keywordset_free(blacklist); } static void update_whitelist(struct mx_mysql *mysql, long unsigned groupid, char *update_whitelist) { struct mx_mysql_stmt *stmt = NULL; unsigned long long num_rows; char *group_whitelist = NULL; struct keywordset *whitelist; if (strcmp(update_whitelist, "") !=0 ) { stmt = mx_mysql_statement_prepare(mysql, "SELECT group_whitelist FROM mxq_group WHERE group_id = ? LIMIT 1"); if (!stmt) die("%m\n"); mx_mysql_statement_param_bind(stmt, 0, uint64, &groupid); if (mx_mysql_statement_execute(stmt, &num_rows) < 0) die("%m\n"); if (num_rows < 1) die("no such group %ld\n", groupid); mx_mysql_statement_result_bind(stmt, 0, string, &group_whitelist); if (mx_mysql_statement_fetch(stmt) < 0) die("%m\n"); mx_mysql_statement_close(&stmt); whitelist = keywordset_new(group_whitelist); free(group_whitelist); } else { whitelist = keywordset_new(NULL); } keywordset_update(whitelist, update_whitelist); group_whitelist = keywordset_get(whitelist); stmt = mx_mysql_statement_prepare(mysql, "UPDATE mxq_group SET group_whitelist = ? WHERE group_id = ?"); if (!stmt) die("%m\n"); mx_mysql_statement_param_bind(stmt, 0, string, &group_whitelist); mx_mysql_statement_param_bind(stmt, 1, uint64, &groupid); if (mx_mysql_statement_execute(stmt, &num_rows) < 0) die("%m\n"); if (num_rows < 1) die("no such group %ld\n", groupid); mx_mysql_statement_close(&stmt); free(group_whitelist); keywordset_free(whitelist); } struct opts { enum OPEN_CLOSED open_closed; char *update_blacklist; char *update_whitelist; }; static error_t parser (int key, char *arg, struct argp_state *state) { struct opts *opts = state->input; switch (key) { case 10: opts->open_closed = OPEN_CLOSED_CLOSED; return 0; case 11: opts->open_closed = OPEN_CLOSED_OPEN; return 0; case 13: opts->update_blacklist = arg; return 0; case 15: opts->update_whitelist = arg; return 0; } return ARGP_ERR_UNKNOWN; } static const struct argp_option options[] = { {"closed", 10, NULL, 0, NULL}, {"open", 11, NULL, 0, NULL}, {"blacklist", 13, "", 0, NULL}, {"whitelist", 15, "", 0, NULL}, {0} }; static const struct argp argp = { options, parser, NULL, NULL }; static __attribute__ ((noreturn)) void exit_usage(char *argv0) { fprintf(stderr, "usage: %s group GID [group-options]\n" "\n" "group options:\n" "\n" " --open\n" " --closed\n" " --blacklist=STRING\n" " --whitelist=STRING\n" ,program_invocation_short_name); exit(EX_USAGE); } int main(int argc, char **argv) { char *argv0=argv[0]; struct mx_mysql *mysql = NULL; int groupid; uid_t uid = getuid(); if (argc<3 || strcmp(argv[1],"group") != 0) exit_usage(argv0); groupid=atoi(argv[2]); int sts; struct opts opts={0}; sts=argp_parse (&argp, argc-3, &argv[3], ARGP_PARSE_ARGV0|ARGP_SILENT, NULL, &opts); if (sts) exit_usage(argv0); assert(mx_mysql_initialize(&mysql) == 0); mx_mysql_option_set_default_file(mysql, MXQ_MYSQL_DEFAULT_FILE); mx_mysql_option_set_default_group(mysql, MXQ_MYSQL_DEFAULT_GROUP); assert(mx_mysql_connect_forever(&mysql) == 0); verify_group_permission(mysql, groupid, uid); if ( opts.open_closed != OPEN_CLOSED_UNSET) update_closed_flag(mysql, groupid, opts.open_closed); if ( opts.update_blacklist != NULL) update_blacklist(mysql, groupid, opts.update_blacklist); if ( opts.update_whitelist != NULL) update_whitelist(mysql, groupid, opts.update_whitelist); mx_mysql_finish(&mysql); }
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
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
You can’t perform that action at this time.