chiark / gitweb /
Miscellanous tidying and security fixes. Lots of thanks due to Clive
[shells] / banned.c
1 /* -*-c-*-
2  *
3  * $Id: banned.c,v 1.2 1999/04/21 09:07:55 mdw Exp $
4  *
5  * Ban a user from logging in
6  *
7  * (c) 1999 Mark Wooding
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This program is free software; you can redistribute it and/or modify
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  * 
17  * This program is distributed in the hope that it will be useful,
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
23  * along with this program; if not, write to the Free Software Foundation,
24  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 /*----- Revision history --------------------------------------------------* 
28  *
29  * $Log: banned.c,v $
30  * Revision 1.2  1999/04/21 09:07:55  mdw
31  * Fiddle with copyright messages so that they're correct.
32  *
33  * Revision 1.1.1.1  1999/04/20 00:19:04  mdw
34  * Initial versions.
35  *
36  */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sys/types.h>
45 #include <pwd.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <syslog.h>
49
50 /*----- Main code ---------------------------------------------------------*/
51
52 static const char *quis = "banned";
53
54 int main(int argc, char *argv[])
55 {
56   struct passwd *pw;
57   int fd;
58   char buf[BUFSIZ];
59   int r;
60
61   /* --- Resolve the program name --- */
62
63   {
64     char *p, *q;
65     p = argv[0];
66     for (q = argv[0]; *q; q++) {
67       if (*q == '/')
68         p = q + 1;
69     }
70     quis = p;
71   }
72
73   /* --- Read the user's name --- */
74
75   pw = getpwuid(getuid());
76   if (!pw) {
77     fprintf(stderr, "%s: you don't exist.  Go away.\n", quis);
78     exit(EXIT_FAILURE);
79   }
80
81   /* --- Open the log file --- */
82
83   openlog(quis, 0, LOG_AUTH);
84   syslog(LOG_CRIT, "banned user `%s' attempted to log in", pw->pw_name);
85
86   /* --- Change directory to the user's home --- */
87
88   if (chdir(pw->pw_dir) < 0) {
89     fprintf(stderr, "%s: couldn't change directory: %s\n",
90             quis, strerror(errno));
91     exit(EXIT_FAILURE);
92   }
93
94   /* --- Open the reason file --- */
95
96   if ((fd = open(".banned", O_RDONLY)) < 0) {
97     fprintf(stderr, "%s: couldn't open `.banned' file: %s\n",
98             quis, strerror(errno));
99     exit(EXIT_FAILURE);
100   }
101
102   /* --- Dump the reason information out --- */
103
104   for (;;) {
105     r = read(fd, buf, sizeof(buf));
106     if (r == 0)
107       break;
108     else if (r < 0) {
109       fprintf(stderr, "%s: couldn't read: %s\n", quis, strerror(errno));
110       exit(EXIT_FAILURE);
111     }
112     write(STDOUT_FILENO, buf, r);
113   }
114
115   /* --- Done --- */
116
117   close(fd);
118   return (EXIT_FAILURE);
119 }
120
121 /*----- That's all, folks -------------------------------------------------*/