Commit | Line | Data |
---|---|---|
6441d3de WJ |
1 | #if !defined(lint) && !defined(LINT) |
2 | static char rcsid[] = "$Header: user.c,v 2.1 90/07/18 00:23:45 vixie Exp $"; | |
3 | #endif | |
4 | ||
5 | /* vix 26jan87 [log is in RCS file] | |
6 | */ | |
7 | ||
8 | /* Copyright 1988,1990 by Paul Vixie | |
9 | * All rights reserved | |
10 | * | |
11 | * Distribute freely, except: don't remove my name from the source or | |
12 | * documentation (don't take credit for my work), mark your changes (don't | |
13 | * get me blamed for your possible bugs), don't alter or remove this | |
14 | * notice. May be sold if buildable source is provided to buyer. No | |
15 | * warrantee of any kind, express or implied, is included with this | |
16 | * software; use at your own risk, responsibility for damages (if any) to | |
17 | * anyone resulting from the use of this software rests entirely with the | |
18 | * user. | |
19 | * | |
20 | * Send bug reports, bug fixes, enhancements, requests, flames, etc., and | |
21 | * I'll try to keep a version up to date. I can be reached as follows: | |
22 | * Paul Vixie, 329 Noe Street, San Francisco, CA, 94114, (415) 864-7013, | |
23 | * paul@vixie.sf.ca.us || {hoptoad,pacbell,decwrl,crash}!vixie!paul | |
24 | */ | |
25 | ||
26 | ||
27 | #include "cron.h" | |
28 | ||
29 | ||
30 | void | |
31 | free_user(u) | |
32 | user *u; | |
33 | { | |
34 | void free_entry(); | |
35 | int free(); | |
36 | entry *e; | |
37 | char **env; | |
38 | ||
39 | for (e = u->crontab; e != NULL; e = e->next) | |
40 | free_entry(e); | |
41 | for (env = u->envp; *env; env++) | |
42 | (void) free(*env); | |
43 | (void) free(u->envp); | |
44 | (void) free(u); | |
45 | } | |
46 | ||
47 | ||
48 | user * | |
49 | load_user(crontab_fd, name, uid, gid, dir, shell) | |
50 | int crontab_fd; | |
51 | char *name; | |
52 | int uid; | |
53 | int gid; | |
54 | char *dir; | |
55 | char *shell; | |
56 | { | |
57 | char *malloc(), *sprintf(), **env_init(), **env_set(); | |
58 | int load_env(); | |
59 | entry *load_entry(); | |
60 | ||
61 | char envstr[MAX_ENVSTR]; | |
62 | FILE *file; | |
63 | user *u; | |
64 | entry *e; | |
65 | int status; | |
66 | ||
67 | if (!(file = fdopen(crontab_fd, "r"))) | |
68 | { | |
69 | perror("fdopen on crontab_fd in load_user"); | |
70 | return NULL; | |
71 | } | |
72 | ||
73 | Debug(DPARS, ("load_user()\n")) | |
74 | ||
75 | /* file is open. build user entry, then read the crontab file. | |
76 | */ | |
77 | u = (user *) malloc(sizeof(user)); | |
78 | u->uid = uid; | |
79 | u->gid = gid; | |
80 | u->envp = env_init(); | |
81 | u->crontab = NULL; | |
82 | ||
83 | /* | |
84 | * do auto env settings that the user could reset in the cron tab | |
85 | */ | |
86 | sprintf(envstr, "SHELL=%s", (*shell) ?shell :"/bin/sh"); | |
87 | u->envp = env_set(u->envp, envstr); | |
88 | ||
89 | sprintf(envstr, "HOME=%s", dir); | |
90 | u->envp = env_set(u->envp, envstr); | |
91 | ||
92 | /* load the crontab | |
93 | */ | |
94 | while ((status = load_env(envstr, file)) >= OK) | |
95 | { | |
96 | if (status == TRUE) | |
97 | { | |
98 | u->envp = env_set(u->envp, envstr); | |
99 | } | |
100 | else | |
101 | { | |
102 | if (NULL != (e = load_entry(file, NULL))) | |
103 | { | |
104 | e->next = u->crontab; | |
105 | u->crontab = e; | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
110 | /* | |
111 | * do automatic env settings that should have precedence over any | |
112 | * set in the cron tab. | |
113 | */ | |
114 | (void) sprintf(envstr, "%s=%s", USERENV, name); | |
115 | u->envp = env_set(u->envp, envstr); | |
116 | ||
117 | /* | |
118 | * done. close file, return pointer to 'user' structure | |
119 | */ | |
120 | fclose(file); | |
121 | ||
122 | Debug(DPARS, ("...load_user() done\n")) | |
123 | ||
124 | return u; | |
125 | } |