ed36b0a2 |
1 | /* -*-c-*- |
2 | * |
4a9966ef |
3 | * $Id$ |
ed36b0a2 |
4 | * |
5 | * Ban a user from logging in |
6 | * |
7 | * (c) 1999 Mark Wooding |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
ffe3ac31 |
12 | * This program is free software; you can redistribute it and/or modify |
ed36b0a2 |
13 | * it under the terms of the GNU General Public License as published by |
14 | * the Free Software Foundation; either version 2 of the License, or |
15 | * (at your option) any later version. |
16 | * |
ffe3ac31 |
17 | * This program is distributed in the hope that it will be useful, |
ed36b0a2 |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU General Public License for more details. |
21 | * |
22 | * You should have received a copy of the GNU General Public License |
ffe3ac31 |
23 | * along with this program; if not, write to the Free Software Foundation, |
ed36b0a2 |
24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
25 | */ |
26 | |
ed36b0a2 |
27 | /*----- Header files ------------------------------------------------------*/ |
28 | |
29 | #include <errno.h> |
30 | #include <stdio.h> |
31 | #include <stdlib.h> |
32 | #include <string.h> |
33 | #include <sys/types.h> |
34 | #include <pwd.h> |
35 | #include <unistd.h> |
36 | #include <fcntl.h> |
37 | #include <syslog.h> |
38 | |
39 | /*----- Main code ---------------------------------------------------------*/ |
40 | |
41 | static const char *quis = "banned"; |
42 | |
43 | int main(int argc, char *argv[]) |
44 | { |
45 | struct passwd *pw; |
46 | int fd; |
47 | char buf[BUFSIZ]; |
48 | int r; |
49 | |
50 | /* --- Resolve the program name --- */ |
51 | |
52 | { |
53 | char *p, *q; |
54 | p = argv[0]; |
55 | for (q = argv[0]; *q; q++) { |
56 | if (*q == '/') |
57 | p = q + 1; |
58 | } |
59 | quis = p; |
60 | } |
61 | |
62 | /* --- Read the user's name --- */ |
63 | |
64 | pw = getpwuid(getuid()); |
65 | if (!pw) { |
66 | fprintf(stderr, "%s: you don't exist. Go away.\n", quis); |
67 | exit(EXIT_FAILURE); |
68 | } |
69 | |
70 | /* --- Open the log file --- */ |
71 | |
72 | openlog(quis, 0, LOG_AUTH); |
73 | syslog(LOG_CRIT, "banned user `%s' attempted to log in", pw->pw_name); |
74 | |
75 | /* --- Change directory to the user's home --- */ |
76 | |
77 | if (chdir(pw->pw_dir) < 0) { |
78 | fprintf(stderr, "%s: couldn't change directory: %s\n", |
79 | quis, strerror(errno)); |
80 | exit(EXIT_FAILURE); |
81 | } |
82 | |
83 | /* --- Open the reason file --- */ |
84 | |
85 | if ((fd = open(".banned", O_RDONLY)) < 0) { |
86 | fprintf(stderr, "%s: couldn't open `.banned' file: %s\n", |
87 | quis, strerror(errno)); |
88 | exit(EXIT_FAILURE); |
89 | } |
90 | |
91 | /* --- Dump the reason information out --- */ |
92 | |
93 | for (;;) { |
94 | r = read(fd, buf, sizeof(buf)); |
95 | if (r == 0) |
96 | break; |
97 | else if (r < 0) { |
98 | fprintf(stderr, "%s: couldn't read: %s\n", quis, strerror(errno)); |
99 | exit(EXIT_FAILURE); |
100 | } |
101 | write(STDOUT_FILENO, buf, r); |
102 | } |
103 | |
104 | /* --- Done --- */ |
105 | |
106 | close(fd); |
107 | return (EXIT_FAILURE); |
108 | } |
109 | |
110 | /*----- That's all, folks -------------------------------------------------*/ |