align CHAR_T string in log
[nvi.git] / ex / ex_init.c
blob05c4c445b7d65f570e81bd8deb80892de36e0294
1 /*-
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_init.c,v 10.29 2000/07/14 14:29:20 skimo Exp $ (Berkeley) $Date: 2000/07/14 14:29:20 $";
14 #endif /* not lint */
16 #include <sys/param.h>
17 #include <sys/types.h> /* XXX: param.h may not have included types.h */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
21 #include <bitstring.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "../common/common.h"
30 #include "tag.h"
31 #include "pathnames.h"
33 enum rc { NOEXIST, NOPERM, RCOK };
34 static enum rc exrc_isok __P((SCR *, struct stat *, char *, int, int));
36 static int ex_run_file __P((SCR *, char *));
39 * ex_screen_copy --
40 * Copy ex screen.
42 * PUBLIC: int ex_screen_copy __P((SCR *, SCR *));
44 int
45 ex_screen_copy(orig, sp)
46 SCR *orig, *sp;
48 EX_PRIVATE *oexp, *nexp;
50 /* Create the private ex structure. */
51 CALLOC_RET(orig, nexp, EX_PRIVATE *, 1, sizeof(EX_PRIVATE));
52 sp->ex_private = nexp;
54 /* Initialize queues. */
55 CIRCLEQ_INIT(&nexp->tq);
56 TAILQ_INIT(&nexp->tagfq);
57 LIST_INIT(&nexp->cscq);
59 if (orig == NULL) {
60 } else {
61 oexp = EXP(orig);
63 if (oexp->lastbcomm != NULL &&
64 (nexp->lastbcomm = v_wstrdup(sp, oexp->lastbcomm,
65 v_strlen(oexp->lastbcomm))) == NULL) {
66 msgq(sp, M_SYSERR, NULL);
67 return(1);
69 if (ex_tag_copy(orig, sp))
70 return (1);
72 return (0);
76 * ex_screen_end --
77 * End a vi screen.
79 * PUBLIC: int ex_screen_end __P((SCR *));
81 int
82 ex_screen_end(sp)
83 SCR *sp;
85 EX_PRIVATE *exp;
86 int rval;
88 if ((exp = EXP(sp)) == NULL)
89 return (0);
91 rval = 0;
93 /* Close down script connections. */
94 if (F_ISSET(sp, SC_SCRIPT) && sscr_end(sp))
95 rval = 1;
97 if (argv_free(sp))
98 rval = 1;
100 if (exp->ibp != NULL)
101 free(exp->ibp);
103 if (exp->lastbcomm != NULL)
104 free(exp->lastbcomm);
106 if (ex_tag_free(sp))
107 rval = 1;
109 /* Free private memory. */
110 free(exp);
111 sp->ex_private = NULL;
113 return (rval);
117 * ex_optchange --
118 * Handle change of options for ex.
120 * PUBLIC: int ex_optchange __P((SCR *, int, char *, u_long *));
123 ex_optchange(sp, offset, str, valp)
124 SCR *sp;
125 int offset;
126 char *str;
127 u_long *valp;
129 switch (offset) {
130 case O_TAGS:
131 return (ex_tagf_alloc(sp, str));
133 return (0);
137 * ex_exrc --
138 * Read the EXINIT environment variable and the startup exrc files,
139 * and execute their commands.
141 * PUBLIC: int ex_exrc __P((SCR *));
144 ex_exrc(sp)
145 SCR *sp;
147 struct stat hsb, lsb;
148 char *p, path[MAXPATHLEN];
149 CHAR_T *wp;
150 size_t wlen;
153 * Source the system, environment, $HOME and local .exrc values.
154 * Vi historically didn't check $HOME/.exrc if the environment
155 * variable EXINIT was set. This is all done before the file is
156 * read in, because things in the .exrc information can set, for
157 * example, the recovery directory.
159 * !!!
160 * While nvi can handle any of the options settings of historic vi,
161 * the converse is not true. Since users are going to have to have
162 * files and environmental variables that work with both, we use nvi
163 * versions of both the $HOME and local startup files if they exist,
164 * otherwise the historic ones.
166 * !!!
167 * For a discussion of permissions and when what .exrc files are
168 * read, see the comment above the exrc_isok() function below.
170 * !!!
171 * If the user started the historic of vi in $HOME, vi read the user's
172 * .exrc file twice, as $HOME/.exrc and as ./.exrc. We avoid this, as
173 * it's going to make some commands behave oddly, and I can't imagine
174 * anyone depending on it.
176 switch (exrc_isok(sp, &hsb, _PATH_SYSEXRC, 1, 0)) {
177 case NOEXIST:
178 case NOPERM:
179 break;
180 case RCOK:
181 if (ex_run_file(sp, _PATH_SYSEXRC))
182 return (1);
183 break;
186 /* Run the commands. */
187 if (EXCMD_RUNNING(sp->wp))
188 (void)ex_cmd(sp);
189 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE))
190 return (0);
192 if ((p = getenv("NEXINIT")) != NULL) {
193 CHAR2INT(sp, p, strlen(p) + 1, wp, wlen);
194 if (ex_run_str(sp, "NEXINIT", wp, wlen - 1, 1, 0))
195 return (1);
196 } else if ((p = getenv("EXINIT")) != NULL) {
197 CHAR2INT(sp, p, strlen(p) + 1, wp, wlen);
198 if (ex_run_str(sp, "EXINIT", wp, wlen - 1, 1, 0))
199 return (1);
200 } else if ((p = getenv("HOME")) != NULL && *p) {
201 (void)snprintf(path, sizeof(path), "%s/%s", p, _PATH_NEXRC);
202 switch (exrc_isok(sp, &hsb, path, 0, 1)) {
203 case NOEXIST:
204 (void)snprintf(path,
205 sizeof(path), "%s/%s", p, _PATH_EXRC);
206 if (exrc_isok(sp,
207 &hsb, path, 0, 1) == RCOK && ex_run_file(sp, path))
208 return (1);
209 break;
210 case NOPERM:
211 break;
212 case RCOK:
213 if (ex_run_file(sp, path))
214 return (1);
215 break;
219 /* Run the commands. */
220 if (EXCMD_RUNNING(sp->wp))
221 (void)ex_cmd(sp);
222 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE))
223 return (0);
225 /* Previous commands may have set the exrc option. */
226 if (O_ISSET(sp, O_EXRC)) {
227 switch (exrc_isok(sp, &lsb, _PATH_NEXRC, 0, 0)) {
228 case NOEXIST:
229 if (exrc_isok(sp, &lsb, _PATH_EXRC, 0, 0) == RCOK &&
230 (lsb.st_dev != hsb.st_dev ||
231 lsb.st_ino != hsb.st_ino) &&
232 ex_run_file(sp, _PATH_EXRC))
233 return (1);
234 break;
235 case NOPERM:
236 break;
237 case RCOK:
238 if ((lsb.st_dev != hsb.st_dev ||
239 lsb.st_ino != hsb.st_ino) &&
240 ex_run_file(sp, _PATH_NEXRC))
241 return (1);
242 break;
244 /* Run the commands. */
245 if (EXCMD_RUNNING(sp->wp))
246 (void)ex_cmd(sp);
247 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE))
248 return (0);
251 return (0);
255 * ex_run_file --
256 * Set up a file of ex commands to run.
258 static int
259 ex_run_file(sp, name)
260 SCR *sp;
261 char *name;
263 EXCMD cmd;
264 CHAR_T *wp;
265 size_t wlen;
267 ex_cinit(sp, &cmd, C_SOURCE, 0, OOBLNO, OOBLNO, 0);
268 CHAR2INT(sp, name, strlen(name)+1, wp, wlen);
269 argv_exp0(sp, &cmd, wp, wlen - 1);
270 return (ex_source(sp, &cmd));
274 * ex_run_str --
275 * Set up a string of ex commands to run.
277 * PUBLIC: int ex_run_str __P((SCR *, char *, CHAR_T *, size_t, int, int));
280 ex_run_str(sp, name, str, len, ex_flags, nocopy)
281 SCR *sp;
282 char *name;
283 CHAR_T *str;
284 size_t len;
285 int ex_flags, nocopy;
287 WIN *wp;
288 EXCMD *ecp;
290 wp = sp->wp;
291 if (EXCMD_RUNNING(wp)) {
292 CALLOC_RET(sp, ecp, EXCMD *, 1, sizeof(EXCMD));
293 LIST_INSERT_HEAD(&wp->ecq, ecp, q);
294 } else
295 ecp = &wp->excmd;
297 F_INIT(ecp,
298 ex_flags ? E_BLIGNORE | E_NOAUTO | E_NOPRDEF | E_VLITONLY : 0);
300 if (nocopy)
301 ecp->cp = str;
302 else
303 if ((ecp->cp = v_wstrdup(sp, str, len)) == NULL)
304 return (1);
305 ecp->clen = len;
307 if (name == NULL)
308 ecp->if_name = NULL;
309 else {
310 if ((ecp->if_name = v_strdup(sp, name, strlen(name))) == NULL)
311 return (1);
312 ecp->if_lno = 1;
313 F_SET(ecp, E_NAMEDISCARD);
316 return (0);
320 * exrc_isok --
321 * Check a .exrc file for source-ability.
323 * !!!
324 * Historically, vi read the $HOME and local .exrc files if they were owned
325 * by the user's real ID, or the "sourceany" option was set, regardless of
326 * any other considerations. We no longer support the sourceany option as
327 * it's a security problem of mammoth proportions. We require the system
328 * .exrc file to be owned by root, the $HOME .exrc file to be owned by the
329 * user's effective ID (or that the user's effective ID be root) and the
330 * local .exrc files to be owned by the user's effective ID. In all cases,
331 * the file cannot be writeable by anyone other than its owner.
333 * In O'Reilly ("Learning the VI Editor", Fifth Ed., May 1992, page 106),
334 * it notes that System V release 3.2 and later has an option "[no]exrc".
335 * The behavior is that local .exrc files are read only if the exrc option
336 * is set. The default for the exrc option was off, so, by default, local
337 * .exrc files were not read. The problem this was intended to solve was
338 * that System V permitted users to give away files, so there's no possible
339 * ownership or writeability test to ensure that the file is safe.
341 * POSIX 1003.2-1992 standardized exrc as an option. It required the exrc
342 * option to be off by default, thus local .exrc files are not to be read
343 * by default. The Rationale noted (incorrectly) that this was a change
344 * to historic practice, but correctly noted that a default of off improves
345 * system security. POSIX also required that vi check the effective user
346 * ID instead of the real user ID, which is why we've switched from historic
347 * practice.
349 * We initialize the exrc variable to off. If it's turned on by the system
350 * or $HOME .exrc files, and the local .exrc file passes the ownership and
351 * writeability tests, then we read it. This breaks historic 4BSD practice,
352 * but it gives us a measure of security on systems where users can give away
353 * files.
355 static enum rc
356 exrc_isok(sp, sbp, path, rootown, rootid)
357 SCR *sp;
358 struct stat *sbp;
359 char *path;
360 int rootown, rootid;
362 enum { ROOTOWN, OWN, WRITER } etype;
363 uid_t euid;
364 int nf1, nf2;
365 char *a, *b, buf[MAXPATHLEN];
367 /* Check for the file's existence. */
368 if (stat(path, sbp))
369 return (NOEXIST);
371 /* Check ownership permissions. */
372 euid = geteuid();
373 if (!(rootown && sbp->st_uid == 0) &&
374 !(rootid && euid == 0) && sbp->st_uid != euid) {
375 etype = rootown ? ROOTOWN : OWN;
376 goto denied;
379 /* Check writeability. */
380 if (sbp->st_mode & (S_IWGRP | S_IWOTH)) {
381 etype = WRITER;
382 goto denied;
384 return (RCOK);
386 denied: a = msg_print(sp, path, &nf1);
387 if (strchr(path, '/') == NULL && getcwd(buf, sizeof(buf)) != NULL) {
388 b = msg_print(sp, buf, &nf2);
389 switch (etype) {
390 case ROOTOWN:
391 msgq(sp, M_ERR,
392 "125|%s/%s: not sourced: not owned by you or root",
393 b, a);
394 break;
395 case OWN:
396 msgq(sp, M_ERR,
397 "126|%s/%s: not sourced: not owned by you", b, a);
398 break;
399 case WRITER:
400 msgq(sp, M_ERR,
401 "127|%s/%s: not sourced: writeable by a user other than the owner", b, a);
402 break;
404 if (nf2)
405 FREE_SPACE(sp, b, 0);
406 } else
407 switch (etype) {
408 case ROOTOWN:
409 msgq(sp, M_ERR,
410 "128|%s: not sourced: not owned by you or root", a);
411 break;
412 case OWN:
413 msgq(sp, M_ERR,
414 "129|%s: not sourced: not owned by you", a);
415 break;
416 case WRITER:
417 msgq(sp, M_ERR,
418 "130|%s: not sourced: writeable by a user other than the owner", a);
419 break;
422 if (nf1)
423 FREE_SPACE(sp, a, 0);
424 return (NOPERM);