Skip to contentSkip to conversation

Here-document...

DISCUSSION Oleg Verych 2 participants 2 messages Oct 30, 2007 – Nov 11, 2007

lore
Share & tools
lore
#0 lore raw

Wanted to make simple makeself analog......

____________
#!/bin/sh -e
set -e
trap 'echo "${0##*/}: Unexpected abort, exiting..."' 0 umask 377

F="./bina$$"

{ dd bs=65536 0<&8 1>"$F" 8<&- 2>&-
exec 8<&-
chmod u+x "$F"
echo 1>&2 "Running binary \`$F'...
"
(exec "$F")
trap "{
echo '
Exit status: '$?
test $? -eq 2 && \
echo '(Possibly kernel and fallback \/bin/sh\ couldn'\''t execute this file, because it is a broken binary or unspecified/unsupported script. File not removed.)
' # disabled for show || rm -f "$F"
} 1>&2" 0
exit 0
} 8<<""
======================

If works with payload like:
_________________
#!/usr/bin/printf $$Running from file: %s\n
not used and ran
=================

olecom@flower:/tmp$ sh bixelf
Running binary `./bina13936'...

$$Running from file: ./bina13936

Exit status: 0
olecom@flower:/tmp$
=================

It fails with binaries due to NUL (and maybe something more) removal.

== Another issues ==

Looking at parser, this piece of code:

#

    TRACE(("Here document %d\n", n->type));
    if (! noexpand(wordtext) || (i = strlen(wordtext)) == 0 || i > EOFMARKLEN)
            synerror("Illegal eof marker for << redirection");

#

[0] google.com/search?q=here-document+opengroup+2004

In [0] it's stated, that delimiter isn't evaluated (expanded), only quoiting must be checked. That if() seems to be completely bogus.

* strlen() == 0 isn't triggered -- empty delimiter is handled by syntax;

* check for max line isn't needed, i think;

* noexpand() checks for special symbols, while [0] says, that `word' must be used as is (with escaping/quotes removed).

pdksh seems to do so, i.e $WHATEVER can be used and it is not expanded. Bash doesn't care also, but i don't know if it expands that.

pdksh doesn't like no delimiter in the end of file with, thus no expansion is obvious (but such behavior is bad, i.e. no possibility to leave whatever stuff until the EOF):

#

olecom@flower:/tmp$ pdksh bixelf
bixelf[27]: here document `$NULL' unclosed
bixelf: Unexpected abort, exiting...
olecom@flower:/tmp$
olecom@flower:/tmp$ bash bixelf
Running binary `./bina26446'...

$$Running from file: /tmp/bina26446

Exit status: 0

olecom@flower:/tmp$ sh bixelf
bixelf: 23: Syntax error: Illegal eof marker for << redirection
bixelf: Unexpected abort, exiting...
olecom@flower:/tmp$
#
== * ==

Placing binary stuff safely with optimized reading/writing by big blocks without parsing, if delimiter is "", are things, that i'd like to propose and possibly fix/implement soon.
--
-o--=O`C
#oo'L O
<___=E M

#1 lore raw
Quoted 3 lines · Oleg Verych

} 8<<""
======================

Actually this (the empty delim) only works with dash by accident. I've tried bash and pdksh and they both terminate on the first empty line which is what you would expect rather than EOF. The real Korn shell does something completely different.

I've fixed this in dash to conform to bash/pdksh.

Quoted 2 lines

In [0] it's stated, that delimiter isn't evaluated (expanded), only quoiting must be checked. That if() seems to be completely bogus.

OK I agree. The reason it was there is because the parser would have already replaced the dollar sign by an internal representation.

I've fixed it properly in the patch below.

Quoted 3 lines

Placing binary stuff safely with optimized reading/writing by big blocks without parsing, if delimiter is "", are things, that i'd like to propose and possibly fix/implement soon.

Sorry but I can't see how we can get this to work in a meaningful way.

Cheers,

Signature · 5 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
ChangeLog +4 -0
index d50d36c..ad98810 100644
@@ -1,3 +1,7 @@
+2007-11-11  Herbert Xu <[email protected]>
+
+	* Removed noexpand/length check on eofmark.
+
 2007-10-20  Herbert Xu <[email protected]>
 
 	* Added configure --enable-glob and --enable-fnmatch options.
src/input.c +0 -27
index 11f7a3f..bc84e57 100644
@@ -125,33 +125,6 @@ RESET {
 
 
 /*
- * Read a line from the script.
- */
-
-char *
-pfgets(char *line, int len)
-{
-	char *p = line;
-	int nleft = len;
-	int c;
-
-	while (--nleft > 0) {
-		c = pgetc2();
-		if (c == PEOF) {
-			if (p == line)
-				return NULL;
-			break;
-		}
-		*p++ = c;
-		if (c == '\n')
-			break;
-	}
-	*p = '\0';
-	return line;
-}
-
-
-/*
  * Read a character from the script, returning PEOF on end of file.
  * Nul characters in the input are silently discarded.
  */
src/input.h +0 -1
index 1ed9ddf..1d6f528 100644
@@ -50,7 +50,6 @@ extern int plinno;
 extern int parsenleft;		/* number of characters left in input buffer */
 extern char *parsenextc;	/* next character in input buffer */
 
-char *pfgets(char *, int);
 int pgetc(void);
 int pgetc2(void);
 int preadbuffer(void);
src/parser.c +39 -49

148 lines not on this page · raw message

src/parser.h +1 -0
index badbd07..6bdf1c9 100644
@@ -65,6 +65,7 @@
 #define CHKALIAS	0x1
 #define CHKKWD		0x2
 #define CHKNL		0x4
+#define CHKEOFMARK	0x8
 
 
 /*