Revert "If we don't have a realm, use server FQDN; only portable thing we can do"
[cyrus-sasl.git] / sample / server.c
index b721054..0fc478c 100644 (file)
  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
+/*
+ * Copyright 2009  by the Massachusetts Institute of Technology.
+ * All Rights Reserved.
+ *
+ * Export of this software from the United States of America may
+ *   require a specific license from the United States Government.
+ *   It is the responsibility of any person or organization contemplating
+ *   export to obtain such a license before exporting.
+ *
+ * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
+ * distribute this software and its documentation for any purpose and
+ * without fee is hereby granted, provided that the above copyright
+ * notice appear in all copies and that both that copyright notice and
+ * this permission notice appear in supporting documentation, and that
+ * the name of M.I.T. not be used in advertising or publicity pertaining
+ * to distribution of the software without specific, written prior
+ * permission.  Furthermore if you modify this software you must label
+ * your software as modified software and not distribute it in such a
+ * fashion that it might be confused with the original M.I.T. software.
+ * M.I.T. makes no representations about the suitability of
+ * this software for any purpose.  It is provided "as is" without express
+ * or implied warranty.
+ *
+ */
 
 #include <config.h>
 
@@ -59,7 +83,9 @@
 
 #include <sasl.h>
 #include <gssapi/gssapi.h>
+#ifndef KRB5_HEIMDAL
 #include <gssapi/gssapi_ext.h>
+#endif
 
 #include "common.h"
 
@@ -161,7 +187,7 @@ int *listensock(const char *port, const int af)
 
 void usage(void)
 {
-    fprintf(stderr, "usage: server [-p port] [-s service] [-m mech]\n");
+    fprintf(stderr, "usage: server [-C] [-h hostname] [-p port] [-s service] [-m mech]\n");
     exit(EX_USAGE);
 }
 
@@ -295,12 +321,26 @@ int main(int argc, char *argv[])
     int c;
     char *port = "12345";
     char *service = "rcmd";
+    char *hostname = NULL;
     int *l, maxfd=0;
     int r, i;
     sasl_conn_t *conn;
+    int cb_flag = 0;
 
-    while ((c = getopt(argc, argv, "p:s:m:")) != EOF) {
+    while ((c = getopt(argc, argv, "Cch:p:s:m:")) != EOF) {
        switch(c) {
+       case 'C':
+           cb_flag = 2;        /* channel bindings are critical */
+           break;
+
+       case 'c':
+           cb_flag = 1;        /* channel bindings are present */
+           break;
+
+       case 'h':
+           hostname = optarg;
+           break;
+
        case 'p':
            port = optarg;
            break;
@@ -344,6 +384,7 @@ int main(int argc, char *argv[])
        int nfds, fd = -1;
        FILE *in, *out;
        fd_set readfds;
+       sasl_channel_binding_t cb;
 
        FD_ZERO(&readfds);
        for (i = 1; i <= l[0]; i++)
@@ -408,13 +449,24 @@ int main(int argc, char *argv[])
        }
        snprintf(remoteaddr, sizeof(remoteaddr), "%s;%s", hbuf, pbuf);
 
-       r = gethostname(myhostname, sizeof(myhostname)-1);
-       if(r == -1) saslfail(r, "getting hostname");
+       if (hostname == NULL) {
+           r = gethostname(myhostname, sizeof(myhostname)-1);
+           if(r == -1) saslfail(r, "getting hostname");
+           hostname = myhostname;
+       }
 
-       r = sasl_server_new(service, myhostname, NULL, localaddr, remoteaddr,
+       r = sasl_server_new(service, hostname, NULL, localaddr, remoteaddr,
                            NULL, 0, &conn);
        if (r != SASL_OK) saslfail(r, "allocating connection state");
 
+       cb.name = "sasl-sample";
+       cb.critical = cb_flag > 1;
+       cb.data = "this is a test of channel binding";
+       cb.len = strlen(cb.data);
+
+       if (cb_flag)
+           sasl_setprop(conn, SASL_CHANNEL_BINDING, &cb);
+
        /* set external properties here
           sasl_setprop(conn, SASL_SSF_EXTERNAL, &extprops); */
 
@@ -549,7 +601,9 @@ enumerateAttributes(OM_uint32 *minor,
             dumpAttribute(minor, name, &attrs->elements[i], noisy);
     }
 
+#ifndef KRB5_HEIMDAL
     gss_release_oid(&tmp, &mech);
+#endif
     gss_release_buffer_set(&tmp, &attrs);
 
     return major;