[196] | 1 | /* |
---|
| 2 | ** regfi.c |
---|
| 3 | ** |
---|
| 4 | ** Made by (mic) |
---|
| 5 | ** Login <mic@laptop> |
---|
| 6 | ** |
---|
| 7 | ** Started on Fri Apr 30 02:06:28 2010 mic |
---|
| 8 | ** Last update Sun May 12 01:17:25 2002 Speed Blue |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | #include "pyregfi.h" |
---|
| 12 | |
---|
| 13 | static int RegistryFile_dest(void *self) { |
---|
| 14 | RegistryFile this = (RegistryFile)self; |
---|
| 15 | |
---|
| 16 | regfi_free(this->reg); |
---|
| 17 | close(this->fd); |
---|
| 18 | |
---|
| 19 | return 0; |
---|
[198] | 20 | } |
---|
[196] | 21 | |
---|
| 22 | static RegistryFile RegistryFile_Con(RegistryFile self, char *filename) { |
---|
| 23 | self->fd = open(filename, O_RDONLY); |
---|
| 24 | if(self->fd < 0) { |
---|
| 25 | RaiseError(EIOError, "Unable to open %s", filename); |
---|
| 26 | goto error; |
---|
[198] | 27 | } |
---|
[196] | 28 | |
---|
| 29 | self->reg = regfi_alloc(self->fd); |
---|
| 30 | |
---|
| 31 | if(!self->reg) { |
---|
[198] | 32 | /*RaiseError(ERuntimeError, "REGFI Error: %s", regfi_log_get_str());*/ |
---|
| 33 | /*char* e = regfi_log_get_str();*/ |
---|
| 34 | /*fprintf(stderr, "%p\n", e);*/ |
---|
[196] | 35 | goto error; |
---|
[198] | 36 | } |
---|
[196] | 37 | |
---|
| 38 | talloc_set_destructor((void *)self, RegistryFile_dest); |
---|
| 39 | return self; |
---|
| 40 | |
---|
| 41 | error: |
---|
| 42 | talloc_free(self); |
---|
| 43 | return NULL; |
---|
[198] | 44 | } |
---|
[196] | 45 | |
---|
| 46 | static KeyIterator RegistryFile_get_key(RegistryFile self, char **path, REGFI_ENCODING encoding) { |
---|
| 47 | return CONSTRUCT(KeyIterator, KeyIterator, Con, NULL, self, path, encoding); |
---|
[198] | 48 | } |
---|
[196] | 49 | |
---|
| 50 | |
---|
| 51 | VIRTUAL(RegistryFile, Object) { |
---|
| 52 | VMETHOD(Con) = RegistryFile_Con; |
---|
| 53 | VMETHOD(get_key) = RegistryFile_get_key; |
---|
| 54 | } END_VIRTUAL |
---|
| 55 | |
---|
| 56 | static int KeyIterator_dest(void *self) { |
---|
| 57 | KeyIterator this = (KeyIterator)self; |
---|
| 58 | |
---|
| 59 | regfi_iterator_free(this->iter); |
---|
| 60 | return 0; |
---|
[198] | 61 | } |
---|
[196] | 62 | |
---|
| 63 | static KeyIterator KeyIterator_Con(KeyIterator self, RegistryFile file, char **path, |
---|
| 64 | REGFI_ENCODING encoding) { |
---|
| 65 | self->iter = regfi_iterator_new(file->reg, encoding); |
---|
| 66 | |
---|
| 67 | if(!self->iter) { |
---|
| 68 | RaiseError(ERuntimeError, "Error: %s", regfi_log_get_str()); |
---|
| 69 | goto error; |
---|
[198] | 70 | } |
---|
[196] | 71 | |
---|
| 72 | talloc_set_destructor((void*)self, KeyIterator_dest); |
---|
| 73 | |
---|
[199] | 74 | /* Traverse to the path */ |
---|
[196] | 75 | if(path[0]) { |
---|
| 76 | if(!regfi_iterator_walk_path(self->iter, (const char **)path)) { |
---|
| 77 | RaiseError(ERuntimeError, "Unable to walk down key path"); |
---|
| 78 | goto error; |
---|
[198] | 79 | } |
---|
| 80 | } |
---|
[196] | 81 | |
---|
[199] | 82 | fprintf(stderr, "Con called\n"); |
---|
| 83 | self->first_called = false; |
---|
[196] | 84 | |
---|
| 85 | return self; |
---|
| 86 | error: |
---|
| 87 | return NULL; |
---|
[198] | 88 | } |
---|
[196] | 89 | |
---|
[199] | 90 | static KeyIterator KeyIterator__iter__(KeyIterator self) { |
---|
| 91 | return self; |
---|
[198] | 92 | } |
---|
[196] | 93 | |
---|
[199] | 94 | |
---|
[198] | 95 | static const REGFI_NK_REC* KeyIterator_next(KeyIterator self) { |
---|
[196] | 96 | |
---|
[199] | 97 | fprintf(stderr, "next called\n"); |
---|
[196] | 98 | |
---|
[199] | 99 | if(!self->first_called) |
---|
| 100 | { |
---|
| 101 | regfi_iterator_first_subkey(self->iter); |
---|
| 102 | self->first_called = true; |
---|
| 103 | } |
---|
| 104 | else |
---|
| 105 | regfi_iterator_next_subkey(self->iter); |
---|
| 106 | |
---|
| 107 | return regfi_iterator_cur_subkey(self->iter); |
---|
| 108 | } |
---|
[196] | 109 | |
---|
| 110 | |
---|
[199] | 111 | static int KeyIterator_down(KeyIterator self) { |
---|
| 112 | fprintf(stderr, "down cur_subkey: %d\n", self->iter->cur_subkey); |
---|
| 113 | int result = regfi_iterator_down(self->iter); |
---|
| 114 | fprintf(stderr, "down result: %d\n", result); |
---|
| 115 | regfi_iterator_first_subkey(self->iter); |
---|
| 116 | regfi_iterator_first_value(self->iter); |
---|
[196] | 117 | return result; |
---|
[198] | 118 | } |
---|
[196] | 119 | |
---|
[199] | 120 | static int KeyIterator_up(KeyIterator self) { |
---|
| 121 | return regfi_iterator_up(self->iter); |
---|
| 122 | } |
---|
| 123 | |
---|
[196] | 124 | static ValueIterator KeyIterator_list_values(KeyIterator self) { |
---|
| 125 | return CONSTRUCT(ValueIterator, ValueIterator, Con, NULL, self); |
---|
[198] | 126 | } |
---|
[196] | 127 | |
---|
| 128 | VIRTUAL(KeyIterator, Object) { |
---|
| 129 | VMETHOD(Con) = KeyIterator_Con; |
---|
| 130 | VMETHOD(iternext) = KeyIterator_next; |
---|
[199] | 131 | VMETHOD(down) = KeyIterator_down; |
---|
| 132 | VMETHOD(up) = KeyIterator_up; |
---|
[196] | 133 | VMETHOD(__iter__) = KeyIterator__iter__; |
---|
| 134 | VMETHOD(list_values) = KeyIterator_list_values; |
---|
| 135 | } END_VIRTUAL |
---|
| 136 | |
---|
| 137 | static int ValueIterator_dest(void *self) { |
---|
| 138 | ValueIterator this = (ValueIterator)self; |
---|
| 139 | |
---|
[199] | 140 | talloc_unlink(this, this->iter); |
---|
[196] | 141 | |
---|
| 142 | return 0; |
---|
[198] | 143 | } |
---|
[196] | 144 | |
---|
| 145 | static ValueIterator ValueIterator_Con(ValueIterator self, KeyIterator key) { |
---|
| 146 | // Take a copy of the iterator |
---|
| 147 | self->iter = key->iter; |
---|
| 148 | talloc_reference(self, self->iter); |
---|
| 149 | |
---|
| 150 | talloc_set_destructor((void *)self, ValueIterator_dest); |
---|
| 151 | |
---|
| 152 | return self; |
---|
[198] | 153 | } |
---|
[196] | 154 | |
---|
[199] | 155 | static ValueIterator ValueIterator__iter__(ValueIterator self) { |
---|
| 156 | return self; |
---|
[198] | 157 | } |
---|
[196] | 158 | |
---|
| 159 | static RawData ValueIterator_iternext(ValueIterator self) { |
---|
| 160 | RawData result; |
---|
[199] | 161 | const REGFI_DATA* data; |
---|
| 162 | const REGFI_VK_REC* rec; |
---|
[196] | 163 | |
---|
[199] | 164 | if(!self->first_called) |
---|
| 165 | { |
---|
| 166 | regfi_iterator_first_value(self->iter); |
---|
| 167 | self->first_called = true; |
---|
| 168 | } |
---|
| 169 | else |
---|
| 170 | regfi_iterator_next_value(self->iter); |
---|
| 171 | |
---|
| 172 | rec = regfi_iterator_cur_value(self->iter); |
---|
| 173 | |
---|
[196] | 174 | if(!rec) return NULL; |
---|
| 175 | |
---|
[199] | 176 | /* XXX: shouldn't parse data here every time we walk over a value. |
---|
| 177 | * Instead, make data fetching a method and parse it then. |
---|
| 178 | */ |
---|
[196] | 179 | data = (REGFI_DATA *)regfi_iterator_fetch_data(self->iter, rec); |
---|
| 180 | if(!data) { |
---|
| 181 | RaiseError(ERuntimeError, "Unable to fetch data: %s", regfi_log_get_str()); |
---|
| 182 | goto error; |
---|
[198] | 183 | } |
---|
[196] | 184 | |
---|
[199] | 185 | switch(rec->type) { |
---|
[196] | 186 | case REG_EXPAND_SZ: |
---|
| 187 | case REG_SZ: |
---|
| 188 | result = (RawData)CONSTRUCT(DataString, RawData, Con, NULL, data, rec); |
---|
| 189 | break; |
---|
| 190 | |
---|
| 191 | case REG_DWORD: |
---|
| 192 | result = (RawData)CONSTRUCT(DWORDData, RawData, Con, NULL, data, rec); |
---|
| 193 | break; |
---|
| 194 | |
---|
| 195 | case REG_BINARY: |
---|
| 196 | default: |
---|
| 197 | result = (RawData)CONSTRUCT(RawData, RawData, Con, NULL, data, rec); |
---|
| 198 | break; |
---|
[198] | 199 | } |
---|
[196] | 200 | |
---|
| 201 | return result; |
---|
| 202 | error: |
---|
| 203 | talloc_free(self); |
---|
| 204 | return NULL; |
---|
[198] | 205 | } |
---|
[196] | 206 | |
---|
| 207 | VIRTUAL(ValueIterator, Object) { |
---|
| 208 | VMETHOD(Con) = ValueIterator_Con; |
---|
| 209 | VMETHOD(__iter__) = ValueIterator__iter__; |
---|
| 210 | VMETHOD(iternext) = ValueIterator_iternext; |
---|
| 211 | } END_VIRTUAL |
---|
| 212 | |
---|
| 213 | static int RawData_dest(void *self) { |
---|
| 214 | RawData this = (RawData)self; |
---|
| 215 | |
---|
| 216 | if(this->data) { |
---|
| 217 | regfi_free_record(this->data); |
---|
| 218 | }; |
---|
| 219 | |
---|
| 220 | if(this->rec) { |
---|
| 221 | regfi_free_record(this->rec); |
---|
| 222 | }; |
---|
| 223 | |
---|
| 224 | return 0; |
---|
[198] | 225 | } |
---|
[196] | 226 | |
---|
| 227 | static RawData RawData_Con(RawData self, REGFI_DATA *data, REGFI_VK_REC *record) { |
---|
| 228 | self->rec = record; |
---|
| 229 | self->data = data; |
---|
| 230 | |
---|
| 231 | talloc_set_destructor((void *)self, RawData_dest); |
---|
| 232 | |
---|
| 233 | return self; |
---|
[198] | 234 | } |
---|
[196] | 235 | |
---|
| 236 | static int RawData_get_value(RawData self, char *buff, int len) { |
---|
| 237 | int available_to_read = min(len, self->data->interpreted_size); |
---|
| 238 | |
---|
| 239 | memcpy(buff, self->data->raw, available_to_read); |
---|
| 240 | |
---|
| 241 | return available_to_read; |
---|
[198] | 242 | } |
---|
[196] | 243 | |
---|
| 244 | VIRTUAL(RawData, Object) { |
---|
| 245 | VMETHOD(Con) = RawData_Con; |
---|
| 246 | VMETHOD(get_value) = RawData_get_value; |
---|
| 247 | } END_VIRTUAL |
---|
| 248 | |
---|
[198] | 249 | static char* DataString_get_value(DataString self) { |
---|
[196] | 250 | RawData this = (RawData)self; |
---|
| 251 | |
---|
[198] | 252 | return (char*)this->data->interpreted.string; |
---|
| 253 | } |
---|
[196] | 254 | |
---|
| 255 | VIRTUAL(DataString, RawData) { |
---|
| 256 | VMETHOD(get_value) = DataString_get_value; |
---|
| 257 | } END_VIRTUAL |
---|
| 258 | |
---|
| 259 | static uint64_t DWORDData_get_value(DWORDData self) { |
---|
| 260 | RawData this = (RawData)self; |
---|
| 261 | |
---|
| 262 | return this->data->interpreted.dword; |
---|
[198] | 263 | } |
---|
[196] | 264 | |
---|
| 265 | VIRTUAL(DWORDData, RawData) { |
---|
| 266 | VMETHOD(get_value) = DWORDData_get_value; |
---|
| 267 | } END_VIRTUAL |
---|
| 268 | |
---|
| 269 | void pyregfi_init() { |
---|
[199] | 270 | regfi_init(); |
---|
[196] | 271 | INIT_CLASS(RegistryFile); |
---|
[198] | 272 | } |
---|