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