user.c revision 29452
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[] =
2029452Scharnier	"$Id: user.c,v 1.5 1997/02/22 16:04:47 peter Exp $";
212311Sjkh#endif
222311Sjkh
232311Sjkh/* vix 26jan87 [log is in RCS file]
242311Sjkh */
252311Sjkh
262311Sjkh
272311Sjkh#include "cron.h"
282311Sjkh
292311Sjkh
302311Sjkhvoid
312311Sjkhfree_user(u)
322311Sjkh	user	*u;
332311Sjkh{
342311Sjkh	entry	*e, *ne;
352311Sjkh
362311Sjkh	free(u->name);
372311Sjkh	for (e = u->crontab;  e != NULL;  e = ne) {
382311Sjkh		ne = e->next;
392311Sjkh		free_entry(e);
402311Sjkh	}
412311Sjkh	free(u);
422311Sjkh}
432311Sjkh
442311Sjkh
452311Sjkhuser *
462311Sjkhload_user(crontab_fd, pw, name)
472311Sjkh	int		crontab_fd;
482311Sjkh	struct passwd	*pw;		/* NULL implies syscrontab */
492311Sjkh	char		*name;
502311Sjkh{
512311Sjkh	char	envstr[MAX_ENVSTR];
522311Sjkh	FILE	*file;
532311Sjkh	user	*u;
542311Sjkh	entry	*e;
552311Sjkh	int	status;
5620573Spst	char	**envp, **tenvp;
572311Sjkh
582311Sjkh	if (!(file = fdopen(crontab_fd, "r"))) {
5929452Scharnier		warn("fdopen on crontab_fd in load_user");
602311Sjkh		return NULL;
612311Sjkh	}
622311Sjkh
632311Sjkh	Debug(DPARS, ("load_user()\n"))
642311Sjkh
652311Sjkh	/* file is open.  build user entry, then read the crontab file.
662311Sjkh	 */
6720573Spst	if ((u = (user *) malloc(sizeof(user))) == NULL) {
6820573Spst		errno = ENOMEM;
6920573Spst		return NULL;
7020573Spst	}
7120573Spst	if ((u->name = strdup(name)) == NULL) {
7220573Spst		free(u);
7320573Spst		errno = ENOMEM;
7420573Spst		return NULL;
7520573Spst	}
762311Sjkh	u->crontab = NULL;
772311Sjkh
7820573Spst	/*
792311Sjkh	 * init environment.  this will be copied/augmented for each entry.
802311Sjkh	 */
8120573Spst	if ((envp = env_init()) == NULL) {
8220573Spst		free(u->name);
8320573Spst		free(u);
8420573Spst		return NULL;
8520573Spst	}
862311Sjkh
872311Sjkh	/*
882311Sjkh	 * load the crontab
892311Sjkh	 */
902311Sjkh	while ((status = load_env(envstr, file)) >= OK) {
912311Sjkh		switch (status) {
922311Sjkh		case ERR:
932311Sjkh			free_user(u);
942311Sjkh			u = NULL;
952311Sjkh			goto done;
962311Sjkh		case FALSE:
972311Sjkh			e = load_entry(file, NULL, pw, envp);
982311Sjkh			if (e) {
992311Sjkh				e->next = u->crontab;
1002311Sjkh				u->crontab = e;
1012311Sjkh			}
1022311Sjkh			break;
1032311Sjkh		case TRUE:
10420573Spst			if ((tenvp = env_set(envp, envstr))) {
10520573Spst				envp = tenvp;
10620573Spst			} else {
10720573Spst				free_user(u);
10820573Spst				u = NULL;
10920573Spst				goto done;
11020573Spst			}
1112311Sjkh			break;
1122311Sjkh		}
1132311Sjkh	}
1142311Sjkh
1152311Sjkh done:
1162311Sjkh	env_free(envp);
1172311Sjkh	fclose(file);
1182311Sjkh	Debug(DPARS, ("...load_user() done\n"))
1192311Sjkh	return u;
1202311Sjkh}
121