/* * * Copyright (C) 2002, 2003, 2008 Florian Boor * 2004 Ole Reinhardt * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * * Configfile I/O routines, part of dynamic interface configuration. * * Wireless LAN support added by Ole Reinhardt * */ #include #include #include #include #include #include #include #include "cfgfile.h" #include "network.h" #include "tools/interface.h" #define _(x) gettext(x) static FILE* configfile; static gchar** configtext; static gint configlen; NWInterface_t* iflist = NULL; gint iflen = 0; gint set_file_open(gint openon); gint get_section_start(gchar* section); gint get_section_end(gchar* section); gint get_section_nr(G_CONST_RETURN gchar* section); gint read_section(gint, NWInterface_t *Scheme); gint rewrite_section(NWInterface_t *Scheme, gint startpos); gint get_section_text(gchar* section); gint get_file_text(); gint get_scheme_list(); gint write_sections(); /* some helper for parsing strings */ gchar last_char(gchar* s); gint count_char(gchar* s, gchar c); gint get_first_char(gchar* s); gint get_param_val(gchar* line, gchar* param, gchar* value); gchar *subst_val(gchar* line, gchar* value); gchar* get_file_var(const gchar *file, const gchar *var) { gchar *content; gchar **lines; gsize length; gchar *delim; gint i = 0; GError *err = NULL; delim = g_strdup ("\n"); if (!g_file_get_contents (file, &content, &length, &err)) { fprintf(stderr,"Could not access file: %s.\n",file); return NULL; } lines = g_strsplit (content, delim, 2048); g_free (delim); delim = NULL; g_free (content); while (lines[i]) { if ((g_strrstr (g_strchomp(lines[i]), var)) && (!g_str_has_prefix (g_strchomp(lines[i]), "#"))) { delim = g_strrstr(lines[i],"="); if (delim) delim = g_strdup(g_strchomp(delim)+1); if (!delim) { delim = g_strrstr(lines[i]," "); if (delim) delim = g_strdup(g_strchomp(delim)+1); } break; } i++; } g_strfreev (lines); return delim; } gchar *subst_val(gchar* line, gchar* value) { gchar param[255]; gchar tmpval[255]; if (get_first_char(value) != -1) { get_param_val(line,param,tmpval); free(line); line = g_strdup_printf("\t%s %s",param,value); } else // remove line { line[0] = 0; } return line; } gint get_param_val(gchar* line, gchar* param, gchar* value) { gint st,sep,a,b; st=get_first_char(line); if (line[st] == '#') return -1; // mark comment for (sep=st;sep 0) value=strncpy(value,&line[a],b-a); value[b-a]='\0'; return 0; } gint read_section(gint iface, NWInterface_t *Iface) { return 0; } gint get_first_char(gchar* s) { gint i=0; for (i=0;i 0) { strncpy(iface->key[keynr-1], lastkey, 63); lastkey[0] = 0; } iface->encmode = ENC_OFF; key_was_last = FALSE; index_was_last = FALSE; } else if ((strcasecmp(token, "open") == 0) || (strcmp(token, "on")== 0)) { if (strlen(lastkey) > 0) { strncpy(iface->key[keynr-1], lastkey, 63); lastkey[0] = 0; } iface->encmode = ENC_OPEN; key_was_last = FALSE; index_was_last = FALSE; } else if (strcasecmp(token, "restricted") == 0) { if (strlen(lastkey) > 0) { strncpy(iface->key[keynr-1], lastkey, 63); lastkey[0] = 0; } iface->encmode = ENC_RESTRICTED; key_was_last = FALSE; index_was_last = FALSE; } else if ((token[0] == '[') && (token[2] == ']')) { keynr = token[1] - '0'; if (keynr < 1) keynr = 1; if (keynr > 4) keynr = 4; if (key_was_last) { iface->keynr = keynr; } else if (strlen(lastkey) > 0) { strncpy(iface->key[keynr-1], lastkey, 63); lastkey[0] = 0; } { index_was_last = TRUE; } key_was_last = FALSE; } else if ((strcasecmp(token, "key") == 0) || (strncasecmp(token, "enc", 3) == 0)) { keynr = 1; if (strlen(lastkey) > 0) { strncpy(iface->key[keynr-1], lastkey, 63); lastkey[0] = 0; } key_was_last = TRUE; index_was_last = FALSE; } else { if (index_was_last) { strncpy(iface->key[keynr-1], token, 63); } else { strncpy(lastkey, token, 63); } key_was_last = FALSE; index_was_last = FALSE; } token = strtok(NULL, " "); } } static gboolean is_present_interface(gchar *ifname) { struct interface *int_list, *ife; /* show devices for pan connections */ if (g_str_has_prefix(ifname, "bnep")) return TRUE; int_list = if_getlist (); g_strstrip(ifname); for (ife = int_list; ife; ife = ife->next) { if (g_str_has_prefix(ifname, ife->name)) return TRUE; } return FALSE; } gint get_scheme_list() { gchar ifname[255] = {0}; gchar header[255] = {0}; gchar paramval[255]; gchar option1[32] = {0}; gchar option2[32] = {0}; gint i, j, k; gint l=0; gchar buffer[256]; gchar *sep; gchar *name; FILE *fd; for (i=0;ipos;a--) { configtext[a]=configtext[a-1]; } configtext[pos] = tmp; strcpy(configtext[pos],line); } gchar *get_iflist() { gint i; gchar *result = malloc(256*sizeof(gchar)); result[0] = '\0'; for (i=0;i 0) { t = lkey; lkey = g_strdup_printf("%s key %s [%d]", lkey, iface.key[count], count+1); g_free(t); } t = lkey; lkey = g_strdup_printf(" %s key [%d]", lkey, iface.keynr); g_free(t); } t = lkey; switch (iface.encmode) { case ENC_OPEN: lkey = g_strdup_printf ("%s key open", lkey); break; case ENC_RESTRICTED: lkey = g_strdup_printf("%s key restricted", lkey); break; case ENC_OFF: lkey = g_strdup_printf("%s key off", lkey); break; default: lkey = g_strdup_printf("%s key off", lkey); break; } g_free(t); *key = lkey; } gint write_sections() { gint i,j; gint l=0; gint in_section = FALSE; gchar outstr[255]; gchar paramval[255]; gchar ifname[255]; gint svd[14]; gint lastwpos = 0; gint last_i; gchar *key; for (i=0;i=0)) { sprintf(outstr,"\taddress %s",iflist[l-1].address); add_line(lastwpos,outstr); i++; } if ((!svd[Snetmask]) && (get_first_char(iflist[l-1].netmask)>=0)) { sprintf(outstr,"\tnetmask %s",iflist[l-1].netmask); add_line(lastwpos,outstr); i++; } if ((!svd[Snetwork]) && (get_first_char(iflist[l-1].network)>=0)) { sprintf(outstr,"\tnetwork %s",iflist[l-1].network); add_line(lastwpos,outstr); i++; } if ((!svd[Sbroadcast]) && (get_first_char(iflist[l-1].broadcast)>=0)) { sprintf(outstr,"\tbroadcast %s",iflist[l-1].broadcast); add_line(lastwpos,outstr); i++; } if ((!svd[Shostname]) && (get_first_char(iflist[l-1].hostname)>=0)) { sprintf(outstr,"\thostname %s",iflist[l-1].hostname); add_line(lastwpos,outstr); i++; } if ((!svd[Sclientid]) && (get_first_char(iflist[l-1].clientid)>=0)) { sprintf(outstr,"\tclient %s",iflist[l-1].clientid); add_line(lastwpos,outstr); i++; } if ((!svd[Sprovider]) && (get_first_char(iflist[l-1].provider)>=0)) { sprintf(outstr,"\tprovider %s",iflist[l-1].provider); add_line(lastwpos,outstr); i++; } if ((!svd[Sgateway]) && (get_first_char(iflist[l-1].gateway)>=0)) { sprintf(outstr,"\tgateway %s",iflist[l-1].gateway); add_line(lastwpos,outstr); i++; } if (iflist[l-1].iswireless) { if (!svd[Swifiessid]) { sprintf(outstr,"\twireless_essid %s",iflist[l-1].essid); add_line(lastwpos,outstr); i++; } if (!svd[Swifimode]) { sprintf(outstr,"\twireless_mode %s",iflist[l-1].mode == MODE_MANAGED ? "managed" : "ad-hoc"); add_line(lastwpos,outstr); i++; } if ((!svd[Swifichannel]) && (strlen (iflist[l-1].channel) > 0)) { sprintf(outstr,"\twireless_channel %s",iflist[l-1].channel); add_line(lastwpos,outstr); i++; } if (!svd[Swifikey]) { get_wifikey_string(iflist[l-1], &key); sprintf(outstr,"\twireless_key %s",key); add_line(lastwpos,outstr); i++; } } for (j=l;j=0) { sprintf(outstr,"\taddress %s",iflist[i].address); add_line(configlen,outstr); } if (get_first_char(iflist[i].netmask)>=0) { sprintf(outstr,"\tnetmask %s",iflist[i].netmask); add_line(configlen,outstr); } if (get_first_char(iflist[i].network)>=0) { sprintf(outstr,"\tnetwork %s",iflist[i].network); add_line(configlen,outstr); } if (get_first_char(iflist[i].broadcast)>=0) { sprintf(outstr,"\tbroadcast %s",iflist[i].broadcast); add_line(configlen,outstr); } if (get_first_char(iflist[i].hostname)>=0) { sprintf(outstr,"\thostname %s",iflist[i].hostname); add_line(configlen,outstr); } if (get_first_char(iflist[i].clientid)>=0) { sprintf(outstr,"\tclient %s",iflist[i].clientid); add_line(configlen,outstr); } if (get_first_char(iflist[i].provider)>=0) { sprintf(outstr,"\tprovider %s",iflist[i].provider); add_line(configlen,outstr); } if (get_first_char(iflist[i].gateway)>=0) { sprintf(outstr,"\tgateway %s",iflist[i].gateway); add_line(configlen,outstr); } if (iflist[i].iswireless) { sprintf(outstr,"\twireless_essid %s",iflist[i].essid); add_line(configlen,outstr); sprintf(outstr,"\twireless_mode %s",iflist[i].mode == MODE_MANAGED ? "managed" : "ad-hoc"); add_line(configlen,outstr); if (strlen (iflist[i].channel) > 0) { sprintf(outstr,"\twireless_channel %s",iflist[i].channel); add_line(configlen,outstr); } get_wifikey_string(iflist[i], &key); sprintf(outstr,"\twireless_key %s",key); g_free(key); add_line(configlen,outstr); } } //if status /* handle auto line if not yet done */ /*if (auto_set == FALSE) { add_line(configlen,g_strdup_printf("auto %s\n",get_iflist())); } */ // write file configfile = fopen(NET_NEWFILE,"w"); if (configfile) { for (i=0;i0) fprintf(configfile,"%s",configtext[i]); else fprintf(configfile,"%s\n",configtext[i]); } } fclose(configfile); } else gpe_error_box(_("No write access to network configuration.")); return l; }