SCS Computing
 Services and Solutions
  links to the SCS and CMU home pages Carnegie Mellon School of Computer Science Carnegie Mellon University
 » How to… 
 » Accounts & passwords 
 » AFS 
 » AV help 
 » Backups & restores 
 » Calendaring 
 » E-mail 
 » Networking 
 » Printing 
 » Purchasing 
 » Resource management 
 » Security 
 » Software licensing 
 » Support charges 
 » Support lifecycle 
 » Web publishing 
 » Mac support 
 » Linux support 
 » Windows PC support 

Authenticating daemons to AFS

In normal situations, daemons that are (for example) run as background processes, do not have authenticated access to AFS. This document describes a mechanism for running background daemons so that they can obtain such access. It is assumed that the reader has a working knowledge of AFS as described in the Facilities AFS documentation and has read the documentation on how the AFS cache manager handles authentication. The procedures described here are for hosts built by SCS Computing.

How to do it

The basic procedure for running authenticated daemons involves the following steps (steps 1 and 2 are setup steps that should only have to be done once):
  1. Create, key and define the username.daemon Kerberos instance
  2. Add the username.daemon instance to AFS ACLs as necessary
  3. Run the daemon so that it gets its own PAG, Kerberos ticket and AFS tokens

Before you begin, you may wish to back up any existing username.daemon.* files in your present working directory.

  1. Create, key and define the username.daemon Kerberos instance

    1. Use the klist command to confirm you have current tickets for your null instance
    2. Change to a non-AFS, non-public directory
    3. Run the following commands:
      1. (umask 077; remctl kerberos instance keytab ${USER}/daemon > ${USER}.daemon.keytab)
      2. remctl afs pts createuser ${USER}.daemon
    4. As root, move the resulting .keytab file to /etc/not-backed-up/
  2. Add the username.daemon instance to AFS ACLs as necessary

    To minimize any potential security risks, you should only add this instance to the ACLs of directories that the daemon needs to access. See the documentation on AFS ACLs for instructions on how to change ACLs.

  3. Running the daemon

    The specific steps for running the daemon vary depending on whether you are performing these steps on a host running Fedora or Ubuntu. Please use the steps indicated for your platform:


    There are two ways to run the daemon depending on how long your daemon or process takes to run:

    • Use kinit
    • Use kstart
    • Use kinit

      If the daemon or process you are running completes in an amount of time shorter than the default ticket lifetime (typically 24 hours), you should use kinit. You can run the daemon with a crontab entry like:kinit -kt /etc/not-backed-up/${USER}.daemon.keytab example/daemon /path/to/script

    • Use kstart

      If the daemon or process you are running will take longer than the default ticket lifetime (typically 24 hours), you should use kstart. Please note that kstart is not available by default but is available for installation via apt-get. You can run the daemon with a crontab entry like:/usr/bin/k5start -t -f /etc/not-backed-up/${USER}.daemon.keytab -K 60 ${USER}/daemon /path/to/script

      (The -K 60 switch specifies a time in minutes for the tickets for tickets to be renewed beyond the default ticket lifetime.)


    It is important that the daemon is run in a way that it gets its own PAG before starting. If it does not do so, all processes without a PAG with the same Unix user ID as the daemon will share the same token and perhaps have undesired authenticated access to AFS. The general procedure to follow is: Before starting the daemon:

    1. Get a new PAG.
    2. Set unique names for KRBTKFILE and KRB5CCNAME in the process environment.
    3. Obtain the Kerberos tickets, including an AFS service ticket and store the AFS token.

    Now execute the daemon. At this point, the daemon will inherit the Kerberos tickets (via the KRBTKFILE and KRB5CCNAME in the environment) and the AFS tokens (via the PAG which is shared by all descendants of the PAG creator).

    After the daemon is running:

    • Have some mechanism to periodically renew tickets before they expire (the default ticket lifetime is usually 25 hours).
    • Have some mechanism to detect when the daemon has exited, either via looking for a /var/run/<daemon>.pid file or through some other means.

    As long as the process that is maintaining the tickets has the same PAG and the same KRBTKFILE and KRB5CCNAME then tickets for the daemon process can be maintained forever.

    The following wrapper Perl script is an example of how to do the above steps: run_with_tickets

    Before using this script, you should read through it and modify variables appropriately for your application. This script has not been extensively tested.

    The sample "run_with_tickets" script uses a child watcher process to maintain the tickets. It exits when it detects that its parent process is gone. The parent process is assumed to be the daemon (or some indefinitely running process) which is exec-ed to replace the parent Perl process.

    It is common for daemon processes to use a paradigm where the daemon will fork, the parent will exit, and the child will continue execution thus placing itself in the background. Such daemon processes must be started with a switch so that the daemon does not perform this forking operation.

    If the daemon does not support startup without placing itself in the background, then you will need maintain your own script using the above guidelines.

    If the daemon does support the "run_with_tickets" script, then you can start your daemon with a "launch_daemon" script that contains the following:

       /usr/mypkg/etc/run_with_tickets /usr/mkpkg/bin/my_daemon arg1 arg2 >/some/log/file 2>&1 &

    The above example assumes a sh-based script with associated files located under "/usr/mypkg"