title: How to use gssapi kerberos in c / c++ client server cross-platform programs?
body: <p>I had to "sporadically" work with Heimdal / MIT Gssapi for kerberos authentication over past couple of years. I had to build an application that was to be used as a web-service running on a Linux box, and serve client applications like browsers, running on Windows and/or Linux Desktops and Workstations. Surely not the easiest of beasts to tame. Eventually when summarizing my work, I could record that the difficulties emanated due to challenges in multiple dimensions. Getting started with gssapi programming is truly a challenge just because of poor documentation, and practically non-existant tutorials. Googling mostly results in either some theoretical discussion on what's kerberos, or leads to content written with presumption that you already know everything besides some particular semantic issue.
  Some really good hacks around here contributed to help me, I therefore suppose it would be a good idea to summarize the stuff, from a developer's perspective, and share it here as some sort of a wiki, to give something back to this fantastic place, and fellow programmers.</p>
  
  <p>Haven't really done a wiki like this before, and I am surely no authority on GSSAPI nor Kerberos, so please be kind, but more than that please contribute and correct my mistakes. Site Editors, I am counting on you to do your magic ;)</p>
  
  <p>Getting your project completed successfully will require 3 specific things to be done correctly:</p>
  
  <ol>
  <li>Setup of your test environment</li>
  <li>Setup of your libraries</li>
  <li>Your code </li>
  </ol>
  
  <p>As I said already, such projects are beasts, just because all the three haven't been put together on the same page anywhere.</p>
  
  <p>Ok So let's begin at the beginning.</p>
  
  <p><strong>Unavoidable theory for a newbie</strong>
  GSSAPI helps a client application to provide credentials for a server to authoritatively identify the user. Extremely useful because the server applications can modulate their served responses if they wish to, as per the user. Very naturally therefore both - the client and the server applications must be kerberized, or as some would state kerberos-aware.</p>
  
  <p>The kerberos based authentication, requires both the client and server applications, to be members of a Kerberos Realm. KDC (Kerberos Domain Controller) is the designated authority that rules the realm. Microsoft's AD servers are one of the most popularly experienced examples of a KDC, though you can of course be using a *NIX based KDC. But surely without a KDC there can be no Kerberos business at all. Desktops, Servers &amp; workstations joined into the domain identify each other as long as all of them remain joined into the domain. </p>
  
  <p>For your initial experiments, setup the client &amp; server applications in the same realm.
  Though Kerberos Authentication can surely be also used across realms by creating trusts between KDCs of these realms, or even merging keytabs from different KDCs that do not trust each other. Your code will not really need any change to accommodate such different and complex-sounding scenarios. </p>
  
  <p>Kerberos Authentication basically works via "tickets (or tokens)". When a member joins the realm, the KDC "grants tokens" to each of them. These tokens are unique; time and FQDN are essential factors for these tickets.</p>
  
  <p>Before you even think of the very first line of your code make sure you have got these two right:</p>
  
  <p><strong>Pitfall #1</strong> When you setup your development and test environment, make sure everything is tested and addressed as FQDN. For example if you want to check connectivity, ping using FQDN, not IP. Needless to say therefore, they must necessarily have the same DNS service configuration.</p>
  
  <p><strong>Pitfall #2</strong> Make sure all the host systems - that are running your KDC, client software, server software have the same time server. Time synchronization is something that one forgets, and realizes to be amiss after a lot of hair-splitting, and head-banging!</p>
  
  <p>Both, the client and server applications NEED kerberos keytabs. So if your application is going to run in a *NIX host, and be a part of a Microsoft Domain, you have to get a kerberos keytab generated, before we start to look at the remaining preparatory steps for gss programming.</p>
  
  <p><strong><a href="http://technet.microsoft.com/en-us/library/bb742433.aspx">Step-by-Step Guide to Kerberos 5 (krb5 1.0) Interoperability</a></strong> at is an absolute must-read.</p>
  
  <p><strong><a href="http://www.shrubbery.net/solaris9ab/SUNWdev/GSSAPIPG/p1.html">GSS-API Programming Guide</a></strong> is an excellent bookmark.</p>
  
  <p>Depending upon your *NIX distribution you can install the headers &amp; libraries for building your code. My suggestion however is to download the source and build it yourself. Yes, you might not get it right at one go, but it surely is worth the trouble.</p>
  
  <p><strong>Pitfall #3</strong> Make sure that your application is running in an Kerberos aware environment.
  I really learnt this the hard way, but maybe because I am not so smart. In my earliest stages of gssapi programming struggle, I had discovered that kerberos keytabs were absolutely necessary for making my application kerberos-aware. But I simply couldn't find anything about how to load these keytabs in my application. You know why?!! Because no such api exists!!!
  Because: The application is to be run in an environment which is aware of the keytabs.
  Ok, let me make this simple: Your application that is supposed to do the GSSAPI / Kerberos things has to run after you have set environment variable <strong><code>KRB5_KTNAME</code></strong> to the path where you have stored the keytabs. So either you do something like:</p>
  
  <pre><code>export KRB5_KTNAME=&lt;path/to/your/keytab&gt;
  </code></pre>
  
  <p>or make use of <code>setenv</code> to set <code>KRB5_KTNAME</code> in your application sufficiently before the very first line of your code that uses gssapi is run.</p>
  
  <p>We are now ready to do the necessary things in the application's code.</p>
  
  <p>I understand there are quite a few other aspects that must be reviewed by the application developer, to write and test an application. I know of a few environment variables, that can be important.</p>
  
  <p>Can anybody please shed some more light upon that? </p>