Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save malfaux/8253233 to your computer and use it in GitHub Desktop.
Save malfaux/8253233 to your computer and use it in GitHub Desktop.
getty can't execute a login program with arguments
diff -Nru util-linux-2.17.2/login-utils/agetty.8 util-linux-2.17.2_login_params_patch/login-utils/agetty.8
--- util-linux-2.17.2/login-utils/agetty.8 2010-02-04 13:53:56.000000000 +0200
+++ util-linux-2.17.2_login_params_patch/login-utils/agetty.8 2011-04-06 14:54:31.962879000 +0300
@@ -12,6 +12,7 @@
.I port
.I baud_rate,...
.RI [ term ]
+.RI "[-- [login_program parameters]]"
.br
.BR "agetty " [\-8ihLmnw]
.RI "[-f " issue_file ]
@@ -22,6 +23,7 @@
.I baud_rate,...
.I port
.RI [ term ]
+.RI "[-- [login_program parameters]]"
.SH DESCRIPTION
.ad
@@ -57,6 +59,8 @@
Optionally turns on hard-ware flow control
.IP o
Optionally forces the line to be local with no need for carrier detect.
+.IP o
+Optionally allows passing arguments to login_program (see EXAMPLES).
.PP
This program does not use the \fI/etc/gettydefs\fP (System V) or
\fI/etc/gettytab\fP (SunOS 4) files.
@@ -200,6 +204,10 @@
.ti +5
/sbin/agetty \-w \-I 'ATE0Q1&D2&C1S0=1\\015' 115200 ttyS1
+Forcing autologin on a secured console:
+.ti +5
+/sbin/agetty -8 -n 38400 tty6 linux -- -f root
+
.SH ISSUE ESCAPES
The issue-file (\fI/etc/issue\fP or the file set with the \-f option)
may contain certain escape codes to display the system name, date and
@@ -304,6 +312,9 @@
Eric Rasmussen <ear@usfirst.org>
Added \-f option to display custom login messages on different terminals.
+Adrian Ilarion Ciobanu <cia@mud.ro>
+Added support for passing login program parameters
+
.SH AVAILABILITY
The agetty command is part of the util-linux-ng package and is available from
ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/.
diff -Nru util-linux-2.17.2/login-utils/agetty.c util-linux-2.17.2_login_params_patch/login-utils/agetty.c
--- util-linux-2.17.2/login-utils/agetty.c 2011-04-06 14:55:03.000000000 +0300
+++ util-linux-2.17.2_login_params_patch/login-utils/agetty.c 2011-04-06 14:48:45.332879000 +0300
@@ -10,6 +10,9 @@
1999-05-05 Thorsten Kranzkowski <dl8bcu@gmx.net>
- enable hardware flow control before displaying /etc/issue
+
+ 2011-04-6 Adrian Ilarion Ciobanu <cia@mud.ro>
+ - added support for login program options
*/
@@ -203,7 +206,7 @@
#define P_(s) s
int main P_((int argc, char **argv));
-void parse_args P_((int argc, char **argv, struct options *op));
+int parse_args P_((int argc, char **argv, struct options *op));
void parse_speeds P_((struct options *op, char *arg));
void update_utmp P_((char *line));
void open_tty P_((char *tty, struct termios *tp, int local));
@@ -234,6 +237,7 @@
#define debug(s) /* nothing */
#endif
+extern char **environ;
int
main(argc, argv)
int argc;
@@ -252,6 +256,13 @@
0, /* no baud rates known yet */
};
+ /* login_optin: hold the argv index of login program options, if any */
+ int login_optind;
+ /* login_args: holds login execve()'s args */
+ char **login_args = NULL;
+ /* login_args "iterator" */
+ int i;
+
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
@@ -281,8 +292,8 @@
#endif
/* Parse command-line arguments. */
-
- parse_args(argc, argv, &options);
+ /* parse_args(...) returns argv's login options index (if any) */
+ login_optind = parse_args(argc, argv, &options);
#if defined (__linux__) || defined(__FreeBSD_kernel__)
setsid();
@@ -358,16 +369,37 @@
(void) write(1, "\n", 1);
+ /* allocate space to hold pointers to: login_program, login_opts if any,
+ * --, logname and a NULL terminator
+ */
+ login_args = (char **)malloc((argc-login_optind+4)*sizeof(char *));
+ /* shouldn't fail at this stage but if we do it's not our fault */
+ if (login_args == NULL) {
+ exit(1);
+ }
+ login_args[0] = options.login;
+ /* we can safely loose login_optind */
+ i = 1;
+ while (login_optind < argc) {
+ login_args[i++] = argv[login_optind++];
+ }
+ /* if F_NOPROMPT is set (that is, '-n' option is used),
+ * the following 2 args are useless but will not
+ * cause any harm; I'd rather use 2 extra char*'s and 2 extra assignments
+ * than complicating the code with runtime-conditioned malloc()'s
+ */
+ login_args[i++] = "--";
+ login_args[i++] = logname;
+ login_args[i] = NULL;
/* Let the login program take care of password validation. */
-
- (void) execl(options.login, options.login, "--", logname, NULL);
+ (void) execve(login_args[0], login_args, environ);
error(_("%s: can't exec %s: %m"), options.tty, options.login);
exit(0); /* quiet GCC */
}
/* parse-args - parse command-line arguments */
-void
+int
parse_args(argc, argv, op)
int argc;
char **argv;
@@ -377,7 +409,13 @@
extern int optind; /* getopt */
int c;
- while (isascii(c = getopt(argc, argv, "8I:LH:f:hil:mt:wUn"))) {
+ /* nonopt_cnt: counter used for error-checking and parsing getty's
+ * non-opts in the getopt() loop
+ */
+ int nonopt_cnt = 0;
+
+ /* handle nonopts as dummy option arguments */
+ while (isascii(c = getopt(argc, argv, "-8I:LH:f:hil:mt:wUn"))) {
switch (c) {
case '8':
op->eightbits = 1;
@@ -457,27 +495,38 @@
case 'U':
op->flags |= F_LCUC;
break;
+ case 1:
+ /*if both tty and baudrate are set: expect a term name, if any */
+ if (nonopt_cnt == 2) {
+ setenv("TERM", optarg, 1);
+ } else {
+ /* any non-opt found after tty,baudrate and term is an error */
+ if (nonopt_cnt > 2) {
+ usage();
+ }
+ /* we loosen up a bit and accept both "baudrate tty" and
+ * "tty baudrate"
+ */
+ if('0' <= optarg[0] && optarg[0] <= '9') {
+ parse_speeds(op, optarg); /* baud rate(s) */
+ nonopt_cnt++;
+ } else {
+ op->tty = optarg; /* tty name */
+ nonopt_cnt++;
+ }
+ }
+ break;
default:
usage();
}
}
debug("after getopt loop\n");
- if (argc < optind + 2) /* check parameter count */
- usage();
- /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
- if('0' <= argv[optind][0] && argv[optind][0] <= '9') {
- /* a number first, assume it's a speed (BSD style) */
- parse_speeds(op, argv[optind++]); /* baud rate(s) */
- op->tty = argv[optind]; /* tty name */
- } else {
- op->tty = argv[optind++]; /* tty name */
- parse_speeds(op, argv[optind]); /* baud rate(s) */
+ /* check if we received baudrate(s) and tty name */
+ if (nonopt_cnt < 2) {
+ usage();
}
- optind++;
- if (argc > optind && argv[optind])
- setenv ("TERM", argv[optind], 1);
#ifdef DO_DEVFS_FIDDLING
/*
@@ -512,6 +561,9 @@
#endif
debug("exiting parseargs\n");
+
+ /* return optind for further processing of login options, if any */
+ return optind;
}
/* parse_speeds - parse alternate baud rates */
@malfaux
Copy link
Author

malfaux commented Jan 4, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment