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