auth/ntlmssp: do map to guest checking after the authentication
[Samba.git] / ctdb / server / ctdb_lock_helper.c
blob7a09ecf3c70edc9777c066384496f97db6407dd1
1 /*
2 ctdb lock helper
4 Copyright (C) Amitay Isaacs 2013
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "tdb.h"
22 #include "system/filesys.h"
23 #include "ctdb_private.h"
25 static char *progname = NULL;
27 static void send_result(int fd, char result)
29 sys_write(fd, &result, 1);
30 if (result == 1) {
31 exit(1);
36 static void usage(void)
38 fprintf(stderr, "\n");
39 fprintf(stderr, "Usage: %s <log-fd> <ctdbd-pid> <output-fd> RECORD <db-path> <db-flags> <db-key>\n",
40 progname);
41 fprintf(stderr, " %s <log-fd> <ctdbd-pid> <output-fd> DB <db1-path> <db1-flags> [<db2-path> <db2-flags>...]\n",
42 progname);
45 static uint8_t *hex_decode_talloc(TALLOC_CTX *mem_ctx,
46 const char *hex_in, size_t *len)
48 int i, num;
49 uint8_t *buffer;
51 *len = strlen(hex_in) / 2;
52 buffer = talloc_array(mem_ctx, unsigned char, *len);
54 for (i=0; i<*len; i++) {
55 sscanf(&hex_in[i*2], "%02X", &num);
56 buffer[i] = (uint8_t)num;
59 return buffer;
62 static int lock_record(const char *dbpath, const char *dbflags, const char *dbkey)
64 TDB_DATA key;
65 struct tdb_context *tdb;
66 int tdb_flags;
68 /* No error checking since CTDB always passes sane values */
69 tdb_flags = strtol(dbflags, NULL, 0);
71 /* Convert hex key to key */
72 if (strcmp(dbkey, "NULL") == 0) {
73 key.dptr = NULL;
74 key.dsize = 0;
75 } else {
76 key.dptr = hex_decode_talloc(NULL, dbkey, &key.dsize);
79 tdb = tdb_open(dbpath, 0, tdb_flags, O_RDWR, 0600);
80 if (tdb == NULL) {
81 fprintf(stderr, "%s: Error opening database %s\n", progname, dbpath);
82 return 1;
85 if (tdb_chainlock(tdb, key) < 0) {
86 fprintf(stderr, "%s: Error getting record lock (%s)\n",
87 progname, tdb_errorstr(tdb));
88 return 1;
91 return 0;
96 static int lock_db(const char *dbpath, const char *dbflags)
98 struct tdb_context *tdb;
99 int tdb_flags;
101 /* No error checking since CTDB always passes sane values */
102 tdb_flags = strtol(dbflags, NULL, 0);
104 tdb = tdb_open(dbpath, 0, tdb_flags, O_RDWR, 0600);
105 if (tdb == NULL) {
106 fprintf(stderr, "%s: Error opening database %s\n", progname, dbpath);
107 return 1;
110 if (tdb_lockall(tdb) < 0) {
111 fprintf(stderr, "%s: Error getting db lock (%s)\n",
112 progname, tdb_errorstr(tdb));
113 return 1;
116 return 0;
120 int main(int argc, char *argv[])
122 int write_fd, log_fd;
123 char result = 0;
124 int ppid;
125 const char *lock_type;
127 progname = argv[0];
129 if (argc < 5) {
130 usage();
131 exit(1);
134 if (!set_scheduler()) {
135 fprintf(stderr, "%s: Unable to set real-time scheduler priority\n",
136 progname);
139 log_fd = atoi(argv[1]);
140 close(STDOUT_FILENO);
141 close(STDERR_FILENO);
142 dup2(log_fd, STDOUT_FILENO);
143 dup2(log_fd, STDERR_FILENO);
144 close(log_fd);
146 ppid = atoi(argv[2]);
147 write_fd = atoi(argv[3]);
148 lock_type = argv[4];
150 if (strcmp(lock_type, "RECORD") == 0) {
151 if (argc != 8) {
152 fprintf(stderr, "%s: Invalid number of arguments (%d)\n",
153 progname, argc);
154 usage();
155 exit(1);
157 result = lock_record(argv[5], argv[6], argv[7]);
159 } else if (strcmp(lock_type, "DB") == 0) {
160 int n;
162 /* If there are no databases specified, no need for lock */
163 if (argc > 5) {
164 for (n=5; n+1<argc; n+=2) {
165 result = lock_db(argv[n], argv[n+1]);
166 if (result != 0) {
167 break;
172 } else {
173 fprintf(stderr, "%s: Invalid lock-type '%s'\n", progname, lock_type);
174 usage();
175 exit(1);
178 send_result(write_fd, result);
180 while (kill(ppid, 0) == 0 || errno != ESRCH) {
181 sleep(5);
183 return 0;