Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / crypto / kerberosIV / appl / sample / sample_server.c
1 /* $FreeBSD: src/crypto/kerberosIV/appl/sample/sample_server.c,v 1.1.1.2.2.1 2000/07/20 14:04:34 assar Exp $ */
2 /* $DragonFly: src/crypto/kerberosIV/appl/sample/Attic/sample_server.c,v 1.2 2003/06/17 04:24:36 dillon Exp $ */
3
4 /*
5  *
6  * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
7  *
8  * For copying and distribution information,
9  * please see the file <mit-copyright.h>.
10  *
11  * sample_server:
12  * A sample Kerberos server, which reads a ticket from a TCP socket,
13  * decodes it, and writes back the results (in ASCII) to the client.
14  *
15  * Usage:
16  * sample_server
17  *
18  * file descriptor 0 (zero) should be a socket connected to the requesting
19  * client (this will be correct if this server is started by inetd).
20  */
21
22 #include "sample.h"
23
24 RCSID("$Id: sample_server.c,v 1.14.2.1 2000/06/28 19:08:00 assar Exp $");
25
26 static void
27 usage (void)
28 {
29     fprintf (stderr, "Usage: %s [-i] [-s service] [-t srvtab]\n",
30              __progname);
31     exit (1);
32 }
33
34 int
35 main(int argc, char **argv)
36 {
37     struct sockaddr_in peername, myname;
38     int namelen = sizeof(peername);
39     int status, count, len;
40     long authopts;
41     AUTH_DAT auth_data;
42     KTEXT_ST clt_ticket;
43     des_key_schedule sched;
44     char instance[INST_SZ];
45     char service[ANAME_SZ];
46     char version[KRB_SENDAUTH_VLEN+1];
47     char retbuf[512];
48     char lname[ANAME_SZ];
49     char srvtab[MaxPathLen];
50     int c;
51     int no_inetd = 0;
52
53     /* open a log connection */
54
55     set_progname (argv[0]);
56
57     roken_openlog(__progname, LOG_ODELAY, LOG_DAEMON);
58
59     strlcpy (service, SAMPLE_SERVICE, sizeof(service));
60     *srvtab = '\0';
61
62     while ((c = getopt (argc, argv, "s:t:i")) != -1)
63         switch (c) {
64         case 's' :
65             strlcpy (service, optarg, sizeof(service));
66             break;
67         case 't' :
68             strlcpy (srvtab, optarg, sizeof(srvtab));
69             break;
70         case 'i':
71             no_inetd = 1;
72             break;
73         case '?' :
74         default :
75             usage ();
76         }
77
78     if (no_inetd)
79         mini_inetd (htons(SAMPLE_PORT));
80
81     /*
82      * To verify authenticity, we need to know the address of the
83      * client.
84      */
85     if (getpeername(STDIN_FILENO,
86                     (struct sockaddr *)&peername,
87                     &namelen) < 0) {
88         syslog(LOG_ERR, "getpeername: %m");
89         return 1;
90     }
91
92     /* for mutual authentication, we need to know our address */
93     namelen = sizeof(myname);
94     if (getsockname(STDIN_FILENO, (struct sockaddr *)&myname, &namelen) < 0) {
95         syslog(LOG_ERR, "getsocknamename: %m");
96         return 1;
97     }
98
99     /* read the authenticator and decode it.  Using `k_getsockinst' we
100      * always get the right instance on a multi-homed host.
101      */
102     k_getsockinst (STDIN_FILENO, instance, sizeof(instance));
103
104     /* we want mutual authentication */
105     authopts = KOPT_DO_MUTUAL;
106     status = krb_recvauth(authopts, STDIN_FILENO, &clt_ticket,
107                           service, instance, &peername, &myname,
108                           &auth_data, srvtab,
109                           sched, version);
110     if (status != KSUCCESS) {
111         snprintf(retbuf, sizeof(retbuf),
112                  "Kerberos error: %s\n",
113                  krb_get_err_text(status));
114         syslog(LOG_ERR, "%s", retbuf);
115     } else {
116         /* Check the version string (KRB_SENDAUTH_VLEN chars) */
117         if (strncmp(version, SAMPLE_VERSION, KRB_SENDAUTH_VLEN)) {
118             /* didn't match the expected version */
119             /* could do something different, but we just log an error
120                and continue */
121             version[8] = '\0';          /* make sure null term */
122             syslog(LOG_ERR, "Version mismatch: '%s' isn't '%s'",
123                    version, SAMPLE_VERSION);
124         }
125         /* now that we have decoded the authenticator, translate
126            the kerberos principal.instance@realm into a local name */
127         if (krb_kntoln(&auth_data, lname) != KSUCCESS)
128             strlcpy(lname,
129                             "*No local name returned by krb_kntoln*",
130                             sizeof(lname));
131         /* compose the reply */
132         snprintf(retbuf, sizeof(retbuf),
133                 "You are %s.%s@%s (local name %s),\n at address %s, version %s, cksum %ld\n",
134                 auth_data.pname,
135                 auth_data.pinst,
136                 auth_data.prealm,
137                 lname,
138                 inet_ntoa(peername.sin_addr),
139                 version,
140                 (long)auth_data.checksum);
141     }
142
143     /* write back the response */
144     if ((count = write(0, retbuf, (len = strlen(retbuf) + 1))) < 0) {
145         syslog(LOG_ERR,"write: %m");
146         return 1;
147     } else if (count != len) {
148         syslog(LOG_ERR, "write count incorrect: %d != %d\n",
149                 count, len);
150         return 1;
151     }
152
153     /* close up and exit */
154     close(0);
155     return 0;
156 }