home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / kerberosIV / kdb / krb_lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-25  |  5.3 KB  |  244 lines

  1. /*
  2.  * $Source: /usr/src/kerberosIV/kdb/RCS/krb_lib.c,v $
  3.  * $Author: bostic $ 
  4.  *
  5.  * Copyright 1988 by the Massachusetts Institute of Technology. 
  6.  *
  7.  * For copying and distribution information, please see the file
  8.  * <mit-copyright.h>. 
  9.  */
  10.  
  11. #ifndef    lint
  12. static char rcsid_krb_lib_c[] =
  13. "$Id: krb_lib.c,v 4.8 91/02/25 15:18:22 bostic Exp $";
  14. #endif    lint
  15.  
  16. #include <mit-copyright.h>
  17. #include <stdio.h>
  18. #include <sys/types.h>
  19. #include <netinet/in.h>
  20. #include <sys/uio.h>
  21. #include <sys/time.h>
  22. #include <sys/resource.h>
  23. #include <strings.h>
  24. #include <des.h>
  25. #include <krb.h>
  26. #include <krb_db.h>
  27.  
  28. #ifdef DEBUG
  29. extern int debug;
  30. extern char *progname;
  31. long    kerb_debug;
  32. #endif
  33.  
  34. extern char *strncpy();
  35. extern char *ctime();
  36. extern char *getenv();
  37.  
  38. static  init = 0;
  39.  
  40. /*
  41.  * initialization routine for data base 
  42.  */
  43.  
  44. int
  45. kerb_init()
  46. {
  47. #ifdef DEBUG
  48.     if (!init) {
  49.     char *dbg = getenv("KERB_DBG");
  50.     if (dbg)
  51.         sscanf(dbg, "%d", &kerb_debug);
  52.     init = 1;
  53.     }
  54. #endif
  55.     kerb_db_init();
  56.  
  57. #ifdef CACHE
  58.     kerb_cache_init();
  59. #endif
  60.  
  61.     /* successful init, return 0, else errcode */
  62.     return (0);
  63. }
  64.  
  65. /*
  66.  * finalization routine for database -- NOTE: MUST be called by any
  67.  * program using kerb_init.  ALSO will have to be modified to finalize
  68.  * caches, if they're ever really implemented. 
  69.  */
  70.  
  71. int
  72. kerb_fini()
  73. {
  74.     kerb_db_fini();
  75. }
  76.  
  77. /*
  78.  * look up a principal in the cache or data base returns number of
  79.  * principals found 
  80.  */
  81.  
  82. int
  83. kerb_get_principal(name, inst, principal, max, more)
  84.     char   *name;        /* could have wild card */
  85.     char   *inst;        /* could have wild card */
  86.     Principal *principal;
  87.     unsigned int max;        /* max number of name structs to return */
  88.     int    *more;        /* more tuples than room for */
  89.  
  90. {
  91.     int     found = 0;
  92. #ifdef CACHE
  93.     static int wild = 0;
  94. #endif
  95.     if (!init)
  96.     kerb_init();
  97.  
  98. #ifdef DEBUG
  99.     if (kerb_debug & 1)
  100.     fprintf(stderr, "\n%s: kerb_get_principal for %s %s max = %d\n",
  101.         progname, name, inst, max);
  102. #endif
  103.     
  104.     /*
  105.      * if this is a request including a wild card, have to go to db
  106.      * since the cache may not be exhaustive. 
  107.      */
  108.  
  109.     /* clear the principal area */
  110.     bzero((char *) principal, max * sizeof(Principal));
  111.  
  112. #ifdef CACHE
  113.     /*
  114.      * so check to see if the name contains a wildcard "*" or "?", not
  115.      * preceeded by a backslash. 
  116.      */
  117.     wild = 0;
  118.     if (index(name, '*') || index(name, '?') ||
  119.     index(inst, '*') || index(inst, '?'))
  120.     wild = 1;
  121.  
  122.     if (!wild) {
  123.     /* try the cache first */
  124.     found = kerb_cache_get_principal(name, inst, principal, max, more);
  125.     if (found)
  126.         return (found);
  127.     }
  128. #endif
  129.     /* If we didn't try cache, or it wasn't there, try db */
  130.     found = kerb_db_get_principal(name, inst, principal, max, more);
  131.     /* try to insert principal(s) into cache if it was found */
  132. #ifdef CACHE
  133.     if (found) {
  134.     kerb_cache_put_principal(principal, found);
  135.     }
  136. #endif
  137.     return (found);
  138. }
  139.  
  140. /* principals */
  141. kerb_put_principal(principal, n)
  142.     Principal *principal;
  143.     unsigned int n;        /* number of principal structs to write */
  144. {
  145.     long time();
  146.     struct tm *tp, *localtime();
  147.  
  148.     /* set mod date */
  149.     principal->mod_date = time((long *)0);
  150.     /* and mod date string */
  151.  
  152.     tp = localtime((time_t *)&principal->mod_date);
  153.     (void) sprintf(principal->mod_date_txt, "%4d-%2d-%2d",
  154.            tp->tm_year > 1900 ? tp->tm_year : tp->tm_year + 1900,
  155.            tp->tm_mon + 1, tp->tm_mday); /* January is 0, not 1 */
  156. #ifdef DEBUG
  157.     if (kerb_debug & 1) {
  158.     int i;
  159.     fprintf(stderr, "\nkerb_put_principal...");
  160.     for (i = 0; i < n; i++) {
  161.         krb_print_principal(&principal[i]);
  162.     }
  163.     }
  164. #endif
  165.     /* write database */
  166.     if (kerb_db_put_principal(principal, n) < 0) {
  167. #ifdef DEBUG
  168.     if (kerb_debug & 1)
  169.         fprintf(stderr, "\n%s: kerb_db_put_principal err", progname);
  170.     /* watch out for cache */
  171. #endif
  172.     return -1;
  173.     }
  174. #ifdef CACHE
  175.     /* write cache */
  176.     if (!kerb_cache_put_principal(principal, n)) {
  177. #ifdef DEBUG
  178.     if (kerb_debug & 1)
  179.         fprintf(stderr, "\n%s: kerb_cache_put_principal err", progname);
  180. #endif
  181.     return -1;
  182.     }
  183. #endif
  184.     return 0;
  185. }
  186.  
  187. int
  188. kerb_get_dba(name, inst, dba, max, more)
  189.     char   *name;        /* could have wild card */
  190.     char   *inst;        /* could have wild card */
  191.     Dba    *dba;
  192.     unsigned int max;        /* max number of name structs to return */
  193.     int    *more;        /* more tuples than room for */
  194.  
  195. {
  196.     int     found = 0;
  197. #ifdef CACHE
  198.     static int wild = 0;
  199. #endif
  200.     if (!init)
  201.     kerb_init();
  202.  
  203. #ifdef DEBUG
  204.     if (kerb_debug & 1)
  205.     fprintf(stderr, "\n%s: kerb_get_dba for %s %s max = %d\n",
  206.         progname, name, inst, max);
  207. #endif
  208.     /*
  209.      * if this is a request including a wild card, have to go to db
  210.      * since the cache may not be exhaustive. 
  211.      */
  212.  
  213.     /* clear the dba area */
  214.     bzero((char *) dba, max * sizeof(Dba));
  215.  
  216. #ifdef CACHE
  217.     /*
  218.      * so check to see if the name contains a wildcard "*" or "?", not
  219.      * preceeded by a backslash. 
  220.      */
  221.  
  222.     wild = 0;
  223.     if (index(name, '*') || index(name, '?') ||
  224.     index(inst, '*') || index(inst, '?'))
  225.     wild = 1;
  226.  
  227.     if (!wild) {
  228.     /* try the cache first */
  229.     found = kerb_cache_get_dba(name, inst, dba, max, more);
  230.     if (found)
  231.         return (found);
  232.     }
  233. #endif
  234.     /* If we didn't try cache, or it wasn't there, try db */
  235.     found = kerb_db_get_dba(name, inst, dba, max, more);
  236. #ifdef CACHE
  237.     /* try to insert dba(s) into cache if it was found */
  238.     if (found) {
  239.     kerb_cache_put_dba(dba, found);
  240.     }
  241. #endif
  242.     return (found);
  243. }
  244.