~ubuntu-branches/debian/squeeze/pgadmin3/squeeze

« back to all changes in this revision

Viewing changes to pgadmin/utils/pgconfig.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Porcheron
  • Date: 2008-02-07 00:56:22 UTC
  • mto: (2.1.6 hardy) (6.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20080207005622-c2ail8p4d0sk3dnw
Tags: upstream-1.8.2
ImportĀ upstreamĀ versionĀ 1.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//////////////////////////////////////////////////////////////////////////
 
2
//
 
3
// pgAdmin III - PostgreSQL Tools
 
4
// RCS-ID:      $Id: pgconfig.cpp 6930 2008-01-02 00:10:01Z dpage $
 
5
// Copyright (C) 2002 - 2008, The pgAdmin Development Team
 
6
// This software is released under the Artistic Licence
 
7
//
 
8
// pgconfig.cpp - backend configuration classes
 
9
//
 
10
//////////////////////////////////////////////////////////////////////////
 
11
 
 
12
#include "pgAdmin3.h"
 
13
 
 
14
#include <wx/textbuf.h>
 
15
 
 
16
#include "utils/pgconfig.h"
 
17
#include "db/pgConn.h"
 
18
#include "db/pgSet.h"
 
19
#include "utils/utffile.h"
 
20
 
 
21
 
 
22
#define DEFAULT_COMMENT_INDEX       wxT("\t\t# ")
 
23
 
 
24
 
 
25
int FindToken(const wxString &str, const wxChar **list)
 
26
{
 
27
    int index;
 
28
    for (index = 0 ; list[index] ; index++)
 
29
    {
 
30
        if (list[index] == str)
 
31
            return index;
 
32
 
 
33
    }
 
34
    return -1;
 
35
}
 
36
 
 
37
 
 
38
const wxChar *pgConfigTypeStrings[]=
 
39
{
 
40
    wxT("bool"),
 
41
    wxT("integer"),
 
42
    wxT("real"),
 
43
    wxT("string"),
 
44
    0
 
45
};
 
46
 
 
47
    
 
48
void pgSettingItem::SetType(const wxString &str)
 
49
{
 
50
    int index=FindToken(str, pgConfigTypeStrings);
 
51
    if (index < 0)
 
52
        type = PGC_STRING;
 
53
    else
 
54
        type = (pgConfigType)index;
 
55
}
 
56
 
 
57
 
 
58
const wxChar *pgConfigContextStrings[]=
 
59
{
 
60
    wxT("internal"),
 
61
    wxT("postmaster"),
 
62
    wxT("sighup"),
 
63
    wxT("backend"),
 
64
    wxT("superuser"),
 
65
    wxT("userlimit"),
 
66
    wxT("user"),
 
67
    0
 
68
};
 
69
 
 
70
 
 
71
void pgSettingItem::SetContext(const wxString &str)
 
72
{
 
73
    int index = FindToken(str, pgConfigContextStrings);
 
74
    if (index < 0)
 
75
        context = PGC_INTERNAL;
 
76
    else
 
77
        context = (pgConfigContext)index;
 
78
}
 
79
 
 
80
 
 
81
const wxChar *pgConfigSourceStrings[]=
 
82
{
 
83
    wxT("default"),
 
84
    wxT("environment variable"),
 
85
    wxT("configuration file"),
 
86
    wxT("command line"),
 
87
    wxT("unprivileged"),
 
88
    wxT("database"),
 
89
    wxT("user"),
 
90
    wxT("client"),
 
91
    wxT("override"),
 
92
    wxT("interactive"),
 
93
    wxT("test"),
 
94
    wxT("session"),
 
95
    0
 
96
};
 
97
 
 
98
    
 
99
void pgSettingItem::SetSource(const wxString &str)
 
100
{
 
101
    int index = FindToken(str, pgConfigSourceStrings);
 
102
    if (index < 0)
 
103
        source = PGC_UNKNOWNSOURCE;
 
104
    else
 
105
        source = (pgConfigSource)index;
 
106
}
 
107
 
 
108
 
 
109
wxString pgSettingItem::GetActiveValue()
 
110
{
 
111
    if (newLine)
 
112
    {
 
113
        if (!newLine->isComment)
 
114
            return newLine->value;
 
115
    }
 
116
    else if (orgLine)
 
117
    {
 
118
        if (!orgLine->isComment)
 
119
            return orgLine->value;
 
120
    }
 
121
    return value;
 
122
}
 
123
 
 
124
 
 
125
pgConfigLine::pgConfigLine(pgConfigLine *line)
 
126
{
 
127
    item=line->item;
 
128
    value=line->value;
 
129
    comment=line->comment;
 
130
    isComment = line->isComment;
 
131
}
 
132
 
 
133
 
 
134
bool pgConfigLine::Differs(pgConfigLine *line)
 
135
{
 
136
    return value != line->value || comment != line->comment || isComment != line->isComment;
 
137
}
 
138
 
 
139
 
 
140
wxString pgConfigLine::GetNewText()
 
141
{
 
142
    wxString quote;
 
143
    wxString newLine;
 
144
    if (item->type == pgSettingItem::PGC_STRING)
 
145
    {
 
146
        if (value.Find('\'') >= 0)
 
147
            quote = wxT("\"");
 
148
        else
 
149
            quote = wxT("'");
 
150
    }
 
151
    if (isComment)
 
152
        newLine = wxT("# ");
 
153
 
 
154
    newLine += item->name + wxT(" = ")
 
155
            + quote + value + quote;
 
156
 
 
157
    if (!comment.IsEmpty())
 
158
    {
 
159
        if (item->orgLine && item->orgLine->commentIndent.Find('#') >= 0)
 
160
            newLine += item->orgLine->commentIndent;
 
161
        else
 
162
            newLine += DEFAULT_COMMENT_INDEX;
 
163
        newLine += comment;
 
164
    }
 
165
    return newLine;
 
166
}
 
167
 
 
168
 
 
169
pgConfigOrgLine::pgConfigOrgLine(pgConfigLine *line) : pgConfigLine(line)
 
170
{
 
171
    text = line->GetNewText();
 
172
    if (isComment)
 
173
        commentIndent = DEFAULT_COMMENT_INDEX;
 
174
}
 
175
 
 
176
 
 
177
wxString pgConfigOrgLine::GetNewText()
 
178
{
 
179
    if (!item || !item->newLine)
 
180
        return text;
 
181
    return item->newLine->GetNewText();
 
182
}
 
183
 
 
184
 
 
185
////////////////////////////////////////////////
 
186
 
 
187
 
 
188
pgSettingFileReader::pgSettingFileReader(bool localized)
 
189
{
 
190
 
 
191
    wxUtfFile file;
 
192
    extern wxString i18nPath;
 
193
 
 
194
    wxString path=i18nPath + wxT("/") + settings->GetCanonicalLanguage() +wxT("/pg_settings.csv");
 
195
    if (localized && wxFile::Exists(path))
 
196
        file.Open(path);
 
197
    else
 
198
        file.Open(i18nPath+wxT("/pg_settings.csv"));
 
199
 
 
200
    if (file.IsOpened())
 
201
    {
 
202
        file.Read(buffer);
 
203
        file.Close();
 
204
    }
 
205
 
 
206
    if (!buffer.IsEmpty())
 
207
    {
 
208
        buffer = wxTextBuffer::Translate(buffer, wxTextFileType_Unix);
 
209
 
 
210
        columnNames = buffer.BeforeFirst('\n');
 
211
        bp = (wxChar*)buffer.c_str() + columnNames.Length()+1;
 
212
    }
 
213
}
 
214
 
 
215
 
 
216
pgSettingFileReader::~pgSettingFileReader()
 
217
{
 
218
}
 
219
 
 
220
 
 
221
pgSettingItem *pgSettingFileReader::GetNextItem()
 
222
{
 
223
    if (!bp || !*bp || *bp == '\n')
 
224
        return 0;
 
225
 
 
226
    pgSettingItem *item=new pgSettingItem;
 
227
    wxStringTokenizer tk(columnNames, wxT(";"));
 
228
    wxString column=tk.GetNextToken();
 
229
 
 
230
    wxChar *c = bp;
 
231
    while (*c)
 
232
    {
 
233
        wxString value;
 
234
 
 
235
        if (*c++ != '"')
 
236
        {
 
237
            // invalid syntax
 
238
            return 0;
 
239
        }
 
240
        while (*c && *c != '\n')
 
241
        {
 
242
            if (*c == '"')
 
243
            {
 
244
                if (c[1] == '"')
 
245
                    c++;
 
246
                else
 
247
                    break;
 
248
            }
 
249
            value.Append(*c++);
 
250
        }
 
251
        if (*c++ != '"')
 
252
        {
 
253
            // format error
 
254
            return 0;
 
255
        }
 
256
        if (column == wxT("name") || column == wxT("\"name\""))
 
257
            item->name = value;
 
258
        else if (column == wxT("category") || column == wxT("\"category\""))
 
259
            item->category = value;
 
260
        else if (column == wxT("short_desc") || column == wxT("\"short_desc\""))
 
261
            item->short_desc = value;
 
262
        else if (column == wxT("extra_desc") || column == wxT("\"extra_desc\""))
 
263
            item->extra_desc = value;
 
264
        else if (column == wxT("min_val") || column == wxT("\"min_val\""))
 
265
            item->min_val = value;
 
266
        else if (column == wxT("max_val") || column == wxT("\"max_val\""))
 
267
            item->max_val = value;
 
268
        else if (column == wxT("context") || column == wxT("\"context\""))
 
269
            item->SetContext(value);
 
270
        else if (column == wxT("vartype") || column == wxT("\"vartype\""))
 
271
            item->SetType(value);
 
272
 
 
273
        column = tk.GetNextToken();
 
274
        if (*c == '\n')
 
275
        {
 
276
            bp = c +1;
 
277
            break;
 
278
        }
 
279
        else if (!*c)
 
280
        {
 
281
            bp=0;
 
282
            break;
 
283
        }
 
284
        else if (*c == ';')
 
285
            c++;
 
286
        else
 
287
        {
 
288
            // invalid syntax
 
289
            return 0;
 
290
        }
 
291
    }
 
292
 
 
293
    return item;
 
294
}
 
295
 
 
296
 
 
297
pgSettingDbReader::pgSettingDbReader(pgConn *conn)
 
298
{
 
299
    set=conn->ExecuteSet(wxT("SELECT name, setting, source, category, short_desc, extra_desc, context, vartype, min_val, max_val FROM pg_settings ORDER BY name"));
 
300
}
 
301
 
 
302
 
 
303
pgSettingDbReader::~pgSettingDbReader()
 
304
{
 
305
    if (set)
 
306
        delete set;
 
307
}
 
308
 
 
309
pgSettingItem *pgSettingDbReader::GetNextItem()
 
310
{
 
311
    if (set->Eof())
 
312
        return 0;
 
313
    
 
314
    pgSettingItem *item=new pgSettingItem;
 
315
 
 
316
    item->name = set->GetVal(wxT("name"));
 
317
    item->category = set->GetVal(wxT("category"));
 
318
    item->short_desc = set->GetVal(wxT("short_desc"));
 
319
    item->extra_desc = set->GetVal(wxT("extra_desc"));
 
320
    item->min_val = set->GetVal(wxT("min_val"));
 
321
    item->max_val = set->GetVal(wxT("max_val"));
 
322
    item->SetContext(set->GetVal(wxT("context")));
 
323
    item->SetType(set->GetVal(wxT("vartype")));
 
324
    item->SetSource(set->GetVal(wxT("source")));
 
325
    item->value = set->GetVal(wxT("setting"));
 
326
 
 
327
    set->MoveNext();
 
328
 
 
329
    return item;
 
330
}
 
331
 
 
332
 
 
333
///////////////////////////////////////////////////////
 
334
 
 
335
// must match enum pgHbaConnectType!!!
 
336
const wxChar *pgHbaConnectTypeStrings[] =
 
337
{
 
338
    wxT("local"),
 
339
    wxT("host"),
 
340
    wxT("hostssl"),
 
341
    wxT("hostnossl"),
 
342
    0
 
343
};
 
344
 
 
345
 
 
346
// must match enum pgHbaMethod!!!
 
347
const wxChar *pgHbaMethodStrings[] =
 
348
{
 
349
    wxT("trust"),
 
350
    wxT("reject"),
 
351
    wxT("md5"),
 
352
    wxT("crypt"),
 
353
    wxT("password"),
 
354
    wxT("krb4"),
 
355
    wxT("krb5"),
 
356
    wxT("ident"),
 
357
    wxT("pam"),
 
358
    0
 
359
};
 
360
 
 
361
 
 
362
bool IsSpaceChar(wxChar c, const wxChar *spaceChars=wxT("\t "))
 
363
{
 
364
    return wxStrchr(spaceChars, c) != NULL;
 
365
}
 
366
 
 
367
 
 
368
void SkipSpace(const wxChar* &ptr, const wxChar *spaceChars=wxT("\t "))
 
369
{
 
370
    while (*ptr && IsSpaceChar(*ptr))
 
371
        ptr++;
 
372
}
 
373
 
 
374
 
 
375
void SkipNonspace(const wxChar* &ptr, const wxChar *spaceChars=wxT("\t "))
 
376
{
 
377
    while (*ptr && !IsSpaceChar(*ptr))
 
378
        ptr++;
 
379
}
 
380
 
 
381
 
 
382
pgHbaConfigLine::pgHbaConfigLine(const wxString &line)
 
383
{
 
384
    item=-1;
 
385
    connectType = PGC_INVALIDCONF;
 
386
    method = PGC_INVALIDMETHOD;
 
387
    Init(line);
 
388
}
 
389
 
 
390
 
 
391
void pgHbaConfigLine::Init(const wxString &line)
 
392
{
 
393
    connectType = PGC_INVALIDCONF;
 
394
    changed = false;
 
395
 
 
396
    if (line.IsEmpty())
 
397
        return;
 
398
 
 
399
    text = line;
 
400
 
 
401
    const wxChar *p0=line.c_str();
 
402
 
 
403
    if (*p0 == '#')
 
404
    {
 
405
        isComment = true;
 
406
        p0++;
 
407
        SkipSpace(p0);
 
408
    }
 
409
    else
 
410
        isComment = false;
 
411
 
 
412
 
 
413
    const wxChar *p1=p0;
 
414
    SkipNonspace(p1);
 
415
 
 
416
    wxString str=line.Mid(p0-line.c_str(), p1-p0);
 
417
    
 
418
    int i=FindToken(str, pgHbaConnectTypeStrings);
 
419
 
 
420
    if (i >= 0)
 
421
        connectType = (pgHbaConnectType)i;
 
422
    else
 
423
    {
 
424
        connectType = PGC_INVALIDCONF;
 
425
        isComment = true;
 
426
        return;
 
427
    }
 
428
 
 
429
    SkipSpace(p1);
 
430
 
 
431
    const wxChar *p2=p1;
 
432
    bool quoted=false;
 
433
 
 
434
    while (*p2)
 
435
    {
 
436
        if (!quoted && IsSpaceChar(*p2))
 
437
            break;
 
438
        if (*p2 == '"')
 
439
            quoted ^= quoted;
 
440
        p2++;
 
441
    }
 
442
 
 
443
    database = line.Mid(p1-line.c_str(), p2-p1);
 
444
 
 
445
    SkipSpace(p2);
 
446
 
 
447
    const wxChar *p3=p2;
 
448
 
 
449
    quoted=false;
 
450
    while (*p3)
 
451
    {
 
452
        if (!quoted && IsSpaceChar(*p3))
 
453
            break;
 
454
        if (*p3 == '"')
 
455
            quoted ^= quoted;
 
456
        p3++;
 
457
    }
 
458
 
 
459
    user = line.Mid(p2-line.c_str(), p3-p2);
 
460
 
 
461
    SkipSpace(p3);
 
462
 
 
463
    const wxChar *p4=p3;
 
464
 
 
465
    if (connectType == PGC_LOCAL)
 
466
    {
 
467
        // no ip address
 
468
    }
 
469
    else
 
470
    {
 
471
        bool hasCidr=false;
 
472
        while (*p4 && !IsSpaceChar(*p4))
 
473
        {
 
474
            if (*p4 == '/')
 
475
                hasCidr=true;
 
476
            p4++;
 
477
        }
 
478
        if (!hasCidr)
 
479
        {
 
480
            SkipSpace(p4);
 
481
            SkipNonspace(p4);
 
482
        }
 
483
 
 
484
        ipaddress = line.Mid(p3-line.c_str(), p4-p3);
 
485
        SkipSpace(p4);
 
486
    }
 
487
 
 
488
    const wxChar *p5=p4;
 
489
    SkipNonspace(p5);
 
490
 
 
491
    str=line.Mid(p4-line.c_str(), p5-p4);
 
492
 
 
493
    i = FindToken(str, pgHbaMethodStrings);
 
494
 
 
495
    if (i >= 0)
 
496
        method = (pgHbaMethod)i;
 
497
    else
 
498
    {
 
499
        connectType = PGC_INVALIDCONF;
 
500
        isComment = true;
 
501
        return;
 
502
    }
 
503
    SkipSpace(p5);
 
504
    option = p5;
 
505
}
 
506
 
 
507
 
 
508
const wxChar *pgHbaConfigLine::GetConnectType()
 
509
{
 
510
    if (connectType >= 0 && connectType < PGC_INVALIDCONF)
 
511
        return pgHbaConnectTypeStrings[connectType];
 
512
    return 0;
 
513
}
 
514
 
 
515
 
 
516
const wxChar *pgHbaConfigLine::GetMethod()
 
517
{
 
518
    if (method >= 0 && method < PGC_INVALIDMETHOD)
 
519
        return pgHbaMethodStrings[method];
 
520
    return 0;
 
521
}
 
522
 
 
523
 
 
524
wxString pgHbaConfigLine::GetText()
 
525
{
 
526
    if (!changed)
 
527
        return text;
 
528
 
 
529
    wxString str;
 
530
    wxString tabspace=wxT("\t ");
 
531
    if (isComment)
 
532
        str = wxT("# ");
 
533
 
 
534
    str += GetConnectType() 
 
535
        +  tabspace + database
 
536
        +  tabspace + user;
 
537
 
 
538
    if (connectType != PGC_LOCAL)
 
539
        str += tabspace + ipaddress;
 
540
 
 
541
    str += tabspace + GetMethod();
 
542
 
 
543
    if (method >= PGC_IDENT && !option.IsEmpty())
 
544
        str += tabspace + option;
 
545
 
 
546
    return str;
 
547
}
 
548
 
 
549
 
 
550
////////////////////////////////////////////////
 
551
pgPassConfigLine::pgPassConfigLine(const wxString &line)
 
552
{
 
553
    item=-1;
 
554
    Init(line);
 
555
}
 
556
 
 
557
void pgPassConfigLine::Init(const wxString &line)
 
558
{
 
559
        text = line;
 
560
 
 
561
    if (line.IsEmpty())
 
562
        return;
 
563
 
 
564
        isComment = line.StartsWith(wxT("#"));
 
565
 
 
566
        wxStringTokenizer tok(isComment?line.Mid(1):line, wxT(":"));
 
567
        if (tok.HasMoreTokens())
 
568
                hostname = tok.GetNextToken();
 
569
        if (tok.HasMoreTokens())
 
570
                port = tok.GetNextToken();
 
571
        if (tok.HasMoreTokens())
 
572
                database = tok.GetNextToken();
 
573
        if (tok.HasMoreTokens())
 
574
                username = tok.GetNextToken();
 
575
        if (tok.HasMoreTokens())
 
576
                password = tok.GetNextToken();
 
577
}
 
578
 
 
579
wxString pgPassConfigLine::GetText()
 
580
{
 
581
        return (isComment?wxT("#"):wxT("")) +
 
582
                   hostname + wxT(":") + 
 
583
                   port + wxT(":") +
 
584
                   database + wxT(":") +
 
585
                   username + wxT(":") +
 
586
                   password;
 
587
}