Mercurial > projects > libpws
annotate pwsdump.c @ 5:b3fc9f7e2b43
Fix manpage title
author | Guido Berhoerster <guido+libpws@berhoerster.name> |
---|---|
date | Thu, 10 Jan 2019 08:05:32 +0100 |
parents | 97097b4b6bfb |
children |
rev | line source |
---|---|
2
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1 /* |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
2 * Copyright (C) 2015 Guido Berhoerster <guido+libpws@berhoerster.name> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
3 * |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
4 * Permission is hereby granted, free of charge, to any person obtaining |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
5 * a copy of this software and associated documentation files (the |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
6 * "Software"), to deal in the Software without restriction, including |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
7 * without limitation the rights to use, copy, modify, merge, publish, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
8 * distribute, sublicense, and/or sell copies of the Software, and to |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
9 * permit persons to whom the Software is furnished to do so, subject to |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
10 * the following conditions: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
11 * |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
12 * The above copyright notice and this permission notice shall be included |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
13 * in all copies or substantial portions of the Software. |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
14 * |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
22 */ |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
23 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
24 #include "compat.h" |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
25 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
26 #include <ctype.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
27 #include <fcntl.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
28 #include <unistd.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
29 #include <stdlib.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
30 #include <string.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
31 #include <strings.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
32 #include <stdio.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
33 #include <inttypes.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
34 #include <limits.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
35 #include <time.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
36 #include <errno.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
37 #include <assert.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
38 #include <libgen.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
39 #include <sys/stat.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
40 #ifdef HAVE_READPASSPHRASE_H |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
41 #include <readpassphrase.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
42 #endif /* HAVE_READPASSPHRASE_H */ |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
43 #ifdef HAVE_VIS_H |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
44 #include <vis.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
45 #endif /* HAVE_VIS_H */ |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
46 #ifdef HAVE_SYS_ENDIAN_H |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
47 #include <sys/endian.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
48 #else |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
49 #ifdef HAVE_ENDIAN_H |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
50 #include <endian.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
51 #endif /* HAVE_ENDIAN_H */ |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
52 #endif /* HAVE_SYS_ENDIAN_H */ |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
53 #ifdef HAVE_ERR_H |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
54 #include <err.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
55 #endif /* HAVE_ERR_H */ |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
56 #include <pws.h> |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
57 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
58 #define EXIT_USAGE 2 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
59 #define TIME_FORMAT "%Y-%m-%dT%TZ" |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
60 #define TIME_SIZE (4 + 1 + 2 + 1 + 2 + 1 + 8 + 1 + 1) |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
61 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
62 enum { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
63 FORMAT_PWS3, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
64 FORMAT_DUMP |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
65 }; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
66 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
67 enum { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
68 INITIAL, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
69 IN_HEADER, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
70 IN_RECORD |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
71 }; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
72 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
73 static void |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
74 header_field_dump_write(struct pws3_field *field, FILE *fp) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
75 size_t i; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
76 const char *text; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
77 char *vis_text; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
78 const unsigned char *bytes; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
79 time_t time; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
80 struct tm *tm; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
81 char time_buf[TIME_SIZE]; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
82 size_t len; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
83 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
84 fprintf(fp, "%02x:", pws3_field_get_type(field)); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
85 switch (pws3_field_get_data_type(field)) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
86 case PWS_DATA_TYPE_UUID: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
87 bytes = pws3_field_get_uuid(field); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
88 for (i = 0; i < 16; i++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
89 fprintf(fp, "%02x", bytes[i]); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
90 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
91 fprintf(fp, "\n"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
92 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
93 case PWS_DATA_TYPE_TEXT: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
94 text = pws3_field_get_text(field); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
95 text = (text != NULL) ? text : ""; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
96 vis_text = malloc(4 * strlen(text) + 1); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
97 if (vis_text == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
98 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
99 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
100 strvis(vis_text, text, VIS_TAB | VIS_NL | VIS_CSTYLE); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
101 fprintf(fp, "%s\n", vis_text); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
102 free(vis_text); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
103 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
104 case PWS_DATA_TYPE_TIME: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
105 time = pws3_field_get_time(field); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
106 tm = gmtime(&time); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
107 strftime(time_buf, sizeof (time_buf), TIME_FORMAT, tm); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
108 fprintf(fp, "%s\n", time_buf); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
109 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
110 case PWS_DATA_TYPE_UINT8: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
111 fprintf(fp, "%02" PRIx8 "\n", |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
112 pws3_field_get_uint8(field)); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
113 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
114 case PWS_DATA_TYPE_UINT16: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
115 fprintf(fp, "%04" PRIx16 "\n", |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
116 pws3_field_get_uint16(field)); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
117 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
118 case PWS_DATA_TYPE_UINT32: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
119 fprintf(fp, "%08" PRIx32 "\n", |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
120 pws3_field_get_uint32(field)); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
121 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
122 case PWS_DATA_TYPE_BYTES: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
123 pws3_field_get_bytes(field, &bytes, &len); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
124 for (i = 0; i < len; i++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
125 fprintf(fp, "%s%02x", (i > 0) ? " " : "", bytes[i]); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
126 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
127 fprintf(fp, "\n"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
128 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
129 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
130 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
131 static void |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
132 record_field_dump_write(struct pws3_field *field, FILE *fp) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
133 uint8_t field_type; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
134 size_t i; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
135 const char *text; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
136 char *vis_text; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
137 const unsigned char *bytes; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
138 time_t time; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
139 struct tm *tm; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
140 char time_buf[TIME_SIZE]; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
141 size_t len; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
142 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
143 field_type = pws3_field_get_type(field); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
144 fprintf(fp, "%02x:", field_type); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
145 switch (pws3_field_get_data_type(field)) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
146 case PWS_DATA_TYPE_UUID: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
147 bytes = pws3_field_get_uuid(field); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
148 for (i = 0; i < 16; i++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
149 fprintf(fp, "%02x", bytes[i]); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
150 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
151 fprintf(fp, "\n"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
152 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
153 case PWS_DATA_TYPE_TEXT: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
154 text = pws3_field_get_text(field); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
155 text = (text != NULL) ? text : ""; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
156 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
157 if (field_type == PWS3_RECORD_FIELD_PASSWORD) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
158 vis_text = malloc(4 * strlen(text) + 1); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
159 if (vis_text == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
160 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
161 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
162 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
163 vis_text = malloc(4 * strlen(text) + 1); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
164 if (vis_text == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
165 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
166 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
167 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
168 strvis(vis_text, text, VIS_TAB | VIS_NL | VIS_CSTYLE); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
169 fprintf(fp, "%s\n", vis_text); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
170 if (field_type == PWS3_RECORD_FIELD_PASSWORD) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
171 free(vis_text); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
172 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
173 free(vis_text); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
174 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
175 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
176 case PWS_DATA_TYPE_TIME: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
177 time = pws3_field_get_time(field); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
178 tm = gmtime(&time); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
179 strftime(time_buf, sizeof (time_buf), TIME_FORMAT, tm); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
180 fprintf(fp, "%s\n", time_buf); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
181 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
182 case PWS_DATA_TYPE_UINT8: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
183 fprintf(fp, "%02" PRIx8 "\n", |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
184 pws3_field_get_uint8(field)); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
185 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
186 case PWS_DATA_TYPE_UINT16: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
187 fprintf(fp, "%04" PRIx16 "\n", |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
188 pws3_field_get_uint16(field)); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
189 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
190 case PWS_DATA_TYPE_UINT32: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
191 fprintf(fp, "%08" PRIx32 "\n", |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
192 pws3_field_get_uint32(field)); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
193 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
194 case PWS_DATA_TYPE_BYTES: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
195 pws3_field_get_bytes(field, &bytes, &len); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
196 for (i = 0; i < len; i++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
197 fprintf(fp, "%s%02x", (i > 0) ? " " : "", bytes[i]); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
198 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
199 fprintf(fp, "\n"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
200 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
201 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
202 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
203 static int |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
204 parse_hex_byte(const char *str, uint8_t *u8p) |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
205 { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
206 size_t i; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
207 uint8_t value; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
208 uint8_t u8 = 0; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
209 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
210 if (!(isascii(str[0]) && isxdigit(str[0])) || |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
211 !(isascii(str[1]) && isxdigit(str[1]))) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
212 return (-1); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
213 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
214 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
215 for (i = 0; i < 2; i++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
216 if (str[i] >= '0' && str[i] <= '9') { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
217 value = (str[i] - '0'); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
218 } else if (str[i] >= 'A' && str[i] <= 'F') { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
219 value = (10 + (str[i] - 'A')); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
220 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
221 value = (10 + (str[i] - 'a')); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
222 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
223 u8 += value << ((2 - 1 - i) * 4); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
224 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
225 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
226 *u8p = u8; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
227 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
228 return (0); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
229 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
230 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
231 static struct pws3_field * |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
232 header_field_dump_parse(const char *line) |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
233 { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
234 const char *p = line; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
235 uint8_t field_type; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
236 struct pws3_field *field = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
237 size_t len; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
238 size_t i; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
239 unsigned char uuid[32]; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
240 char *text = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
241 struct tm tm; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
242 uint8_t u8; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
243 uint16_t u16; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
244 uint32_t u32; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
245 unsigned char *bytes = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
246 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
247 if (strlen(line) < 3) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
248 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
249 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
250 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
251 if (parse_hex_byte(p, &field_type) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
252 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
253 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
254 p += 2; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
255 field = pws3_field_create(1, field_type); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
256 if (field == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
257 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
258 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
259 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
260 if (*p++ != ':') { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
261 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
262 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
263 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
264 len = strlen(p); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
265 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
266 switch (pws3_field_get_data_type(field)) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
267 case PWS_DATA_TYPE_UUID: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
268 for (i = 0; i < 16; i++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
269 if (parse_hex_byte(p, &uuid[i]) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
270 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
271 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
272 p += 2; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
273 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
274 while (*p == ' ') { p++; } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
275 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
276 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
277 if (*p != '\0') { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
278 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
279 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
280 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
281 if (pws3_field_set_uuid(field, uuid) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
282 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
283 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
284 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
285 case PWS_DATA_TYPE_TEXT: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
286 if (len > PWS3_MAX_FIELD_SIZE) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
287 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
288 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
289 text = malloc(len + 1); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
290 if (text == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
291 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
292 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
293 if (strunvis(text, p) == -1) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
294 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
295 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
296 if (pws3_field_set_text(field, text) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
297 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
298 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
299 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
300 case PWS_DATA_TYPE_TIME: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
301 p = strptime(p, TIME_FORMAT, &tm); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
302 if ((p == NULL) || (*p != '\0')) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
303 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
304 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
305 tm.tm_isdst = -1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
306 pws3_field_set_time(field, mktime(&tm)); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
307 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
308 case PWS_DATA_TYPE_UINT8: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
309 if (len != 2) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
310 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
311 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
312 if (parse_hex_byte(p, &u8) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
313 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
314 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
315 pws3_field_set_uint8(field, u8); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
316 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
317 case PWS_DATA_TYPE_UINT16: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
318 if (len != 4) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
319 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
320 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
321 for (i = 0; i < 2; i++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
322 if (parse_hex_byte(p, &((unsigned char *)&u16)[i]) != |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
323 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
324 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
325 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
326 p += 2; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
327 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
328 pws3_field_set_uint16(field, be16toh(u16)); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
329 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
330 case PWS_DATA_TYPE_UINT32: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
331 if (len != 8) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
332 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
333 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
334 for (i = 0; i < 4; i++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
335 if (parse_hex_byte(p, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
336 &((unsigned char *)&u32)[i]) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
337 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
338 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
339 p += 2; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
340 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
341 pws3_field_set_uint16(field, be32toh(u32)); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
342 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
343 case PWS_DATA_TYPE_BYTES: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
344 bytes = malloc(len / 2); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
345 if (bytes == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
346 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
347 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
348 for (i = 0; (*p != '\0') && (i < PWS3_MAX_FIELD_SIZE); i++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
349 if (parse_hex_byte(p, &bytes[i]) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
350 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
351 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
352 p += 2; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
353 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
354 while (*p == ' ') { p++; } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
355 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
356 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
357 if (*p != '\0') { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
358 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
359 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
360 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
361 if (pws3_field_set_bytes(field, bytes, i) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
362 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
363 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
364 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
365 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
366 free(bytes); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
367 free(text); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
368 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
369 return (field); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
370 err: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
371 free(bytes); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
372 free(text); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
373 pws3_field_destroy(field); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
374 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
375 return (NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
376 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
377 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
378 static struct pws3_field * |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
379 record_field_dump_parse(const char *line) |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
380 { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
381 const char *p = line; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
382 uint8_t field_type = 0xff; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
383 struct pws3_field *field = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
384 size_t len; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
385 size_t i; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
386 unsigned char uuid[32]; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
387 char *text = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
388 struct tm tm; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
389 uint8_t u8; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
390 uint16_t u16; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
391 uint32_t u32; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
392 unsigned char *bytes = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
393 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
394 if (strlen(line) < 3) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
395 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
396 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
397 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
398 if (parse_hex_byte(p, &field_type) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
399 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
400 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
401 p += 2; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
402 field = pws3_field_create(0, field_type); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
403 if (field == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
404 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
405 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
406 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
407 if (*p++ != ':') { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
408 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
409 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
410 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
411 len = strlen(p); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
412 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
413 switch (pws3_field_get_data_type(field)) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
414 case PWS_DATA_TYPE_UUID: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
415 for (i = 0; i < 16; i++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
416 if (parse_hex_byte(p, &uuid[i]) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
417 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
418 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
419 p += 2; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
420 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
421 while (*p == ' ') { p++; } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
422 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
423 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
424 if (*p != '\0') { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
425 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
426 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
427 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
428 if (pws3_field_set_uuid(field, uuid) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
429 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
430 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
431 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
432 case PWS_DATA_TYPE_TEXT: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
433 if (((field_type == PWS3_RECORD_FIELD_PASSWORD) && |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
434 (len > PWS3_MAX_PASSWORD_LEN)) || |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
435 (len > PWS3_MAX_FIELD_SIZE)) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
436 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
437 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
438 if (field_type == PWS3_RECORD_FIELD_PASSWORD) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
439 text = malloc(len + 1); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
440 if (text == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
441 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
442 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
443 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
444 text = malloc(len + 1); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
445 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
446 if (strunvis(text, p) == -1) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
447 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
448 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
449 if (pws3_field_set_text(field, text) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
450 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
451 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
452 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
453 case PWS_DATA_TYPE_TIME: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
454 p = strptime(p, TIME_FORMAT, &tm); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
455 if ((p == NULL) || (*p != '\0')) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
456 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
457 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
458 tm.tm_isdst = -1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
459 pws3_field_set_time(field, mktime(&tm)); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
460 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
461 case PWS_DATA_TYPE_UINT8: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
462 if (len != 2) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
463 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
464 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
465 if (parse_hex_byte(p, &u8) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
466 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
467 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
468 pws3_field_set_uint8(field, u8); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
469 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
470 case PWS_DATA_TYPE_UINT16: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
471 if (len != 4) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
472 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
473 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
474 for (i = 0; i < 2; i++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
475 if (parse_hex_byte(p, &((unsigned char *)&u16)[i]) != |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
476 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
477 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
478 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
479 p += 2; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
480 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
481 pws3_field_set_uint16(field, be16toh(u16)); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
482 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
483 case PWS_DATA_TYPE_UINT32: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
484 if (len != 8) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
485 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
486 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
487 for (i = 0; i < 4; i++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
488 if (parse_hex_byte(p, &((unsigned char *)&u32)[i]) != |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
489 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
490 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
491 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
492 p += 2; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
493 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
494 pws3_field_set_uint16(field, be32toh(u32)); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
495 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
496 case PWS_DATA_TYPE_BYTES: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
497 bytes = malloc(len / 2); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
498 if (bytes == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
499 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
500 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
501 for (i = 0; (*p != '\0') && (i < PWS3_MAX_FIELD_SIZE); i++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
502 if (parse_hex_byte(p, &bytes[i]) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
503 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
504 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
505 p += 2; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
506 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
507 while (*p == ' ') { p++; } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
508 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
509 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
510 if (*p != '\0') { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
511 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
512 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
513 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
514 if (pws3_field_set_bytes(field, bytes, i) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
515 goto err; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
516 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
517 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
518 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
519 free(bytes); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
520 if (field_type == PWS3_RECORD_FIELD_PASSWORD) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
521 free(text); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
522 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
523 free(text); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
524 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
525 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
526 return (field); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
527 err: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
528 free(bytes); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
529 if (field_type == PWS3_RECORD_FIELD_PASSWORD) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
530 free(text); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
531 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
532 free(text); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
533 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
534 pws3_field_destroy(field); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
535 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
536 return (NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
537 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
538 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
539 static int |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
540 dump_read(struct pws3_file *file, FILE *fp) |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
541 { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
542 int retval = -1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
543 ssize_t line_len; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
544 char *line = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
545 size_t line_size = 0; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
546 size_t line_no = 0; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
547 int state = INITIAL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
548 struct pws3_field *header_field = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
549 struct pws3_record *record = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
550 struct pws3_field *record_field = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
551 struct pws3_field *field_uuid; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
552 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
553 errno = 0; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
554 while ((line_len = getline(&line, &line_size, fp)) != -1) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
555 line_no++; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
556 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
557 /* skip empty lines and comments */ |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
558 if (line_len <= 1 || (line[0] == '#')) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
559 continue; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
560 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
561 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
562 /* remove trailing newline */ |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
563 if (line[line_len - 1] == '\n') { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
564 line[line_len - 1] = '\0'; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
565 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
566 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
567 switch (state) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
568 case INITIAL: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
569 if (strcasecmp(line, "HEADER") == 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
570 state = IN_HEADER; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
571 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
572 warnx("syntax error in line %zu", line_no); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
573 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
574 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
575 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
576 case IN_HEADER: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
577 if (strncasecmp(line, "RECORD", |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
578 strlen("RECORD")) == 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
579 state = IN_RECORD; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
580 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
581 header_field = header_field_dump_parse(line); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
582 if (header_field == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
583 warnx("syntax error in line %zu", |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
584 line_no); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
585 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
586 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
587 pws3_file_set_header_field(file, header_field); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
588 header_field = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
589 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
590 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
591 case IN_RECORD: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
592 if (strncasecmp(line, "RECORD", |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
593 strlen("RECORD")) == 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
594 if (record == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
595 warnx("syntax error in line %zu", |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
596 line_no); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
597 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
598 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
599 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
600 /* check for mandatory UUID field */ |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
601 if (((field_uuid = pws3_record_get_field(record, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
602 PWS3_RECORD_FIELD_UUID)) == NULL) || |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
603 (pws3_field_get_uuid(field_uuid) == |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
604 NULL)) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
605 warnx("record ending on line %zu is " |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
606 "missing UUID field", line_no); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
607 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
608 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
609 pws3_file_insert_record(file, record); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
610 record = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
611 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
612 if (record == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
613 record = pws3_record_create(); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
614 if (record == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
615 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
616 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
617 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
618 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
619 record_field = record_field_dump_parse(line); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
620 if (record_field == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
621 warnx("syntax error in line %zu", |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
622 line_no); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
623 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
624 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
625 pws3_record_set_field(record, record_field); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
626 record_field = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
627 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
628 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
629 errno = 0; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
630 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
631 if (errno != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
632 warn("failed to read from input file"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
633 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
634 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
635 if (record != NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
636 /* check for mandatory UUID field */ |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
637 if (((field_uuid = pws3_record_get_field(record, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
638 PWS3_RECORD_FIELD_UUID)) == NULL) || |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
639 (pws3_field_get_uuid(field_uuid) == NULL)) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
640 warnx("record ending on line %zu is missing UUID " |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
641 "field", line_no); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
642 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
643 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
644 pws3_file_insert_record(file, record); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
645 record = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
646 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
647 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
648 retval = 0; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
649 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
650 out: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
651 pws3_field_destroy(header_field); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
652 pws3_field_destroy(record_field); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
653 pws3_record_destroy(record); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
654 free(line); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
655 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
656 return (retval); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
657 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
658 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
659 static int |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
660 dump_write(struct pws3_file *file, FILE *fp) |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
661 { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
662 size_t i; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
663 struct pws3_field *header_field; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
664 struct pws3_record *record; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
665 size_t n; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
666 struct pws3_field *record_field; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
667 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
668 if (fprintf(fp, "# Passwordsafe v3 database dump\nHeader\n") < 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
669 warn("failed to write to output file"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
670 return (-1); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
671 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
672 for (i = 0x00; i < 0xff; i++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
673 header_field = pws3_file_get_header_field(file, i); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
674 if (header_field != NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
675 header_field_dump_write(header_field, fp); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
676 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
677 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
678 while ((i == PWS3_HEADER_FIELD_EMPTY_GROUPS) && |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
679 (header_field != NULL) && |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
680 (header_field = pws3_file_next_empty_group(file, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
681 header_field)) != NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
682 header_field_dump_write(header_field, fp); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
683 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
684 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
685 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
686 for (record = pws3_file_first_record(file), n = 1; record != NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
687 record = pws3_file_next_record(file, record), n++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
688 if (fprintf(fp, "Record %zu\n", n) < 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
689 warn("failed to write to output file"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
690 return (-1); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
691 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
692 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
693 for (i = 0x00; i <= 0xff; i++) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
694 record_field = pws3_record_get_field(record, i); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
695 if (record_field != NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
696 record_field_dump_write(record_field, fp); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
697 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
698 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
699 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
700 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
701 return (0); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
702 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
703 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
704 void |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
705 usage(void) |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
706 { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
707 fprintf(stderr, "usage: pwsdump [-f pws3 | dump] [-o filename] " |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
708 "[-p password_file] [-P password_fd] [-t pws3 | dump] " |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
709 "input_file\n" |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
710 " pwsdump -f dump [-o filename] [-t pws3 | dump]\n"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
711 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
712 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
713 int |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
714 main(int argc, char *argv[]) |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
715 { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
716 int status = EXIT_FAILURE; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
717 int c; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
718 int errflag = 0; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
719 int from_format = FORMAT_PWS3; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
720 int to_format = FORMAT_DUMP; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
721 const char *in_filename = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
722 const char *out_filename = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
723 const char *new_password_filename = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
724 const char *password_filename = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
725 int fd_value = -1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
726 char *p; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
727 int fd_new_password = -1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
728 FILE * fp_new_password = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
729 int fd_password = -1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
730 FILE * fp_password = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
731 struct pws3_file *file = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
732 FILE *fp_in = stdin; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
733 char *password = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
734 ssize_t password_len; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
735 size_t password_size = PWS3_MAX_PASSWORD_LEN + 1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
736 char *new_password = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
737 ssize_t new_password_len; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
738 size_t new_password_size = PWS3_MAX_PASSWORD_LEN + 1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
739 char *confirm_password = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
740 FILE *fp_out = stdout; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
741 struct stat statbuf_in; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
742 struct stat statbuf_out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
743 int need_tmpfile = 0; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
744 int fd_tmp = -1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
745 char *out_filename_tmp = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
746 char *out_dir = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
747 char *tmp_filename = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
748 int len; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
749 mode_t old_mode; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
750 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
751 setprogname(argv[0]); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
752 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
753 if (pws_init() != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
754 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
755 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
756 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
757 /* timestamps are processed as UTC */ |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
758 if (setenv("TZ", "", 1) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
759 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
760 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
761 tzset(); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
762 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
763 while (!errflag && ((c = getopt(argc, argv, "f:n:N:o:p:P:t:")) != -1)) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
764 switch (c) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
765 case 'f': |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
766 if (strcmp(optarg, "pws3") == 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
767 from_format = FORMAT_PWS3; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
768 } else if (strcmp(optarg, "dump") == 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
769 from_format = FORMAT_DUMP; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
770 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
771 errflag = 1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
772 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
773 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
774 case 'n': |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
775 fd_new_password = -1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
776 new_password_filename = optarg; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
777 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
778 case 'N': |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
779 new_password_filename = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
780 errno = 0; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
781 fd_value = strtol(optarg, &p, 10); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
782 if ((errno == 0) && (p != optarg) && (*p == '\0') && |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
783 (fd_value >= 0) && (fd_value < INT_MAX)) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
784 fd_new_password = (int)fd_value; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
785 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
786 errflag = 1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
787 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
788 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
789 case 'o': |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
790 out_filename = optarg; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
791 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
792 case 'p': |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
793 fd_password = -1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
794 password_filename = optarg; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
795 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
796 case 'P': |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
797 password_filename = NULL; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
798 errno = 0; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
799 fd_value = strtol(optarg, &p, 10); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
800 if ((errno == 0) && (p != optarg) && (*p == '\0') && |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
801 (fd_value >= 0) && |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
802 (fd_value < INT_MAX)) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
803 fd_password = (int)fd_value; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
804 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
805 errflag = 1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
806 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
807 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
808 case 't': |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
809 if (strcmp(optarg, "pws3") == 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
810 to_format = FORMAT_PWS3; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
811 } else if (strcmp(optarg, "dump") == 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
812 to_format = FORMAT_DUMP; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
813 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
814 errflag = 1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
815 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
816 break; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
817 default: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
818 errflag = 1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
819 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
820 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
821 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
822 if (errflag || ((from_format == FORMAT_PWS3) && (argc != optind + 1)) || |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
823 (argc > optind + 1)) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
824 usage(); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
825 status = EXIT_USAGE; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
826 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
827 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
828 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
829 if (optind == argc - 1) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
830 in_filename = argv[optind]; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
831 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
832 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
833 if (fd_password != -1) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
834 fp_password = fdopen(fd_password, "r"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
835 if (fp_password == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
836 warn("invalid password fd %d", fd_password); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
837 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
838 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
839 fd_password = -1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
840 } else if (password_filename != NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
841 fp_password = fopen(password_filename, "r"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
842 if (fp_password == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
843 warn("could not open password file"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
844 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
845 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
846 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
847 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
848 if (fd_new_password != -1) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
849 fp_new_password = fdopen(fd_new_password, "r"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
850 if (fp_new_password == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
851 warn("invalid password fd %d", fd_new_password); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
852 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
853 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
854 fd_new_password = -1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
855 } else if (new_password_filename != NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
856 fp_new_password = fopen(new_password_filename, "r"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
857 if (fp_new_password == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
858 warn("could not open password file"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
859 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
860 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
861 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
862 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
863 if (in_filename != NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
864 fp_in = fopen(in_filename, "r"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
865 if (fp_in == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
866 warn("could not open input file"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
867 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
868 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
869 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
870 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
871 if (out_filename != NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
872 if (in_filename != NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
873 if (fstat(fileno(fp_in), &statbuf_in) == -1) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
874 warn("could not stat input file"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
875 status = EXIT_FAILURE; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
876 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
877 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
878 if (stat(out_filename, &statbuf_out) == -1) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
879 if (errno != ENOENT) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
880 warn("could not stat output file"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
881 status = 1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
882 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
883 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
884 } else if ((statbuf_in.st_ino == statbuf_out.st_ino) && |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
885 (statbuf_in.st_dev == statbuf_out.st_dev)) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
886 need_tmpfile = 1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
887 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
888 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
889 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
890 if (need_tmpfile) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
891 out_filename_tmp = strdup(out_filename); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
892 if (out_filename_tmp == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
893 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
894 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
895 out_dir = dirname(out_filename_tmp); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
896 len = snprintf(NULL, 0, "%s/pwsdumpXXXXXX", out_dir); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
897 if (len < 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
898 warn(NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
899 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
900 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
901 tmp_filename = malloc((size_t)len + 1); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
902 if (tmp_filename == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
903 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
904 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
905 if (snprintf(tmp_filename, (size_t)len + 1, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
906 "%s/pwsdumpXXXXXX", out_dir) != len) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
907 warn(NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
908 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
909 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
910 old_mode = umask(077); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
911 fd_tmp = mkstemp(tmp_filename); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
912 umask(old_mode); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
913 if (fd_tmp == -1) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
914 warn("could not create temporary file"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
915 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
916 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
917 fp_out = fdopen(fd_tmp, "w"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
918 if (fp_out == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
919 warn("could not open temporary file"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
920 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
921 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
922 fd_tmp = -1; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
923 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
924 old_mode = umask(077); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
925 fp_out = fopen(out_filename, "w"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
926 umask(old_mode); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
927 if (fp_out == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
928 warn("could not open output file"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
929 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
930 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
931 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
932 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
933 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
934 file = pws3_file_create(); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
935 if (file == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
936 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
937 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
938 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
939 if (from_format == FORMAT_PWS3) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
940 password = malloc(password_size); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
941 if (password == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
942 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
943 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
944 if (fp_password != NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
945 errno = 0; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
946 if (getline(&password, &password_size, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
947 fp_password) == -1) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
948 if (errno != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
949 warn("failed to read password"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
950 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
951 warnx("failed to read password"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
952 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
953 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
954 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
955 password_len = strlen(password); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
956 /* strip trailing newline */ |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
957 if ((password_len > 0) && |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
958 (password[password_len - 1] == '\n')) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
959 password[password_len - 1] = '\0'; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
960 password_len--; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
961 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
962 if (password_len == 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
963 warnx("invalid password"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
964 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
965 } else if (password_len > PWS3_MAX_PASSWORD_LEN) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
966 warnx("password too long"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
967 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
968 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
969 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
970 if (readpassphrase("Enter password: ", password, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
971 password_size, RPP_ECHO_OFF | |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
972 RPP_REQUIRE_TTY) == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
973 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
974 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
975 password_len = strlen(password); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
976 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
977 if (password_len == 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
978 warnx("invalid password"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
979 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
980 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
981 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
982 if (pws3_file_read_stream(file, password, fp_in) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
983 warnx("%s", pws3_file_get_error_message(file)); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
984 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
985 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
986 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
987 if (dump_read(file, fp_in) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
988 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
989 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
990 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
991 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
992 if (to_format == FORMAT_PWS3) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
993 new_password = malloc(new_password_size); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
994 if (new_password == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
995 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
996 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
997 if (fp_new_password != NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
998 errno = 0; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
999 if (getline(&new_password, &new_password_size, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1000 fp_new_password) == -1) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1001 if (errno != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1002 warn("failed to read password"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1003 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1004 warnx("failed to read password"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1005 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1006 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1007 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1008 new_password_len = strlen(new_password); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1009 /* strip trailing newline */ |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1010 if ((new_password_len > 0) && |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1011 (new_password[new_password_len - 1] == '\n')) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1012 new_password[new_password_len - 1] = '\0'; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1013 new_password_len--; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1014 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1015 if (new_password_len == 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1016 warnx("invalid password"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1017 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1018 } else if (new_password_len > PWS3_MAX_PASSWORD_LEN) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1019 warnx("password too long"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1020 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1021 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1022 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1023 if (readpassphrase("Enter new password: ", new_password, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1024 PWS3_MAX_PASSWORD_LEN + 1, RPP_ECHO_OFF | |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1025 RPP_REQUIRE_TTY) == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1026 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1027 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1028 if (strlen(new_password) == 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1029 warnx("invalid password"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1030 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1031 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1032 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1033 confirm_password = malloc(PWS3_MAX_PASSWORD_LEN + 1); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1034 if (confirm_password == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1035 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1036 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1037 if (readpassphrase("Confirm new password: ", |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1038 confirm_password, PWS3_MAX_PASSWORD_LEN + 1, |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1039 RPP_ECHO_OFF | RPP_REQUIRE_TTY) == NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1040 err(1, NULL); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1041 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1042 if (strcmp(new_password, confirm_password) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1043 warnx("password mismatch"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1044 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1045 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1046 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1047 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1048 if (pws3_file_write_stream(file, new_password, 10000, fp_out) != |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1049 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1050 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1051 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1052 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1053 if (dump_write(file, fp_out) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1054 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1055 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1056 if (fflush(fp_out) != 0) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1057 warn("failed to flush output file"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1058 goto out; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1059 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1060 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1061 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1062 status = EXIT_SUCCESS; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1063 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1064 out: |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1065 if (fd_new_password != -1) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1066 close(fd_new_password); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1067 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1068 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1069 if (fp_new_password != NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1070 fclose(fp_new_password); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1071 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1072 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1073 if (fd_password != -1) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1074 close(fd_password); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1075 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1076 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1077 if (fp_password != NULL) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1078 fclose(fp_password); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1079 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1080 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1081 if ((fp_in != NULL) && (fp_in != stdin)) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1082 fclose(fp_in); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1083 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1084 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1085 if (fd_tmp != -1) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1086 close(fd_tmp); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1087 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1088 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1089 if ((fp_out != NULL) && (fp_out != stdout)) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1090 fclose(fp_out); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1091 if (status == EXIT_SUCCESS) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1092 if (need_tmpfile) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1093 if (rename(tmp_filename, out_filename) == -1) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1094 warn("could not create output file"); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1095 status = EXIT_FAILURE; |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1096 unlink(tmp_filename); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1097 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1098 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1099 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1100 if (need_tmpfile) { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1101 unlink(tmp_filename); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1102 } else { |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1103 unlink(out_filename); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1104 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1105 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1106 } |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1107 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1108 pws3_file_destroy(file); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1109 free(out_filename_tmp); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1110 free(tmp_filename); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1111 free(confirm_password); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1112 free(new_password); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1113 free(password); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1114 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1115 pws_finalize(); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1116 |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1117 exit(status); |
97097b4b6bfb
Add pwsdump utility
Guido Berhoerster <guido+libpws@berhoerster.name>
parents:
diff
changeset
|
1118 } |