Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / cron / cron / user.c
1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
2  * All rights reserved
3  *
4  * Distribute freely, except: don't remove my name from the source or
5  * documentation (don't take credit for my work), mark your changes (don't
6  * get me blamed for your possible bugs), don't alter or remove this
7  * notice.  May be sold if buildable source is provided to buyer.  No
8  * warrantee of any kind, express or implied, is included with this
9  * software; use at your own risk, responsibility for damages (if any) to
10  * anyone resulting from the use of this software rests entirely with the
11  * user.
12  *
13  * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
14  * I'll try to keep a version up to date.  I can be reached as follows:
15  * Paul Vixie          <paul@vix.com>          uunet!decwrl!vixie!paul
16  *
17  * $FreeBSD: src/usr.sbin/cron/cron/user.c,v 1.8 1999/08/28 01:15:50 peter Exp $
18  * $DragonFly: src/usr.sbin/cron/cron/user.c,v 1.2 2003/06/17 04:29:53 dillon Exp $
19  */
20
21 /* vix 26jan87 [log is in RCS file]
22  */
23
24
25 #include "cron.h"
26
27 static char *User_name;
28
29 void
30 free_user(u)
31         user    *u;
32 {
33         entry   *e, *ne;
34
35         free(u->name);
36         for (e = u->crontab;  e != NULL;  e = ne) {
37                 ne = e->next;
38                 free_entry(e);
39         }
40         free(u);
41 }
42
43 static void
44 log_error(msg)
45         char    *msg;
46 {
47         log_it(User_name, getpid(), "PARSE", msg);
48 }
49
50 user *
51 load_user(crontab_fd, pw, name)
52         int             crontab_fd;
53         struct passwd   *pw;            /* NULL implies syscrontab */
54         char            *name;
55 {
56         char    envstr[MAX_ENVSTR];
57         FILE    *file;
58         user    *u;
59         entry   *e;
60         int     status;
61         char    **envp, **tenvp;
62
63         if (!(file = fdopen(crontab_fd, "r"))) {
64                 warn("fdopen on crontab_fd in load_user");
65                 return NULL;
66         }
67
68         Debug(DPARS, ("load_user()\n"))
69
70         /* file is open.  build user entry, then read the crontab file.
71          */
72         if ((u = (user *) malloc(sizeof(user))) == NULL) {
73                 errno = ENOMEM;
74                 return NULL;
75         }
76         if ((u->name = strdup(name)) == NULL) {
77                 free(u);
78                 errno = ENOMEM;
79                 return NULL;
80         }
81         u->crontab = NULL;
82
83         /* 
84          * init environment.  this will be copied/augmented for each entry.
85          */
86         if ((envp = env_init()) == NULL) {
87                 free(u->name);
88                 free(u);
89                 return NULL;
90         }
91
92         /*
93          * load the crontab
94          */
95         while ((status = load_env(envstr, file)) >= OK) {
96                 switch (status) {
97                 case ERR:
98                         free_user(u);
99                         u = NULL;
100                         goto done;
101                 case FALSE:
102                         User_name = u->name;    /* for log_error */
103                         e = load_entry(file, log_error, pw, envp);
104                         if (e) {
105                                 e->next = u->crontab;
106                                 u->crontab = e;
107                         }
108                         break;
109                 case TRUE:
110                         if ((tenvp = env_set(envp, envstr))) {
111                                 envp = tenvp;
112                         } else {
113                                 free_user(u);
114                                 u = NULL;
115                                 goto done;
116                         }
117                         break;
118                 }
119         }
120
121  done:
122         env_free(envp);
123         fclose(file);
124         Debug(DPARS, ("...load_user() done\n"))
125         return u;
126 }