12311Sjkh/* Copyright 1988,1990,1993,1994 by Paul Vixie
22311Sjkh * All rights reserved
32311Sjkh *
42311Sjkh * Distribute freely, except: don't remove my name from the source or
52311Sjkh * documentation (don't take credit for my work), mark your changes (don't
62311Sjkh * get me blamed for your possible bugs), don't alter or remove this
72311Sjkh * notice.  May be sold if buildable source is provided to buyer.  No
82311Sjkh * warrantee of any kind, express or implied, is included with this
92311Sjkh * software; use at your own risk, responsibility for damages (if any) to
102311Sjkh * anyone resulting from the use of this software rests entirely with the
112311Sjkh * user.
122311Sjkh *
132311Sjkh * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
142311Sjkh * I'll try to keep a version up to date.  I can be reached as follows:
152311Sjkh * Paul Vixie          <paul@vix.com>          uunet!decwrl!vixie!paul
162311Sjkh */
172311Sjkh
182311Sjkh#if !defined(lint) && !defined(LINT)
1929452Scharnierstatic const char rcsid[] =
2050479Speter  "$FreeBSD$";
212311Sjkh#endif
222311Sjkh
232311Sjkh/* vix 26jan87 [log is in RCS file]
242311Sjkh */
252311Sjkh
262311Sjkh
272311Sjkh#include "cron.h"
282311Sjkh
2930895Sachestatic char *User_name;
302311Sjkh
312311Sjkhvoid
322311Sjkhfree_user(u)
332311Sjkh	user	*u;
342311Sjkh{
352311Sjkh	entry	*e, *ne;
362311Sjkh
372311Sjkh	free(u->name);
382311Sjkh	for (e = u->crontab;  e != NULL;  e = ne) {
392311Sjkh		ne = e->next;
402311Sjkh		free_entry(e);
412311Sjkh	}
422311Sjkh	free(u);
432311Sjkh}
442311Sjkh
4530895Sachestatic void
4630895Sachelog_error(msg)
4730895Sache	char	*msg;
4830895Sache{
4930895Sache	log_it(User_name, getpid(), "PARSE", msg);
5030895Sache}
512311Sjkh
522311Sjkhuser *
532311Sjkhload_user(crontab_fd, pw, name)
542311Sjkh	int		crontab_fd;
552311Sjkh	struct passwd	*pw;		/* NULL implies syscrontab */
562311Sjkh	char		*name;
572311Sjkh{
582311Sjkh	char	envstr[MAX_ENVSTR];
592311Sjkh	FILE	*file;
602311Sjkh	user	*u;
612311Sjkh	entry	*e;
622311Sjkh	int	status;
6320573Spst	char	**envp, **tenvp;
642311Sjkh
652311Sjkh	if (!(file = fdopen(crontab_fd, "r"))) {
6629452Scharnier		warn("fdopen on crontab_fd in load_user");
672311Sjkh		return NULL;
682311Sjkh	}
692311Sjkh
702311Sjkh	Debug(DPARS, ("load_user()\n"))
712311Sjkh
722311Sjkh	/* file is open.  build user entry, then read the crontab file.
732311Sjkh	 */
7420573Spst	if ((u = (user *) malloc(sizeof(user))) == NULL) {
7520573Spst		errno = ENOMEM;
7620573Spst		return NULL;
7720573Spst	}
7820573Spst	if ((u->name = strdup(name)) == NULL) {
7920573Spst		free(u);
8020573Spst		errno = ENOMEM;
8120573Spst		return NULL;
8220573Spst	}
832311Sjkh	u->crontab = NULL;
842311Sjkh
8520573Spst	/*
862311Sjkh	 * init environment.  this will be copied/augmented for each entry.
872311Sjkh	 */
8820573Spst	if ((envp = env_init()) == NULL) {
8920573Spst		free(u->name);
9020573Spst		free(u);
9120573Spst		return NULL;
9220573Spst	}
932311Sjkh
942311Sjkh	/*
952311Sjkh	 * load the crontab
962311Sjkh	 */
972311Sjkh	while ((status = load_env(envstr, file)) >= OK) {
982311Sjkh		switch (status) {
992311Sjkh		case ERR:
1002311Sjkh			free_user(u);
1012311Sjkh			u = NULL;
1022311Sjkh			goto done;
1032311Sjkh		case FALSE:
10430895Sache			User_name = u->name;    /* for log_error */
10530895Sache			e = load_entry(file, log_error, pw, envp);
1062311Sjkh			if (e) {
1072311Sjkh				e->next = u->crontab;
1082311Sjkh				u->crontab = e;
1092311Sjkh			}
1102311Sjkh			break;
1112311Sjkh		case TRUE:
11220573Spst			if ((tenvp = env_set(envp, envstr))) {
11320573Spst				envp = tenvp;
11420573Spst			} else {
11520573Spst				free_user(u);
11620573Spst				u = NULL;
11720573Spst				goto done;
11820573Spst			}
1192311Sjkh			break;
1202311Sjkh		}
1212311Sjkh	}
1222311Sjkh
1232311Sjkh done:
1242311Sjkh	env_free(envp);
1252311Sjkh	fclose(file);
1262311Sjkh	Debug(DPARS, ("...load_user() done\n"))
1272311Sjkh	return u;
1282311Sjkh}
129