]> git.decadent.org.uk Git - ap-utils.git/blob - lib/file.c
Imported Upstream version 1.5~pre1
[ap-utils.git] / lib / file.c
1 /*
2  *      file.c from Access Point SNMP Utils for Linux
3  * file accessing functions
4  *
5  * Copyright (c) 2002 Roman Festchook <roma at polesye dot net>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License Version 2 from
9  * June 1991 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 #include <sys/wait.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include "ap-utils.h"
29
30 extern WINDOW *main_sub, *win_for_help, *main_win;
31 extern char *ap_types[], *ap_vendorexts[][3];
32 extern short ap_type, ap_vendorext;
33 extern struct in_addr ap_ip;
34 extern int atmel410_filter;
35 #define MAX_LINES LINES-6
36
37 struct APList {
38     char *ip;
39     char *passwd;
40     int type;
41     int vendorext;
42     char *label;
43
44     struct APList * next;
45 };
46
47 void
48 _scroll_rows(struct APList *first, int begin, int end)
49 {
50     int i = 1;
51     struct APList *curr = first;
52     char message[80];
53
54     clear_main(3);
55
56     while (i++ < begin)
57         curr = curr->next;
58
59     i = 0;
60     while (end-- > begin) {
61         sprintf(message, "  %3u  %-15s  %-10s  %-8s  %-16s", begin + i,
62             curr->ip, ap_types[curr->type],
63             ap_vendorexts[curr->type][curr->vendorext], curr->label);
64         mvwaddstr(main_sub, 2 + i, 0, message);
65         i++;
66         curr = curr->next;
67     }
68     wrefresh(main_sub);
69 }
70
71 struct APList *parse_db_str(char *str)
72 {
73     struct APList *curr=NULL;
74     char *ip=NULL, *passwd=NULL, *label=NULL, *aptype=NULL, mess[1024];
75     int i=0, pos=0, j=0;
76
77     while(str[i] != '\0') {
78         if(str[i] == ':') {
79             switch (pos) {
80                 case 0: 
81                     ip = (char *) malloc(j+1);
82                     memcpy(ip, mess, j+1);
83                     ip[j] = '\0';
84                     break;
85                 case 1:
86                     passwd = (char *) malloc(j+1);
87                     memcpy(passwd, mess, j+1);
88                     passwd[j] = '\0';
89                     break;
90                 case 2:
91                     label = (char *) malloc(j+1);
92                     memcpy(label, mess, j+1);
93                     label[j > 16 ? 16 : j] = '\0';
94                     break;
95                 case 3:
96                     aptype = (char *) malloc(j+1);
97                     memcpy(aptype, mess, j+1);
98                     aptype[j] = '\0';
99                     break;
100             }
101             i++;
102             j=0;
103             pos++;
104         } else
105             mess[j++] = str[i++];
106
107     }
108
109     mess[j]='\0';
110     if (pos==4 && ip && passwd && ((atmel410_filter && atoi(aptype) == ATMEL410)
111         || !atmel410_filter)) {
112         curr = (struct APList *) malloc(sizeof(struct APList));
113         curr->type = atoi(aptype);
114         curr->vendorext = atoi(mess);
115         curr->next = NULL;
116         curr->ip = (char *) malloc(strlen(ip) + 1);
117         strcpy(curr->ip, ip);
118         curr->passwd = (char *) malloc(strlen(passwd) + 1);
119         strcpy(curr->passwd, passwd);
120         curr->label = (char *) malloc(strlen(label) + 1);
121         strcpy(curr->label, label);
122     }
123
124     if (ip)
125         free(ip);
126
127     if (passwd)
128         free(passwd);
129
130     if (label)
131         free(label);
132
133     if (aptype)
134         free(aptype);
135
136     return curr;
137 }
138
139 int get_opts()
140 {
141     extern char *community;
142     extern struct in_addr ap_ip;
143     extern int sockfd;
144         
145     char *home_dir, buf[1024], mess[64];
146     char message[50];
147     int c, fd, rval = 0, pos;
148     signed int j, i, begin, end, record_num = 0;
149     struct APList *first = NULL, *curr = NULL, *pmac; 
150     struct sockaddr_in client;
151
152
153     memset(&client, 0, sizeof client);
154     client.sin_family = AF_INET;
155     client.sin_port = INADDR_ANY;
156     client.sin_addr.s_addr = INADDR_ANY;
157
158     if ((home_dir = getenv("HOME")) == NULL)
159         return 0;
160
161     sprintf(buf, "%s/.ap-config", home_dir);
162     if ((fd = open(buf, O_RDONLY)) == -1)
163         return 0;
164
165     pos=0;
166     while((j = read(fd, buf, sizeof(buf))) > 0) 
167         for(i=0; i < j; i++) {
168             if (buf[i] == 0x0a) {
169                 mess[pos]='\0';
170                 if (first == NULL) {
171                     if ((first = parse_db_str(mess)) != NULL) {
172                         curr = first;
173                         record_num = 1;
174                     }
175                 } else {
176                     if ((curr->next = parse_db_str(mess)) != NULL) {
177                         curr = curr->next;
178                         record_num++;
179                     }
180                 }
181                 pos=0;
182             } else      
183                 mess[pos++] = buf[i];
184
185         }
186
187     mess[pos]='\0';
188     if (first == NULL) {
189         if ((first = parse_db_str(mess)) != NULL) {
190             curr = first;
191             record_num = 1;
192         }
193     } else {
194         if ((curr->next = parse_db_str(mess)) != NULL) 
195             curr = curr->next;
196
197         record_num++;
198     }
199
200     close(fd);
201     if (!record_num)
202         return 0;
203
204     mvwaddstr(main_sub, 0, 2,
205         _("NUM  IP ADDRESS       MIB TYPE    MIB EXT.  LABEL"));
206     print_top(NULL, _("Choose an AP to connect to"));
207     begin = 1;
208     end = (MAX_LINES < record_num) ? MAX_LINES : record_num;
209     _scroll_rows(first, begin, end);
210     noecho();
211     while (1) {
212         print_help(_("1-9,C: connect; N: new; D: delete; W: save; Q: quit; arrows: scroll"));
213         switch (c = getch()) {
214             case 'q':
215             case 'Q':
216                 exit_program();
217
218             case 'n':
219             case 'N':
220                 goto quit;
221
222             case '0':
223             case '1':
224             case '2':
225             case '3':
226             case '4':
227             case '5':
228             case '6':
229             case '7':
230             case '8':
231             case '9':
232                 i = c - '0';
233                 if (record_num <= i || i <= 0)
234                     goto wrong_num;
235
236                 curr = first;
237                 while (--i > 0)
238                     curr = curr->next;
239
240                 inet_aton(curr->ip, &ap_ip);
241                 if (community)
242                     free(community);
243
244                 i = strlen(curr->passwd) + 1;
245                 community = (char *) malloc(i);
246                 strncpy(community, curr->passwd, i);
247                 ap_type = curr->type;
248                 ap_vendorext = curr->vendorext;
249                 rval = 1;
250                 if (sockfd)
251                     close(sockfd);
252
253                 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
254                     rval = 0;
255
256                 if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1)
257                     rval = 0;
258
259                 print_bottom(inet_ntoa(ap_ip));
260                 goto quit;
261
262             case 'c':
263             case 'C':
264                 /* Nothing to connect */
265                 if (record_num == 1)
266                     continue;
267
268                 mvwaddstr(main_sub, 1, 1, _("Connect to AP num:"));
269                 get_value(message, 1, 20, 6, INT_STRING, 1, record_num - 1,
270                     NULL);
271                 i = atoi(message);
272                 curr = first;
273                 while (--i > 0)
274                     curr = curr->next;
275
276                 inet_aton(curr->ip, &ap_ip);
277                 if (community)
278                     free(community);
279
280                 i = strlen(curr->passwd) + 1;
281                 community = (char *) malloc(i);
282                 strncpy(community, curr->passwd, i);
283                 ap_type = curr->type;
284                 ap_vendorext = curr->vendorext;
285                 rval = 1;
286                 if (sockfd)
287                     close(sockfd);
288
289                 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
290                     rval = 0;
291
292                 if (bind(sockfd, (struct sockaddr *) &client, SIZE) == -1)
293                     rval = 0;
294
295                 print_bottom(inet_ntoa(ap_ip));
296                 goto quit;
297
298             case 'd':
299             case 'D':
300                 /* Nothing to delete */
301                 if (record_num == 1)
302                     continue;
303
304                 mvwaddstr(main_sub, 1, 0, _("Delete num:"));
305                 get_value(message, 1, 15, 6, INT_STRING,
306                     1, (record_num == 1 ? 1 : record_num - 1), NULL);
307                 i = atoi(message);
308                 if (i == 1) {
309                     curr = first;
310                     first = first->next;
311                     free(curr->ip);
312                     free(curr->passwd);
313                     free(curr->label);
314                     free(curr);
315                 } else {
316                     curr = first;
317                     while (--i > 1)
318                         curr = curr->next;
319
320                     pmac = curr->next;
321                     curr->next = pmac->next;
322                     free(pmac->ip);
323                     free(pmac->passwd);
324                     free(pmac->label);
325                     free(pmac);
326                 }
327                 record_num--;
328                 /* Clear incl. line with last AP record */
329                 if (record_num == 1) {
330                     clear_main_new(1, 3);
331                     continue;
332                 }
333
334                 begin = 1;
335                 end = (MAX_LINES < record_num) ? MAX_LINES : record_num;
336                 _scroll_rows(first, begin, end);
337 wrong_num:
338                 clear_main_new(1, 2);
339                 continue;
340
341             case KEY_DOWN:
342             case KEY_RIGHT:
343                 if (end < record_num) {
344                     begin++;
345                     end++;
346                     _scroll_rows(first, begin, end);
347                 }
348                 continue;
349
350             case KEY_UP:
351             case KEY_LEFT:
352                 if (begin > 1) {
353                     begin--;
354                     end--;
355                     _scroll_rows(first, begin, end);
356                 }
357                 continue;
358
359             case 'w':
360             case 'W':
361                 sprintf(buf, "%s/.ap-config", home_dir);
362                 if ((fd = creat(buf, 0600)) != -1) {
363                     curr=first;
364                     while (curr) {
365                         sprintf(buf, "%s:%s:%s:%d:%d\n", curr->ip, curr->passwd,
366                             curr->label,curr->type, curr->vendorext);
367                         write(fd, buf, strlen(buf));
368                         curr = curr->next;
369                     }
370                     close(fd);
371                     print_help(
372                         _("AP list file ~/.ap-config successfully written. "
373                           "Press any key."));
374                 } else {
375                     print_helperr(
376                         _("Unable to write AP list file ~/.ap-config. "
377                           "Press any key."));
378                 }
379                 getch();
380                 continue;
381         }
382     }
383 /*
384     print_help(ANY_KEY);
385     getch();
386 */  quit:
387     while ((curr = first)) {
388         first = curr->next;
389         free(curr->ip);
390         free(curr->passwd);
391         free(curr->label);
392         free(curr);
393     }
394     print_help("");
395     print_top(NULL, NULL);
396     wclear(main_sub);
397     wrefresh(main_sub);
398     return rval;
399 }
400
401 void save_Stations(struct MacListStat *curr)
402 {
403     int fd, err_f = 0;
404     char *home_dir;
405     char message[1024];
406     if ((home_dir = getenv("HOME"))) {
407         sprintf(message, "%s/ap-%s-%s-%s.stations", home_dir, inet_ntoa(ap_ip),
408             ap_types[ap_type],ap_vendorexts[ap_type][ap_vendorext]);
409         if ((fd = creat(message, 0600)) != -1) {
410             while (curr) {
411                 sprintf(message, "%02X%02X%02X%02X%02X%02X\n",
412                     curr->addr[0] & 0xFF, curr->addr[1] & 0xFF,
413                     curr->addr[2] & 0xFF, curr->addr[3] & 0xFF,
414                     curr->addr[4] & 0xFF, curr->addr[5] & 0xFF);
415                 write(fd, message, 13);
416                 curr = curr->next;
417             }
418             close(fd);
419         } else {
420             err_f = 1;
421         }
422     } else {
423         err_f = 1;
424     }
425     if (err_f)
426         print_helperr(_("Unable to write stations file. Press any key."));
427     else
428         print_help(_("Stations file succesfully written. Press any key."));
429     getch();
430     print_help("");
431 }
432