lib/mscat: fix logging in wscript
[samba.git] / lib / mscat / dumpmscat.c
1 /*
2  * Copyright (c) 2016      Andreas Schneider <asn@samba.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <errno.h>
19 #include <stdbool.h>
20 #include <stdarg.h>
21 #include <stdint.h>
22 #include <stdio.h>
23
24 #include <talloc.h>
25
26 #include <libtasn1.h>
27 #include <gnutls/pkcs7.h>
28
29 #include "mscat.h"
30
31 static const char *mac_to_string(enum mscat_mac_algorithm algo) {
32         switch(algo) {
33                 case MSCAT_MAC_NULL:
34                         return "NULL";
35                 case MSCAT_MAC_MD5:
36                         return "MD5";
37                 case MSCAT_MAC_SHA1:
38                         return "SHA1";
39                 case MSCAT_MAC_SHA256:
40                         return "SHA256";
41                 case MSCAT_MAC_SHA512:
42                         return "SHA512";
43                 case MSCAT_MAC_UNKNOWN:
44                         return "UNKNOWN";
45         }
46
47         return "UNKNOWN";
48 }
49
50 int main(int argc, char *argv[]) {
51         TALLOC_CTX *mem_ctx;
52         const char *filename = NULL;
53         const char *ca_file = NULL;
54         struct mscat_pkcs7 *cat_pkcs7;
55         struct mscat_ctl *msctl;
56         unsigned int member_count = 0;
57         unsigned int attribute_count = 0;
58         unsigned int i;
59         int rc;
60
61         if (argc < 1) {
62                 return -1;
63         }
64         filename = argv[1];
65
66         if (filename == NULL || filename[0] == '\0') {
67                 return -1;
68         }
69
70         mem_ctx = talloc_init("dumpmscat");
71         if (mem_ctx == NULL) {
72                 fprintf(stderr, "Failed to initialize talloc\n");
73                 exit(1);
74         }
75
76         /* READ MS ROOT CERTIFICATE */
77
78         cat_pkcs7 = mscat_pkcs7_init(mem_ctx);
79         if (cat_pkcs7 == NULL) {
80                 exit(1);
81         }
82
83         rc = mscat_pkcs7_import_catfile(cat_pkcs7,
84                                         filename);
85         if (rc != 0) {
86                 exit(1);
87         }
88
89         if (argc >= 2) {
90                 ca_file = argv[2];
91         }
92
93         rc = mscat_pkcs7_verify(cat_pkcs7, ca_file);
94         if (rc != 0) {
95                 printf("FAILED TO VERIFY CATALOG FILE!\n");
96                 exit(1);
97         }
98         printf("CATALOG FILE VERIFIED!\n\n");
99
100         msctl = mscat_ctl_init(mem_ctx);
101         if (msctl == NULL) {
102                 exit(1);
103         }
104
105         rc = mscat_ctl_import(msctl, cat_pkcs7);
106         if (rc != 0) {
107                 exit(1);
108         }
109
110         member_count = mscat_ctl_get_member_count(msctl);
111         printf("CATALOG MEMBER COUNT=%d\n", member_count);
112
113         for (i = 0; i < member_count; i++) {
114                 struct mscat_ctl_member *m;
115                 size_t j;
116
117                 rc = mscat_ctl_get_member(msctl,
118                                           mem_ctx,
119                                           i + 1,
120                                           &m);
121                 if (rc != 0) {
122                         exit(1);
123                 }
124
125                 printf("CATALOG MEMBER\n");
126                 if (m->checksum.type == MSCAT_CHECKSUM_STRING) {
127                         printf("  CHECKSUM: %s\n", m->checksum.string);
128                 } else if (m->checksum.type == MSCAT_CHECKSUM_BLOB) {
129                         printf("  CHECKSUM: ");
130                         for (j = 0; j < m->checksum.size; j++) {
131                                 printf("%X", m->checksum.blob[j]);
132                         }
133                         printf("\n");
134                 }
135                 printf("\n");
136
137                 if (m->file.name != NULL) {
138                         printf("  FILE: %s, FLAGS=0x%08x\n",
139                                m->file.name,
140                                m->file.flags);
141                 }
142
143                 if (m->info.guid != NULL) {
144                         printf("  GUID: %s, ID=0x%08x\n",
145                                m->info.guid,
146                                m->info.id);
147                 }
148
149                 if (m->osattr.value != NULL) {
150                         printf("  OSATTR: %s, FLAGS=0x%08x\n",
151                                m->osattr.value,
152                                m->osattr.flags);
153                 }
154
155                 if (m->mac.type != MSCAT_MAC_UNKNOWN) {
156                         printf("  MAC: %s, DIGEST: ",
157                                mac_to_string(m->mac.type));
158                         for (j = 0; j < m->mac.digest_size; j++) {
159                                 printf("%X", m->mac.digest[j]);
160                         }
161                         printf("\n");
162                 }
163                 printf("\n");
164         }
165         printf("\n");
166
167         attribute_count = mscat_ctl_get_attribute_count(msctl);
168         printf("CATALOG ATTRIBUTE COUNT=%d\n", attribute_count);
169
170         for (i = 0; i < attribute_count; i++) {
171                 struct mscat_ctl_attribute *a;
172
173                 rc = mscat_ctl_get_attribute(msctl,
174                                              mem_ctx,
175                                              i + 1,
176                                              &a);
177                 if (rc != 0) {
178                         exit(1);
179                 }
180
181                 printf("  NAME=%s, FLAGS=0x%08x, VALUE=%s\n",
182                        a->name,
183                        a->flags,
184                        a->value);
185         }
186         talloc_free(mem_ctx);
187         return 0;
188 }