chiark / gitweb /
some todos done
[innduct.git] / authprogs / domain.c
1 /*  $Id: domain.c 7141 2005-03-17 11:42:46Z vinocur $
2 **
3 **  Domain authenticator.
4 **
5 **  Compares the domain of the client connection to the first argument given
6 **  on the command line, and returns the host portion of the connecting host
7 **  as the user if it matches.
8 */
9
10 #include "config.h"
11 #include "clibrary.h"
12
13 #include "inn/messages.h"
14 #include "libinn.h"
15 #include "libauth.h"
16
17 int
18 main(int argc, char *argv[])
19 {
20     char *p, *host;
21     struct res_info *res;
22
23     if (argc != 2)
24         die("Usage: domain <domain>");
25     message_program_name = "domain";
26
27     /* Read the connection information from stdin. */
28     res = get_res_info(stdin);
29     if (res == NULL)
30         die("did not get ClientHost data from nnrpd");
31     host = res->clienthostname;
32
33     /* Check the host against the provided domain.  Allow the domain to be
34        specified both with and without a leading period; if without, make sure
35        that there is a period right before where it matches in the host. */
36     p = strstr(host, argv[1]);
37     if (p == host)
38         die("host %s matches the domain exactly", host);
39     if (p == NULL || (argv[1][0] != '.' && p != host && *(p - 1) != '.'))
40         die("host %s didn't match domain %s", host, argv[1]);
41
42     /* Peel off the portion of the host before where the provided domain
43        matches and return it as the user. */
44     if (argv[1][0] != '.')
45         p--;
46     *p = '\0';
47     printf("User:%s\n", host);
48     return 0;
49 }