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.
13 static const char sccsid
[] = "$Id: ex_init.c,v 10.27 1996/12/05 12:27:16 bostic Exp $ (Berkeley) $Date: 1996/12/05 12:27:16 $";
16 #include <sys/param.h>
17 #include <sys/types.h> /* XXX: param.h may not have included types.h */
18 #include <sys/queue.h>
21 #include <bitstring.h>
29 #include "../common/common.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 *));
42 * PUBLIC: int ex_screen_copy __P((SCR *, SCR *));
45 ex_screen_copy(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
);
63 if (oexp
->lastbcomm
!= NULL
&&
64 (nexp
->lastbcomm
= strdup(oexp
->lastbcomm
)) == NULL
) {
65 msgq(sp
, M_SYSERR
, NULL
);
68 if (ex_tag_copy(orig
, sp
))
78 * PUBLIC: int ex_screen_end __P((SCR *));
87 if ((exp
= EXP(sp
)) == NULL
)
92 /* Close down script connections. */
93 if (F_ISSET(sp
, SC_SCRIPT
) && sscr_end(sp
))
102 if (exp
->lastbcomm
!= NULL
)
103 free(exp
->lastbcomm
);
108 /* Free private memory. */
110 sp
->ex_private
= NULL
;
117 * Handle change of options for ex.
119 * PUBLIC: int ex_optchange __P((SCR *, int, char *, u_long *));
122 ex_optchange(sp
, offset
, str
, valp
)
130 return (ex_tagf_alloc(sp
, str
));
137 * Read the EXINIT environment variable and the startup exrc files,
138 * and execute their commands.
140 * PUBLIC: int ex_exrc __P((SCR *));
146 struct stat hsb
, lsb
;
147 char *p
, path
[MAXPATHLEN
];
150 * Source the system, environment, $HOME and local .exrc values.
151 * Vi historically didn't check $HOME/.exrc if the environment
152 * variable EXINIT was set. This is all done before the file is
153 * read in, because things in the .exrc information can set, for
154 * example, the recovery directory.
157 * While nvi can handle any of the options settings of historic vi,
158 * the converse is not true. Since users are going to have to have
159 * files and environmental variables that work with both, we use nvi
160 * versions of both the $HOME and local startup files if they exist,
161 * otherwise the historic ones.
164 * For a discussion of permissions and when what .exrc files are
165 * read, see the comment above the exrc_isok() function below.
168 * If the user started the historic of vi in $HOME, vi read the user's
169 * .exrc file twice, as $HOME/.exrc and as ./.exrc. We avoid this, as
170 * it's going to make some commands behave oddly, and I can't imagine
171 * anyone depending on it.
173 switch (exrc_isok(sp
, &hsb
, _PATH_SYSEXRC
, 1, 0)) {
178 if (ex_run_file(sp
, _PATH_SYSEXRC
))
183 /* Run the commands. */
184 if (EXCMD_RUNNING(sp
->gp
))
186 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
))
189 if ((p
= getenv("NEXINIT")) != NULL
) {
190 if (ex_run_str(sp
, "NEXINIT", p
, strlen(p
), 1, 0))
192 } else if ((p
= getenv("EXINIT")) != NULL
) {
193 if (ex_run_str(sp
, "EXINIT", p
, strlen(p
), 1, 0))
195 } else if ((p
= getenv("HOME")) != NULL
&& *p
) {
196 (void)snprintf(path
, sizeof(path
), "%s/%s", p
, _PATH_NEXRC
);
197 switch (exrc_isok(sp
, &hsb
, path
, 0, 1)) {
200 sizeof(path
), "%s/%s", p
, _PATH_EXRC
);
202 &hsb
, path
, 0, 1) == RCOK
&& ex_run_file(sp
, path
))
208 if (ex_run_file(sp
, path
))
214 /* Run the commands. */
215 if (EXCMD_RUNNING(sp
->gp
))
217 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
))
220 /* Previous commands may have set the exrc option. */
221 if (O_ISSET(sp
, O_EXRC
)) {
222 switch (exrc_isok(sp
, &lsb
, _PATH_NEXRC
, 0, 0)) {
224 if (exrc_isok(sp
, &lsb
, _PATH_EXRC
, 0, 0) == RCOK
&&
225 (lsb
.st_dev
!= hsb
.st_dev
||
226 lsb
.st_ino
!= hsb
.st_ino
) &&
227 ex_run_file(sp
, _PATH_EXRC
))
233 if ((lsb
.st_dev
!= hsb
.st_dev
||
234 lsb
.st_ino
!= hsb
.st_ino
) &&
235 ex_run_file(sp
, _PATH_NEXRC
))
239 /* Run the commands. */
240 if (EXCMD_RUNNING(sp
->gp
))
242 if (F_ISSET(sp
, SC_EXIT
| SC_EXIT_FORCE
))
251 * Set up a file of ex commands to run.
254 ex_run_file(sp
, name
)
260 ex_cinit(sp
, &cmd
, C_SOURCE
, 0, OOBLNO
, OOBLNO
, 0);
261 argv_exp0(sp
, &cmd
, name
, strlen(name
));
262 return (ex_source(sp
, &cmd
));
267 * Set up a string of ex commands to run.
269 * PUBLIC: int ex_run_str __P((SCR *, char *, char *, size_t, int, int));
272 ex_run_str(sp
, name
, str
, len
, ex_flags
, nocopy
)
276 int ex_flags
, nocopy
;
282 if (EXCMD_RUNNING(gp
)) {
283 CALLOC_RET(sp
, ecp
, EXCMD
*, 1, sizeof(EXCMD
));
284 LIST_INSERT_HEAD(&gp
->ecq
, ecp
, q
);
289 ex_flags
? E_BLIGNORE
| E_NOAUTO
| E_NOPRDEF
| E_VLITONLY
: 0);
294 if ((ecp
->cp
= v_strdup(sp
, str
, len
)) == NULL
)
301 if ((ecp
->if_name
= v_strdup(sp
, name
, strlen(name
))) == NULL
)
304 F_SET(ecp
, E_NAMEDISCARD
);
312 * Check a .exrc file for source-ability.
315 * Historically, vi read the $HOME and local .exrc files if they were owned
316 * by the user's real ID, or the "sourceany" option was set, regardless of
317 * any other considerations. We no longer support the sourceany option as
318 * it's a security problem of mammoth proportions. We require the system
319 * .exrc file to be owned by root, the $HOME .exrc file to be owned by the
320 * user's effective ID (or that the user's effective ID be root) and the
321 * local .exrc files to be owned by the user's effective ID. In all cases,
322 * the file cannot be writeable by anyone other than its owner.
324 * In O'Reilly ("Learning the VI Editor", Fifth Ed., May 1992, page 106),
325 * it notes that System V release 3.2 and later has an option "[no]exrc".
326 * The behavior is that local .exrc files are read only if the exrc option
327 * is set. The default for the exrc option was off, so, by default, local
328 * .exrc files were not read. The problem this was intended to solve was
329 * that System V permitted users to give away files, so there's no possible
330 * ownership or writeability test to ensure that the file is safe.
332 * POSIX 1003.2-1992 standardized exrc as an option. It required the exrc
333 * option to be off by default, thus local .exrc files are not to be read
334 * by default. The Rationale noted (incorrectly) that this was a change
335 * to historic practice, but correctly noted that a default of off improves
336 * system security. POSIX also required that vi check the effective user
337 * ID instead of the real user ID, which is why we've switched from historic
340 * We initialize the exrc variable to off. If it's turned on by the system
341 * or $HOME .exrc files, and the local .exrc file passes the ownership and
342 * writeability tests, then we read it. This breaks historic 4BSD practice,
343 * but it gives us a measure of security on systems where users can give away
347 exrc_isok(sp
, sbp
, path
, rootown
, rootid
)
353 enum { ROOTOWN
, OWN
, WRITER
} etype
;
356 char *a
, *b
, buf
[MAXPATHLEN
];
358 /* Check for the file's existence. */
362 /* Check ownership permissions. */
364 if (!(rootown
&& sbp
->st_uid
== 0) &&
365 !(rootid
&& euid
== 0) && sbp
->st_uid
!= euid
) {
366 etype
= rootown
? ROOTOWN
: OWN
;
370 /* Check writeability. */
371 if (sbp
->st_mode
& (S_IWGRP
| S_IWOTH
)) {
377 denied
: a
= msg_print(sp
, path
, &nf1
);
378 if (strchr(path
, '/') == NULL
&& getcwd(buf
, sizeof(buf
)) != NULL
) {
379 b
= msg_print(sp
, buf
, &nf2
);
383 "125|%s/%s: not sourced: not owned by you or root",
388 "126|%s/%s: not sourced: not owned by you", b
, a
);
392 "127|%s/%s: not sourced: writeable by a user other than the owner", b
, a
);
396 FREE_SPACE(sp
, b
, 0);
401 "128|%s: not sourced: not owned by you or root", a
);
405 "129|%s: not sourced: not owned by you", a
);
409 "130|%s: not sourced: writeable by a user other than the owner", a
);
414 FREE_SPACE(sp
, a
, 0);