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.
26 * Routines to parse a REGEDIT4 file
28 * The file consists of:
35 * [cmd:]name=type:value
37 * cmd = a|d|c|add|delete|change|as|ds|cs
39 * There can be more than one key-path and value-spec.
41 * Since we want to support more than one type of file format, we
42 * construct a command-file structure that keeps info about the command file
46 #define FMT_REGEDIT4 0
47 #define FMT_EDITREG1_1 1
49 #define FMT_STRING_REGEDIT4 "REGEDIT4"
50 #define FMT_STRING_EDITREG1_0 "EDITREG1.0"
59 #include <include/includes.h>
61 typedef struct val_spec_list {
62 struct val_spec_list *next;
65 char *val; /* Kept as a char string, really? */
68 typedef struct command_s {
72 VAL_SPEC_LIST *val_spec_list, *val_spec_last;
75 typedef struct cmd_line {
80 static void free_val_spec_list(VAL_SPEC_LIST *vl)
83 if (vl->name) free(vl->name);
84 if (vl->val) free(vl->val);
90 * Some routines to handle lines of info in the command files
92 static void skip_to_eol(int fd)
97 while ((rc = read(fd, &ch, 1)) == 1) {
98 if (ch == 0x0A) return;
101 DEBUG(0, ("Could not read file descriptor: %d, %s\n",
102 fd, strerror(errno)));
107 static void free_cmd(CMD *cmd)
111 while (cmd->val_spec_list) {
114 tmp = cmd->val_spec_list;
115 cmd->val_spec_list = tmp->next;
123 static void free_cmd_line(CMD_LINE *cmd_line)
126 if (cmd_line->line) free(cmd_line->line);
131 static void print_line(struct cmd_line *cl)
137 pl = smb_xmalloc(cl->line_len + 1);
139 strncpy(pl, cl->line, cl->line_len);
140 pl[cl->line_len] = 0;
142 fprintf(stdout, "%s\n", pl);
146 #define INIT_ALLOC 10
149 * Read a line from the input file.
150 * NULL returned when EOF and no chars read
151 * Otherwise we return a cmd_line *
152 * Exit if other errors
154 static struct cmd_line *get_cmd_line(int fd)
156 struct cmd_line *cl = (CMD_LINE *)smb_xmalloc(sizeof(CMD_LINE));
160 cl->len = INIT_ALLOC;
163 * Allocate some space for the line. We extend later if needed.
166 cl->line = (char *)smb_xmalloc(INIT_ALLOC);
169 * Now read in the chars to EOL. Don't store the EOL in the
170 * line. What about CR?
173 while ((rc = read(fd, &ch, 1)) == 1 && ch != '\n') {
174 if (ch == '\r') continue; /* skip CR */
175 if (i == cl->len-1) {
177 * Allocate some more memory
179 if ((cl->line = realloc(cl->line, cl->len + INIT_ALLOC)) == NULL) {
180 DEBUG(0, ("Unable to realloc space for line: %s\n",
184 cl->len += INIT_ALLOC;
190 /* read 0 and we were at loc'n 0, return NULL */
191 if (rc == 0 && i == 0) {
204 * parse_value: parse out a value. We pull it apart as:
206 * <value> ::= <value-name>=<type>:<value-string>
208 * <value-name> ::= char-string-without-spaces | '"' char-string '"'
210 * If it parsed OK, return the <value-name> as a string, and the
211 * value type and value-string in parameters.
213 * The value name can be empty. There can only be one empty name in
214 * a list of values. A value of - removes the value entirely.
217 static char *parse_name(char *nstr)
219 int len = 0, start = 0;
220 if (!nstr) return NULL;
224 while (len && nstr[len - 1] == ' ') len--;
226 nstr[len] = 0; /* Trim any spaces ... if there were none, doesn't matter */
229 * Beginning and end should be '"' or neither should be so
231 if ((nstr[0] == '"' && nstr[len - 1] != '"') ||
232 (nstr[0] != '"' && nstr[len - 1] == '"'))
235 if (nstr[0] == '"') {
240 return strndup(&nstr[start], len);
243 static int parse_value_type(char *tstr)
245 int len = strlen(tstr);
247 while (len && tstr[len - 1] == ' ') len--;
250 if (strcmp(tstr, "REG_DWORD") == 0)
252 else if (strcmp(tstr, "dword") == 0)
254 else if (strcmp(tstr, "REG_EXPAND_SZ") == 0)
255 return REG_EXPAND_SZ;
256 else if (strcmp(tstr, "REG_BIN") == 0)
258 else if (strcmp(tstr, "REG_SZ") == 0)
260 else if (strcmp(tstr, "REG_MULTI_SZ") == 0)
262 else if (strcmp(tstr, "-") == 0)
268 static char *parse_val_str(char *vstr)
271 return strndup(vstr, strlen(vstr));
275 static char *parse_value(struct cmd_line *cl, int *vtype, char **val)
277 char *p1 = NULL, *p2 = NULL, *nstr = NULL, *tstr = NULL, *vstr = NULL;
279 if (!cl || !vtype || !val) return NULL;
280 if (!cl->line[0]) return NULL;
282 p1 = strdup(cl->line);
283 /* FIXME: Better return codes etc ... */
284 if (!p1) return NULL;
285 p2 = strchr(p1, '=');
286 if (!p2) return NULL;
288 *p2 = 0; p2++; /* Split into two strings at p2 */
290 /* Now, parse the name ... */
292 nstr = parse_name(p1);
293 if (!nstr) goto error;
295 /* Now, split the remainder and parse on type and val ... */
298 while (*tstr == ' ') tstr++; /* Skip leading white space */
299 p2 = strchr(p2, ':');
302 *p2 = 0; p2++; /* split on the : */
305 *vtype = parse_value_type(tstr);
307 if (!vtype) goto error;
309 if (!p2 || !*p2) return nstr;
311 /* Now, parse the value string. It should return a newly malloc'd string */
313 while (*p2 == ' ') p2++; /* Skip leading space */
314 vstr = parse_val_str(p2);
316 if (!vstr) goto error;
324 if (nstr) free(nstr);
325 if (vstr) free(vstr);
330 * Parse out a key. Look for a correctly formatted key [...]
331 * and whether it is a delete or add? A delete is signalled
332 * by a - in front of the key.
333 * Assumes that there are no leading and trailing spaces
336 static char *parse_key(struct cmd_line *cl, int *cmd)
341 if (cl->line[0] != '[' ||
342 cl->line[cl->line_len - 1] != ']') return NULL;
343 if (cl->line_len == 2) return NULL;
345 if (cl->line[1] == '-') {
346 if (cl->line_len == 3) return NULL;
350 tmp = smb_xmalloc(cl->line_len - 1 - start + 1);
351 strncpy(tmp, &cl->line[start], cl->line_len - 1 - start);
352 tmp[cl->line_len - 1 - start] = 0;
357 * Parse a line to determine if we have a key or a value
358 * We only check for key or val ...
361 static int parse_line(struct cmd_line *cl)
364 if (!cl || cl->len == 0) return 0;
366 if (cl->line[0] == '[') /* No further checking for now */
373 * We seek to offset 0, read in the required number of bytes,
374 * and compare to the correct value.
375 * We then seek back to the original location
377 static int regedit4_file_type(int fd)
382 cur_ofs = lseek(fd, 0, SEEK_CUR); /* Get current offset */
384 DEBUG(0, ("Unable to get current offset: (%d) %s\n", cur_ofs, strerror(errno)));
389 lseek(fd, 0, SEEK_SET);
392 if (read(fd, desc, 8) < 8) {
393 DEBUG(0, ("Unable to read command file format\n"));
399 if (strcmp(desc, FMT_STRING_REGEDIT4) == 0) {
401 lseek(fd, cur_ofs, SEEK_SET);
412 * Run though the data in the line and strip anything after a comment
415 static void strip_comment(struct cmd_line *cl)
421 for (i = 0; i < cl->line_len; i++) {
422 if (cl->line[i] == ';') {
431 * Get a command ... This consists of possibly multiple lines:
434 * possibly Empty line
436 * value ::= <value-name>=<value-type>':'<value-string>
437 * <value-name> is some path, possibly enclosed in quotes ...
438 * We alctually look for the next key to terminate a previous key
439 * if <value-type> == '-', then it is a delete type.
441 static CMD *regedit4_get_cmd(int fd)
443 struct command_s *cmd = NULL;
444 struct cmd_line *cl = NULL;
445 struct val_spec_list *vl = NULL;
447 cmd = (struct command_s *)smb_xmalloc(sizeof(struct command_s));
452 cmd->val_spec_list = cmd->val_spec_last = NULL;
453 while ((cl = get_cmd_line(fd))) {
456 * If it is an empty command line, and we already have a key
457 * then exit from here ... FIXME: Clean up the parser
460 if (cl->line_len == 0 && cmd->key) {
465 strip_comment(cl); /* remove anything beyond a comment char */
466 trim_string(cl->line, " \t", " \t");
468 if (!cl->line[0]) { /* An empty line */
471 else { /* Else, non-empty ... */
473 * Parse out the bits ...
475 switch (parse_line(cl)) {
477 if ((cmd->key = parse_key(cl, &cmd->cmd)) == NULL) {
478 DEBUG(0, ("Error parsing key from line: "));
486 * We need to add the value stuff to the list
487 * There could be a \ on the end which we need to
488 * handle at some time
490 vl = (struct val_spec_list *)smb_xmalloc(sizeof(struct val_spec_list));
493 vl->name = parse_value(cl, &vl->type, &vl->val);
494 if (!vl->name) goto error;
495 if (cmd->val_spec_list == NULL) {
496 cmd->val_spec_list = cmd->val_spec_last = vl;
499 cmd->val_spec_last->next = vl;
500 cmd->val_spec_last = vl;
506 DEBUG(0, ("Unrecognized line in command file: \n"));
513 if (!cmd->cmd) goto error; /* End of file ... */
519 if (cmd) free_cmd(cmd);
523 static int regedit4_exec_cmd(CMD *cmd)
529 static int editreg_1_0_file_type(int fd)
534 cur_ofs = lseek(fd, 0, SEEK_CUR); /* Get current offset */
536 DEBUG(0, ("Unable to get current offset: %s\n", strerror(errno)));
541 lseek(fd, 0, SEEK_SET);
544 if (read(fd, desc, 10) < 10) {
545 DEBUG(0, ("Unable to read command file format\n"));
551 if (strcmp(desc, FMT_STRING_EDITREG1_0) == 0) {
552 lseek(fd, cur_ofs, SEEK_SET);
559 static CMD *editreg_1_0_get_cmd(int fd)
564 static int editreg_1_0_exec_cmd(CMD *cmd)
570 typedef struct command_ops_s {
572 int (*file_type)(int fd);
573 CMD *(*get_cmd)(int fd);
574 int (*exec_cmd)(CMD *cmd);
577 CMD_OPS default_cmd_ops[] = {
578 {0, regedit4_file_type, regedit4_get_cmd, regedit4_exec_cmd},
579 {1, editreg_1_0_file_type, editreg_1_0_get_cmd, editreg_1_0_exec_cmd},
580 {-1, NULL, NULL, NULL}
583 typedef struct command_file_s {
590 * Create a new command file structure
593 static CMD_FILE *cmd_file_create(const char *file)
600 * Let's check if the file exists ...
601 * No use creating the cmd_file structure if the file does not exist
604 if (stat(file, &sbuf) < 0) { /* Not able to access file */
605 DEBUG(0,("Stat on %s failed\n", file));
609 tmp = (CMD_FILE *)smb_xmalloc(sizeof(CMD_FILE));
612 * Let's fill in some of the fields;
615 tmp->name = strdup(file);
617 if ((tmp->fd = open(file, O_RDONLY, 666)) < 0) {
618 DEBUG(0,("Error opening %s\n", file));
624 * Now, try to find the format by indexing through the table
626 while (default_cmd_ops[i].type != -1) {
627 if ((tmp->type = default_cmd_ops[i].file_type(tmp->fd)) >= 0) {
628 tmp->cmd_ops = default_cmd_ops[i];
635 * If we got here, return NULL, as we could not figure out the type
642 DEBUG(0,("Unknown type\n"));
647 * Extract commands from the command file, and execute them.
648 * We pass a table of command callbacks for that
654 * Main code from here on ...
658 * key print function here ...
662 * Sec Desc print functions
665 char *str_type(unsigned char type);
667 int nt_apply_reg_command_file(REG_HANDLE *regf, const char *cmd_file_name)
671 CMD_FILE *cmd_file = NULL;
672 cmd_file = cmd_file_create(cmd_file_name);
674 while ((cmd = cmd_file->cmd_ops.get_cmd(cmd_file->fd)) != NULL) {
677 * Now, apply the requests to the tree ...
682 tmp = reg_open_key(reg_get_root(regf), cmd->key);
683 /* If we found it, apply the other bits, else create such a key */
685 if(reg_key_add_name_recursive(reg_get_root(regf), cmd->key)) {
686 tmp = reg_open_key(reg_get_root(regf), cmd->key);
688 DEBUG(0, ("Error adding new key '%s'\n", cmd->key));
693 while (cmd->val_count) {
694 VAL_SPEC_LIST *val = cmd->val_spec_list;
695 REG_VAL *reg_val = NULL;
697 if (val->type == REG_DELETE) {
698 reg_val = reg_key_get_value_by_name( tmp, val->name);
699 reg_val_del(reg_val);
704 reg_val = nt_add_reg_value(tmp, val->name, val->type,
709 cmd->val_spec_list = val->next;
710 free_val_spec_list(val);
719 * Any value does not matter ...
720 * Find the key if it exists, and delete it ...
723 reg_key_del_recursive(reg_open_key(reg_get_root(regf), cmd->key));
733 int main (int argc, char **argv)
735 uint32 setparms, checkparms;
739 const char *location;
741 char *backend = "dir";
743 int fullpath = 0, no_values = 0;
744 struct poptOption long_options[] = {
746 {"backend", 'b', POPT_ARG_STRING, &backend, 'b', "backend to use", NULL},
750 pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
752 while((opt = poptGetNextOpt(pc)) != -1) {
755 setup_logging(argv[0], True);
757 location = poptGetArg(pc);
759 poptPrintUsage(pc, stderr, 0);
763 h = reg_open(backend, location, True);
765 fprintf(stderr, "Unable to open '%s' with backend '%s'\n", location, backend);
769 patch = poptGetArg(pc);
770 if(!patch) patch = "/dev/stdin";
773 nt_apply_reg_command_file(h, patch);