Minor refactoring of XML import code
[moonshot-ui.git] / src / moonshot-webp-parser.vala
1 /*
2  * Copyright (c) 2011-2014, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31 */
32 using Moonshot;
33
34 namespace WebProvisioning
35
36     private MoonshotLogger logger;
37
38     public static int main(string[] args)
39     {
40         logger = new MoonshotLogger("WebProvisioning");
41
42         int arg_index = -1;
43         int force_flat_file_store = 0;
44         bool bad_switch = false;
45         for (arg_index = 1; arg_index < args.length; arg_index++) {
46             string arg = args[arg_index];
47             unichar c = arg.get_char();
48             if (c == '-') {
49                 arg = arg.next_char();
50                 c = arg.get_char();
51                 switch (c) {
52                 case 'f':
53                     force_flat_file_store = 1;
54                     break;
55                 default:
56                     bad_switch = true;
57                     break;
58                 }
59             } else {
60                 break; // arg is not a switch; presume it's the file
61             }
62         }
63         if (bad_switch || (arg_index != args.length - 1))
64         {
65             stdout.printf(_("Usage %s [-f] WEB_PROVISIONING_FILE\n -f: add identities to flat file store.\n"), args[0]);
66             return -1;
67         }
68         string webp_file = args[arg_index];
69     
70         if (!FileUtils.test(webp_file, FileTest.EXISTS | FileTest.IS_REGULAR))
71         {
72             stdout.printf(_("%s does not exist\n"), webp_file);
73             return -1;
74         }
75     
76         var webp = new Parser(webp_file);
77         webp.parse();
78         logger.trace(@"Have $(webp.cards.length) IdCards");
79         foreach (IdCard card in webp.cards)
80         {
81
82             if (card == null) {
83                 logger.trace(@"Skipping null IdCard");
84                 continue;
85             }
86
87             Moonshot.Error error;
88             string[] rules_patterns = {};
89             string[] rules_always_confirm = {};
90         
91             /* use temp arrays to workaround centos array property bug */
92             var rules = card.rules;
93             var services = card.services;
94             if (rules.length > 0)
95             {
96                 int i = 0;
97                 rules_patterns = new string[rules.length];
98                 rules_always_confirm = new string[rules.length];
99                 foreach (Rule r in rules)
100                 {
101                     rules_patterns[i] = r.pattern;
102                     rules_always_confirm[i] = r.always_confirm;
103                     i++;
104                 }
105             }
106
107             logger.trace(@"Installing IdCard named '$(card.display_name)'");
108             Moonshot.install_id_card(card.display_name,
109                                      card.username,
110                                      card.password,
111                                      card.issuer,
112                                      rules_patterns,
113                                      rules_always_confirm,
114                                      services,
115                                      card.trust_anchor.ca_cert,
116                                      card.trust_anchor.subject,
117                                      card.trust_anchor.subject_alt,
118                                      card.trust_anchor.server_cert,
119                                      force_flat_file_store,
120                                      out error);
121
122             if (error != null)
123             {
124                 stderr.printf("Error: %s", error.message);
125                 continue;
126             }
127         }
128     
129         return 0;
130     }
131 }