Diffstat (limited to 'noncore/settings/networksettings/ppp/modem.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/settings/networksettings/ppp/modem.cpp | 192 |
1 files changed, 165 insertions, 27 deletions
diff --git a/noncore/settings/networksettings/ppp/modem.cpp b/noncore/settings/networksettings/ppp/modem.cpp index e9e3f06..a2f77c5 100644 --- a/noncore/settings/networksettings/ppp/modem.cpp +++ b/noncore/settings/networksettings/ppp/modem.cpp @@ -9,79 +9,107 @@ * This file was added by Harri Porten <porten@tu-harburg.de> * * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include <errno.h> #include <stdlib.h> #include <fcntl.h> #include <signal.h> #include <sys/ioctl.h> #include <setjmp.h> +#include <regex.h> #include <qregexp.h> #include <assert.h> +#include "auth.h" #include "modem.h" #include "pppdata.h" -//#include "requester.h" //#include <klocale.h> #define i18n QObject::tr #define qError qDebug //#include <kdebug.h> //#include <config.h> #define MY_ASSERT(x) if (!(x)) { \ qFatal( "ASSERT: \"%s\" in %s (%d)\n",#x,__FILE__,__LINE__); \ exit(1); } static sigjmp_buf jmp_buffer; Modem *Modem::modem = 0; -Modem::Modem() : - modemfd(-1), - sn(0L), - data_mode(false), - modem_is_locked(false) + +const char* pppdPath() { + // wasting a few bytes + static char buffer[sizeof(PPPDSEARCHPATH)+sizeof(PPPDNAME)]; + static char *pppdPath = 0L; + char *p; + + if(pppdPath == 0L) { + const char *c = PPPDSEARCHPATH; + while(*c != '\0') { + while(*c == ':') + c++; + p = buffer; + while(*c != '\0' && *c != ':') + *p++ = *c++; + *p = '\0'; + strcat(p, "/"); + strcat(p, PPPDNAME); + if(access(buffer, F_OK) == 0) + return (pppdPath = buffer); + } + } + + return pppdPath; +} + + +Modem::Modem() { + if (Modem::modem != 0) return; //CORRECT? + modemfd = -1; + sn = 0L; + data_mode = false; + modem_is_locked = false; lockfile[0] = '\0'; device = "/dev/modem"; - assert(modem==0); - modem = this; + modem = this; } Modem::~Modem() { modem = 0; } speed_t Modem::modemspeed() { // convert the string modem speed int the gpppdata object to a t_speed type // to set the modem. The constants here should all be ifdef'd because // other systems may not have them int i = gpppdata.speed().toInt()/100; switch(i) { case 24: return B2400; break; case 96: return B9600; break; case 192: return B19200; break; @@ -623,49 +651,159 @@ int Modem::openLockfile( QString lockfile, int flags) // } else { // // make sure that this is a regular file // if(!S_ISREG(st.st_mode)) // return -1; // } if ((fd = open(lockfile, flags, mode)) == -1) { qDebug("error opening lockfile!"); lockfile = QString::null; fd = open(DEVNULL, O_RDONLY); } else fchown(fd, 0, 0); return fd; } void alarm_handler(int) { // fprintf(stderr, "alarm_handler(): Received SIGALRM\n"); // jump siglongjmp(jmp_buffer, 1); } +const char* Modem::authFile(Auth method, int version) { + switch(method|version) { + case PAP|Original: + return PAP_AUTH_FILE; + break; + case PAP|New: + return PAP_AUTH_FILE".new"; + break; + case PAP|Old: + return PAP_AUTH_FILE".old"; + break; + case CHAP|Original: + return CHAP_AUTH_FILE; + break; + case CHAP|New: + return CHAP_AUTH_FILE".new"; + break; + case CHAP|Old: + return CHAP_AUTH_FILE".old"; + break; + default: + return 0L; + } +} -const char* pppdPath() { - // wasting a few bytes - static char buffer[sizeof(PPPDSEARCHPATH)+sizeof(PPPDNAME)]; - static char *pppdPath = 0L; - char *p; - if(pppdPath == 0L) { - const char *c = PPPDSEARCHPATH; - while(*c != '\0') { - while(*c == ':') - c++; - p = buffer; - while(*c != '\0' && *c != ':') - *p++ = *c++; - *p = '\0'; - strcat(p, "/"); - strcat(p, PPPDNAME); - if(access(buffer, F_OK) == 0) - return (pppdPath = buffer); +bool Modem::createAuthFile(Auth method, const char *username, const char *password) { + const char *authfile, *oldName, *newName; + char line[100]; + char regexp[2*MaxStrLen+30]; + regex_t preg; + + if(!(authfile = authFile(method))) + return false; + + if(!(newName = authFile(method, New))) + return false; + + // look for username, "username" or 'username' + // if you modify this RE you have to adapt regexp's size above + snprintf(regexp, sizeof(regexp), "^[ \t]*%s[ \t]\\|^[ \t]*[\"\']%s[\"\']", + username,username); + MY_ASSERT(regcomp(&preg, regexp, 0) == 0); + + // copy to new file pap- or chap-secrets + int old_umask = umask(0077); + FILE *fout = fopen(newName, "w"); + if(fout) { + // copy old file + FILE *fin = fopen(authfile, "r"); + if(fin) { + while(fgets(line, sizeof(line), fin)) { + if(regexec(&preg, line, 0, 0L, 0) == 0) + continue; + fputs(line, fout); + } + fclose(fin); } + + // append user/pass pair + fprintf(fout, "\"%s\"\t*\t\"%s\"\n", username, password); + fclose(fout); } - return pppdPath; + // restore umask + umask(old_umask); + + // free memory allocated by regcomp + regfree(&preg); + + if(!(oldName = authFile(method, Old))) + return false; + + // delete old file if any + unlink(oldName); + + rename(authfile, oldName); + rename(newName, authfile); + + return true; +} + + +bool Modem::setSecret(int method, const char* name, const char* password) +{ + + Auth auth; + if(method == AUTH_PAPCHAP) + return setSecret(AUTH_PAP, name, password) && + setSecret(AUTH_CHAP, name, password); + + switch(method) { + case AUTH_PAP: + auth = Modem::PAP; + break; + case AUTH_CHAP: + auth = Modem::CHAP; + break; + default: + return false; + } + + return createAuthFile(auth, name, password); + +} + +bool Modem::removeSecret(int) +{ + return true; +} + +void Modem::killPPPDaemon() +{ +} + +int Modem::pppdExitStatus() +{ + return -1; } + +bool Modem::execPPPDaemon(const QString & arguments) +{ + return true; +} + +int Modem::openResolv(int flags) +{ + return -1; +} + +bool Modem::setHostname(const QString & name) +{ + return true; +} + |