1 /* $OpenBSD: user.c,v 1.16 2015/11/04 20:28:17 millert Exp $ */ 2 3 /* Copyright 1988,1990,1993,1994 by Paul Vixie 4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (c) 1997,2000 by Internet Software Consortium, Inc. 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/types.h> 21 22 #include <bitstring.h> /* for structs.h */ 23 #include <ctype.h> 24 #include <errno.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <time.h> /* for structs.h */ 29 30 #include "macros.h" 31 #include "structs.h" 32 #include "funcs.h" 33 34 void 35 free_user(user *u) 36 { 37 entry *e, *ne; 38 39 free(u->name); 40 for (e = u->crontab; e != NULL; e = ne) { 41 ne = e->next; 42 free_entry(e); 43 } 44 free(u); 45 } 46 47 user * 48 load_user(int crontab_fd, struct passwd *pw, const char *name) 49 { 50 char envstr[MAX_ENVSTR]; 51 FILE *file; 52 user *u; 53 entry *e; 54 int status, save_errno; 55 char **envp, **tenvp; 56 57 if (!(file = fdopen(crontab_fd, "r"))) { 58 perror("fdopen on crontab_fd in load_user"); 59 return (NULL); 60 } 61 62 /* file is open. build user entry, then read the crontab file. 63 */ 64 if ((u = malloc(sizeof(user))) == NULL) 65 return (NULL); 66 if ((u->name = strdup(name)) == NULL) { 67 save_errno = errno; 68 free(u); 69 errno = save_errno; 70 return (NULL); 71 } 72 u->crontab = NULL; 73 74 /* init environment. this will be copied/augmented for each entry. 75 */ 76 if ((envp = env_init()) == NULL) { 77 save_errno = errno; 78 free(u->name); 79 free(u); 80 errno = save_errno; 81 return (NULL); 82 } 83 84 /* load the crontab 85 */ 86 while ((status = load_env(envstr, file)) >= 0) { 87 switch (status) { 88 case FALSE: 89 e = load_entry(file, NULL, pw, envp); 90 if (e) { 91 e->next = u->crontab; 92 u->crontab = e; 93 } 94 break; 95 case TRUE: 96 if ((tenvp = env_set(envp, envstr)) == NULL) { 97 save_errno = errno; 98 free_user(u); 99 u = NULL; 100 errno = save_errno; 101 goto done; 102 } 103 envp = tenvp; 104 break; 105 } 106 } 107 108 done: 109 env_free(envp); 110 fclose(file); 111 return (u); 112 } 113