File: context.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (716 lines) | stat: -rw-r--r-- 19,291 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/*
 * ====================================================================
 * Copyright (c) 2002-2009 The RapidSvn Group.  All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (in the file GPL.txt.  
 * If not, see <http://www.gnu.org/licenses/>.
 *
 * This software consists of voluntary contributions made by many
 * individuals.  For exact contribution history, see the revision
 * history and logs, available at http://rapidsvn.tigris.org/.
 * ====================================================================
 */

/**
 * @todo implement 1.3 SVN api:
 *
 *  SVN_CLIENT_COMMIT_ITEM_LOCK_TOKEN
 *  svn_client_add3
 *  svn_client_copy2
 *  svn_client_commit3
 *  svn_client_delete2
 *  svn_client_move3
 *  svn_client_mkdir2
 *  svn_client_import2
 *  svn_client_info
 */


// Apache Portable Runtime
#include "apr_xlate.h"

// Subversion api
#include "svn_auth.h"
#include "svn_config.h"
#include "svn_subst.h"
//#include "svn_utf.h"

// svncpp
#include "kdevsvncpp/apr.hpp"
#include "kdevsvncpp/context.hpp"
#include "kdevsvncpp/context_listener.hpp"

namespace svn
{
  struct Context::Data
  {
public:
    /** The usage of Apr makes sure Apr is initialized
     * before any use of apr functions.
     */
    Apr apr;

    ContextListener * listener;
    bool logIsSet;
    int promptCounter;
    Pool pool;
    svn_client_ctx_t * ctx;
    std::string username;
    std::string password;
    std::string logMessage;
    std::string configDir;

    /**
     * translate native c-string to utf8
     */
    static svn_error_t *
    translateString(const char * str, const char ** newStr,
                    apr_pool_t * /*pool*/)
    {
      // due to problems with apr_xlate we do not perform
      // any conversion at this place. YOU will have to make
      // sure any strings passed are UTF 8 strings
      // svn_string_t *string = svn_string_create ("", pool);
      //
      // string->data = str;
      // string->len = strlen (str);
      //
      // const char * encoding = APR_LOCALE_CHARSET;
      //
      // SVN_ERR (svn_subst_translate_string (&string, string,
      //                                      encoding, pool));
      //
      // *newStr = string->data;
      *newStr = str;
      return SVN_NO_ERROR;
    }

    /**
     * the @a baton is interpreted as Data *
     * Several checks are performed on the baton:
     * - baton == 0?
     * - baton->Data
     * - listener set?
     *
     * @param data returned data if everything is OK
     * @retval SVN_NO_ERROR if everything is fine
     * @retval SVN_ERR_CANCELLED on invalid values
     */
    static svn_error_t *
    getData(void * baton, Data ** data)
    {
      if (baton == nullptr)
        return svn_error_create(SVN_ERR_CANCELLED, nullptr,
                                "invalid baton");

      Data * data_ = static_cast <Data *>(baton);

      if (data_->listener == nullptr)
        return svn_error_create(SVN_ERR_CANCELLED, nullptr,
                                "invalid listener");

      *data = data_;
      return SVN_NO_ERROR;
    }

    Data(const std::string & configDir_)
        : listener(nullptr), logIsSet(false),
        promptCounter(0), configDir(configDir_)
    {
      const char * c_configDir = nullptr;
      if (configDir.length() > 0)
        c_configDir = configDir.c_str();

      // make sure the configuration directory exists
      svn_config_ensure(c_configDir, pool);


      // initialize authentication providers
      // * simple
      // * username
      // * simple prompt
      // * ssl server trust file
      // * ssl server trust prompt
      // * ssl client cert pw file
      // * ssl client cert pw prompt
      // * ssl client cert file
      // ===================
      // 8 providers

      apr_array_header_t *providers =
        apr_array_make(pool, 8,
                       sizeof(svn_auth_provider_object_t *));
      svn_auth_provider_object_t *provider;

      svn_client_get_simple_provider(
        &provider,
        pool);
      *(svn_auth_provider_object_t **)apr_array_push(providers) =
        provider;

      svn_client_get_username_provider(
        &provider,
        pool);
      *(svn_auth_provider_object_t **)apr_array_push(providers) =
        provider;

      svn_client_get_simple_prompt_provider(
        &provider,
        onSimplePrompt,
        this,
        100000000, // not very nice. should be infinite...
        pool);
      *(svn_auth_provider_object_t **)apr_array_push(providers) =
        provider;

      // add ssl providers

      // file first then prompt providers
      svn_client_get_ssl_server_trust_file_provider(&provider, pool);
      *(svn_auth_provider_object_t **)apr_array_push(providers) =
        provider;

      svn_client_get_ssl_client_cert_file_provider(&provider, pool);
      *(svn_auth_provider_object_t **)apr_array_push(providers) =
        provider;

      svn_client_get_ssl_client_cert_pw_file_provider(&provider, pool);
      *(svn_auth_provider_object_t **)apr_array_push(providers) =
        provider;

      svn_client_get_ssl_server_trust_prompt_provider(
        &provider, onSslServerTrustPrompt, this, pool);
      *(svn_auth_provider_object_t **)apr_array_push(providers) =
        provider;

      // plugged in 3 as the retry limit - what is a good limit?
      svn_client_get_ssl_client_cert_pw_prompt_provider(
        &provider, onSslClientCertPwPrompt, this, 3, pool);
      *(svn_auth_provider_object_t **)apr_array_push(providers) =
        provider;

      svn_auth_baton_t *ab;
      svn_auth_open(&ab, providers, pool);

      // initialize ctx structure
      svn_client_create_context(&ctx, pool);

      // get the config based on the configDir passed in
      svn_config_get_config(&ctx->config, c_configDir, pool);

      // disable external diff and diff3 commands
      svn_config_t *config = (svn_config_t *)apr_hash_get(
        ctx->config, SVN_CONFIG_CATEGORY_CONFIG, APR_HASH_KEY_STRING);
      svn_config_set(config, SVN_CONFIG_SECTION_HELPERS,
                     SVN_CONFIG_OPTION_DIFF_CMD, nullptr);
      svn_config_set(config, SVN_CONFIG_SECTION_HELPERS,
                     SVN_CONFIG_OPTION_DIFF3_CMD, nullptr);

      // tell the auth functions where the config is
      svn_auth_set_parameter(ab, SVN_AUTH_PARAM_CONFIG_DIR,
                             c_configDir);

      ctx->auth_baton = ab;
      ctx->log_msg_func = onLogMsg;
      ctx->log_msg_baton = this;
      ctx->notify_func = onNotify;
      ctx->notify_baton = this;
      ctx->cancel_func = onCancel;
      ctx->cancel_baton = this;

      ctx->notify_func2 = onNotify2;
      ctx->notify_baton2 = this;
    }

    void setAuthCache(bool value)
    {
      void *param = nullptr;
      if (!value)
        param = (void *)"1";

      svn_auth_set_parameter(ctx->auth_baton,
                             SVN_AUTH_PARAM_NO_AUTH_CACHE,
                             param);
    }

    /** @see Context::setLogin */
    void setLogin(const char * usr, const char * pwd)
    {
      username = usr;
      password = pwd;

      svn_auth_baton_t * ab = ctx->auth_baton;
      svn_auth_set_parameter(ab, SVN_AUTH_PARAM_DEFAULT_USERNAME,
                             username.c_str());
      svn_auth_set_parameter(ab, SVN_AUTH_PARAM_DEFAULT_PASSWORD,
                             password.c_str());

    }

    /** @see Context::setLogMessage */
    void setLogMessage(const char * msg)
    {
      logMessage = msg;
      logIsSet = true;
    }

    /**
     * this function gets called by the subversion api function
     * when a log message is needed. This is the case on a commit
     * for example
     */
    static svn_error_t *
    onLogMsg(const char **log_msg,
             const char **tmp_file,
             apr_array_header_t *, //UNUSED commit_items
             void *baton,
             apr_pool_t * pool)
    {
      Data * data = nullptr;
      SVN_ERR(getData(baton, &data));

      std::string msg;
      if (data->logIsSet)
        msg = data->getLogMessage();
      else
      {
        if (!data->retrieveLogMessage(msg))
          return svn_error_create(SVN_ERR_CANCELLED, nullptr, "");
      }

      *log_msg = apr_pstrdup(pool, msg.c_str());

      *tmp_file = nullptr;

      return SVN_NO_ERROR;
    }

    /**
     * this is the callback function for the subversion
     * api functions to signal the progress of an action
     */
    static void
    onNotify(void * baton,
             const char *path,
             svn_wc_notify_action_t action,
             svn_node_kind_t kind,
             const char *mime_type,
             svn_wc_notify_state_t content_state,
             svn_wc_notify_state_t prop_state,
             svn_revnum_t revision)
    {
      if (baton == nullptr)
        return;

      Data * data = static_cast <Data *>(baton);

      data->notify(path, action, kind, mime_type, content_state,
                   prop_state, revision);
    }


    /**
     * this is the callback function for the subversion 1.2
     * api functions to signal the progress of an action
     *
     * @todo right now we forward only to @a onNotify,
     *       but maybe we should a notify2 to the listener
     * @since subversion 1.2
     */
    static void
    onNotify2(void*baton,const svn_wc_notify_t *action,apr_pool_t *)
    {
      if (!baton)
        return;

      // for now forward the call to @a onNotify
      onNotify(baton, action->path, action->action,
               action->kind, action->mime_type,
               action->content_state, action->prop_state,
               action->revision);
    }

    /**
     * this is the callback function for the subversion
     * api functions to signal the progress of an action
     */
    static svn_error_t *
    onCancel(void * baton)
    {
      if (baton == nullptr)
        return SVN_NO_ERROR;

      Data * data = static_cast <Data *>(baton);

      if (data->cancel())
        return svn_error_create(SVN_ERR_CANCELLED, nullptr, "cancelled by user");
      else
        return SVN_NO_ERROR;
    }

    /**
     * @see svn_auth_simple_prompt_func_t
     */
    static svn_error_t *
    onSimplePrompt(svn_auth_cred_simple_t **cred,
                   void *baton,
                   const char *realm,
                   const char *username,
                   svn_boolean_t _may_save,
                   apr_pool_t *pool)
    {
      Data * data = nullptr;
      SVN_ERR(getData(baton, &data));

      bool may_save = _may_save != 0;
      if (!data->retrieveLogin(username, realm, may_save))
        return svn_error_create(SVN_ERR_CANCELLED, nullptr, "");

      svn_auth_cred_simple_t* lcred = (svn_auth_cred_simple_t*)
                                      apr_palloc(pool, sizeof(svn_auth_cred_simple_t));
      /*      SVN_ERR (svn_utf_cstring_to_utf8 (
                       &lcred->password,
                       data->getPassword (), pool));
            SVN_ERR (svn_utf_cstring_to_utf8 (
                       &lcred->username,
                       data->getUsername (), pool)); */
      lcred->password = data->getPassword();
      lcred->username = data->getUsername();

      // tell svn if the credentials need to be saved
      lcred->may_save = may_save;
      *cred = lcred;

      return SVN_NO_ERROR;
    }

    /**
     * @see svn_auth_ssl_server_trust_prompt_func_t
     */
    static svn_error_t *
    onSslServerTrustPrompt(svn_auth_cred_ssl_server_trust_t **cred,
                           void *baton,
                           const char *realm,
                           apr_uint32_t failures,
                           const svn_auth_ssl_server_cert_info_t *info,
                           svn_boolean_t may_save,
                           apr_pool_t *pool)
    {
      Data * data = nullptr;
      SVN_ERR(getData(baton, &data));

      ContextListener::SslServerTrustData trustData(failures);
      if (realm != nullptr)
        trustData.realm = realm;
      trustData.hostname = info->hostname;
      trustData.fingerprint = info->fingerprint;
      trustData.validFrom = info->valid_from;
      trustData.validUntil = info->valid_until;
      trustData.issuerDName = info->issuer_dname;
      trustData.maySave = may_save != 0;

      apr_uint32_t acceptedFailures;
      ContextListener::SslServerTrustAnswer answer =
        data->listener->contextSslServerTrustPrompt(
          trustData, acceptedFailures);

      if (answer == ContextListener::DONT_ACCEPT)
        *cred = nullptr;
      else
      {
        svn_auth_cred_ssl_server_trust_t *cred_ =
          (svn_auth_cred_ssl_server_trust_t*)
          apr_palloc(pool, sizeof(svn_auth_cred_ssl_server_trust_t));

        if (answer == ContextListener::ACCEPT_PERMANENTLY)
        {
          cred_->may_save = 1;
          cred_->accepted_failures = acceptedFailures;
        }
        *cred = cred_;
      }

      return SVN_NO_ERROR;
    }

    /**
     * @see svn_auth_ssl_client_cert_prompt_func_t
     */
    static svn_error_t *
    onSslClientCertPrompt(svn_auth_cred_ssl_client_cert_t **cred,
                          void *baton,
                          apr_pool_t *pool)
    {
      Data * data = nullptr;
      SVN_ERR(getData(baton, &data));

      std::string certFile;
      if (!data->listener->contextSslClientCertPrompt(certFile))
        return svn_error_create(SVN_ERR_CANCELLED, nullptr, "");

      svn_auth_cred_ssl_client_cert_t *cred_ =
        (svn_auth_cred_ssl_client_cert_t*)
        apr_palloc(pool, sizeof(svn_auth_cred_ssl_client_cert_t));

      /*      SVN_ERR (svn_utf_cstring_to_utf8 (
                       &cred_->cert_file,
                       certFile.c_str (),
                       pool)); */
      cred_->cert_file = certFile.c_str();

      *cred = cred_;

      return SVN_NO_ERROR;
    }

    /**
     * @see svn_auth_ssl_client_cert_pw_prompt_func_t
     */
    static svn_error_t *
    onSslClientCertPwPrompt(
      svn_auth_cred_ssl_client_cert_pw_t **cred,
      void *baton,
      const char *realm,
      svn_boolean_t maySave,
      apr_pool_t *pool)
    {
      Data * data = nullptr;
      SVN_ERR(getData(baton, &data));

      std::string password;
      bool may_save = maySave != 0;
      if (!data->listener->contextSslClientCertPwPrompt(password, realm, may_save))
        return svn_error_create(SVN_ERR_CANCELLED, nullptr, "");

      svn_auth_cred_ssl_client_cert_pw_t *cred_ =
        (svn_auth_cred_ssl_client_cert_pw_t *)
        apr_palloc(pool, sizeof(svn_auth_cred_ssl_client_cert_pw_t));

      /*      SVN_ERR (svn_utf_cstring_to_utf8 (
                       &cred_->password,
                       password.c_str (),
                       pool)); */
      cred_->password = password.c_str();

      cred_->may_save = may_save;
      *cred = cred_;

      return SVN_NO_ERROR;
    }

    const char *
    getUsername() const
    {
      return username.c_str();
    }

    const char *
    getPassword() const
    {
      return password.c_str();
    }

    const char *
    getLogMessage() const
    {
      return logMessage.c_str();
    }

    /**
     * if the @a listener is set, use it to retrieve the log
     * message using ContextListener::contextGetLogMessage.
     * This return values is given back, then.
     *
     * if the @a listener is not set the its checked whether
     * the log message has been set using @a setLogMessage
     * yet. If not, return false otherwise true
     *
     * @param msg log message
     * @retval false cancel
     */
    bool
    retrieveLogMessage(std::string & msg)
    {
      bool ok;

      if (listener == nullptr)
        return false;

      ok = listener->contextGetLogMessage(logMessage);
      if (ok)
        msg = logMessage;
      else
        logIsSet = false;

      return ok;
    }

    /**
     * if the @a listener is set and no password has been
     * set yet, use it to retrieve login and password using
     * ContextListener::contextGetLogin.
     *
     * if the @a listener is not set, check if setLogin
     * has been called yet.
     *
     * @return continue?
     * @retval false cancel
     */
    bool
    retrieveLogin(const char * username_,
                  const char * realm,
                  bool &may_save)
    {
      bool ok;

      if (listener == nullptr)
        return false;

      if (username_ == nullptr)
        username = "";
      else
        username = username_;

      ok = listener->contextGetLogin(realm, username, password, may_save);

      return ok;
    }

    /**
     * if the @a listener is set call the method
     * @a contextNotify
     */
    void
    notify(const char *path,
           svn_wc_notify_action_t action,
           svn_node_kind_t kind,
           const char *mime_type,
           svn_wc_notify_state_t content_state,
           svn_wc_notify_state_t prop_state,
           svn_revnum_t revision)
    {
      if (listener != nullptr)
      {
        listener->contextNotify(path, action, kind, mime_type,
                                content_state, prop_state, revision);
      }
    }

    /**
     * if the @a listener is set call the method
     * @a contextCancel
     */
    bool
    cancel()
    {
      if (listener != nullptr)
      {
        return listener->contextCancel();
      }
      else
      {
        // don't cancel if no listener
        return false;
      }
    }
  };

  Context::Context(const std::string &configDir)
  {
    m = new Data(configDir);
  }

  Context::Context(const Context & src)
  {
    m = new Data(src.m->configDir);
    setLogin(src.getUsername(), src.getPassword());
  }

  Context::~Context()
  {
    delete m;
  }

  void
  Context::setAuthCache(bool value)
  {
    m->setAuthCache(value);
  }

  void
  Context::setLogin(const char * username, const char * password)
  {
    m->setLogin(username, password);
  }

  Context::operator svn_client_ctx_t * ()
  {
    return m->ctx;
  }

  svn_client_ctx_t *
  Context::ctx()
  {
    return m->ctx;
  }

  void
  Context::setLogMessage(const char * msg)
  {
    m->setLogMessage(msg);
  }

  const char *
  Context::getUsername() const
  {
    return m->getUsername();
  }

  const char *
  Context::getPassword() const
  {
    return m->getPassword();
  }

  const char *
  Context::getLogMessage() const
  {
    return m->getLogMessage();
  }

  void
  Context::setListener(ContextListener * listener)
  {
    m->listener = listener;
  }

  ContextListener *
  Context::getListener() const
  {
    return m->listener;
  }

  void
  Context::reset()
  {
    m->promptCounter = 0;
    m->logIsSet = false;
  }
}

/* -----------------------------------------------------------------
 * local variables:
 * eval: (load-file "../../rapidsvn-dev.el")
 * end:
 */