2 Unix SMB/CIFS implementation.
3 simple registry frontend
5 Copyright (C) 2002, Richard Sharpe, rsharpe@richardsharpe.com
6 Copyright (C) 2004, Jelmer Vernooij, jelmer@samba.org
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "dynconfig.h"
26 #include "lib/cmdline/popt_common.h"
27 #include "system/filesys.h"
30 * Routines to parse a REGEDIT4 file
32 * The file consists of:
39 * [cmd:]name=type:value
41 * cmd = a|d|c|add|delete|change|as|ds|cs
43 * There can be more than one key-path and value-spec.
45 * Since we want to support more than one type of file format, we
46 * construct a command-file structure that keeps info about the command file
50 #define FMT_REGEDIT4 0
51 #define FMT_EDITREG1_1 1
53 #define FMT_STRING_REGEDIT4 "REGEDIT4"
54 #define FMT_STRING_EDITREG1_0 "EDITREG1.0"
63 typedef struct val_spec_list {
64 struct val_spec_list *next;
67 char *val; /* Kept as a char string, really? */
70 typedef struct command_s {
74 VAL_SPEC_LIST *val_spec_list, *val_spec_last;
77 typedef struct cmd_line {
82 static void free_val_spec_list(VAL_SPEC_LIST *vl)
85 if (vl->name) free(vl->name);
86 if (vl->val) free(vl->val);
92 * Some routines to handle lines of info in the command files
94 static void skip_to_eol(int fd)
99 while ((rc = read(fd, &ch, 1)) == 1) {
100 if (ch == 0x0A) return;
103 DEBUG(0, ("Could not read file descriptor: %d, %s\n",
104 fd, strerror(errno)));
109 static void free_cmd(CMD *cmd)
113 while (cmd->val_spec_list) {
116 tmp = cmd->val_spec_list;
117 cmd->val_spec_list = tmp->next;
125 static void free_cmd_line(CMD_LINE *cmd_line)
128 if (cmd_line->line) free(cmd_line->line);
133 static void print_line(struct cmd_line *cl)
139 pl = smb_xmalloc(cl->line_len + 1);
141 strncpy(pl, cl->line, cl->line_len);
142 pl[cl->line_len] = 0;
144 fprintf(stdout, "%s\n", pl);
148 #define INIT_ALLOC 10
151 * Read a line from the input file.
152 * NULL returned when EOF and no chars read
153 * Otherwise we return a cmd_line *
154 * Exit if other errors
156 static struct cmd_line *get_cmd_line(int fd)
158 CMD_LINE *cl = smb_xmalloc_p(CMD_LINE);
162 cl->len = INIT_ALLOC;
165 * Allocate some space for the line. We extend later if needed.
168 cl->line = (char *)smb_xmalloc(INIT_ALLOC);
171 * Now read in the chars to EOL. Don't store the EOL in the
172 * line. What about CR?
175 while ((rc = read(fd, &ch, 1)) == 1 && ch != '\n') {
176 if (ch == '\r') continue; /* skip CR */
177 if (i == cl->len-1) {
179 * Allocate some more memory
181 if ((cl->line = realloc(cl->line, cl->len + INIT_ALLOC)) == NULL) {
182 DEBUG(0, ("Unable to realloc space for line: %s\n",
186 cl->len += INIT_ALLOC;
192 /* read 0 and we were at loc'n 0, return NULL */
193 if (rc == 0 && i == 0) {
206 * parse_value: parse out a value. We pull it apart as:
208 * <value> ::= <value-name>=<type>:<value-string>
210 * <value-name> ::= char-string-without-spaces | '"' char-string '"'
212 * If it parsed OK, return the <value-name> as a string, and the
213 * value type and value-string in parameters.
215 * The value name can be empty. There can only be one empty name in
216 * a list of values. A value of - removes the value entirely.
219 static char *parse_name(char *nstr)
221 int len = 0, start = 0;
222 if (!nstr) return NULL;
226 while (len && nstr[len - 1] == ' ') len--;
228 nstr[len] = 0; /* Trim any spaces ... if there were none, doesn't matter */
231 * Beginning and end should be '"' or neither should be so
233 if ((nstr[0] == '"' && nstr[len - 1] != '"') ||
234 (nstr[0] != '"' && nstr[len - 1] == '"'))
237 if (nstr[0] == '"') {
242 return strndup(&nstr[start], len);
245 static int parse_value_type(char *tstr)
247 int len = strlen(tstr);
249 while (len && tstr[len - 1] == ' ') len--;
252 if (strcmp(tstr, "REG_DWORD") == 0)
254 else if (strcmp(tstr, "dword") == 0)
256 else if (strcmp(tstr, "REG_EXPAND_SZ") == 0)
257 return REG_EXPAND_SZ;
258 else if (strcmp(tstr, "REG_BIN") == 0)
260 else if (strcmp(tstr, "REG_SZ") == 0)
262 else if (strcmp(tstr, "REG_MULTI_SZ") == 0)
264 else if (strcmp(tstr, "-") == 0)
270 static char *parse_val_str(char *vstr)
273 return strndup(vstr, strlen(vstr));
277 static char *parse_value(struct cmd_line *cl, int *vtype, char **val)
279 char *p1 = NULL, *p2 = NULL, *nstr = NULL, *tstr = NULL, *vstr = NULL;
281 if (!cl || !vtype || !val) return NULL;
282 if (!cl->line[0]) return NULL;
284 p1 = strdup(cl->line);
285 /* FIXME: Better return codes etc ... */
286 if (!p1) return NULL;
287 p2 = strchr(p1, '=');
288 if (!p2) return NULL;
290 *p2 = 0; p2++; /* Split into two strings at p2 */
292 /* Now, parse the name ... */
294 nstr = parse_name(p1);
295 if (!nstr) goto error;
297 /* Now, split the remainder and parse on type and val ... */
300 while (*tstr == ' ') tstr++; /* Skip leading white space */
301 p2 = strchr(p2, ':');
304 *p2 = 0; p2++; /* split on the : */
307 *vtype = parse_value_type(tstr);
309 if (!vtype) goto error;
311 if (!p2 || !*p2) return nstr;
313 /* Now, parse the value string. It should return a newly malloc'd string */
315 while (*p2 == ' ') p2++; /* Skip leading space */
316 vstr = parse_val_str(p2);
318 if (!vstr) goto error;
326 if (nstr) free(nstr);
327 if (vstr) free(vstr);
332 * Parse out a key. Look for a correctly formatted key [...]
333 * and whether it is a delete or add? A delete is signalled
334 * by a - in front of the key.
335 * Assumes that there are no leading and trailing spaces
338 static char *parse_key(struct cmd_line *cl, int *cmd)
343 if (cl->line[0] != '[' ||
344 cl->line[cl->line_len - 1] != ']') return NULL;
345 if (cl->line_len == 2) return NULL;
347 if (cl->line[1] == '-') {
348 if (cl->line_len == 3) return NULL;
352 tmp = smb_xmalloc(cl->line_len - 1 - start + 1);
353 strncpy(tmp, &cl->line[start], cl->line_len - 1 - start);
354 tmp[cl->line_len - 1 - start] = 0;
359 * Parse a line to determine if we have a key or a value
360 * We only check for key or val ...
363 static int parse_line(struct cmd_line *cl)
366 if (!cl || cl->len == 0) return 0;
368 if (cl->line[0] == '[') /* No further checking for now */
375 * We seek to offset 0, read in the required number of bytes,
376 * and compare to the correct value.
377 * We then seek back to the original location
379 static int regedit4_file_type(int fd)
384 cur_ofs = lseek(fd, 0, SEEK_CUR); /* Get current offset */
386 DEBUG(0, ("Unable to get current offset: (%d) %s\n", cur_ofs, strerror(errno)));
391 lseek(fd, 0, SEEK_SET);
394 if (read(fd, desc, 8) < 8) {
395 DEBUG(0, ("Unable to read command file format\n"));
401 if (strcmp(desc, FMT_STRING_REGEDIT4) == 0) {
403 lseek(fd, cur_ofs, SEEK_SET);
414 * Run though the data in the line and strip anything after a comment
417 static void strip_comment(struct cmd_line *cl)
423 for (i = 0; i < cl->line_len; i++) {
424 if (cl->line[i] == ';') {
433 * Get a command ... This consists of possibly multiple lines:
436 * possibly Empty line
438 * value ::= <value-name>=<value-type>':'<value-string>
439 * <value-name> is some path, possibly enclosed in quotes ...
440 * We alctually look for the next key to terminate a previous key
441 * if <value-type> == '-', then it is a delete type.
443 static CMD *regedit4_get_cmd(int fd)
445 struct command_s *cmd = NULL;
446 struct cmd_line *cl = NULL;
447 struct val_spec_list *vl = NULL;
449 cmd = smb_xmalloc_p(struct command_s);
454 cmd->val_spec_list = cmd->val_spec_last = NULL;
455 while ((cl = get_cmd_line(fd))) {
458 * If it is an empty command line, and we already have a key
459 * then exit from here ... FIXME: Clean up the parser
462 if (cl->line_len == 0 && cmd->key) {
467 strip_comment(cl); /* remove anything beyond a comment char */
468 trim_string(cl->line, " \t", " \t");
470 if (!cl->line[0]) { /* An empty line */
473 else { /* Else, non-empty ... */
475 * Parse out the bits ...
477 switch (parse_line(cl)) {
479 if ((cmd->key = parse_key(cl, &cmd->cmd)) == NULL) {
480 DEBUG(0, ("Error parsing key from line: "));
488 * We need to add the value stuff to the list
489 * There could be a \ on the end which we need to
490 * handle at some time
492 vl = smb_xmalloc_p(struct val_spec_list);
495 vl->name = parse_value(cl, &vl->type, &vl->val);
496 if (!vl->name) goto error;
497 if (cmd->val_spec_list == NULL) {
498 cmd->val_spec_list = cmd->val_spec_last = vl;
501 cmd->val_spec_last->next = vl;
502 cmd->val_spec_last = vl;
508 DEBUG(0, ("Unrecognized line in command file: \n"));
515 if (!cmd->cmd) goto error; /* End of file ... */
521 if (cmd) free_cmd(cmd);
525 static int regedit4_exec_cmd(CMD *cmd)
531 static int editreg_1_0_file_type(int fd)
536 cur_ofs = lseek(fd, 0, SEEK_CUR); /* Get current offset */
538 DEBUG(0, ("Unable to get current offset: %s\n", strerror(errno)));
543 lseek(fd, 0, SEEK_SET);
546 if (read(fd, desc, 10) < 10) {
547 DEBUG(0, ("Unable to read command file format\n"));
553 if (strcmp(desc, FMT_STRING_EDITREG1_0) == 0) {
554 lseek(fd, cur_ofs, SEEK_SET);
561 static CMD *editreg_1_0_get_cmd(int fd)
566 static int editreg_1_0_exec_cmd(CMD *cmd)
572 typedef struct command_ops_s {
574 int (*file_type)(int fd);
575 CMD *(*get_cmd)(int fd);
576 int (*exec_cmd)(CMD *cmd);
579 static CMD_OPS default_cmd_ops[] = {
580 {0, regedit4_file_type, regedit4_get_cmd, regedit4_exec_cmd},
581 {1, editreg_1_0_file_type, editreg_1_0_get_cmd, editreg_1_0_exec_cmd},
582 {-1, NULL, NULL, NULL}
585 typedef struct command_file_s {
592 * Create a new command file structure
595 static CMD_FILE *cmd_file_create(const char *file)
602 * Let's check if the file exists ...
603 * No use creating the cmd_file structure if the file does not exist
606 if (stat(file, &sbuf) < 0) { /* Not able to access file */
607 DEBUG(0,("Stat on %s failed\n", file));
611 tmp = smb_xmalloc_p(CMD_FILE);
614 * Let's fill in some of the fields;
617 tmp->name = strdup(file);
619 if ((tmp->fd = open(file, O_RDONLY, 666)) < 0) {
620 DEBUG(0,("Error opening %s\n", file));
626 * Now, try to find the format by indexing through the table
628 while (default_cmd_ops[i].type != -1) {
629 if ((tmp->type = default_cmd_ops[i].file_type(tmp->fd)) >= 0) {
630 tmp->cmd_ops = default_cmd_ops[i];
637 * If we got here, return NULL, as we could not figure out the type
644 DEBUG(0,("Unknown type\n"));
649 * Extract commands from the command file, and execute them.
650 * We pass a table of command callbacks for that
656 * Main code from here on ...
660 * key print function here ...
664 * Sec Desc print functions
667 char *str_type(uint8_t type);
669 static int nt_apply_reg_command_file(struct registry_context *r, const char *cmd_file_name)
672 BOOL modified = False;
673 CMD_FILE *cmd_file = NULL;
674 TALLOC_CTX *mem_ctx = talloc_init("apply_cmd_file");
675 struct registry_key *tmp = NULL;
677 cmd_file = cmd_file_create(cmd_file_name);
679 while ((cmd = cmd_file->cmd_ops.get_cmd(cmd_file->fd)) != NULL) {
682 * Now, apply the requests to the tree ...
686 error = reg_open_key_abs(mem_ctx, r, cmd->key, &tmp);
688 /* If we found it, apply the other bits, else create such a key */
689 if (W_ERROR_EQUAL(error, WERR_DEST_NOT_FOUND)) {
690 if(!W_ERROR_IS_OK(reg_key_add_abs(mem_ctx, r, cmd->key, 0, NULL, &tmp))) {
691 DEBUG(0, ("Error adding new key '%s'\n", cmd->key));
697 while (cmd->val_count) {
698 VAL_SPEC_LIST *val = cmd->val_spec_list;
700 if (val->type == REG_DELETE) {
701 error = reg_del_value(tmp, val->name);
702 if(!W_ERROR_IS_OK(error)) {
703 DEBUG(0, ("Error removing value '%s'\n", val->name));
708 if(!W_ERROR_IS_OK(reg_val_set(tmp, val->name, val->type, val->val, strlen(val->val)))) {
709 DEBUG(0, ("Error adding new value '%s'\n", val->name));
715 cmd->val_spec_list = val->next;
716 free_val_spec_list(val);
724 * Any value does not matter ...
725 * Find the key if it exists, and delete it ...
728 error = reg_key_del_abs(r, cmd->key);
729 if(!W_ERROR_IS_OK(error)) {
730 DEBUG(0, ("Unable to delete key '%s'\n", cmd->key));
742 int main(int argc, char **argv)
747 struct registry_context *h;
748 const char *remote = NULL;
750 struct poptOption long_options[] = {
752 POPT_COMMON_CREDENTIALS
753 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL},
757 regpatch_init_subsystems;
759 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
760 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
764 pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
766 while((opt = poptGetNextOpt(pc)) != -1) {
769 setup_logging(argv[0], DEBUG_STDOUT);
772 error = reg_open_remote (&h, cmdline_credentials, remote);
774 error = reg_open_local (&h);
777 if (W_ERROR_IS_OK(error)) {
778 fprintf(stderr, "Error: %s\n", win_errstr(error));
782 patch = poptGetArg(pc);
783 if(!patch) patch = "/dev/stdin";
786 nt_apply_reg_command_file(h, patch);