]> git.ipfire.org Git - thirdparty/util-linux.git/blame - login-utils/nologin.c
nologin: require /etc/nologin.txt to be file
[thirdparty/util-linux.git] / login-utils / nologin.c
CommitLineData
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
22static void __attribute__((__noreturn__)) usage(FILE *out)
23{
24 fputs(USAGE_HEADER, out);
88407b93
KZ
25 fprintf(out,
26 _(" %s [options]\n"), program_invocation_short_name);
27
451dbcfa
BS
28 fputs(USAGE_SEPARATOR, out);
29 fputs(_("Politely refuse a login.\n"), out);
30
88407b93
KZ
31 fputs(USAGE_OPTIONS, out);
32 fputs(USAGE_HELP, out);
33 fputs(USAGE_VERSION, out);
34
35 fprintf(out, USAGE_MAN_TAIL("nologin(8)"));
36 exit(EXIT_FAILURE);
37}
38
39int main(int argc, char *argv[])
40{
41 int c, fd;
bd299782 42 struct stat st;
88407b93
KZ
43 static const struct option longopts[] = {
44 { "help", 0, 0, 'h' },
45 { "version", 0, 0, 'V' },
46 { NULL, 0, 0, 0 }
47 };
48
49 setlocale(LC_ALL, "");
50 bindtextdomain(PACKAGE, LOCALEDIR);
51 textdomain(PACKAGE);
52
53 while ((c = getopt_long(argc, argv, "hV", longopts, NULL)) != -1) {
54 switch (c) {
55 case 'h':
56 usage(stdout);
57 break;
58 case 'V':
59 printf(UTIL_LINUX_VERSION);
60 return EXIT_FAILURE;
61 default:
62 usage(stderr);
63 break;
64 }
65 }
66
67 fd = open(_PATH_NOLOGIN_TXT, O_RDONLY);
bd299782
SK
68 c = fstat(fd, &st);
69 if (fd >= 0 && !c && S_ISREG(st.st_mode)) {
88407b93
KZ
70 char buf[BUFSIZ];
71 ssize_t rd;
72
73 while ((rd = read(fd, buf, sizeof(buf))) > 0)
74 ignore_result( write(STDOUT_FILENO, buf, rd) );
75 close(fd);
76 } else
77 fprintf(stdout, _("This account is currently not available.\n"));
78
79 return EXIT_FAILURE;
80}