]> git.ipfire.org Git - thirdparty/util-linux.git/blob - login-utils/nologin.c
nologin: silently ignore well known shell command-line options
[thirdparty/util-linux.git] / login-utils / nologin.c
1 /*
2 * Copyright (C) 2013 Karel Zak <kzak@redhat.com>
3 */
4
5 #include <stdio.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 #include <fcntl.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <getopt.h>
13
14 #include "c.h"
15 #include "nls.h"
16 #include "pathnames.h"
17
18 /*
19 * Always return EXIT_FAILURE (1), don't try to be smart!
20 */
21
22 static void __attribute__((__noreturn__)) usage(void)
23 {
24 FILE *out = stdout;
25 fputs(USAGE_HEADER, out);
26 fprintf(out,
27 _(" %s [options]\n"), program_invocation_short_name);
28
29 fputs(USAGE_SEPARATOR, out);
30 fputs(_("Politely refuse a login.\n"), out);
31
32 fputs(USAGE_OPTIONS, out);
33 fputs(_(" -c, --command <command> does nothing (for compatibility with su -c)\n"), out);
34 printf(USAGE_HELP_OPTIONS(26));
35
36 printf(USAGE_MAN_TAIL("nologin(8)"));
37 exit(EXIT_FAILURE);
38 }
39
40 int main(int argc, char *argv[])
41 {
42 int c, fd = -1;
43 struct stat st;
44 enum {
45 OPT_INIT_FILE = CHAR_MAX + 1,
46 OPT_NOPROFILE,
47 OPT_NORC,
48 OPT_POSIX,
49 OPT_RCFILE
50 };
51 static const struct option longopts[] = {
52 { "command", required_argument, NULL, 'c' },
53 { "init-file", required_argument, NULL, OPT_INIT_FILE },
54 { "interactive", no_argument, NULL, 'i' },
55 { "login", no_argument, NULL, 'l' },
56 { "noprofile", no_argument, NULL, OPT_NOPROFILE },
57 { "norc", no_argument, NULL, OPT_NORC },
58 { "posix", no_argument, NULL, OPT_POSIX },
59 { "rcfile", required_argument, NULL, OPT_RCFILE },
60 { "restricted", no_argument, NULL, 'r' },
61 { "help", no_argument, NULL, 'h' },
62 { "version", no_argument, NULL, 'V' },
63 { NULL, 0, NULL, 0 }
64 };
65
66 setlocale(LC_ALL, "");
67 bindtextdomain(PACKAGE, LOCALEDIR);
68 textdomain(PACKAGE);
69
70 while ((c = getopt_long(argc, argv, "c:ilrhV", longopts, NULL)) != -1) {
71 switch (c) {
72 case 'c':
73 case OPT_INIT_FILE:
74 case 'i':
75 case 'l':
76 case OPT_NOPROFILE:
77 case OPT_NORC:
78 case OPT_POSIX:
79 case OPT_RCFILE:
80 case 'r':
81 /* Ignore well known shell command-line options */
82 break;
83 case 'h':
84 usage();
85 case 'V':
86 print_version(EXIT_FAILURE); /* yes FAILURE! */
87 default:
88 errtryhelp(EXIT_FAILURE);
89 }
90 }
91
92 fd = open(_PATH_NOLOGIN_TXT, O_RDONLY);
93 if (fd < 0)
94 goto dflt;
95
96 c = fstat(fd, &st);
97 if (c < 0 || !S_ISREG(st.st_mode))
98 goto dflt;
99 else {
100 char buf[BUFSIZ];
101 ssize_t rd;
102
103 while ((rd = read(fd, buf, sizeof(buf))) > 0)
104 ignore_result( write(STDOUT_FILENO, buf, rd) );
105
106 close(fd);
107 return EXIT_FAILURE;
108 }
109
110 dflt:
111 if (fd >= 0)
112 close(fd);
113 fprintf(stdout, _("This account is currently not available.\n"));
114 return EXIT_FAILURE;
115 }