Refactored the IdCard services list to fix new bugs and (hopefully) prevent even...
[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             string[] svcs = new string[card.services.size];
94             for (int i = 0; i < card.services.size; i++) {
95                 svcs[i] = card.services[i];
96             }
97
98             if (rules.length > 0)
99             {
100                 int i = 0;
101                 rules_patterns = new string[rules.length];
102                 rules_always_confirm = new string[rules.length];
103                 foreach (Rule r in rules)
104                 {
105                     rules_patterns[i] = r.pattern;
106                     rules_always_confirm[i] = r.always_confirm;
107                     i++;
108                 }
109             }
110
111             logger.trace(@"Installing IdCard named '$(card.display_name)'");
112             Moonshot.install_id_card(card.display_name,
113                                      card.username,
114                                      card.password,
115                                      card.issuer,
116                                      rules_patterns,
117                                      rules_always_confirm,
118                                      svcs,
119                                      card.trust_anchor.ca_cert,
120                                      card.trust_anchor.subject,
121                                      card.trust_anchor.subject_alt,
122                                      card.trust_anchor.server_cert,
123                                      force_flat_file_store,
124                                      out error);
125
126             if (error != null)
127             {
128                 stderr.printf("Error: %s", error.message);
129                 continue;
130             }
131         }
132     
133         return 0;
134     }
135 }