Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / ex / ex_init.c
blob8243bd67d1a6ae34953e2ac2b83b77e0654bf5ec
1 /* $NetBSD: ex_init.c,v 1.1.1.2 2008/05/18 14:31:16 aymeric Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 static const char sccsid[] = "Id: ex_init.c,v 10.31 2001/06/25 15:19:16 skimo Exp (Berkeley) Date: 2001/06/25 15:19:16";
16 #endif /* not lint */
18 #include <sys/param.h>
19 #include <sys/types.h> /* XXX: param.h may not have included types.h */
20 #include <sys/queue.h>
21 #include <sys/stat.h>
23 #include <bitstring.h>
24 #include <fcntl.h>
25 #include <limits.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "../common/common.h"
32 #include "tag.h"
33 #include "pathnames.h"
35 enum rc { NOEXIST, NOPERM, RCOK };
36 static enum rc exrc_isok __P((SCR *, struct stat *, const char *, int, int));
38 static int ex_run_file __P((SCR *, const char *));
41 * ex_screen_copy --
42 * Copy ex screen.
44 * PUBLIC: int ex_screen_copy __P((SCR *, SCR *));
46 int
47 ex_screen_copy(SCR *orig, SCR *sp)
49 EX_PRIVATE *oexp, *nexp;
51 /* Create the private ex structure. */
52 CALLOC_RET(orig, nexp, EX_PRIVATE *, 1, sizeof(EX_PRIVATE));
53 sp->ex_private = nexp;
55 /* Initialize queues. */
56 CIRCLEQ_INIT(&nexp->tq);
57 TAILQ_INIT(&nexp->tagfq);
58 LIST_INIT(&nexp->cscq);
60 if (orig == NULL) {
61 } else {
62 oexp = EXP(orig);
64 if (oexp->lastbcomm != NULL &&
65 (nexp->lastbcomm = v_wstrdup(sp, oexp->lastbcomm,
66 STRLEN(oexp->lastbcomm))) == NULL) {
67 msgq(sp, M_SYSERR, NULL);
68 return(1);
70 if (ex_tag_copy(orig, sp))
71 return (1);
73 return (0);
77 * ex_screen_end --
78 * End a vi screen.
80 * PUBLIC: int ex_screen_end __P((SCR *));
82 int
83 ex_screen_end(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, const char *, u_long *));
123 ex_optchange(SCR *sp, int offset, const char *str, u_long *valp)
125 switch (offset) {
126 case O_TAGS:
127 return (ex_tagf_alloc(sp, str));
129 return (0);
133 * ex_exrc --
134 * Read the EXINIT environment variable and the startup exrc files,
135 * and execute their commands.
137 * PUBLIC: int ex_exrc __P((SCR *));
140 ex_exrc(SCR *sp)
142 struct stat hsb, lsb;
143 char *p, path[MAXPATHLEN];
144 const CHAR_T *wp;
145 size_t wlen;
148 * Source the system, environment, $HOME and local .exrc values.
149 * Vi historically didn't check $HOME/.exrc if the environment
150 * variable EXINIT was set. This is all done before the file is
151 * read in, because things in the .exrc information can set, for
152 * example, the recovery directory.
154 * !!!
155 * While nvi can handle any of the options settings of historic vi,
156 * the converse is not true. Since users are going to have to have
157 * files and environmental variables that work with both, we use nvi
158 * versions of both the $HOME and local startup files if they exist,
159 * otherwise the historic ones.
161 * !!!
162 * For a discussion of permissions and when what .exrc files are
163 * read, see the comment above the exrc_isok() function below.
165 * !!!
166 * If the user started the historic of vi in $HOME, vi read the user's
167 * .exrc file twice, as $HOME/.exrc and as ./.exrc. We avoid this, as
168 * it's going to make some commands behave oddly, and I can't imagine
169 * anyone depending on it.
171 switch (exrc_isok(sp, &hsb, _PATH_SYSEXRC, 1, 0)) {
172 case NOEXIST:
173 case NOPERM:
174 break;
175 case RCOK:
176 if (ex_run_file(sp, _PATH_SYSEXRC))
177 return (1);
178 break;
181 /* Run the commands. */
182 if (EXCMD_RUNNING(sp->wp))
183 (void)ex_cmd(sp);
184 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE))
185 return (0);
187 if ((p = getenv("NEXINIT")) != NULL) {
188 CHAR2INT(sp, p, strlen(p) + 1, wp, wlen);
189 if (ex_run_str(sp, "NEXINIT", wp, wlen - 1, 1, 0))
190 return (1);
191 } else if ((p = getenv("EXINIT")) != NULL) {
192 CHAR2INT(sp, p, strlen(p) + 1, wp, wlen);
193 if (ex_run_str(sp, "EXINIT", wp, wlen - 1, 1, 0))
194 return (1);
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)) {
198 case NOEXIST:
199 (void)snprintf(path,
200 sizeof(path), "%s/%s", p, _PATH_EXRC);
201 if (exrc_isok(sp,
202 &hsb, path, 0, 1) == RCOK && ex_run_file(sp, path))
203 return (1);
204 break;
205 case NOPERM:
206 break;
207 case RCOK:
208 if (ex_run_file(sp, path))
209 return (1);
210 break;
214 /* Run the commands. */
215 if (EXCMD_RUNNING(sp->wp))
216 (void)ex_cmd(sp);
217 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE))
218 return (0);
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)) {
223 case NOEXIST:
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))
228 return (1);
229 break;
230 case NOPERM:
231 break;
232 case RCOK:
233 if ((lsb.st_dev != hsb.st_dev ||
234 lsb.st_ino != hsb.st_ino) &&
235 ex_run_file(sp, _PATH_NEXRC))
236 return (1);
237 break;
239 /* Run the commands. */
240 if (EXCMD_RUNNING(sp->wp))
241 (void)ex_cmd(sp);
242 if (F_ISSET(sp, SC_EXIT | SC_EXIT_FORCE))
243 return (0);
246 return (0);
250 * ex_run_file --
251 * Set up a file of ex commands to run.
253 static int
254 ex_run_file(SCR *sp, const char *name)
256 EXCMD cmd;
257 const CHAR_T *wp;
258 size_t wlen;
260 ex_cinit(sp, &cmd, C_SOURCE, 0, OOBLNO, OOBLNO, 0);
261 CHAR2INT(sp, name, strlen(name)+1, wp, wlen);
262 argv_exp0(sp, &cmd, wp, wlen - 1);
263 return (ex_source(sp, &cmd));
267 * ex_run_str --
268 * Set up a string of ex commands to run.
270 * PUBLIC: int ex_run_str __P((SCR *, char *, CHAR_T *, size_t, int, int));
273 ex_run_str(SCR *sp, const char *name, const CHAR_T *str, size_t len, int ex_flags, int nocopy)
275 WIN *wp;
276 EXCMD *ecp;
278 wp = sp->wp;
279 if (EXCMD_RUNNING(wp)) {
280 CALLOC_RET(sp, ecp, EXCMD *, 1, sizeof(EXCMD));
281 LIST_INSERT_HEAD(&wp->ecq, ecp, q);
282 } else
283 ecp = &wp->excmd;
285 F_INIT(ecp,
286 ex_flags ? E_BLIGNORE | E_NOAUTO | E_NOPRDEF | E_VLITONLY : 0);
288 if (nocopy)
289 ecp->cp = __UNCONST(str);
290 else
291 if ((ecp->cp = v_wstrdup(sp, str, len)) == NULL)
292 return (1);
293 ecp->clen = len;
295 if (name == NULL)
296 ecp->if_name = NULL;
297 else {
298 if ((ecp->if_name = v_strdup(sp, name, strlen(name))) == NULL)
299 return (1);
300 ecp->if_lno = 1;
301 F_SET(ecp, E_NAMEDISCARD);
304 return (0);
308 * exrc_isok --
309 * Check a .exrc file for source-ability.
311 * !!!
312 * Historically, vi read the $HOME and local .exrc files if they were owned
313 * by the user's real ID, or the "sourceany" option was set, regardless of
314 * any other considerations. We no longer support the sourceany option as
315 * it's a security problem of mammoth proportions. We require the system
316 * .exrc file to be owned by root, the $HOME .exrc file to be owned by the
317 * user's effective ID (or that the user's effective ID be root) and the
318 * local .exrc files to be owned by the user's effective ID. In all cases,
319 * the file cannot be writeable by anyone other than its owner.
321 * In O'Reilly ("Learning the VI Editor", Fifth Ed., May 1992, page 106),
322 * it notes that System V release 3.2 and later has an option "[no]exrc".
323 * The behavior is that local .exrc files are read only if the exrc option
324 * is set. The default for the exrc option was off, so, by default, local
325 * .exrc files were not read. The problem this was intended to solve was
326 * that System V permitted users to give away files, so there's no possible
327 * ownership or writeability test to ensure that the file is safe.
329 * POSIX 1003.2-1992 standardized exrc as an option. It required the exrc
330 * option to be off by default, thus local .exrc files are not to be read
331 * by default. The Rationale noted (incorrectly) that this was a change
332 * to historic practice, but correctly noted that a default of off improves
333 * system security. POSIX also required that vi check the effective user
334 * ID instead of the real user ID, which is why we've switched from historic
335 * practice.
337 * We initialize the exrc variable to off. If it's turned on by the system
338 * or $HOME .exrc files, and the local .exrc file passes the ownership and
339 * writeability tests, then we read it. This breaks historic 4BSD practice,
340 * but it gives us a measure of security on systems where users can give away
341 * files.
343 static enum rc
344 exrc_isok(SCR *sp, struct stat *sbp, const char *path, int rootown, int rootid)
346 enum { ROOTOWN, OWN, WRITER } etype;
347 uid_t euid;
348 int nf1, nf2;
349 char *a, *b, buf[MAXPATHLEN];
351 /* Check for the file's existence. */
352 if (stat(path, sbp))
353 return (NOEXIST);
355 /* Check ownership permissions. */
356 euid = geteuid();
357 if (!(rootown && sbp->st_uid == 0) &&
358 !(rootid && euid == 0) && sbp->st_uid != euid) {
359 etype = rootown ? ROOTOWN : OWN;
360 goto denied;
363 /* Check writeability. */
364 if (sbp->st_mode & (S_IWGRP | S_IWOTH)) {
365 etype = WRITER;
366 goto denied;
368 return (RCOK);
370 denied: a = msg_print(sp, path, &nf1);
371 if (strchr(path, '/') == NULL && getcwd(buf, sizeof(buf)) != NULL) {
372 b = msg_print(sp, buf, &nf2);
373 switch (etype) {
374 case ROOTOWN:
375 msgq(sp, M_ERR,
376 "125|%s/%s: not sourced: not owned by you or root",
377 b, a);
378 break;
379 case OWN:
380 msgq(sp, M_ERR,
381 "126|%s/%s: not sourced: not owned by you", b, a);
382 break;
383 case WRITER:
384 msgq(sp, M_ERR,
385 "127|%s/%s: not sourced: writeable by a user other than the owner", b, a);
386 break;
388 if (nf2)
389 FREE_SPACE(sp, b, 0);
390 } else
391 switch (etype) {
392 case ROOTOWN:
393 msgq(sp, M_ERR,
394 "128|%s: not sourced: not owned by you or root", a);
395 break;
396 case OWN:
397 msgq(sp, M_ERR,
398 "129|%s: not sourced: not owned by you", a);
399 break;
400 case WRITER:
401 msgq(sp, M_ERR,
402 "130|%s: not sourced: writeable by a user other than the owner", a);
403 break;
406 if (nf1)
407 FREE_SPACE(sp, a, 0);
408 return (NOPERM);