8f250ff3a5f89534a3f79d21712d447da3fbc92a
[ossec-hids.git] / src / addagent / manage_keys.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All rights reserved.
5  *
6  * This program is a free software; you can redistribute it
7  * and/or modify it under the terms of the GNU General Public
8  * License (version 2) as published by the FSF - Free Software
9  * Foundation.
10  *
11  * License details at the LICENSE file included with OSSEC or 
12  * online at: http://www.ossec.net/en/licensing.html
13  */
14
15
16 #include "manage_agents.h"
17
18
19 /* b64 function prototypes */
20 char *decode_base64(const char *src);
21 char *encode_base64(int size, char *src);
22
23
24 /* Import a key */
25 int k_import(char *cmdimport)
26 {
27     FILE *fp;
28     char *user_input;
29     char *b64_dec;
30    
31     char *name; char *ip; char *tmp_key;
32      
33     char line_read[FILE_SIZE +1];
34     
35
36     /* Parsing user argument. */
37     if(cmdimport)
38     {
39         user_input = cmdimport;
40     }
41     else
42     {
43         printf(IMPORT_KEY);
44
45         user_input = read_from_user();
46     }
47
48
49     /* quit */
50     if(strcmp(user_input, QUIT) == 0)
51         return(0);
52     
53     b64_dec = decode_base64(user_input);
54     if(b64_dec == NULL)
55     {
56         printf(NO_KEY);
57         printf(PRESS_ENTER);
58         read_from_user();
59         return(0);
60     }
61
62     
63     memset(line_read, '\0', FILE_SIZE +1);
64     strncpy(line_read, b64_dec, FILE_SIZE);
65
66
67     name = strchr(b64_dec, ' ');
68     if(name && strlen(line_read) < FILE_SIZE)
69     {
70         *name = '\0';
71         name++;
72         ip = strchr(name, ' ');
73         if(ip)
74         {
75             *ip = '\0';
76             ip++;
77
78             tmp_key = strchr(ip, ' ');
79             if(!tmp_key)
80             {
81                 printf(NO_KEY);
82                 return(0);
83             }
84             *tmp_key = '\0';
85         
86             printf("\n");   
87             printf(AGENT_INFO, b64_dec, name, ip);
88             
89             while(1)
90             {
91                 printf(ADD_CONFIRM);
92                 fflush(stdout);
93
94                 user_input = read_from_user();
95
96                 if(user_input[0] == 'y' || user_input[0] == 'Y')
97                 {
98                     fp = fopen(KEYS_FILE,"w");
99                     if(!fp)
100                     {
101                         ErrorExit(FOPEN_ERROR, ARGV0, KEYS_FILE);
102                     }
103                     fprintf(fp,"%s\n",line_read);
104                     fclose(fp);
105                     #ifndef WIN32
106                     chmod(KEYS_FILE, 0440);
107                     #endif
108
109                     /* Removing sender counter. */
110                     OS_RemoveCounter("sender");
111                             
112                     printf(ADDED);
113                     printf(PRESS_ENTER);
114                     read_from_user();
115                     restart_necessary = 1;
116                     return(1);
117                 }
118                 else if(user_input[0] == 'n' || user_input[0] == 'N')
119                 {
120                     printf("%s", ADD_NOT);
121                     return(0);
122                 }
123             }
124         }
125     }
126     
127     printf(NO_KEY);
128     printf(PRESS_ENTER);
129     read_from_user();
130     return(0);
131
132 }
133
134
135 /* extract base64 for a specific agent */
136 int k_extract(char *cmdextract)
137 {
138     FILE *fp;
139     char *user_input;
140     char *b64_enc;
141     char line_read[FILE_SIZE +1];
142     char n_id[USER_SIZE +1];
143
144
145     if(cmdextract)
146     {
147         user_input = cmdextract;
148
149         if(!IDExist(user_input))
150         {
151             printf(NO_ID, user_input);
152             exit(1);
153         }
154     }
155
156     else
157     {
158         if(!print_agents(0, 0, 0))
159         {
160             printf(NO_AGENT);
161             printf(PRESS_ENTER);
162             read_from_user();
163             return(0);
164         }
165
166         do
167         {
168             printf(EXTRACT_KEY);
169             fflush(stdout);
170             user_input = read_from_user();
171
172             /* quit */
173             if(strcmp(user_input, QUIT) == 0)
174                 return(0);
175
176             if(!IDExist(user_input))
177                 printf(NO_ID, user_input);
178
179         } while(!IDExist(user_input));
180     }
181
182     
183     /* Trying to open the auth file */
184     fp = fopen(AUTH_FILE, "r");
185     if(!fp)
186     {
187         ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE);
188     }
189     
190     fsetpos(fp, &fp_pos);
191
192     memset(n_id, '\0', USER_SIZE +1);
193     strncpy(n_id, user_input, USER_SIZE -1);
194     
195     
196     if(fgets(line_read, FILE_SIZE, fp) == NULL)
197     {
198         printf(ERROR_KEYS);
199         fclose(fp);
200         exit(1);
201     }
202     chomp(line_read);
203
204     
205     b64_enc = encode_base64(strlen(line_read),line_read);
206     if(b64_enc == NULL)
207     {
208         printf(EXTRACT_ERROR);
209         fclose(fp);
210         exit(1);
211     }
212
213     printf(EXTRACT_MSG, n_id, b64_enc);
214     if(!cmdextract)
215     {
216         printf("\n" PRESS_ENTER);
217         read_from_user();
218     }
219
220     free(b64_enc);
221     fclose(fp);
222
223     return(0);
224 }
225
226
227 /* EOF */