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