root/src/user.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. log_error
  2. free_user
  3. load_user

   1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
   2  * All rights reserved
   3  */
   4 
   5 /*
   6  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
   7  * Copyright (c) 1997,2000 by Internet Software Consortium, Inc.
   8  *
   9  * Permission to use, copy, modify, and distribute this software for any
  10  * purpose with or without fee is hereby granted, provided that the above
  11  * copyright notice and this permission notice appear in all copies.
  12  *
  13  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
  14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  15  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
  16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  19  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  20  */
  21 
  22 /* vix 26jan87 [log is in RCS file]
  23  */
  24 
  25 #include "config.h"
  26 
  27 #include <errno.h>
  28 #include <stdlib.h>
  29 #include <string.h>
  30 #include <unistd.h>
  31 
  32 #include "funcs.h"
  33 #include "globals.h"
  34 
  35 static const char *FileName;
  36 
  37 static void
  38 log_error (const char *msg)
     /* [previous][next][first][last][top][bottom][index][help]  */
  39 {
  40         log_it ("CRON", getpid (), msg, FileName, 0);
  41 }
  42 
  43 void
  44 free_user (user * u) {
     /* [previous][next][first][last][top][bottom][index][help]  */
  45         entry *e, *ne;
  46 
  47         if (!u) {
  48                 return;
  49         }
  50 
  51         free(u->name);
  52         free(u->tabname);
  53         for (e = u->crontab; e != NULL; e = ne) {
  54                 ne = e->next;
  55                 free_entry(e);
  56         }
  57 #ifdef WITH_SELINUX
  58         free_security_context(&(u->scontext));
  59 #endif
  60         free(u);
  61 }
  62 
  63 user *
  64 load_user (int crontab_fd, struct passwd *pw, const char *uname,
     /* [previous][next][first][last][top][bottom][index][help]  */
  65                    const char *fname, const char *tabname) {
  66         char envstr[MAX_ENVSTR];
  67         FILE *file;
  68         user *u;
  69         entry *e;
  70         int status = TRUE, save_errno = 0;
  71         char **envp = NULL, **tenvp;
  72 
  73         if (!(file = fdopen(crontab_fd, "r")))  {
  74                 save_errno = errno;
  75                 log_it(uname, getpid (), "FAILED", "fdopen on crontab_fd in load_user",
  76                         save_errno);
  77                 close(crontab_fd);
  78                 return (NULL);
  79         }
  80 
  81         Debug(DPARS, ("load_user()\n"));
  82         /* file is open.  build user entry, then read the crontab file.
  83          */
  84         if ((u = (user *) malloc (sizeof (user))) == NULL) {
  85                 save_errno = errno;
  86                 goto done;
  87         }
  88         memset(u, 0, sizeof(*u));
  89 
  90         if (((u->name = strdup(fname)) == NULL)
  91                 || ((u->tabname = strdup(tabname)) == NULL)) {
  92                 save_errno = errno;
  93                 goto done;
  94         }
  95 
  96         u->system = pw == NULL;
  97 
  98         /* init environment.  this will be copied/augmented for each entry.
  99         */
 100         if ((envp = env_init()) == NULL) {
 101                 save_errno = errno;
 102                 goto done;
 103         }
 104 
 105         if (env_set_from_environ(&envp) == FALSE) {
 106                 save_errno = errno;
 107                 goto done;
 108         }
 109 
 110 #ifdef WITH_SELINUX
 111         if (get_security_context(pw == NULL ? NULL : uname,
 112                 crontab_fd, &u->scontext, tabname) != 0) {
 113                 goto done;
 114         }
 115 #endif
 116         /* load the crontab
 117         */
 118         while ((status = load_env (envstr, file)) >= OK) {
 119                 switch (status) {
 120                         case FALSE:
 121                                 FileName = tabname;
 122                                 e = load_entry(file, log_error, pw, envp);
 123                                 if (e) {
 124                                         e->next = u->crontab;
 125                                         u->crontab = e;
 126                                 }
 127                                 break;
 128                         case TRUE:
 129                                 if ((tenvp = env_set (envp, envstr)) == NULL) {
 130                                         save_errno = errno;
 131                                         goto done;
 132                                 }
 133                                 envp = tenvp;
 134                                 break;
 135                 }
 136         }
 137 
 138 done:
 139         if (status == TRUE) {
 140                 log_it(uname, getpid(), "FAILED", "loading cron table",
 141                         save_errno);
 142                 free_user(u);
 143                 u = NULL;
 144         }
 145         if (envp)
 146                 env_free(envp);
 147         fclose(file);
 148         Debug(DPARS, ("...load_user() done\n"));
 149         errno = save_errno;
 150         return (u);
 151 }

/* [previous][next][first][last][top][bottom][index][help]  */