Adding signature checking
[platform/core/system/tizen-platform-wrapper.git] / src / tzplatform_get.c
1 /*
2  * Copyright (C) 2013-2014 Intel Corporation.
3  * 
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors:
19  *   José Bollo <jose.bollo@open.eurogiciel.org>
20  *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
21  *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
22  *
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <pwd.h>
30
31 #include <tzplatform_config.h>
32
33 #define basename(x) (x)
34
35 static const char usage [] = "type '%s --help' to get help.\n";
36
37 static const char help [] = "\
38 \n\
39 usage: %s [options...] [keys...]\n\
40 \n\
41 options:\n\
42 \n\
43 -a --all        all keys (must not be followed by keys)\n\
44 -n --not        not in the keys\n\
45 -e --export     prefix with export\n\
46 -l --list       prints only the key names, not the values\n\
47 -s --space      separate with spaces instead of new-lines\n\
48 -q --query      silently check that given variables are existing\n\
49 -c --continue   continue to process if error\n\
50 -u --user  id   set the user using its 'id' (name or numeric)\n\
51 \n\
52 ";
53
54 int main(int argc, char **argv)
55 {
56         char *progname = *argv++, *user = 0;
57         int all = 0, not = 0, query = 0, export = 0, space = 0, list = 0, cont = 0;
58         int i, n, *sel, p;
59         enum tzplatform_variable id;
60         struct passwd *spw;
61
62         /* parse args */
63         while(*argv && **argv=='-') {
64                 char c, *opt = 1+*argv++;
65                 while(opt) {
66                         if (*opt == '-') {
67                                 char *x = 0;
68                                 c = *++opt;
69                                 switch(c) {
70                                         case 'a': x = "all"; break;
71                                         case 'n': x = "not"; break;
72                                         case 'q': x = "query"; break;
73                                         case 'e': x = "export"; break;
74                                         case 's': x = "space"; break;
75                                         case 'l': x = "list"; break;
76                                         case 'c': x = "continue"; break;
77                                         case 'h': x = "help"; break;
78                                         case 'u': x = "user"; break;
79                                 }
80                                 if (!x || strcmp(x,opt))
81                                         c = 0;
82                                 opt = 0;
83                         }
84                         else {
85                                 c = *opt;
86                                 if (!*++opt)
87                                         opt = 0;
88                         }
89                         switch(c) {
90                         case 'a': all = 1; break;
91                         case 'n': not = 1; break;
92                         case 'q': query = 1; break;
93                         case 'e': export = 1; break;
94                         case 's': space = 1; break;
95                         case 'l': list = 1; break;
96                         case 'c': cont = 1; break;
97                         case 'u': user = *argv; if (user) argv++; break;
98                         case 'h':
99                                 fprintf( stdout, help, basename(progname));
100                                 return 0;
101                         default:
102                                 fprintf( stderr, usage, basename(progname));
103                                 return 1;
104                         }
105                 }
106         }
107
108         /* some checks */
109         if (query) {
110                 if (all) {
111                         fprintf( stderr, 
112                                 "error! --all and --query aren't compatibles.\n");
113                         return 1;
114                 }
115                 if (not) {
116                         fprintf( stderr, 
117                                 "error! --not and --query aren't compatibles.\n");
118                         return 1;
119                 }
120                 if (list) {
121                         fprintf( stderr, 
122                                 "error! --list and --query aren't compatibles.\n");
123                         return 1;
124                 }
125                 if (cont) {
126                         fprintf( stderr, 
127                                 "error! --continue and --query aren't compatibles.\n");
128                         return 1;
129                 }
130                 if (space) {
131                         fprintf( stderr, 
132                                 "warning! --space option ignored for queries.\n");
133                 }
134                 if (export) {
135                         fprintf( stderr, 
136                                 "warning! --export option ignored for queries.\n");
137                 }
138         }
139         if (all) {
140                 if (*argv) {
141                         fprintf( stderr, 
142                                 "error! --all doesn't accept any key.\n");
143                         return 1;
144                 }
145                 /* all is like not nothing!! */
146                 not = 1;
147         }
148
149         /* process */
150         n = tzplatform_getcount();
151         sel = calloc( sizeof(int), n);
152         if (sel == NULL) {
153                 fprintf( stderr, "error! out of memory!\n");
154                 return 1;
155         }
156
157         /* get the variables from the list */
158         while (*argv) {
159                 id = tzplatform_getid( *argv);
160                 if (id == _TZPLATFORM_VARIABLES_INVALID_) {
161                         if (query)
162                                 return 1;
163                         fprintf( stderr, "error! %s isn't a variable.\n", *argv);
164                         if (!cont)
165                                 return 1;
166                 }
167                 else {
168                         sel[(int)id] = 1;
169                 }
170                 argv++;
171         }
172
173         /* finished for queries */
174         if (query) 
175                 return 0;
176
177         /* set the user */
178         if (user) {
179                 for (i=0 ; '0' <= user[i] && user[i] <= '9' ; i++);
180                 if (user[i])
181                         spw = getpwnam(user);
182                 else
183                         spw = getpwuid((uid_t)atoi(user));
184                 if (!spw) {
185                         fprintf( stderr, "error! %s isn't standing for a valid user.\n", user);
186                         if (!cont)
187                                 return 1;
188                 } else {
189                         i = tzplatform_set_user(spw->pw_uid);
190                         if (i) {
191                                 fprintf( stderr, "error! can't set the valid user %s.\n", user);
192                                 if (!cont)
193                                         return 1;
194                         }
195                 }
196         }
197
198         /* emits the result */
199         for (p = i = 0 ; i < n ; i++) {
200                 if (sel[i] != not) {
201                         if (p++) 
202                                 printf( space ? " " : export ? "\nexport " : "\n");
203                         else if (export)
204                                 printf( "export ");
205                         id = (enum tzplatform_variable) i;
206                         printf( "%s", tzplatform_getname(id));
207                         if (!list)      
208                                 printf( "=%s", tzplatform_getenv(id));
209                 }
210         }
211         if (p)
212                 printf("\n");
213         return 0;
214 }
215