Codebase list cyrus-sasl2 / upstream/2.1.24_rc1.dfsg1+cvs2011-05-23 doc / programming.html
upstream/2.1.24_rc1.dfsg1+cvs2011-05-23

Tree @upstream/2.1.24_rc1.dfsg1+cvs2011-05-23 (Download .tar.gz)

programming.html @upstream/2.1.24_rc1.dfsg1+cvs2011-05-23raw · history · blame

   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
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta name="generator" content="HTML Tidy, see www.w3.org">
<title>SASL Application Programmer's Guide</title>
</head>
<body>
<h1>SASL Application Programmer's Guide</h1>

<h3>NOTE: This is a work in progress. Any contributions would be
<i>very</i> appreciated</h3>

<h2>Contents</h2>

<ul>
<li><a href="#intro">Introduction</a> 

<ul>
<li><a href="#about_this_guide">About this guide</a></li>

<li><a href="#what_is_sasl">What is SASL?</a></li>
</ul>
</li>

<li><a href="#background">Background</a> 

<ul>
<li><a href="#world_before">How did the world work before
SASL?</a></li>

<li><a href="#sasl_rescue">SASL to the rescue!</a></li>
</ul>
</li>

<li><a href="#briefly">Briefly</a> 

<ul>
<li><a href="#what_good_for">What is the Cyrus SASL library good
for?</a></li>

<li><a href="#what_do">What does the Cyrus SASL library
do?</a></li>

<li><a href="#what_doesnt">What doesn't the Cyrus SASL library
do?</a></li>
</ul>
</li>

<li><a href="#client_only">Client-only Section</a> 

<ul>
<li><a href="#client_typical">A typical interaction from the
client's perspective</a></li>

<li><a href="#client_code">How does this look in code?</a></li>

<li><a href="#sasl_client_init">sasl_client_init</a></li>

<li><a href="#sasl_client_new">sasl_client_new</a></li>

<li><a href="#sasl_client_start">sasl_client_start</a></li>

<li><a href="#sasl_client_step">sasl_client_step</a></li>
</ul>
</li>

<li><a href="#server_section">Server-only Section</a> 

<ul>
<li><a href="#server_typical">A typical interaction from the
server's perspective</a></li>

<li><a href="#server_code">How does this look in code?</a></li>

<li><a href="#sasl_server_init">sasl_server_init</a></li>

<li><a href="#sasl_server_new">sasl_server_new</a></li>

<li><a href="#sasl_server_start">sasl_server_start</a></li>

<li><a href="#sasl_server_step">sasl_server_step</a></li>

<li><a href="#sasl_listmech">sasl_listmech</a></li>

<li><a href="#sasl_checkpass">sasl_checkpass</a></li>
</ul>
</li>

<li><a href="#common_section">Common Section</a> 

<ul>
<li><a href="#callbacks_interactions">Callbacks and
Interaction</a></li>

<li><a href="#layers">Security layers</a></li>
</ul>
</li>

<li><a href="#example_section">Example applications that come with
the Cyrus SASL library</a> 

<ul>
<li><a href="#sample_client"><tt>sample-client</tt> and
<tt>sample-server</tt></a></li>

<li><a href="#cyrus_imapd">Cyrus imapd v2.1.0 or later</a></li>

<li><a href="#imtest"><tt>imtest</tt>, from cyrus imapd 2.1.0 or
later</a></li>
</ul>
</li>

<li><a href="#random_things">Miscellaneous Information</a> 

<ul>
<li><a href="#empty_exchanges">Empty exchanges</a></li>
</ul>
</li>
</ul>

<h2><a name="intro">Introduction</a></h2>

<h3><a name="about_this_guide">About this Guide</a></h3>

<p>This guide gives a tutorial on the use of the Cyrus SASL library
for a client or server application. It complies with versions
including and after 2.0.0. The following pages should only be
considered a guide, not the final word on programming with the
Cyrus SASL library. Consult the header files in the distribution in
the case of ambiguities.</p>

<h3><a name="what_is_sasl">What is SASL?</a></h3>

<p>SASL stands for Simple Authentication Security Layer and is
explained in <a href="http://ftp.ietf.org/rfc/rfc2222.txt">RFC
2222</a>. That document is very difficult to understand however and
it should be unnecessary to consult it.</p>

<h2><a name="background">Background</a></h2>

<h3><a name="world_before">How did the world work before
SASL?</a></h3>

<p>Before SASL, when a new protocol was written which required
authentication (users proving who they are to an entity), the
protocol had to allow explicitly for each individual authentication
mechanism. There had to be a distinct way to say "I want to log in
with Kerberos V4". There had to be another distinct way to say "I
want to log in with CRAM-MD5". There had to be yet a different way
to say "I want to log in anonymously," and so on. This was
non-ideal for both the protocol and application writers.</p>

<p>Additionally, many programmers were not very familiar with
security, so the protocol did support many mechanisms, or worse,
they were supported incorrectly. Moreover, when a new
authentication method was invented the protocol needed to be
modified to support that mechanism.</p>

<p>This system also was not ideal for application writer. She had
to have a special case for each mechanism she wished her
application to support. Also, the mechanisms were difficult to
implement. Even with a good library, an understanding of how the
mechanism worked was still necessary. Finally if an application
used more than one protocol (for example a mail client might use
IMAP, POP, and SMTP) then "Kerberos V4 for IMAP", "Kerberos V4 for
POP", "Kerberos V4 for SMTP", "CRAM MD5 for IMAP", "CRAM-MD5 for
POP", etc... would need to be written. This could quickly create a
huge number of different mechanism-protocol pairs to implement.</p>

<h3><a name="sasl_rescue">SASL to the rescue!</a></h3>

<p>SASL hopefully solves all these problems. In practice it makes
many of them easier to deal with.</p>

<p>Protocol designers simply have to support SASL (in particular
RFC 2222). Consequently, any mechanism that supports SASL (just
about anything you would want to use does now) is supported by the
protocol. If a new authentication mechanism is invented the
protocol automatically supports it without any modifications.</p>

<p>Application writers, instead of having to support every
mechanism for every protocol, only need to support SASL for every
protocol. Application writers do not need to understand the
authentication mechanisms at all: the SASL library handles all
that. Also with the Cyrus SASL library if a new mechanism is
invented you do not have rewrite your application at all. You may
not even have to restart your application if it is a long running
process. This is because the Cyrus SASL library loads each
mechanism from a shared library. Simply copying a shared library
into a directory will magically make your application support a new
mechanism.</p>

<p>Cyrus SASL version 2 supports a much improved API over version
1, that allows for much smarter and faster memory allocation for
the mechanisms as well as the applications. It is also provides for
several new types of plugins to allow for greater overall
flexibility. Unfortunately, though similar, this new API is
completely incompatible with the old API, and applications will
need to be rewritten.</p>

<h2><a name="briefly">Briefly</a></h2>

<h3><a name="what_good_for">What is the Cyrus SASL library good
for?</a></h3>

<p>The Cyrus SASL library is good for applications that wish to use
protocols that support SASL authentication. An non-exhaustive list
of these are: IMAP, SMTP, ACAP, and LDAP. Also if you are making a
proprietary system and wish to support authentication it is a good
way of supporting many different authentication types.</p>

<h3><a name="what_do">What does the Cyrus SASL library do?</a></h3>

<p>From a client point of view, the Cyrus SASL library, given a
list of mechanisms the server supports it will decide the best
mechanism to use and tell you what to send to the server at each
step of the authentication. From a server perspective, it handles
authentication requests from clients.</p>

<h3><a name="what_doesnt">What doesn't the Cyrus SASL library
do?</a></h3>

<p>The Cyrus SASL library is neither network nor protocol aware. It
is up to the application to send the data over the wire as well as
to send the data in the protocol specific manner. With IMAP this
means putting it in the form: <tt>+ [base64'ed data]\r\n</tt>. LDAP
just sends data in binary via bind requests. The Cyrus SASL library
has utility base64 encode and decode routines to help with
this.</p>

<h2><a name="client_section">Client-only Section</a></h2>

<h3><a name="client_typical">A typical interaction from the
client's perspective</a></h3>

<ol>
<li>A client makes a few calls (explained later) to initialize
SASL.</li>

<li>Every time the client application makes a new connection it
should make a new context that is kept for the life of the
connection.</li>

<li>Ask the server for the list of supported mechanisms</li>

<li>Feed this list to the library</li>

<li>Start the authentication with the mechanism the library
chose</li>

<li>The server will return some bytes</li>

<li>Give these to the library</li>

<li>The library returns some bytes to the application</li>

<li>Application sends these bytes over the network</li>

<li>repeat the last 4 steps until the server tells you that the
authentication is completed</li>
</ol>

<h3><a name="client_code">How does this look in code</a></h3>

<b>Initialize the library</b>. (done once). 

<pre>
    
        int result;

        /* attempt to start sasl 
         * See the section on Callbacks and Interactions for an 
         * explanation of the variable callbacks
         */ 

        result=sasl_client_init(callbacks);
            
            /* check to see if that worked */
            if (result!=SASL_OK) [failure]
    
</pre>

<b>For every network connection, make a new SASL connection</b>: 

<pre>
       
            /* The SASL context kept for the life of the connection */
            sasl_conn_t *conn;

 
            /* client new connection */
            result=sasl_client_new("imap",     /* The service we are using */
                       serverFQDN, /* The fully qualified domain
                                                  name of the server we're
                                                  connecting to */
                       NULL, NULL, /* Local and remote IP
                                                  address strings
                                                  (NULL disables mechanisms
                                                   which require this info)*/
                                   NULL,       /* connection-specific
                                                  callbacks */
                       0,          /* security flags */
                       &amp;conn);     /* allocated on success */

            /* check to see if that worked */
            if (result!=SASL_OK) [failure]

     
</pre>

Next get the list of SASL mechanisms the server supports. This is
usually done through a capability command. Format the list as a
single string separated by spaces. Feed this string into SASL to
begin the authentication process. 

<pre>
       
            sasl_interact_t *client_interact=NULL;
            const char *out, *mechusing;
            unsigned outlen;

            do {

              result=sasl_client_start(conn,      /* the same context from
                                                     above */ 
                                       mechlist,  /* the list of mechanisms
                                                     from the server */
                                       &amp;client_interact, /* filled in if an
                                                            interaction is needed */
                       &amp;out,      /* filled in on success */
                                       &amp;outlen,   /* filled in on success */
                       &amp;mechusing);

              if (result==SASL_INTERACT)
              {
                 [deal with the interactions. See interactions section below]
              }


           } while (result==SASL_INTERACT); /* the mechanism may ask us to fill
                                               in things many times. result is 
                                               SASL_CONTINUE on success */
           if (result!=SASL_CONTINUE) [failure]

       
</pre>

Note that you do not need to worry about the allocation and freeing
of the output buffer out. This is all handled inside of the
mechanism. It is important to note, however, that the output buffer
is not valid after the next call to <tt>sasl_client_start</tt> or
<tt>sasl_client_step</tt>.

<p>If this is successful send the protocol specific command to
start the authentication process. This may or may not allow for
initial data to be sent (see the documentation of the protocol to
see).</p>

<pre>
        For IMAP this might look like:
          {tag} "AUTHENTICATE" {mechusing}\r\n
          A01 AUTHENTICATE KERBEROS_V4\r\n
           
        SMTP looks like:
         "AUTH" {mechusing}[ {out base64 encoded}]
         AUTH DIGEST-MD5 GHGJJGDDFDKHGHJG=
       
</pre>

<br>
 <a name="client_authentication_step"><b>Check Results</b></a> <br>
 Next, read what the server sent back. It can be one of three
things: 

<ol>
<li>Authentication failure. Authentication process is halted. This
might look like <tt>A01 NO Authentication failure</tt> in IMAP or
<tt>501 Failed</tt> in SMTP. Either retry the authentication or
abort.</li>

<li>Authentication success. We're now successfully authenticated.
This might look like <tt>A01 OK Authenticated successful</tt> in
IMAP or <tt>235 Authentication successful</tt> in SMTP. Go <a href=
"#client_authentication_success">here</a></li>

<li>Another step in the authentication process is necessary. This
might look like <tt>+ HGHDS1HAFJ=</tt> in IMAP or <tt>334
PENCeUxFREJoU0NnbmhNWitOMjNGNndAZWx3b29kLmlubm9zb2Z0LmNvbT4=</tt>
in SMTP. Note it could be an empty string such as <tt>+ \r\n</tt>
in IMAP.</li>
</ol>

Convert the continuation data to binary format (for example, this
may include base64 decoding it). Perform another step in the
authentication. 

<pre>
              do {
                result=sasl_client_step(conn,  /* our context */
                        in,    /* the data from the server */
                        inlen, /* it's length */
                        &amp;client_interact,  /* this should be
                                                              unallocated and NULL */
                        &amp;out,     /* filled in on success */
                        &amp;outlen); /* filled in on success */

                if (result==SASL_INTERACT)
                {
                   [deal with the interactions. See below]
                }


              } while (result==SASL_INTERACT || result == SASL_CONTINUE);

              if (result!=SASL_OK) [failure]
              
         
</pre>

Format the output (variable out of length outlen) in the protocol
specific manner and send it across the network to the server. <br>
 Goto <a href="#client_authentication_step">here</a> (this process
repeats until authentication either succeeds or fails. <br>
<br>
<br>
 <a name="client_authentication_success"><b>Authentication
Successful</b></a><br>
<br>
 

<p>Before we're done we need to call sasl_client_step() one more
time to make sure the server isn't trying to fool us. Some
protocols include data along with the last step. If so this data
should be used here. If not use a length of zero.</p>

<pre>
                result=sasl_client_step(conn,  /* our context */
                        in,    /* the data from the server */
                        inlen, /* it's length */
                        &amp;client_interact,  /* this should be unallocated and NULL */
                        &amp;out,     /* filled in on success */
                        &amp;outlen); /* filled in on success */

                if (result!=SASL_OK) [failure]
       
</pre>

<p>Congratulations. You have successfully authenticated to the
server.</p>

<p>Don't throw away the SASL connection object (sasl_conn_t *) yet
though. If a security layer was negotiated you will need it to
encode and decode the data sent over the network.</p>

<br>
 When you are finally done with connection to server, dispose of
SASL connection. 

<pre>
      
               sasl_dispose(&amp;conn);

         
</pre>

If you are done with SASL forever (application quiting for
example): 

<pre>
            sasl_done();         
     
</pre>

<h3>sasl_client_init</h3>

<pre>
    int sasl_client_init(const sasl_callback_t *callbacks);
   
</pre>

Parameters: 

<ul>
<li>callbacks - List of callbacks. See Callbacks section</li>
</ul>

This function initializes the SASL library. This must be called
before any other SASL calls. See the callbacks section for complete
description of callbacks. 

<h3>sasl_client_new</h3>

<pre>
    int sasl_client_new(const char *service,
                const char *serverFQDN,
                        const char *iplocalport,
                        const char *ipremoteport,
                const sasl_callback_t *prompt_supp,
                unsigned secflags,
                sasl_conn_t **pconn);
   
</pre>

Parameters: 

<ul>
<li>service - the service name being used. This usually is the
protocol name (e.g. "ldap")</li>

<li>serverFQDN - Fully qualified domain name of server</li>

<li>iplocalport and ipremoteport - a string of the format
"a.b.c.d;p" detailing the local or remote IP and port, or NULL
(which will disable mechanisms that require this information)</li>

<li>prompt_supp - List off callbacks specific to this
connection</li>

<li>secflags - security flags ORed together requested (e.g.
SASL_SEC_NOPLAINTEXT)</li>

<li>pconn - the SASL connection object allocated upon success</li>
</ul>

This function creates a new SASL connection object. It should be
called once for every connection you want to authenticate for. 

<h3>sasl_client_start</h3>

<pre>
    int sasl_client_start(sasl_conn_t *conn,
                  const char *mechlist,
              sasl_interact_t **prompt_need,
              const char **clientout,
              unsigned *clientoutlen,
              const char **mech);
   
</pre>

Parameters: 

<ul>
<li>conn - the SASL connection object gotten from
sasl_client_new()</li>

<li>mechlist - the list of mechanisms to try (separated by
spaces)</li>

<li>prompt_need - filled in when a SASL_INTERACT is returned</li>

<li>clientout - filled in upon success with data to send to
server</li>

<li>clientoutlen - length of that data</li>

<li>mech - filled in with mechanism being used</li>
</ul>

This function starts an authentication session. It takes a list of
possible mechanisms (usually gotten from the server through a
capability command) and chooses the "best" mechanism to try. Upon
success clientout points at data to send to the server. 

<h3>sasl_client_step</h3>

<pre>
    int sasl_client_step(sasl_conn_t *conn,
         const char *serverin,
         unsigned serverinlen,
         sasl_interact_t **prompt_need,
         const char **clientout,
         unsigned *clientoutlen);
   
</pre>

Parameters: 

<ul>
<li>conn - the SASL connection object gotten from
sasl_client_new()</li>

<li>serverin - data from the server</li>

<li>serverinlen - length of data from the server</li>

<li>prompt_need - filled in with a SASL_INTERACT is returned</li>

<li>clientout - filled in upon success with data to send to
server</li>

<li>clientoutlen - length of that data</li>
</ul>

This step preforms a step in the authentication process. It takes
the data from the server (serverin) and outputs data to send to the
server (clientout) upon success. SASL_CONTINUE is returned if
another step in the authentication process is necessary. SASL_OK is
returned if we're all done. 

<h2><a name="server_section">Server-only Section</a></h2>

<h3><a name="server_typical">A typical interaction from the
server's perspective</a></h3>

The server makes a few Cyrus SASL calls for initialization. When it
gets a new connection it should make a new context for that
connection immediately. The client may then request a list of
mechanisms the server supports. The client also may request to
authenticate at some point. The client will specify the mechanism
it wishes to use. The server should negotiate this authentication
and keep around the context afterwards for encoding and decoding
the layers. 

<h3><a name="server_code">How does this look in code?</a></h3>

Initialization (done once). The application name is used for
reading configuration information. 

<pre>
    
    int result;

    /* Initialize SASL */
    result=sasl_server_init(callbacks,      /* Callbacks supported */
                            "TestServer");  /* Name of the application */

   
</pre>

This should be called for each new connection. It probably should
be called right when the socket is accepted. 

<pre>
    sasl_conn_t *conn;
    int result;

    /* Make a new context for this connection */
    result=sasl_server_new("smtp", /* Registered name of service */
                   NULL, /* my fully qualified domain name; 
                        NULL says use gethostname() */
                           NULL, /* The user realm used for password
                        lookups; NULL means default to serverFQDN
                                    Note: This does not affect Kerberos */
                       NULL, NULL, /* IP Address information strings */
                   NULL, /* Callbacks supported only for this connection */
                       0, /* security flags (security layers are enabled
                               * using security properties, separately)
               &amp;conn);

   
</pre>

When a client requests the list of mechanisms supported by the
server. This particular call might produce the string: <i>"{PLAIN,
KERBEROS_V4, CRAM-MD5, DIGEST-MD5}"</i> 

<pre>
    result=sasl_listmech(conn,  /* The context for this connection */
             NULL,  /* not supported */
             "{",   /* What to prepend the string with */
             ", ",  /* What to separate mechanisms with */
             "}",   /* What to append to the string */
             &amp;result_string, /* The produced string. */
                         &amp;string_length, /* length of the string */
                         &amp;number_of_mechanisms); /* Number of mechanisms in
                                                the string */
    
   
</pre>

When a client requests to authenticate: 

<pre>
    int result;
    const char *out;
    unsigned outlen;

    result=sasl_server_start(conn, /* context */
                             mechanism_client_chose,
                             clientin,    /* the optional string the client gave us */
                             clientinlen, /* and it's length */
                             &amp;out, /* The output of the library.
                                      Might not be NULL terminated */
                             &amp;outlen);

    if ((result!=SASL_OK) &amp;&amp; (result!=SASL_CONTINUE))
      [failure. Send protocol specific message that says authentication failed]
    else if (result==SASL_OK)
      [authentication succeeded. Send client the protocol specific message 
       to say that authentication is complete]
    else 
      [send data 'out' with length 'outlen' over the network in protocol
       specific format]
   
</pre>

When a response is returned by the client. <i>clientin</i> is the
data from the client decoded from protocol specific format to a
string of bytes of length <i>clientinlen</i>. This step may occur
zero or more times. An application must be able to deal with it
occurring an arbitrary number of times. 

<pre>
    int result;
   
    result=sasl_server_step(conn,
                            clientin,      /* what the client gave */
                            clientinlen,   /* it's length */
                            &amp;out,          /* allocated by library on success. 
                                              Might not be NULL terminated */
                            &amp;outlen);

    if ((result!=SASL_OK) &amp;&amp; (result!=SASL_CONTINUE))
      [failure. Send protocol specific message that says authentication failed]
    else if (result==SASL_OK)
      [authentication succeeded. Send client the protocol specific message 
       to say that authentication is complete]
    else 
      [send data 'out' with length 'outlen' over the network in protocol
       specific format]
   
</pre>

This continues until authentication succeeds. When the connection
is concluded, make a call to <tt>sasl_dispose</tt> as with the
client connection. 

<h3><a name="sasl_server_init">sasl_server_init</a></h3>

<pre>
    int sasl_server_init(const sasl_callback_t *callbacks,
                         const char *appname);
</pre>

Parameters: 

<ul>
<li>callbacks - A list of callbacks supported by the application
(see Interaction and Callbacks section)</li>

<li>appname - A string of the name of the application. This string
is what is used when loading configuration options.</li>
</ul>

sasl_server_init() initializes the session. This should be the
first function called. In this function the shared library
authentication mechanisms are loaded. 

<h3><a name="sasl_server_new">sasl_server_new</a></h3>

<pre>
    int sasl_server_new(const char *service,
            const char *serverFQDN,
            const char *user_realm,
                        const char *iplocalport,
                        const char *ipremoteport,
            const sasl_callback_t *callbacks,
            unsigned secflags,
            sasl_conn_t **pconn);
</pre>

Parameters: 

<ul>
<li>service - The name of the service you are supporting. This
might be "acap" or "smtp". This is used by Kerberos mechanisms and
possibly other mechanisms. It is also used for PAM
authentication.</li>

<li>serverFQDN - This is the fully qualified domain name of the
server (i.e. your hostname); if NULL, the library calls
<tt>gethostbyname()</tt>.</li>

<li>user_realm - The realm the connected client is in. The Kerberos
mechanisms ignore this parameter and default to the local Kerberos
realm. A value of NULL makes the library default, usually to the
serverFQDN; a value of "" specifies that the client should specify
the realm; this also changes the semantics of "@" in a username for
mechanisms that don't support realms.</li>

<li>iplocalport and ipremoteport - a string of the format
"a.b.c.d;p" detailing the local or remote IP and port, or NULL
(which will disable mechanisms that require this information)</li>

<li>callbacks - Additional callbacks that you wish only to apply to
this connection.</li>

<li>secflags - security flags.</li>

<li>pconn - Context. Filled in on success.</li>
</ul>

<h3><a name="sasl_server_start">sasl_server_start</a></h3>

<pre>
     int sasl_server_start(sasl_conn_t *conn,
               const char *mech,
               const char *clientin,
               unsigned clientinlen,
               const char **serverout,
               unsigned *serveroutlen);
   
</pre>

Parameters: 

<ul>
<li>conn - The context for the connection</li>

<li>mech - The authentication mechanism the client wishes to try
(e.g. <tt>KERBEROS_V4</tt>)</li>

<li>clientin - Initial client challenge bytes. Note: some protocols
do not allow this. If this is the case passing NULL is valid</li>

<li>clientinlen - The length of the challenge. 0 is there is
none.</li>

<li>serverout - allocated and filled in by the function. These are
the bytes that should be encoded as per the protocol and sent over
the network back to the client.</li>

<li>serveroutlen - length of bytes to send to client</li>
</ul>

This function begins the authentication process with a client. If
the program returns SASL_CONTINUE that means <tt>serverout</tt>
should be sent to the client. If SASL_OK is returned that means
authentication is complete and the application should tell the
client the authentication was successful. Any other return code
means the authentication failed and the client should be notified
of this. 

<h3><a name="sasl_server_step">sasl_server_step</a></h3>

<pre>
    int sasl_server_step(sasl_conn_t *conn,
                 const char *clientin,
                 unsigned clientinlen,
                 const char **serverout,
                 unsigned *serveroutlen);
   
</pre>

Parameters: 

<ul>
<li>conn - The context for the connection</li>

<li>clientin - Data sent by the client.</li>

<li>clientinlen - The length of the client data. Note that this may
be 0</li>

<li>serverout - allocated and filled in by the function. These are
the bytes that should be encoded as per the protocol and sent over
the network back to the client.</li>

<li>serveroutlen - length of bytes to send to client. Note that
this may be 0</li>
</ul>

This function preforms a step of the authentication. This may need
to be called an arbitrary number of times. If the program returns
SASL_CONTINUE that means <tt>serverout</tt> should be sent to the
client. If SASL_OK is returned that means authentication is
complete and the application should tell the client the
authentication was successful. Any other return code means the
authentication failed and the client should be notified of this. 

<h3><a name="sasl_listmech">sasl_listmech</a></h3>

<pre>
    int sasl_listmech(sasl_conn_t *conn,
              const char *user,
              const char *prefix,
              const char *sep,
              const char *suffix,
              const char **result,
              unsigned *plen,
              unsigned *pcount);
   
</pre>

Parameters: 

<ul>
<li>conn - The context for this connection</li>

<li>user - Currently not implemented</li>

<li>prefix - The string to prepend</li>

<li>sep - The string to separate mechanisms with</li>

<li>suffix - The string to end with</li>

<li>result - Resultant string</li>

<li>plen - Number of characters in the result string</li>

<li>pcount - Number of mechanisms listed in the result string</li>
</ul>

This function is used to create a string with a list of SASL
mechanisms supported by the server. This string is often needed for
a capability statement. 

<h3><a name="sasl_checkpass">sasl_checkpass</a></h3>

<pre>
    int sasl_checkpass(sasl_conn_t *conn,
                       const char *user,
                       unsigned userlen,
               const char *pass,
               unsigned passlen);
   
</pre>

Parameters: 

<ul>
<li>conn - The context for this connection</li>

<li>user - The user trying to check the password for</li>

<li>userlen - The user length</li>

<li>pass - The password</li>

<li>passlen - The password length</li>
</ul>

This checks a plaintext password <i>pass</i> for user <i>user</i>
Some protocols have legacy systems for plaintext authentication
where this might be used. 

<h2><a name="common_section">Common Section</a></h2>

<h3><a name="callbacks_interactions">Callbacks and
Interactions</a></h3>

When the application starts and calls sasl_client_init() you must
specify for what data you support callbacks and/or interactions.
These are for the library getting information needed for
authentication from the application. This is needed for things like
authentication name and password. If you do not declare supporting
a callback you will not be able to use mechanisms that need that
data. A callback is for when you have the information before you
start the authentication. The SASL library calls a function you
specify and your function fills in the requested information. For
example if you had the userid of the user already for some reason.
An interaction is usually for things you support but will need to
ask the user for (e.g. password). sasl_client_start() or
sasl_client_step() will return SASL_INTERACT. This will be a list
of sasl_interact_t's which contain a human readable string you can
prompt the user with, a possible computer readable string, and a
default result. The nice thing about interactions is you get them
all at once so if you had a GUI application you could bring up a
dialog box asking for authentication name and password together
instead of one at a time.

<p>Any memory that is given to the SASL library for the purposes of
callbacks and interactions must persist until the exchange
completes in either success or failure. That is, the data must
persist until <tt>sasl_client_start</tt> or
<tt>sasl_client_step</tt> returns something other than
<tt>SASL_INTERACT</tt> or <tt>SASL_CONTINUE</tt>.</p>

<p><b>Memory management:</b>As in the rest of the SASLv2 API,
whoever allocates the memory is responsible for freeing it. In
almost all cases this should be fairly easy to manage, however a
slight exception where the interaction sasl_interact_t structure is
allocated and freed by the library, while the results are allocated
and freed by the application. As noted above, however, <i>the
results may not be freed until after the exchange completes, in
either success or failure</i>.</p>

<p>For a detailed description of what each of the callback types
are see the sasl.h file. Here are some brief explanations:</p>

<ul>
<li>SASL_CB_AUTHNAME - the name of the user authenticating</li>

<li>SASL_CB_USER - the name of the user acting for. (for example
postman delivering mail for tmartin might have an AUTHNAME of
postman and a USER of tmartin)</li>

<li>SASL_CB_PASS - password for AUTHNAME</li>

<li>SASL_CB_GETREALM - Realm of the server</li>
</ul>

An example of a way to handle callbacks: 

<pre>
    /* callbacks we support. This is a global variable at the 
       top of the program */
    static sasl_callback_t callbacks[] = {
    {
      SASL_CB_GETREALM, NULL, NULL  /* we'll just use an interaction if this comes up */
    }, {
      SASL_CB_USER, NULL, NULL      /* we'll just use an interaction if this comes up */
    }, {
      SASL_CB_AUTHNAME, &amp;getauthname_func, NULL /* A mechanism should call getauthname_func
                                                   if it needs the authentication name */
    }, { 
      SASL_CB_PASS, &amp;getsecret_func, NULL      /* Call getsecret_func if need secret */
    }, {
      SASL_CB_LIST_END, NULL, NULL
    }
    };


    static int getsecret_func(sasl_conn_t *conn,
      void *context __attribute__((unused)),
      int id,
      sasl_secret_t **psecret)
    {
       [ask the user for their secret]

       [allocate psecret and insert the secret]

      return SASL_OK;
    }

    static int getauthname_func(void *context,
                                int id,
                                const char **result,
                                unsigned *len)
    {
       if (id!=SASL_CB_AUTHNAME) return SASL_FAIL;

       [fill in result and len]

       return SASL_OK;
     }

    
    in the main program somewhere
    
    sasl_client_init(callbacks);

   
</pre>

<h3><a name="layers">Security layers</a></h3>

<p>All is well and good to securely authenticate, but if you don't
have some sort of integrity or privacy layer, anyone can hijack
your TCP session after authentication. If your application has
indicated that it can support a security layer, one <i>might</i> be
negotiated.</p>

<p>To set that you support a security layer, set a security
property structure with <tt>max_ssf</tt> set to a non-zero
number:</p>

<pre>
   sasl_security_properties_t secprops;

   secprops.min_ssf = 0;
   secprops.max_ssf = 256;
   secprops.maxbufsize = /* SEE BELOW */;

   secprops.property_names = NULL;
   secprops.property_values = NULL;
   secprops.security_flags = SASL_SEC_NOANONYMOUS; /* as appropriate */

   sasl_setprop(conn, SASL_SEC_PROPS, &amp;secprops);
</pre>

The <tt>secprops</tt> variable will be copied during the call to
<tt>sasl_setprop</tt>, so you may free its memory immediately. The
<i>SSF</i> stands for <i>security strength factor</i> and is a
rough indication of how "secure" the connection is. A connection
supplying only integrity with no privacy would have an SSF of 1. A
connection secured by 56-bit DES would have an SSF of 56. 

<p>To require a security layer, set <tt>min_ssf</tt> to the minimum
acceptable security layer strength.</p>

<p>After authentication is successful, you can determine whether or
not a security layer has been negotiated by looking at the SASL_SSF
property:</p>

<pre>
   const int *ssfp;

   result = sasl_getprop(conn, SASL_SSF, (const **) &amp;ssfp);
   if (result != SASL_OK) {
       /* ??? */
   }
   if (*ssfp &gt; 0) {
       /* yay, we have a security layer! */
   }
</pre>

<p>If a security layer has been negotiated, your application must
make use of the <tt>sasl_encode()</tt> and <tt>sasl_decode()</tt>
calls. All output must be passed through <tt>sasl_encode()</tt>
before being written to the wire; all input must be passed through
<tt>sasl_decode()</tt> before being looked at by the application.
Your application must also be prepared to deal with
<tt>sasl_decode()</tt> not returning any data in the rare case that
the peer application did something strange (by splitting a single
SASL blob into two seperate TCP packets).</p>

<p>The only subtlety dealing with security layers is the maximum size
of data that can be passed through <tt>sasl_encode()</tt> or
<tt>sasl_decode()</tt>. This must be limited to make sure that only
a finite amount of data needs to be buffered. The simple rules to
follow:</p>

<ul>
<li>Before starting authentication, set <tt>maxbufsize</tt> in your
security properties to be the buffer size that you pass to the
<tt>read()</tt> system call&mdash;that is, the amount of data
you're prepared to read at any one time.</li>

<li>After authentication finishes, use <tt>sasl_getprop()</tt> to
retrieve the <tt>SASL_MAXOUTBUF</tt> value, and call
<tt>sasl_encode()</tt> with chunks of data of that size or less.
<tt>sasl_encode()</tt> will throw an error if you call it with a
larger chunk of data, so be careful!</li>
</ul>

<p><b>Memory management:</b> As usual, whoever allocates the memory
must free it. The SASL library will keep the data returned from
<tt>sasl_encode()</tt> until the next call to <tt>sasl_encode()</tt>
on that connection. (<tt>sasl_decode()</tt> results persist until the
next call to <tt>sasl_decode()</tt> on that connection.) The
application must not attempt to free the memory returned from either
function.</p>

<p><b>Internally:</b></p>

<ul>
<li>your application sets SASL_SEC_PROPS with the buffer size X of
the amount of data it will be using to read() from the socket.</li>

<li>libsasl passes this number to the mechanism.</li>

<li>the mechanism passes this number to the other side. the other
side gives the corresponding read() size to our side.</li>

<li>the mechanism subtracts the overhead of the layers from the
size retrieved from the other side and returns it to the
libsasl.</li>

<li>libsasl then returns (via SASL_MAXOUTBUF) this number as the
maximum amount of plaintext material that can be encoded at any one
time, Y.</li>

<li>sasl_encode() enforces the restriction of the length Y.</li>
</ul>

<h2><a name="example_section">Example applications that come with
the Cyrus SASL library</a></h2>

<h3><a name="sample_client"><tt>sample-client</tt> and
<tt>sample-server</tt></a></h3>

The sample client and server included with this distribution were
initially written to help debug mechanisms. They base64 encode all
the data and print it out on standard output. 

<p>Make sure that you set the IP addresses, the username, the
authenticate name, and anything else on the command line (some
mechanisms depend on these being present).</p>

<p>Also, sometimes you will receive a get "<tt>realm: Information
not available</tt>" message, or similar; this is due to the fact
that some mechanisms do not support realms and therefore never set
it.</p>

<h3><a name="cyrus_imapd">Cyrus imapd v2.1.0 or later</a></h3>

The Cyrus IMAP server now incorporates SASLv2 for all its
authentication needs. It is a good example of a fairly large server
application. Also of interest is the prot layer, included in
libcyrus. This is a stdio-like interface that automatically takes
care of layers using a simple "<tt>prot_setsasl()</tt>" call. 

<p>Cyrus imapd also sets a <tt>SASL_CB_PROXY_POLICY</tt> callback,
which should be of interest to many applications.</p>

<h3><a name="imtest"><tt>imtest</tt>, from Cyrus 2.1.0 or
later</a></h3>

<tt>imtest</tt> is an application included with Cyrus imapd. It is
a very simple IMAP client, but should be of interest to those
writing applications. It also uses the prot layer, but it is easy
to incorporate similar support without using the prot layer.
Likewise, there are other sample client applications that you can
look at including <tt>smtptest</tt> and <tt>pop3test</tt> in the
SASL distribution and the Cyrus IMAPd distribution, respectively. 

<h2><a name="random_things">Miscellaneous Information</a></h2>

<h3><a name="empty_exchanges">Empty exchanges</a></h3>

<p>Some SASL mechanisms intentionally send no data; an application
should be prepared to either send or receive an empty exchange. The
SASL profile for the protocol should define how to send an empty
string; make sure to send an empty string when requested, and when
receiving an empty string make sure that the "<tt>inlength</tt>"
passed in is 0.</p>

<p>Note especially that the distinction between the empty string ""
and the lack of a string (NULL) is extremely important in many
cases (most notably, the client-send first scenario), and the
application must ensure that it is passing the correct values to
the SASL library at all times.</p>

<h3><a name="idle">Idle</a></h3>

While the implementation and the plugins correctly implement the
idle calls, none of them currently do anything. 

<hr>
Please send any questions or comments to: 

<address><a href=
"mailto:cyrus-bugs@andrew.cmu.edu">cyrus-bugs@andrew.cmu.edu</a></address>

<br>
 Back to the <a href="index.html">index</a>
</body>
</html>