home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / vcron.t.Z / vcron.t / VCRON / user.c < prev    next >
Text File  |  1988-11-15  |  3KB  |  146 lines

  1. /* $Source: /usr/src/local/vix/cron/user.c,v $
  2.  * $Revision: 1.2 $
  3.  * $Log:    user.c,v $
  4.  * Revision 1.2  87/05/02  17:34:11  paul
  5.  * baseline for mod.sources release
  6.  * 
  7.  * Revision 1.1  87/01/26  23:48:47  paul
  8.  * Initial revision
  9.  */
  10.  
  11. /* Copyright 1987 by Vixie Enterprises
  12.  * All rights reserved
  13.  *
  14.  * Distribute freely, except: don't sell it, don't remove my name from the
  15.  * source or documentation (don't take credit for my work), mark your changes
  16.  * (don't get me blamed for your possible bugs), don't alter or remove this
  17.  * notice.  Commercial redistribution is negotiable; contact me for details.
  18.  *
  19.  * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
  20.  * I'll try to keep a version up to date.  I can be reached as follows:
  21.  * Paul Vixie, Vixie Enterprises, 329 Noe Street, San Francisco, CA, 94114,
  22.  * (415) 864-7013, {ucbvax!dual,ames,ucsfmis,lll-crg,sun}!ptsfa!vixie!paul.
  23.  */
  24.  
  25.  
  26. #include "cron.h"
  27. #include <errno.h>
  28.  
  29.  
  30. extern    int errno;
  31.  
  32. void
  33. free_user(u)
  34.     user    *u;
  35. {
  36.     void    free_entry();
  37.     int    free();
  38.     REG entry    *e;
  39.     REG char    **env;
  40.  
  41.     for (e = u->crontab;  e != NULL;  e = e->next)
  42.         free_entry(e);
  43.     for (env = u->envp;  *env;  env++)
  44.         (void) free(*env);
  45.     free((char *) u->envp);
  46.     (void) free((char *) u);
  47. }
  48.  
  49.  
  50. user *
  51. load_user(name, uid, gid, dir, xdir, shell)
  52.     char    *name;
  53.     int    uid;
  54.     int    gid;
  55.     char    *dir;
  56.     char    *xdir;
  57.     char    *shell;
  58. {
  59.     extern    char spooldir[];
  60.     char    *malloc(), *savestr(), *sprintf(),
  61.         **env_init(), **env_set();
  62.     int    load_env();
  63.     entry    *load_entry();
  64.  
  65.     char    filename[MAX_FNAME], envstr[MAX_TEMPSTR];
  66.     REG FILE    *file;
  67.     REG user    *u;
  68.     REG entry    *e;
  69.     REG int    status;
  70.  
  71.     strcpy( filename, spooldir);
  72.     strcat( filename, name);
  73.     if (!(file = fopen(filename, "r")))
  74.     {
  75.         /* file can't be opened.  if it's because the file doesn't
  76.          * exist, it isn't critical -- it means that the user doesn't
  77.          * have a crontab file.  just return NULL, and let our caller
  78.          * worry about it, or not.
  79.          *
  80.          * if it isn't ENOENT, we have a problem that the caller
  81.          * probably can't handle; print the error before returning.
  82.          */
  83.         if (errno != E_PNNF)
  84.             perror(filename);
  85.         return NULL;
  86.     }
  87.  
  88. #if DEBUGGING
  89.     Debug(DPARS, "load_user()\n");
  90. #endif
  91.  
  92.     /* file is open.  build user entry, then read the crontab file.
  93.      */
  94.     u = (user *) malloc(sizeof(user));
  95.     u->uid   = uid;
  96.     u->gid   = gid;
  97.     u->envp  = env_init();
  98.     u->crontab = NULL;
  99.  
  100.     /*
  101.      * do auto env settings that the user could reset in the cron tab
  102.      */
  103.     sprintf(envstr, "SHELL=%s", shell);
  104.     u->envp = env_set(u->envp, envstr);
  105.  
  106.     sprintf(envstr, "HOME=%s", dir);
  107.     u->envp = env_set(u->envp, envstr);
  108.  
  109.     sprintf(envstr, "XDIR=%s", xdir);
  110.     u->envp = env_set(u->envp, envstr);
  111.  
  112.     while (ERR != (status = load_env(envstr, file)))
  113.     {
  114.         if (status == TRUE)
  115.         {
  116.             u->envp = env_set(u->envp, envstr);
  117.         }
  118.         else
  119.         {
  120.             if ((e = load_entry(file, NULL)) != NULL)
  121.             {
  122.                 e->next = u->crontab;
  123.                 u->crontab = e;
  124.             }
  125.         }
  126.     }
  127.  
  128.     /*
  129.      * do automatic env settings that should have precedence over any
  130.      * set in the cron tab.
  131.      */
  132.     (void) sprintf(envstr, "USER=%s", name);
  133.     u->envp = env_set(u->envp, envstr);
  134.  
  135.     /*
  136.      * done. close file, return pointer to 'user' structure
  137.      */
  138.     fclose(file);
  139.  
  140. #if DEBUGGING
  141.     Debug(DPARS, "...load_user() done\n");
  142. #endif
  143.  
  144.     return u;
  145. }
  146.