home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 March B / SCO_CASTOR4RRT.iso / nsfast / root.9 / usr / ns-home / nsapi / examples / auth.c / auth
Text File  |  1998-08-19  |  3KB  |  122 lines

  1. /*
  2.  * Copyright (c) 1994, 1995, 1996.  Netscape Communications Corporation.
  3.  * All rights reserved.
  4.  * 
  5.  * Use of this software is governed by the terms of the license agreement
  6.  * for the Netscape Enterprise or Netscape Personal Server between the
  7.  * parties.
  8.  *
  9.  */
  10.  
  11.  
  12. /* ------------------------------------------------------------------------ */
  13.  
  14.  
  15. /*
  16.  * auth.c: Example NSAPI functions to perform user authorization
  17.  *
  18.  * The Server Application Functions in this file are AuthTrans class
  19.  * functions, and designed to demonstrate in general how to use the 
  20.  * server's authorization facilities.
  21.  * 
  22.  * Rob McCool
  23.  */
  24.  
  25.  
  26. /* 
  27.    The following three are standard headers for SAFs.  They're used to
  28.    get the data structures and prototypes needed to declare and use SAFs.
  29.  */
  30.  
  31.  
  32. #include "base/pblock.h"
  33. #include "base/session.h"
  34. #include "frame/req.h"
  35.  
  36.  
  37. /* ---------------------------- hardcoded_auth ---------------------------- */
  38.  
  39.  
  40. /*
  41.  
  42.    This function is kind of silly, but is used to demonstrate how to
  43.    use your own custom ways of verifying that the username and
  44.    password that a remote client provided is accurate. This program
  45.    uses a hard coded table of usernames and passwords and checks a
  46.    given user's password against the one in the static data array.
  47.  
  48.    Note that this function doesn't actually enforce authorization
  49.    requirements, it only takes given information and tells the server
  50.    if it's correct or not. The PathCheck function require-auth
  51.    performs the enforcement.
  52.  
  53.    The userdb parameter is not used by this function, but your
  54.    function can use it as an opaque string to tell you which database
  55.    to use.
  56.  
  57.    Usage:
  58.    At the beginning of obj.conf:
  59.       Init fn=load-modules shlib=example.<ext> funcs=hardcoded-auth
  60.    Inside an object in obj.conf:
  61.       AuthTrans fn=basic-auth auth-type="basic" userdb=garbage
  62.                 userfn=hardcoded-auth
  63.  
  64.       PathCheck fn="require-auth" auth-type="basic" realm="auth-test"
  65.  
  66.    <ext> = so on UNIX
  67.    <ext> = dll on NT.
  68.  
  69.  
  70.  */
  71.  
  72.  
  73. typedef struct {
  74.     char *name;
  75.     char *pw;
  76. } user_s;
  77.  
  78. static user_s user_set[] = {
  79.     {"joe", "shmoe"},
  80.     {"suzy", "creamcheese"},
  81.     {NULL, NULL}
  82. };
  83.  
  84. #include "frame/log.h"
  85.  
  86. #ifdef XP_WIN32
  87. #define NSAPI_PUBLIC __declspec(dllexport)
  88. #else /* !XP_WIN32 */
  89. #define NSAPI_PUBLIC
  90. #endif /* !XP_WIN32 */
  91.  
  92.  
  93. NSAPI_PUBLIC int hardcoded_auth(pblock *param, Session *sn, Request *rq)
  94. {
  95.     /* Parameters given to us by auth-basic */
  96.     char *pwfile = pblock_findval("userdb", param);
  97.     char *user = pblock_findval("user", param);
  98.     char *pw = pblock_findval("pw", param);
  99.  
  100.     /* Temp variables */
  101.     register int x;
  102.  
  103.     for(x = 0; user_set[x].name != NULL; ++x) {
  104.         /* If this isn't the user we want, keep going */
  105.         if(strcmp(user, user_set[x].name) != 0)
  106.             continue;
  107.         /* Verify password */
  108.         if(strcmp(pw, user_set[x].pw)) {
  109.             log_error(LOG_SECURITY, "hardcoded-auth", sn, rq, 
  110.                   "user %s entered wrong password", user);
  111.             /* This will cause the enforcement function to ask user again */
  112.             return REQ_NOACTION;
  113.         }
  114.         /* If we return REQ_PROCEED, the username will be accepted */
  115.         return REQ_PROCEED;
  116.     }
  117.     /* No match, have it ask them again */
  118.     log_error(LOG_SECURITY, "hardcoded-auth", sn, rq, 
  119.               "unknown user %s", user);
  120.     return REQ_NOACTION;
  121. }
  122.