1fce688475abe753b24cb242b68ee9119ded9cfe
[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
37
38   public static int main (string[] args)
39   {
40     int arg_index = -1;
41     int force_flat_file_store = 0;
42     bool bad_switch = false;
43     for (arg_index = 1; arg_index < args.length; arg_index++) {
44       string arg = args[arg_index];
45       unichar c = arg.get_char();
46       if (c=='-') {
47           arg = arg.next_char();
48           c = arg.get_char();
49           switch (c) {
50             case 'f':
51               force_flat_file_store = 1;
52               break;
53             default:
54               bad_switch = true;
55               break;
56           }
57       } else {
58         break; // arg is not a switch; presume it's the file
59       }
60     }
61     if (bad_switch || (arg_index != args.length - 1))
62     {
63       stdout.printf (_("Usage %s [-f] WEB_PROVISIONING_FILE\n -f: add identities to flat file store.\n"), args[0]);
64       return -1;
65     }
66     string webp_file = args[arg_index];
67     
68     if (!FileUtils.test (webp_file, FileTest.EXISTS | FileTest.IS_REGULAR))
69     {
70       stdout.printf (_("%s does not exist\n"), webp_file);
71       return -1;
72     }
73     
74     var webp = new Parser (webp_file);
75     webp.parse();
76     
77     foreach (IdCard card in cards)
78     {
79       Moonshot.Error error;
80       string[] rules_patterns = {};
81       string[] rules_always_confirm = {};
82         
83       /* use temp arrays to workaround centos array property bug */
84       var rules = card.rules;
85       var services = card.services;
86       if (rules.length > 0)
87       {
88         int i = 0;
89         rules_patterns = new string[rules.length];
90         rules_always_confirm = new string[rules.length];
91         foreach (Rule r in rules)
92         {
93           rules_patterns[i] = r.pattern;
94           rules_always_confirm[i] = r.always_confirm;
95           i++;
96         }
97       }
98
99       Moonshot.install_id_card (card.display_name,
100                                 card.username,
101                                 card.password,
102                                 card.issuer,
103                                 rules_patterns,
104                                 rules_always_confirm,
105                                 services,
106                                 card.trust_anchor.ca_cert,
107                                 card.trust_anchor.subject,
108                                 card.trust_anchor.subject_alt,
109                                 card.trust_anchor.server_cert,
110                                 force_flat_file_store,
111                                 out error);
112
113       if (error != null)
114       {
115         stderr.printf ("Error: %s", error.message);
116         continue;
117       }
118     }
119     
120     return 0;
121   }
122 }