summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings/ppp/modem.cpp
Unidiff
Diffstat (limited to 'noncore/settings/networksettings/ppp/modem.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/networksettings/ppp/modem.cpp192
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
@@ -32,2 +32,3 @@
32#include <setjmp.h> 32#include <setjmp.h>
33#include <regex.h>
33#include <qregexp.h> 34#include <qregexp.h>
@@ -35,5 +36,5 @@
35 36
37#include "auth.h"
36#include "modem.h" 38#include "modem.h"
37#include "pppdata.h" 39#include "pppdata.h"
38//#include "requester.h"
39//#include <klocale.h> 40//#include <klocale.h>
@@ -53,12 +54,39 @@ Modem *Modem::modem = 0;
53 54
54Modem::Modem() : 55
55 modemfd(-1), 56const char* pppdPath() {
56 sn(0L), 57 // wasting a few bytes
57 data_mode(false), 58 static char buffer[sizeof(PPPDSEARCHPATH)+sizeof(PPPDNAME)];
58 modem_is_locked(false) 59 static char *pppdPath = 0L;
60 char *p;
61
62 if(pppdPath == 0L) {
63 const char *c = PPPDSEARCHPATH;
64 while(*c != '\0') {
65 while(*c == ':')
66 c++;
67 p = buffer;
68 while(*c != '\0' && *c != ':')
69 *p++ = *c++;
70 *p = '\0';
71 strcat(p, "/");
72 strcat(p, PPPDNAME);
73 if(access(buffer, F_OK) == 0)
74 return (pppdPath = buffer);
75 }
76 }
77
78 return pppdPath;
79}
80
81
82Modem::Modem()
59{ 83{
84 if (Modem::modem != 0) return; //CORRECT?
85 modemfd = -1;
86 sn = 0L;
87 data_mode = false;
88 modem_is_locked = false;
60 lockfile[0] = '\0'; 89 lockfile[0] = '\0';
61 device = "/dev/modem"; 90 device = "/dev/modem";
62 assert(modem==0); 91 modem = this;
63 modem = this;
64} 92}
@@ -646,26 +674,136 @@ void alarm_handler(int) {
646 674
675const char* Modem::authFile(Auth method, int version) {
676 switch(method|version) {
677 case PAP|Original:
678 return PAP_AUTH_FILE;
679 break;
680 case PAP|New:
681 return PAP_AUTH_FILE".new";
682 break;
683 case PAP|Old:
684 return PAP_AUTH_FILE".old";
685 break;
686 case CHAP|Original:
687 return CHAP_AUTH_FILE;
688 break;
689 case CHAP|New:
690 return CHAP_AUTH_FILE".new";
691 break;
692 case CHAP|Old:
693 return CHAP_AUTH_FILE".old";
694 break;
695 default:
696 return 0L;
697 }
698}
647 699
648const char* pppdPath() {
649 // wasting a few bytes
650 static char buffer[sizeof(PPPDSEARCHPATH)+sizeof(PPPDNAME)];
651 static char *pppdPath = 0L;
652 char *p;
653 700
654 if(pppdPath == 0L) { 701bool Modem::createAuthFile(Auth method, const char *username, const char *password) {
655 const char *c = PPPDSEARCHPATH; 702 const char *authfile, *oldName, *newName;
656 while(*c != '\0') { 703 char line[100];
657 while(*c == ':') 704 char regexp[2*MaxStrLen+30];
658 c++; 705 regex_t preg;
659 p = buffer; 706
660 while(*c != '\0' && *c != ':') 707 if(!(authfile = authFile(method)))
661 *p++ = *c++; 708 return false;
662 *p = '\0'; 709
663 strcat(p, "/"); 710 if(!(newName = authFile(method, New)))
664 strcat(p, PPPDNAME); 711 return false;
665 if(access(buffer, F_OK) == 0) 712
666 return (pppdPath = buffer); 713 // look for username, "username" or 'username'
714 // if you modify this RE you have to adapt regexp's size above
715 snprintf(regexp, sizeof(regexp), "^[ \t]*%s[ \t]\\|^[ \t]*[\"\']%s[\"\']",
716 username,username);
717 MY_ASSERT(regcomp(&preg, regexp, 0) == 0);
718
719 // copy to new file pap- or chap-secrets
720 int old_umask = umask(0077);
721 FILE *fout = fopen(newName, "w");
722 if(fout) {
723 // copy old file
724 FILE *fin = fopen(authfile, "r");
725 if(fin) {
726 while(fgets(line, sizeof(line), fin)) {
727 if(regexec(&preg, line, 0, 0L, 0) == 0)
728 continue;
729 fputs(line, fout);
730 }
731 fclose(fin);
667 } 732 }
733
734 // append user/pass pair
735 fprintf(fout, "\"%s\"\t*\t\"%s\"\n", username, password);
736 fclose(fout);
668 } 737 }
669 738
670 return pppdPath; 739 // restore umask
740 umask(old_umask);
741
742 // free memory allocated by regcomp
743 regfree(&preg);
744
745 if(!(oldName = authFile(method, Old)))
746 return false;
747
748 // delete old file if any
749 unlink(oldName);
750
751 rename(authfile, oldName);
752 rename(newName, authfile);
753
754 return true;
755}
756
757
758bool Modem::setSecret(int method, const char* name, const char* password)
759{
760
761 Auth auth;
762 if(method == AUTH_PAPCHAP)
763 return setSecret(AUTH_PAP, name, password) &&
764 setSecret(AUTH_CHAP, name, password);
765
766 switch(method) {
767 case AUTH_PAP:
768 auth = Modem::PAP;
769 break;
770 case AUTH_CHAP:
771 auth = Modem::CHAP;
772 break;
773 default:
774 return false;
775 }
776
777 return createAuthFile(auth, name, password);
778
779}
780
781bool Modem::removeSecret(int)
782{
783 return true;
784}
785
786void Modem::killPPPDaemon()
787{
788}
789
790int Modem::pppdExitStatus()
791{
792 return -1;
671} 793}
794
795bool Modem::execPPPDaemon(const QString & arguments)
796{
797 return true;
798}
799
800int Modem::openResolv(int flags)
801{
802 return -1;
803}
804
805bool Modem::setHostname(const QString & name)
806{
807 return true;
808}
809