]>
Commit | Line | Data |
---|---|---|
88407b93 KZ |
1 | /* |
2 | * Copyright (C) 2013 Karel Zak <kzak@redhat.com> | |
3 | */ | |
4 | ||
5 | #include <stdio.h> | |
bd299782 | 6 | #include <sys/stat.h> |
88407b93 KZ |
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 | ||
86be6a32 | 22 | static void __attribute__((__noreturn__)) usage(void) |
88407b93 | 23 | { |
86be6a32 | 24 | FILE *out = stdout; |
88407b93 | 25 | fputs(USAGE_HEADER, out); |
88407b93 KZ |
26 | fprintf(out, |
27 | _(" %s [options]\n"), program_invocation_short_name); | |
28 | ||
451dbcfa BS |
29 | fputs(USAGE_SEPARATOR, out); |
30 | fputs(_("Politely refuse a login.\n"), out); | |
31 | ||
88407b93 | 32 | fputs(USAGE_OPTIONS, out); |
a174eefb SB |
33 | fputs(_(" -c, --command <command> does nothing (for compatibility with su -c)\n"), out); |
34 | printf(USAGE_HELP_OPTIONS(26)); | |
88407b93 | 35 | |
f45f3ec3 | 36 | printf(USAGE_MAN_TAIL("nologin(8)")); |
88407b93 KZ |
37 | exit(EXIT_FAILURE); |
38 | } | |
39 | ||
40 | int main(int argc, char *argv[]) | |
41 | { | |
dd732fa2 | 42 | int c, fd = -1; |
bd299782 | 43 | struct stat st; |
beb61b07 SK |
44 | enum { |
45 | OPT_INIT_FILE = CHAR_MAX + 1, | |
46 | OPT_NOPROFILE, | |
47 | OPT_NORC, | |
48 | OPT_POSIX, | |
49 | OPT_RCFILE | |
50 | }; | |
88407b93 | 51 | static const struct option longopts[] = { |
beb61b07 SK |
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' }, | |
87918040 | 63 | { NULL, 0, NULL, 0 } |
88407b93 KZ |
64 | }; |
65 | ||
66 | setlocale(LC_ALL, ""); | |
67 | bindtextdomain(PACKAGE, LOCALEDIR); | |
68 | textdomain(PACKAGE); | |
69 | ||
beb61b07 | 70 | while ((c = getopt_long(argc, argv, "c:ilrhV", longopts, NULL)) != -1) { |
88407b93 | 71 | switch (c) { |
a174eefb | 72 | case 'c': |
beb61b07 SK |
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 */ | |
a174eefb | 82 | break; |
88407b93 | 83 | case 'h': |
86be6a32 | 84 | usage(); |
88407b93 | 85 | case 'V': |
2c308875 | 86 | print_version(EXIT_FAILURE); /* yes FAILURE! */ |
88407b93 | 87 | default: |
677ec86c | 88 | errtryhelp(EXIT_FAILURE); |
88407b93 KZ |
89 | } |
90 | } | |
91 | ||
92 | fd = open(_PATH_NOLOGIN_TXT, O_RDONLY); | |
dd732fa2 KZ |
93 | if (fd < 0) |
94 | goto dflt; | |
95 | ||
bd299782 | 96 | c = fstat(fd, &st); |
dd732fa2 KZ |
97 | if (c < 0 || !S_ISREG(st.st_mode)) |
98 | goto dflt; | |
99 | else { | |
88407b93 KZ |
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) ); | |
dd732fa2 | 105 | |
88407b93 | 106 | close(fd); |
dd732fa2 KZ |
107 | return EXIT_FAILURE; |
108 | } | |
88407b93 | 109 | |
dd732fa2 KZ |
110 | dflt: |
111 | if (fd >= 0) | |
112 | close(fd); | |
113 | fprintf(stdout, _("This account is currently not available.\n")); | |
88407b93 KZ |
114 | return EXIT_FAILURE; |
115 | } |