annotate TOOLS/modify_reg.c @ 31603:2b455d7357cc

Check packet size before memmove to avoid crashes e.g. if we recognized the wrong type and subtracted more header bytes than there are overall bytes.
author reimar
date Sat, 10 Jul 2010 16:43:00 +0000
parents 8b0734633619
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23996
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
1 /*
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
2 * A program to modify a registry file.
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
3 *
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
4 * Copyright (C) 2007 Alan Nisota
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
5 *
26743
0f42fb42843c Use standard license headers with standard formatting.
diego
parents: 26575
diff changeset
6 * This file is part of MPlayer.
0f42fb42843c Use standard license headers with standard formatting.
diego
parents: 26575
diff changeset
7 *
0f42fb42843c Use standard license headers with standard formatting.
diego
parents: 26575
diff changeset
8 * MPlayer is free software; you can redistribute it and/or modify
0f42fb42843c Use standard license headers with standard formatting.
diego
parents: 26575
diff changeset
9 * it under the terms of the GNU General Public License as published by
0f42fb42843c Use standard license headers with standard formatting.
diego
parents: 26575
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
0f42fb42843c Use standard license headers with standard formatting.
diego
parents: 26575
diff changeset
11 * (at your option) any later version.
0f42fb42843c Use standard license headers with standard formatting.
diego
parents: 26575
diff changeset
12 *
0f42fb42843c Use standard license headers with standard formatting.
diego
parents: 26575
diff changeset
13 * MPlayer is distributed in the hope that it will be useful,
0f42fb42843c Use standard license headers with standard formatting.
diego
parents: 26575
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0f42fb42843c Use standard license headers with standard formatting.
diego
parents: 26575
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0f42fb42843c Use standard license headers with standard formatting.
diego
parents: 26575
diff changeset
16 * GNU General Public License for more details.
0f42fb42843c Use standard license headers with standard formatting.
diego
parents: 26575
diff changeset
17 *
0f42fb42843c Use standard license headers with standard formatting.
diego
parents: 26575
diff changeset
18 * You should have received a copy of the GNU General Public License along
0f42fb42843c Use standard license headers with standard formatting.
diego
parents: 26575
diff changeset
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
0f42fb42843c Use standard license headers with standard formatting.
diego
parents: 26575
diff changeset
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23996
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
21 */
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
22
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
23 #include <unistd.h>
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
24 #include <getopt.h>
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
25 #include "loader/registry.c"
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
26
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
27 #include "mp_msg.h"
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
28 #ifdef __GNUC__
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
29 #define mp_msg(t, l, m, args...) fprintf(stderr, m, ##args)
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
30 #else
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
31 #define mp_msg(t, l, ...) fprintf(stderr, __VA_ARGS__)
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
32 #endif
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
33
31332
8b0734633619 get_path.c was renamed to path.c.
diego
parents: 30633
diff changeset
34 #include "path.c"
23996
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
35
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
36 static void remove_key(long handle, const char* name) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
37 int i, len;
24180
73aafdcc92fc warning fix:
diego
parents: 23996
diff changeset
38 char *fullname;
23996
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
39
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
40 fullname = build_keyname(handle, name);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
41 len = strlen(fullname);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
42 for (i=0; i < reg_size;) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
43 if (!strncmp(regs[i].name, fullname, len)) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
44 free(regs[i].value);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
45 free(regs[i].name);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
46 memmove(&regs[i], &regs[i+1], --reg_size*sizeof(struct reg_value));
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
47 } else {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
48 i++;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
49 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
50 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
51 free(fullname);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
52 save_registry();
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
53 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
54
26575
1ca484e74f18 Mark all functions that are only used within the file as static.
diego
parents: 24180
diff changeset
55 static void parse_key(char *raw, HKEY *root, char *path, char *key) {
23996
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
56 char *tmpkey, *start;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
57 tmpkey = strrchr(raw, '\\');
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
58 if (tmpkey == raw || tmpkey == NULL) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
59 printf("Couldn't process key \"%s\"\n", raw);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
60 return;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
61 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
62 start = strchr(raw, '\\');
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
63 if (start == raw || start == NULL) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
64 printf("Couldn't process key \"%s\"\n", raw);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
65 return;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
66 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
67 if (strncmp(raw, "HKEY_CURRENT_USER\\", 18) == 0 ||
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
68 strncmp(raw, "HKCU\\", 5) == 0) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
69 *root = HKEY_CURRENT_USER;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
70 } else if (strncmp(raw, "HKEY_LOCAL_MACHINE\\", 19) == 0 ||
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
71 strncmp(raw, "HKLM\\", 5) == 0) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
72 *root = HKEY_LOCAL_MACHINE;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
73 } else {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
74 printf("Couldn't process key \"%s\"\n", raw);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
75 return;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
76 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
77 strncpy(key, tmpkey + 1, strlen(tmpkey)-1);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
78 key[strlen(tmpkey)-1] = 0;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
79 while(*start == '\\')
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
80 start++;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
81 while(*(tmpkey-1) == '\\')
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
82 tmpkey--;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
83 strncpy(path, start, tmpkey - start);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
84 path[tmpkey - start] = 0;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
85 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
86
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
87 int main(int argc, char *argv[]) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
88 int i;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
89 long type = REG_SZ;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
90 char c, path[256], key[256], *value = NULL;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
91 HKEY root = 0;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
92 int Option_Index;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
93 int list = 0, del = 0;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
94 int newkey, status;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
95 static struct option Long_Options[] = {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
96 {"registry", 1, 0, 'r'},
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
97 {"list", 0, 0, 'l'},
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
98 {"key", 1, 0, 'k'},
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
99 {"value", 1, 0, 'v'},
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
100 {"type", 1, 0, 't'},
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
101 {"del", 0, 0, 'd'},
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
102 };
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
103 while(1) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
104 c = getopt_long(argc, argv, "r:lk:v:t:id", Long_Options, &Option_Index);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
105 if (c == EOF)
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
106 break;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
107 switch(c) {
29263
0f1b5b68af32 whitespace cosmetics: Remove all trailing whitespace.
diego
parents: 26743
diff changeset
108 case 'r':
23996
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
109 localregpathname = optarg;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
110 break;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
111 case 'l':
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
112 list = 1;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
113 break;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
114 case 'k':
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
115 parse_key(optarg, &root, path, key);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
116 break;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
117 case 'v':
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
118 value = optarg;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
119 break;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
120 case 't':
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
121 if (!strcmp(optarg, "string"))
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
122 type = REG_SZ;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
123 else if (!strcmp(optarg, "dword"))
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
124 type = REG_DWORD;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
125 break;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
126 case 'd':
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
127 del = 1;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
128 break;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
129 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
130 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
131 if (localregpathname == NULL || (! list && ! root)) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
132 printf("Must specify '-r' and either '-k' or '-l'\n");
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
133 return 1;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
134 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
135 if (del && (list || value)) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
136 printf("Can't specify '-d' along with '-l' or '-v'\n");
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
137 return 1;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
138 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
139 open_registry();
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
140 insert_handle(HKEY_LOCAL_MACHINE, "HKLM");
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
141 insert_handle(HKEY_CURRENT_USER, "HKCU");
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
142
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
143 if (del) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
144 char tmpname[256];
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
145 sprintf(tmpname, "%s\\%s", path, key);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
146 remove_key(root, tmpname);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
147 return 0;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
148 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
149
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
150 if (list) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
151 for (i=0; i < reg_size; i++) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
152 if (regs[i].type == DIR) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
153 printf("Directory: %s\n", regs[i].name);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
154 } else if (regs[i].type == REG_DWORD) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
155 DWORD v = *(DWORD *)regs[i].value;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
156 printf("%s :: %08x type: DWORD\n", regs[i].name, v);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
157 } else if (regs[i].type == REG_SZ) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
158 printf("%s :: '%s' len: %d type: String\n",
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
159 regs[i].name, regs[i].value, regs[i].len);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
160 } else {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
161 printf("%s :: '%s' len: %d type: %08x\n",
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
162 regs[i].name, regs[i].value, regs[i].len, regs[i].type);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
163 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
164 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
165 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
166 if (root) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
167 RegCreateKeyExA(root, path, 0, 0, 0, 0, 0, &newkey, &status);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
168 if (value != NULL) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
169 int len;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
170 DWORD v;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
171 if (type == REG_DWORD) {
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
172 len = sizeof(DWORD);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
173 v = strtoul(value, NULL, 0);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
174 value = (char *)&v;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
175 } else
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
176 len = strlen(value)+1;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
177 printf("%08x -- %d\n", *value, len);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
178 RegSetValueExA(newkey, key, 0, type, value, len);
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
179 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
180 }
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
181 return 0;
f579fc609048 Added Alan Nisotas modify_reg.
cehoyos
parents:
diff changeset
182 }