~ubuntu-branches/ubuntu/quantal/wvdial/quantal

« back to all changes in this revision

Viewing changes to wvpapchap.cc

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2004-10-06 11:05:06 UTC
  • mfrom: (0.1.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20041006110506-138x19fe4q0iu46e
Tags: 1.54.0-1ubuntu1
postinst: Disable command-line configuration to not disturb installation;
the script just exits early, so the postinst code is not completely lost
(Warty bug #2069)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Worldvisions Weaver Software:
 
3
 *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
 
4
 *
 
5
 * Re-write of wvpapsecrets.cc.  This one supports CHAP as well, and is also
 
6
 * much safer.
 
7
 */
 
8
 
 
9
#include "wvpapchap.h"
 
10
#include "wvfile.h"
 
11
#include <assert.h>
 
12
#include <ctype.h>
 
13
#include <sys/stat.h>
 
14
 
 
15
 
 
16
///////////////////////////////////////////////////////////
 
17
// WvPapChap public functions
 
18
///////////////////////////////////////////////////////////
 
19
 
 
20
void WvPapChap::put_secret( WvString username, WvString password,
 
21
                            WvString remote )
 
22
/*******************************************/
 
23
{
 
24
    assert( remote[0] );
 
25
 
 
26
    // PAP secrets:
 
27
    contents.zap();
 
28
    load_file( PAP_SECRETS );
 
29
    do_secret( username, password, remote );
 
30
    if( write_file( PAP_SECRETS ) == false )
 
31
        pap_success = false;
 
32
 
 
33
    // CHAP secrets:
 
34
    contents.zap();
 
35
    load_file( CHAP_SECRETS );
 
36
    do_secret( username, password, remote );
 
37
    if( write_file( CHAP_SECRETS ) == false )
 
38
        chap_success = false;
 
39
}
 
40
 
 
41
 
 
42
///////////////////////////////////////////////////////////
 
43
// WvPapChap private functions
 
44
///////////////////////////////////////////////////////////
 
45
 
 
46
bool WvPapChap::load_file( char * filename )
 
47
/******************************************/
 
48
// Loads filename into the "contents" string list, one line per entry.
 
49
{
 
50
    char *      from_file;
 
51
    WvString *  tmp;
 
52
 
 
53
    WvFile file( filename, O_RDONLY );
 
54
    if( file.isok() == false )
 
55
        return( false );
 
56
 
 
57
    from_file = file.getline( 0 );
 
58
    while( from_file ) {
 
59
        tmp = new WvString( from_file );
 
60
        contents.append( tmp, true );
 
61
        from_file = file.getline( 0 );
 
62
    }
 
63
    file.close();
 
64
 
 
65
    return( true );
 
66
}
 
67
 
 
68
bool WvPapChap::write_file( char * filename )
 
69
/*******************************************/
 
70
// Writes the "contents" list to the file, one entry per line.
 
71
{
 
72
    WvFile file( filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR );
 
73
    if( file.isok() == false )
 
74
        return( false );
 
75
 
 
76
    WvStringList::Iter  iter( contents );
 
77
    for( iter.rewind(); iter.next(); )
 
78
        file.print( "%s\n", *iter );
 
79
 
 
80
    file.close();
 
81
    return( true );
 
82
}
 
83
 
 
84
void WvPapChap::do_secret( const char * _username, const char * _password, 
 
85
                           const char * _remote )
 
86
/***********************************************/
 
87
// Goes through the "contents" list, looking for lines that have the same
 
88
// username.  If they do, and the remote value is either "*" or remote,
 
89
// the secret is removed.  Otherwise, it is left in place.  At the end of the
 
90
// list, the secret "username remote password" is added.
 
91
// remote defaults to "wvdial".
 
92
{
 
93
    WvStringList::Iter  iter( contents );
 
94
    WvString username;
 
95
    WvString password;
 
96
    WvString remote;
 
97
 
 
98
    if( !_username || !_password )
 
99
        return;
 
100
    
 
101
    // we need to backslash-escape all punctuation, so that pppd reads it
 
102
    // correctly.
 
103
    username = backslash_escape( _username );
 
104
    password = backslash_escape( _password );
 
105
    remote   = _remote;
 
106
 
 
107
    if( !remote )
 
108
        remote = "*";
 
109
 
 
110
    for( iter.rewind(); iter.next(); ) {
 
111
        // Is this line a comment?
 
112
        if( iter()[0] == '#' )
 
113
            continue;
 
114
 
 
115
        // Is the line blank?
 
116
        const char * p = iter();
 
117
        do 
 
118
            p++;
 
119
        while( *p != '\0' && isspace( *p ) );
 
120
        p--;
 
121
        if( *p == '\0' )
 
122
            continue;
 
123
 
 
124
        // p points at the first non-whitespace char.
 
125
        const char * q = p;
 
126
        do
 
127
            q++;
 
128
        while( *q != '\0' && !isspace( *q ) );
 
129
        q--;
 
130
        if( *q == '\0' ) {
 
131
            // illegal line, so get rid of it.
 
132
            iter.unlink();
 
133
            iter.rewind();
 
134
            continue;
 
135
        }
 
136
        if( strncmp( username, p, q-p ) != 0 )
 
137
            // different username, so let it stay.
 
138
            continue;
 
139
 
 
140
        p=q;
 
141
        do
 
142
            p++;
 
143
        while( *p != '\0' && isspace( *p ) );
 
144
        // p now points to the beginning of the "remote" section.
 
145
        if( strncmp( p, remote, strlen( remote ) ) == 0 || *p == '*' ) {
 
146
            // conflicting secret, so get rid of it.
 
147
            iter.unlink();
 
148
            iter.rewind();
 
149
            continue;
 
150
        }
 
151
 
 
152
        // This secret line should be fine.
 
153
    }
 
154
 
 
155
    contents.append( new WvString( "%s\t%s\t%s", username, remote, password ), 
 
156
                     true );
 
157
}