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"
28 #include "librpc/gen_ndr/winreg.h"
31 * Routines to parse a REGEDIT4 file
33 * The file consists of:
40 * [cmd:]name=type:value
42 * cmd = a|d|c|add|delete|change|as|ds|cs
44 * There can be more than one key-path and value-spec.
46 * Since we want to support more than one type of file format, we
47 * construct a command-file structure that keeps info about the command file
51 #define FMT_REGEDIT4 0
52 #define FMT_EDITREG1_1 1
54 #define FMT_STRING_REGEDIT4 "REGEDIT4"
55 #define FMT_STRING_EDITREG1_0 "EDITREG1.0"
64 typedef struct val_spec_list {
65 struct val_spec_list *next;
68 char *val; /* Kept as a char string, really? */
71 typedef struct command_s {
75 VAL_SPEC_LIST *val_spec_list, *val_spec_last;
78 typedef struct cmd_line {
83 static void free_val_spec_list(VAL_SPEC_LIST *vl)
86 if (vl->name) free(vl->name);
87 if (vl->val) free(vl->val);
93 * Some routines to handle lines of info in the command files
95 static void skip_to_eol(int fd)
100 while ((rc = read(fd, &ch, 1)) == 1) {
101 if (ch == 0x0A) return;
104 DEBUG(0, ("Could not read file descriptor: %d, %s\n",
105 fd, strerror(errno)));
110 static void free_cmd(CMD *cmd)
114 while (cmd->val_spec_list) {
117 tmp = cmd->val_spec_list;
118 cmd->val_spec_list = tmp->next;
126 static void free_cmd_line(CMD_LINE *cmd_line)
129 if (cmd_line->line) free(cmd_line->line);
134 static void print_line(struct cmd_line *cl)
140 pl = smb_xmalloc(cl->line_len + 1);
142 strncpy(pl, cl->line, cl->line_len);
143 pl[cl->line_len] = 0;
145 fprintf(stdout, "%s\n", pl);
149 #define INIT_ALLOC 10
152 * Read a line from the input file.
153 * NULL returned when EOF and no chars read
154 * Otherwise we return a cmd_line *
155 * Exit if other errors
157 static struct cmd_line *get_cmd_line(int fd)
159 CMD_LINE *cl = smb_xmalloc_p(CMD_LINE);
163 cl->len = INIT_ALLOC;
166 * Allocate some space for the line. We extend later if needed.
169 cl->line = (char *)smb_xmalloc(INIT_ALLOC);
172 * Now read in the chars to EOL. Don't store the EOL in the
173 * line. What about CR?
176 while ((rc = read(fd, &ch, 1)) == 1 && ch != '\n') {
177 if (ch == '\r') continue; /* skip CR */
178 if (i == cl->len-1) {
180 * Allocate some more memory
182 if ((cl->line = realloc(cl->line, cl->len + INIT_ALLOC)) == NULL) {
183 DEBUG(0, ("Unable to realloc space for line: %s\n",
187 cl->len += INIT_ALLOC;
193 /* read 0 and we were at loc'n 0, return NULL */
194 if (rc == 0 && i == 0) {
207 * parse_value: parse out a value. We pull it apart as:
209 * <value> ::= <value-name>=<type>:<value-string>
211 * <value-name> ::= char-string-without-spaces | '"' char-string '"'
213 * If it parsed OK, return the <value-name> as a string, and the
214 * value type and value-string in parameters.
216 * The value name can be empty. There can only be one empty name in
217 * a list of values. A value of - removes the value entirely.
220 static char *parse_name(char *nstr)
222 int len = 0, start = 0;
223 if (!nstr) return NULL;
227 while (len && nstr[len - 1] == ' ') len--;
229 nstr[len] = 0; /* Trim any spaces ... if there were none, doesn't matter */
232 * Beginning and end should be '"' or neither should be so
234 if ((nstr[0] == '"' && nstr[len - 1] != '"') ||
235 (nstr[0] != '"' && nstr[len - 1] == '"'))
238 if (nstr[0] == '"') {
243 return strndup(&nstr[start], len);
246 static int parse_value_type(char *tstr)
248 int len = strlen(tstr);
250 while (len && tstr[len - 1] == ' ') len--;
253 if (strcmp(tstr, "REG_DWORD") == 0)
255 else if (strcmp(tstr, "dword") == 0)
257 else if (strcmp(tstr, "REG_EXPAND_SZ") == 0)
258 return REG_EXPAND_SZ;
259 else if (strcmp(tstr, "REG_BIN") == 0)
261 else if (strcmp(tstr, "REG_SZ") == 0)
263 else if (strcmp(tstr, "REG_MULTI_SZ") == 0)
265 else if (strcmp(tstr, "-") == 0)
271 static char *parse_val_str(char *vstr)
274 return strndup(vstr, strlen(vstr));
278 static char *parse_value(struct cmd_line *cl, int *vtype, char **val)
280 char *p1 = NULL, *p2 = NULL, *nstr = NULL, *tstr = NULL, *vstr = NULL;
282 if (!cl || !vtype || !val) return NULL;
283 if (!cl->line[0]) return NULL;
285 p1 = strdup(cl->line);
286 /* FIXME: Better return codes etc ... */
287 if (!p1) return NULL;
288 p2 = strchr(p1, '=');
289 if (!p2) return NULL;
291 *p2 = 0; p2++; /* Split into two strings at p2 */
293 /* Now, parse the name ... */
295 nstr = parse_name(p1);
296 if (!nstr) goto error;
298 /* Now, split the remainder and parse on type and val ... */
301 while (*tstr == ' ') tstr++; /* Skip leading white space */
302 p2 = strchr(p2, ':');
305 *p2 = 0; p2++; /* split on the : */
308 *vtype = parse_value_type(tstr);
310 if (!vtype) goto error;
312 if (!p2 || !*p2) return nstr;
314 /* Now, parse the value string. It should return a newly malloc'd string */
316 while (*p2 == ' ') p2++; /* Skip leading space */
317 vstr = parse_val_str(p2);
319 if (!vstr) goto error;
327 if (nstr) free(nstr);
328 if (vstr) free(vstr);
333 * Parse out a key. Look for a correctly formatted key [...]
334 * and whether it is a delete or add? A delete is signalled
335 * by a - in front of the key.
336 * Assumes that there are no leading and trailing spaces
339 static char *parse_key(struct cmd_line *cl, int *cmd)
344 if (cl->line[0] != '[' ||
345 cl->line[cl->line_len - 1] != ']') return NULL;
346 if (cl->line_len == 2) return NULL;
348 if (cl->line[1] == '-') {
349 if (cl->line_len == 3) return NULL;
353 tmp = smb_xmalloc(cl->line_len - 1 - start + 1);
354 strncpy(tmp, &cl->line[start], cl->line_len - 1 - start);
355 tmp[cl->line_len - 1 - start] = 0;
360 * Parse a line to determine if we have a key or a value
361 * We only check for key or val ...
364 static int parse_line(struct cmd_line *cl)
367 if (!cl || cl->len == 0) return 0;
369 if (cl->line[0] == '[') /* No further checking for now */
376 * We seek to offset 0, read in the required number of bytes,
377 * and compare to the correct value.
378 * We then seek back to the original location
380 static int regedit4_file_type(int fd)
385 cur_ofs = lseek(fd, 0, SEEK_CUR); /* Get current offset */
387 DEBUG(0, ("Unable to get current offset: (%d) %s\n", cur_ofs, strerror(errno)));
392 lseek(fd, 0, SEEK_SET);
395 if (read(fd, desc, 8) < 8) {
396 DEBUG(0, ("Unable to read command file format\n"));
402 if (strcmp(desc, FMT_STRING_REGEDIT4) == 0) {
404 lseek(fd, cur_ofs, SEEK_SET);
415 * Run though the data in the line and strip anything after a comment
418 static void strip_comment(struct cmd_line *cl)
424 for (i = 0; i < cl->line_len; i++) {
425 if (cl->line[i] == ';') {
434 * Get a command ... This consists of possibly multiple lines:
437 * possibly Empty line
439 * value ::= <value-name>=<value-type>':'<value-string>
440 * <value-name> is some path, possibly enclosed in quotes ...
441 * We alctually look for the next key to terminate a previous key
442 * if <value-type> == '-', then it is a delete type.
444 static CMD *regedit4_get_cmd(int fd)
446 struct command_s *cmd = NULL;
447 struct cmd_line *cl = NULL;
448 struct val_spec_list *vl = NULL;
450 cmd = smb_xmalloc_p(struct command_s);
455 cmd->val_spec_list = cmd->val_spec_last = NULL;
456 while ((cl = get_cmd_line(fd))) {
459 * If it is an empty command line, and we already have a key
460 * then exit from here ... FIXME: Clean up the parser
463 if (cl->line_len == 0 && cmd->key) {
468 strip_comment(cl); /* remove anything beyond a comment char */
469 trim_string(cl->line, " \t", " \t");
471 if (!cl->line[0]) { /* An empty line */
474 else { /* Else, non-empty ... */
476 * Parse out the bits ...
478 switch (parse_line(cl)) {
480 if ((cmd->key = parse_key(cl, &cmd->cmd)) == NULL) {
481 DEBUG(0, ("Error parsing key from line: "));
489 * We need to add the value stuff to the list
490 * There could be a \ on the end which we need to
491 * handle at some time
493 vl = smb_xmalloc_p(struct val_spec_list);
496 vl->name = parse_value(cl, &vl->type, &vl->val);
497 if (!vl->name) goto error;
498 if (cmd->val_spec_list == NULL) {
499 cmd->val_spec_list = cmd->val_spec_last = vl;
502 cmd->val_spec_last->next = vl;
503 cmd->val_spec_last = vl;
509 DEBUG(0, ("Unrecognized line in command file: \n"));
516 if (!cmd->cmd) goto error; /* End of file ... */
522 if (cmd) free_cmd(cmd);
526 static int regedit4_exec_cmd(CMD *cmd)
532 static int editreg_1_0_file_type(int fd)
537 cur_ofs = lseek(fd, 0, SEEK_CUR); /* Get current offset */
539 DEBUG(0, ("Unable to get current offset: %s\n", strerror(errno)));
544 lseek(fd, 0, SEEK_SET);
547 if (read(fd, desc, 10) < 10) {
548 DEBUG(0, ("Unable to read command file format\n"));
554 if (strcmp(desc, FMT_STRING_EDITREG1_0) == 0) {
555 lseek(fd, cur_ofs, SEEK_SET);
562 static CMD *editreg_1_0_get_cmd(int fd)
567 static int editreg_1_0_exec_cmd(CMD *cmd)
573 typedef struct command_ops_s {
575 int (*file_type)(int fd);
576 CMD *(*get_cmd)(int fd);
577 int (*exec_cmd)(CMD *cmd);
580 static CMD_OPS default_cmd_ops[] = {
581 {0, regedit4_file_type, regedit4_get_cmd, regedit4_exec_cmd},
582 {1, editreg_1_0_file_type, editreg_1_0_get_cmd, editreg_1_0_exec_cmd},
583 {-1, NULL, NULL, NULL}
586 typedef struct command_file_s {
593 * Create a new command file structure
596 static CMD_FILE *cmd_file_create(const char *file)
603 * Let's check if the file exists ...
604 * No use creating the cmd_file structure if the file does not exist
607 if (stat(file, &sbuf) < 0) { /* Not able to access file */
608 DEBUG(0,("Stat on %s failed\n", file));
612 tmp = smb_xmalloc_p(CMD_FILE);
615 * Let's fill in some of the fields;
618 tmp->name = strdup(file);
620 if ((tmp->fd = open(file, O_RDONLY, 666)) < 0) {
621 DEBUG(0,("Error opening %s\n", file));
627 * Now, try to find the format by indexing through the table
629 while (default_cmd_ops[i].type != -1) {
630 if ((tmp->type = default_cmd_ops[i].file_type(tmp->fd)) >= 0) {
631 tmp->cmd_ops = default_cmd_ops[i];
638 * If we got here, return NULL, as we could not figure out the type
645 DEBUG(0,("Unknown type\n"));
650 * Extract commands from the command file, and execute them.
651 * We pass a table of command callbacks for that
657 * Main code from here on ...
661 * key print function here ...
665 * Sec Desc print functions
668 char *str_type(uint8_t type);
670 static int nt_apply_reg_command_file(struct registry_context *r, const char *cmd_file_name)
673 BOOL modified = False;
674 CMD_FILE *cmd_file = NULL;
675 TALLOC_CTX *mem_ctx = talloc_init("apply_cmd_file");
676 struct registry_key *tmp = NULL;
678 cmd_file = cmd_file_create(cmd_file_name);
680 while ((cmd = cmd_file->cmd_ops.get_cmd(cmd_file->fd)) != NULL) {
683 * Now, apply the requests to the tree ...
687 error = reg_open_key_abs(mem_ctx, r, cmd->key, &tmp);
689 /* If we found it, apply the other bits, else create such a key */
690 if (W_ERROR_EQUAL(error, WERR_DEST_NOT_FOUND)) {
691 if(!W_ERROR_IS_OK(reg_key_add_abs(mem_ctx, r, cmd->key, 0, NULL, &tmp))) {
692 DEBUG(0, ("Error adding new key '%s'\n", cmd->key));
698 while (cmd->val_count) {
699 VAL_SPEC_LIST *val = cmd->val_spec_list;
701 if (val->type == REG_DELETE) {
702 error = reg_del_value(tmp, val->name);
703 if(!W_ERROR_IS_OK(error)) {
704 DEBUG(0, ("Error removing value '%s'\n", val->name));
709 blob.data = (uint8_t *)val->val;
710 blob.length = strlen(val->val);
711 if(!W_ERROR_IS_OK(reg_val_set(tmp, val->name, val->type, blob))) {
712 DEBUG(0, ("Error adding new value '%s'\n", val->name));
718 cmd->val_spec_list = val->next;
719 free_val_spec_list(val);
727 * Any value does not matter ...
728 * Find the key if it exists, and delete it ...
731 error = reg_key_del_abs(r, cmd->key);
732 if(!W_ERROR_IS_OK(error)) {
733 DEBUG(0, ("Unable to delete key '%s'\n", cmd->key));
745 int main(int argc, char **argv)
750 struct registry_context *h;
751 const char *remote = NULL;
753 struct poptOption long_options[] = {
755 {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL},
757 POPT_COMMON_CREDENTIALS
761 regpatch_init_subsystems;
763 pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
765 while((opt = poptGetNextOpt(pc)) != -1) {
769 error = reg_open_remote (&h, cmdline_credentials, remote, NULL);
771 error = reg_open_local (&h);
774 if (W_ERROR_IS_OK(error)) {
775 fprintf(stderr, "Error: %s\n", win_errstr(error));
779 patch = poptGetArg(pc);
780 if(!patch) patch = "/dev/stdin";
783 nt_apply_reg_command_file(h, patch);