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.cpp6
1 files changed, 4 insertions, 2 deletions
diff --git a/noncore/settings/networksettings/ppp/modem.cpp b/noncore/settings/networksettings/ppp/modem.cpp
index 7b2e2a3..17ada9b 100644
--- a/noncore/settings/networksettings/ppp/modem.cpp
+++ b/noncore/settings/networksettings/ppp/modem.cpp
@@ -723,98 +723,100 @@ bool Modem::createAuthFile(Auth method, const char *username, const char *passwo
723 char line[100]; 723 char line[100];
724 char regexp[2*MaxStrLen+30]; 724 char regexp[2*MaxStrLen+30];
725 regex_t preg; 725 regex_t preg;
726 726
727 if(!(authfile = authFile(method))) 727 if(!(authfile = authFile(method)))
728 return false; 728 return false;
729 729
730 if(!(newName = authFile(method, New))) 730 if(!(newName = authFile(method, New)))
731 return false; 731 return false;
732 732
733 // look for username, "username" or 'username' 733 // look for username, "username" or 'username'
734 // if you modify this RE you have to adapt regexp's size above 734 // if you modify this RE you have to adapt regexp's size above
735 snprintf(regexp, sizeof(regexp), "^[ \t]*%s[ \t]\\|^[ \t]*[\"\']%s[\"\']", 735 snprintf(regexp, sizeof(regexp), "^[ \t]*%s[ \t]\\|^[ \t]*[\"\']%s[\"\']",
736 username,username); 736 username,username);
737 MY_ASSERT(regcomp(&preg, regexp, 0) == 0); 737 MY_ASSERT(regcomp(&preg, regexp, 0) == 0);
738 738
739 // copy to new file pap- or chap-secrets 739 // copy to new file pap- or chap-secrets
740 int old_umask = umask(0077); 740 int old_umask = umask(0077);
741 FILE *fout = fopen(newName, "w"); 741 FILE *fout = fopen(newName, "w");
742 if(fout) { 742 if(fout) {
743 // copy old file 743 // copy old file
744 FILE *fin = fopen(authfile, "r"); 744 FILE *fin = fopen(authfile, "r");
745 if(fin) { 745 if(fin) {
746 while(fgets(line, sizeof(line), fin)) { 746 while(fgets(line, sizeof(line), fin)) {
747 if(regexec(&preg, line, 0, 0L, 0) == 0) 747 if(regexec(&preg, line, 0, 0L, 0) == 0)
748 continue; 748 continue;
749 fputs(line, fout); 749 fputs(line, fout);
750 } 750 }
751 fclose(fin); 751 fclose(fin);
752 } 752 }
753 753
754 // append user/pass pair 754 // append user/pass pair
755 fprintf(fout, "\"%s\"\t*\t\"%s\"\n", username, password); 755 fprintf(fout, "\"%s\"\t*\t\"%s\"\n", username, password);
756 fclose(fout); 756 fclose(fout);
757 } 757 }
758 758
759 // restore umask 759 // restore umask
760 umask(old_umask); 760 umask(old_umask);
761 761
762 // free memory allocated by regcomp 762 // free memory allocated by regcomp
763 regfree(&preg); 763 regfree(&preg);
764 764
765 if(!(oldName = authFile(method, Old))) 765 if(!(oldName = authFile(method, Old)))
766 return false; 766 return false;
767 767
768 // delete old file if any 768 // delete old file if any
769 unlink(oldName); 769 unlink(oldName);
770 770
771 rename(authfile, oldName); 771 if (rename(authfile, oldName) == -1)
772 rename(newName, authfile); 772 return false;
773 if (rename(newName, authfile) == -1)
774 return false;
773 775
774 return true; 776 return true;
775} 777}
776 778
777 779
778bool Modem::removeAuthFile(Auth method) { 780bool Modem::removeAuthFile(Auth method) {
779 const char *authfile, *oldName; 781 const char *authfile, *oldName;
780 782
781 if(!(authfile = authFile(method))) 783 if(!(authfile = authFile(method)))
782 return false; 784 return false;
783 if(!(oldName = authFile(method, Old))) 785 if(!(oldName = authFile(method, Old)))
784 return false; 786 return false;
785 787
786 if(access(oldName, F_OK) == 0) { 788 if(access(oldName, F_OK) == 0) {
787 unlink(authfile); 789 unlink(authfile);
788 return (rename(oldName, authfile) == 0); 790 return (rename(oldName, authfile) == 0);
789 } else 791 } else
790 return false; 792 return false;
791} 793}
792 794
793 795
794bool Modem::setSecret(int method, const char* name, const char* password) 796bool Modem::setSecret(int method, const char* name, const char* password)
795{ 797{
796 798
797 Auth auth; 799 Auth auth;
798 if(method == AUTH_PAPCHAP) 800 if(method == AUTH_PAPCHAP)
799 return setSecret(AUTH_PAP, name, password) && 801 return setSecret(AUTH_PAP, name, password) &&
800 setSecret(AUTH_CHAP, name, password); 802 setSecret(AUTH_CHAP, name, password);
801 803
802 switch(method) { 804 switch(method) {
803 case AUTH_PAP: 805 case AUTH_PAP:
804 auth = Modem::PAP; 806 auth = Modem::PAP;
805 break; 807 break;
806 case AUTH_CHAP: 808 case AUTH_CHAP:
807 auth = Modem::CHAP; 809 auth = Modem::CHAP;
808 break; 810 break;
809 default: 811 default:
810 return false; 812 return false;
811 } 813 }
812 814
813 return createAuthFile(auth, name, password); 815 return createAuthFile(auth, name, password);
814 816
815} 817}
816 818
817bool Modem::removeSecret(int method) 819bool Modem::removeSecret(int method)
818{ 820{
819 Auth auth; 821 Auth auth;
820 822