XS(3pm)

πŸ“¦
version 1.00 Β· 5 versions
, in ubuntu stonking

✨ XS(3pm) β€” Authen::SASL::XS ✨

User Contributed Perl Documentation

okay bestie sit down, grab a strawberry πŸ“, we're doing the whole thing, no skipping


🌸 NAME 🌸

Authen::SASL::XS β€” XS code to glue Perl SASL to Cyrus SASL πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

use Authen::SASL;

my $sasl = Authen::SASL->new(
       mechanism => 'NAME',
       callback => { NAME => VALUE, NAME => VALUE, ... },
);

my $conn = $sasl->client_new(<service>, <server>, <iplocalport>, <ipremoteport>);

my $conn = $sasl->server_new(<service>, <host>, <iplocalport>, <ipremoteport>);

like. that's the whole vibe. copy, paste, slay. 🫢


🌈 DESCRIPTION 🌈

SASL is a generic mechanism for authentication used by several network protocols. Authen::SASL::XS provides an implementation framework that all protocols should be able to share. no cap.

The XS framework makes calls into the existing libsasl.so resp. libsasl2 shared library to perform SASL client connection functionality, including loading existing shared library mechanisms. so it's not reinventing anything, it's just calling the libs that already exist. very much "why would I do it myself when libsasl is right there" energy. πŸ’…


πŸ‘‘ CONSTRUCTOR πŸ‘‘

The constructor may be called with or without arguments. Passing arguments is just a short cut to calling the mechanism and callback methods. so like, same thing, just fewer lines, iykyk.

You have to use the Authen::SASL new-constructor to create a SASL object. The Authen::SASL object then holds all necessary variables and callbacks, which you gave when creating the object. client_new and server_new will retrieve needed information from this object. it's giving main character storage unit β€” everything lives in there and the other methods come ask it for stuff. ✨


πŸ’– CALLBACKS πŸ’–

Callbacks are very important. it depends on the mechanism which callbacks have to be set. It is not a failure to set callbacks even they aren't used. (e.g. password-callback when using GSSAPI or KERBEROS_V4) β€” so yeah, setting a callback nobody calls is fine, not a crime, nobody's mad at you bestie.

The Cyrus-SASL library uses callbacks when the application needs some information. Common reasons are getting usernames and passwords.

Authen::SASL::XS allows Cyrus-SASL to use perl-variables and perl-subs as callback-targets. πŸŽ€

Currently Authen::SASL::XS supports the following Callback types: (for a more detailed description on what the callback type is used for see the respective man pages)

Remark: All callbacks, which have to return some values (e.g.: **result in sasl_getsimple_t) do this by returning the value(s). See example below.

  • user (client)
  • auth (client)
  • language (client) β€” This callbacks represent the sasl_getsimple_t from the library.
    • Input: none
    • Output: username, authname or language
  • password (client)
  • pass (client) β€” This callbacks represent the sasl_getsecret_t from the library.
    • Input: none
    • Output: password
  • realm <client> β€” This callback represents the sasl_getrealm_t from the library.
    • Input: a list of available realms
    • Output: the chosen realm
    • (This has nothing to do with GSSAPI or KERBEROS_V4 realm). don't get it twisted!! πŸ™ˆ
  • checkpass (server, SASL v2 only) β€” This callback represents the sasl_server_userdb_checkpass_t from the library.
    • Input: username, password
    • Output: true or false
  • getsecret (server, SASL v1 only) β€” This callback represents the sasl_server_getsecret_t from the library. Sasl will check if the passwords are matching.
    • Input: mechanism, username, default_realm
    • Output: secret_phrase (password)
    • Remark: Programmers that are using should specify both callbacks (getsecret and checkpass). Then, depending on you Cyrus SASL library either the one or the other is called. Cyrus SASL v1 ignores checkpass and Cyrus SASL v2 ignores getsecret.
  • putsecret (SASL v1) and setpass (SASL v2) β€” are currently not supported (and won't be, unless someone needs it). πŸ’€ not me adding a feature nobody asked for
  • canonuser (server/client in SASL v2, server only in SASL v1) β€” This callback name represents the sasl_canon_user_t from the library.
    • Input: Type of principal, principal, userrealm and maximal allowed length of the output.
    • Output: canonicalised principal
    • Type of principal is AUTHID for Authentication ID or AUTHZID for Authorisation ID.
    • Remark: This callback is ideal to get the username of the user using your service. If Authen::SASL::XS is linked to Cyrus SASL v1, which doesn't have a canonuser callback, it will simulate this callback by using the authorize callback internally. Don't worry, the authorize callback is available anyway. 🫢
  • authorize (server) β€” This callback represents the sasl_authorize_t from the library.
    • Input: authenticated_username, requested_username, (default_realm SASL v2 only)
    • Output: canonicalised_username SASL v1 resp. true or false when using SASL v2 lib There is something TODO, I think. πŸ’€ fr fr someone left a note and walked away
  • setpass (server, SASL v2 only) β€” This callback represents the sasl_server_userdb_setpass_t from the library.
    • Input: username, new_password, flags (0x01 CREATE, 0x02 DISABLE, 0x04 NOPLAIN)
    • Out: true or false

πŸŽ€ Ways to pass a callback πŸŽ€

Authen::SASL::XS supports three different ways to pass a callback πŸ’…

  • CODEREF β€” If the value passed is a code reference then, when needed, it will be called.
  • ARRAYREF β€” If the value passed is an array reference, the first element in the array must be a code reference. When the callback is called the code reference will be called with the value from the array passed after.
  • SCALAR β€” All other values passed will be returned directly to the SASL library as the answer to the callback.

πŸ¦‹ Example of setting callbacks πŸ¦‹

$sasl = new Authen::SASL (
  mechanism => "PLAIN",
    callback => {
      # Scalar
      user => "mannfred",
      pass => $password,
      language => 1,

      # Coderef
      auth => sub { return "klaus", }
      realm => \&getrealm,

      # Arrayref
      canonuser => [ \&canon, $self ],
   }
);

The last example is ideal for using object methods as callback functions. Then you can do something like this:

sub canon {
  my ($this,$type,$realm,$maxlen,$user) = @_;
  $this->{_username} = $user if ($type eq "AUTHID");
  return $user; }

there's more coming bestie, hold on β€” part 2 is on the way 🧚✨

✨ Authen::SASL::XS METHODS ✨

okay bestie, this is the main character section β€” all the methods you can actually call on your connection object. let's go πŸ’…

  • server_new ( SERVICE , HOST = "" , IPLOCALPORT , IPREMOTEPORT ) β€” the constructor that makes you a server-side sasl context. It creates and hands back a brand new connection object blessed into Authen::SASL::XS, and it's on that returned reference that all the methods below live. The SERVICE is the name of the service you're implementing, which the underlying mechanism might peek at β€” a classic example of a service is "ldap".
  • client_new ( SERVICE , HOST , IPLOCALPORT , IPREMOTEPORT ) β€” the constructor for creating server-side sasl contexts. (yes, the page literally says "server-side" here, we're just reporting the lore πŸ™ˆ) It creates and returns a new connection object blessed into Authen::SASL::XS, and again, that returned reference is where all the following methods hang out. The SERVICE is the name of the service you're implementing, possibly used by the underlying mechanism β€” "ldap" is the go-to example. The HOST is the name of the server you're contacting, which the underlying mechanism may also use.

Peep the SYNOPSIS for a working example, bestie.

Remark: this function and the server_new function are called by Authen::SASL when you use its *_new function. And since you have to use Authen::SASL anyway, normally it is not necessary to call this function directly. so like… noted, but also you probably won't need to.

The IPLOCALPORT and IPREMOTEPORT arguments are only available when ASC is linked against Cyrus SASL 2.x πŸ’€. These arguments are needed for KERBEROS_V4 and CS 2.x on the server side. Whether they're necessary on the client side β€” the page says it doesn't know. In an IPv4 environment the format of these arguments should be:

a.b.c.d;port

See sasl_server_new(3) for the deets.


  • server_start ( CHALLENGE ) β€” server_start kicks off the authentication using the chosen mechanism. If the mechanism isn't supported by the installed Cyrus-SASL, it fails, no cap. And because some mechanisms need the client to start the negotiation, you can hand the client challenge in as a parameter.
  • client_start ( ) β€” the initial step to be performed. It returns the initial value to pass to the server. The client has to start the negotiation always.
  • server_step ( CHALLENGE ) β€” server_step does the next step in the negotiation process. The first parameter you give it is the client's challenge/response.
  • client_step ( CHALLENGE ) β€” (the page gives this one zero description, iconic minimalist behaviour, we move πŸ’…)
Remark: client_start, client_step, server_start and server_step will return the respective sasl response or undef. And the returned value says nothing about the current negotiation status. It is absolutely possible that one of these functions returns undef and everything is perfectly fine for SASL β€” there's just another step needed.

So therefore you have to check need_step and code during negotiation, fr fr. See the example below πŸ‘‡

  • listmech( START , SEPARATOR , END ) β€” listmech returns a string containing all mechanisms allowed for the user set by user. START is the token that gets put at the beginning of the string, SEPARATOR is the token used to separate the mechanisms, and END is the token put at the end of the returned string.
  • setpass(user, newpassword, oldpassword, flags)
  • checkpass(user, password) β€” setpass and checkpass are only available when using the Cyrus-SASL 2.x library.

setpass sets a new password (whether the setpass callback gets called depends on the mechanism). checkpass checks a password for the user (it calls the checkpass callback).

For both functions, see the man pages of the Cyrus SASL for a detailed description.

Both functions return true on success, false otherwise. periodt.

  • global_listmech ( ) β€” global_listmech is only available when using the Cyrus-SASL 2.x library.

It returns an array with all mechanisms loaded by the library. πŸŽ€

  • encode ( STRING )
  • decode ( STRING ) β€” the Cyrus-SASL developers suggest using the encode and decode functions for every bit of traffic that will run over the network after a successful authentication. obsessed with the foresight honestly.

encode returns the encrypted string generated from STRING. decode returns the decrypted string generated from STRING.

How secure that encryption actually is depends on the mechanism you used. πŸ‘‘

  • error ( ) β€” error returns an array with all known error messages. Basically the sasl_errstring function is called with the current error_code. When using the Cyrus-SASL 2.x library, the string returned by sasl_errdetail is also given back. Additionally, the special Authen::SASL::XS advise is returned if it's set. And after you call the error function, the error code and the special advice are thrown away β€” gone, deleted, no takebacks.
  • code ( ) β€” code returns the current Cyrus-SASL error code.
  • mechanism ( ) β€” mechanism returns the current used authentication mechanism.
  • need_step ( ) β€” need_step returns true if another step is needed by the SASL library. Otherwise false is returned. You can also use code == 1 but honestly it looks smarter this way I think. That's why we're all using perl, eh? 😌

🌸 EXAMPLE 🌸

Server-side 🫢

# The example uses Cyrus-SASL v2
# Set the SASL_PATH to the location of the SASL-Plugins
# default is /usr/lib/sasl2
$ENV{'SASL_PATH'} = "/opt/products/sasl/2.1.15/lib/sasl2";

#
my $sasl = Authen::SASL->new (
   mechanism => "PLAIN",
   callback => {
     checkpass => \&checkpass,
     canonuser => \&canonuser,
   }
);

# Creating the Authen::SASL::XS object
my $conn = $sasl->server_new("service","","ip;port local","ip;port remote");

# Clients first string (maybe "", depends on mechanism)
# Client has to start always
sendreply( $conn->server_start( &getreply() ) );

while ($conn->need_step) {
   sendreply( $conn->server_step( &getreply() ) );
}

if ($conn->code == 0) {
   print "Negotiation succeeded.\n";
} else {
   print "Negotiation failed.\n";
}

Client-side πŸ¦‹

# The example uses Cyrus-SASL v2
# Set the SASL_PATH to the location of the SASL-Plugins
# default is /usr/lib/sasl2
$ENV{'SASL_PATH'} = "/opt/products/sasl/2.1.15/lib/sasl2";

#
my $sasl = Authen::SASL->new (
   mechanism => "PLAIN",
   callback => {
     user => \&getusername,
     pass => \&getpassword,
   }
);

# Creating the Authen::SASL::XS object
my $conn = $sasl->client_new("service", "hostname.domain.tld");

# Client begins always
sendreply($conn->client_start());

while ($conn->need_step) {
   sendreply($conn->client_step( &getreply() ) );
}

if ($conn->code == 0) {
   print STDERR "Negotiation succeeded.\n";
} else {
   print STDERR "Negotiation failed.\n";
}

See t/plain.t for a working script. πŸ“


πŸ’€ TESTING πŸ’€

The author tested ASC (server and client) with the following mechanisms:

  • GSSAPI β€” don't forget to create a keytab. Non-root keytabs can be specified through $ENV{'KRB5_KTNAME'} (Heimdal >= 0.6, MIT).
  • KERBEROS_V4 β€” available since 0.10, and you have to add IPLOCALPORT and IPREMOTEPORT to the *_new functions.
  • PLAIN

πŸ‘€ SEE ALSO πŸ‘€

  • Authen::SASL
  • man pages for sasl_* library functions.

πŸ‘‘ AUTHOR πŸ‘‘

Originally written by Mark Adamson <mark@nb.net>

Cyrus-SASL 2.x support by Leif Johansson

Glue for server_* and many other structural improvements by Patrick Boettcher <patrick.boettcher@desy.de>

Please report any bugs, or post any suggestions, to the authors. they literally asked for it 🫢


πŸ’– THANKS πŸ’–

  • Guillaume Filion for testing the server part and for giving hints about some bugs (documentation).
  • Wolfgang Friebel for bother around with rpm building of test releases.

Copyright (c) 2003-5 Patrick Boettcher, DESY Zeuthen. All rights reserved. Copyright (c) 2003 Carnegie Mellon University. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's the whole page bestie, we ate and left absolutely zero crumbs 🍽️✨ this was perl v5.42.3, dated 2026-08-19, from the XS(3pm) man page β€” go forth and authenticate, slay, and remember: undef doesn't mean it's over πŸ’…πŸ¦‹

πŸ“¦ where this page comes from