Do not use obsolete YAZ defines
[yaz-moved-to-github.git] / client / client.c
index 7dd97d9..be1d9b6 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 1995-2004, Index Data
  * See the file LICENSE for details.
  *
- * $Id: client.c,v 1.236 2004-03-17 10:59:41 adam Exp $
+ * $Id: client.c,v 1.243 2004-05-10 10:45:28 adam Exp $
  */
 
 #include <stdio.h>
 #include <langinfo.h>
 #endif
 
+#if HAVE_OPENSSL_SSL_H
+#include <openssl/bio.h>
+#include <openssl/crypto.h>
+#include <openssl/x509.h>
+#include <openssl/pem.h>
+#include <openssl/ssl.h>
+#include <openssl/err.h>
+#endif
+
 #include <time.h>
 #include <ctype.h>
 
@@ -529,6 +538,9 @@ int session_connect(const char *arg)
     void *add;
     char type_and_host[101];
     const char *basep = 0;
+#if HAVE_OPENSSL_SSL_H
+    SSL *ssl;
+#endif
     if (conn)
     {
         cs_close (conn);
@@ -586,6 +598,30 @@ int session_connect(const char *arg)
         return 0;
     }
     printf("OK.\n");
+#if HAVE_OPENSSL_SSL_H
+    if ((ssl = (SSL *) cs_get_ssl(conn)))
+    {
+       X509 *server_cert = SSL_get_peer_certificate (ssl);
+
+       if (server_cert)
+       {
+           char *pem_buf;
+           int pem_len;
+           BIO *bio = BIO_new(BIO_s_mem());
+
+           /* get PEM buffer in memory */
+           PEM_write_bio_X509(bio, server_cert);
+           pem_len = BIO_get_mem_data(bio, &pem_buf);
+           fwrite(pem_buf, pem_len, 1, stdout);
+       
+           /* print all info on screen .. */
+           X509_print_fp(stdout, server_cert);
+           BIO_free(bio);
+
+           X509_free (server_cert);
+       }
+    }
+#endif
     if (basep && *basep)
         set_base (basep);
     if (protocol == PROTO_Z3950)
@@ -741,16 +777,16 @@ static void display_record(Z_External *r)
             }
         }
     }
-    if (ent && ent->oclass != CLASS_RECSYN) 
-        return;
     if (ent && ent->value == VAL_SOIF)
         print_record((const unsigned char *) r->u.octet_aligned->buf,
                      r->u.octet_aligned->len);
     else if (r->which == Z_External_octet)
     {
         const char *octet_buf = (char*)r->u.octet_aligned->buf;
-        if (ent->value == VAL_TEXT_XML || ent->value == VAL_APPLICATION_XML ||
-            ent->value == VAL_HTML)
+        if (ent->oclass == CLASS_RECSYN && 
+               (ent->value == VAL_TEXT_XML || 
+                ent->value == VAL_APPLICATION_XML ||
+                ent->value == VAL_HTML))
         {
             print_record((const unsigned char *) octet_buf,
                          r->u.octet_aligned->len);
@@ -1074,7 +1110,7 @@ static int send_deleteResultSetRequest(const char *arg)
         odr_malloc (out, sizeof(*req->deleteFunction));
     if (req->num_resultSetList > 0)
     {
-        *req->deleteFunction = Z_DeleteRequest_list;
+        *req->deleteFunction = Z_DeleteResultSetRequest_list;
         req->resultSetList = (char **)
             odr_malloc (out, sizeof(*req->resultSetList)*
                         req->num_resultSetList);
@@ -1083,7 +1119,7 @@ static int send_deleteResultSetRequest(const char *arg)
     }
     else
     {
-        *req->deleteFunction = Z_DeleteRequest_all;
+        *req->deleteFunction = Z_DeleteResultSetRequest_all;
         req->resultSetList = 0;
     }
     
@@ -1228,7 +1264,6 @@ static int send_searchRequest(const char *arg)
     Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
     Z_SearchRequest *req = apdu->u.searchRequest;
     Z_Query query;
-    int oid[OID_SIZE];
     struct ccl_rpn_node *rpn = NULL;
     int error, pos;
     char setstring[100];
@@ -1297,13 +1332,9 @@ static int send_searchRequest(const char *arg)
     if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
         mediumSetPresentNumber > 0))
     {
-        oident prefsyn;
-
-        prefsyn.proto = protocol;
-        prefsyn.oclass = CLASS_RECSYN;
-        prefsyn.value = recordsyntax;
         req->preferredRecordSyntax =
-            odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
+            yaz_oidval_to_z3950oid(out, CLASS_RECSYN, recordsyntax);
+
         req->smallSetElementSetNames =
             req->mediumSetElementSetNames = elementSetNames;
     }
@@ -1927,8 +1958,6 @@ static int cmd_update_common(const char *arg, int version)
     Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
     Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
     Z_External *r;
-    int oid[OID_SIZE];
-    oident update_oid;
     char action[20], recid[20], fname[80];
     int action_no;
     Z_External *record_this = 0;
@@ -1989,21 +2018,17 @@ static int cmd_update_common(const char *arg, int version)
         record_this = record_last;
     }
 
-    update_oid.proto = PROTO_Z3950;
-    update_oid.oclass = CLASS_EXTSERV;
-    if (version == 0)
-       update_oid.value = VAL_DBUPDATE0;
-    else
-       update_oid.value = VAL_DBUPDATE;
-    oid_ent_to_oid (&update_oid, oid);
-    req->packageType = odr_oiddup(out,oid);
+    req->packageType =
+       yaz_oidval_to_z3950oid(out, CLASS_EXTSERV,
+                              version == 0 ? VAL_DBUPDATE0 : VAL_DBUPDATE);
+
     req->packageName = esPackageName;
     
     req->referenceId = set_refid (out);
 
     r = req->taskSpecificParameters = (Z_External *)
         odr_malloc (out, sizeof(*r));
-    r->direct_reference = odr_oiddup(out,oid);
+    r->direct_reference = req->packageType;
     r->indirect_reference = 0;
     r->descriptor = 0;
     if (version == 0)
@@ -2307,9 +2332,7 @@ static int send_presentRequest(const char *arg)
     Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
     Z_PresentRequest *req = apdu->u.presentRequest;
     Z_RecordComposition compo;
-    oident prefsyn;
     int nos = 1;
-    int oid[OID_SIZE];
     char setstring[100];
 
     req->referenceId = set_refid (out);
@@ -2320,20 +2343,12 @@ static int send_presentRequest(const char *arg)
 
     req->resultSetStartPoint = &setno;
     req->numberOfRecordsRequested = &nos;
-    prefsyn.proto = protocol;
-    prefsyn.oclass = CLASS_RECSYN;
-    prefsyn.value = recordsyntax;
+
     req->preferredRecordSyntax =
-        odr_oiddup (out, oid_ent_to_oid(&prefsyn, oid));
+        yaz_oidval_to_z3950oid(out, CLASS_RECSYN, recordsyntax);
 
     if (record_schema)
     {
-        oident prefschema;
-
-        prefschema.proto = protocol;
-        prefschema.oclass = CLASS_SCHEMA;
-        prefschema.value = oid_getvalbyname(record_schema);
-
         req->recordComposition = &compo;
         compo.which = Z_RecordComp_complex;
         compo.u.complex = (Z_CompSpec *)
@@ -2345,14 +2360,15 @@ static int send_presentRequest(const char *arg)
         compo.u.complex->generic = (Z_Specification *)
             odr_malloc(out, sizeof(*compo.u.complex->generic));
         compo.u.complex->generic->which = Z_Schema_oid;
-        compo.u.complex->generic->schema.oid = (Odr_oid *)
-            odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
+
+        compo.u.complex->generic->schema.oid =
+           yaz_str_to_z3950oid(out, CLASS_SCHEMA, record_schema);
+
         if (!compo.u.complex->generic->schema.oid)
         {
             /* OID wasn't a schema! Try record syntax instead. */
-            prefschema.oclass = CLASS_RECSYN;
             compo.u.complex->generic->schema.oid = (Odr_oid *)
-                odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
+               yaz_str_to_z3950oid(out, CLASS_RECSYN, record_schema);
         }
         if (!elementSetNames)
             compo.u.complex->generic->elementSpec = 0;
@@ -2504,7 +2520,7 @@ int cmd_cancel(const char *arg)
         printf("Target doesn't support cancel (trigger resource ctrl)\n");
         return 0;
     }
-    *req->requestedAction = Z_TriggerResourceCtrl_cancel;
+    *req->requestedAction = Z_TriggerResourceControlRequest_cancel;
     req->resultSetWanted = &rfalse;
 
     send_apdu(apdu);
@@ -2516,13 +2532,11 @@ int send_scanrequest(const char *query, int pp, int num, const char *term)
 {
     Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
     Z_ScanRequest *req = apdu->u.scanRequest;
-    int oid[OID_SIZE];
     
     if (only_z3950())
        return 0;
     if (queryType == QueryType_CCL2RPN)
     {
-        oident bib1;
         int error, pos;
         struct ccl_rpn_node *rpn;
 
@@ -2532,10 +2546,8 @@ int send_scanrequest(const char *query, int pp, int num, const char *term)
             printf("CCL ERROR: %s\n", ccl_err_msg(error));
             return -1;
         }
-        bib1.proto = PROTO_Z3950;
-        bib1.oclass = CLASS_ATTSET;
-        bib1.value = VAL_BIB1;
-        req->attributeSet = oid_ent_to_oid (&bib1, oid);
+        req->attributeSet =
+           yaz_oidval_to_z3950oid(out, CLASS_ATTSET, VAL_BIB1);
         if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
         {
             printf("Couldn't convert CCL to Scan term\n");
@@ -2676,11 +2688,11 @@ void process_sortResponse(Z_SortResponse *res)
     printf("Received SortResponse: status=");
     switch (*res->sortStatus)
     {
-    case Z_SortStatus_success:
+    case Z_SortResponse_success:
         printf ("success"); break;
-    case Z_SortStatus_partial_1:
+    case Z_SortResponse_partial_1:
         printf ("partial"); break;
-    case Z_SortStatus_failure:
+    case Z_SortResponse_failure:
         printf ("failure"); break;
     default:
         printf ("unknown (%d)", *res->sortStatus);
@@ -2864,10 +2876,7 @@ int cmd_refid (const char *arg)
     xfree (refid);
     refid = NULL;
     if (*arg)
-    {
-        refid = (char *) xmalloc (strlen(arg)+1);
-        strcpy (refid, arg);
-    }
+        refid = xstrdup (arg);
     return 1;
 }
 
@@ -2977,10 +2986,7 @@ int cmd_lang(const char* arg)
     xfree (yazLang);
     yazLang = NULL;
     if (*arg)
-    {
-        yazLang = (char *) xmalloc (strlen(arg)+1);
-        strcpy (yazLang, arg);
-    } 
+        yazLang = xstrdup(arg);
     return 1;
 }
 
@@ -3172,7 +3178,7 @@ int cmd_register_oid(const char* args) {
     
     if (sscanf (args, "%100[^ ] %100[^ ] %100s",
                 oname_str,oclass_str, oid_str) < 1) {
-        printf("Error in regristrate command \n");
+        printf("Error in register command \n");
         return 0;
     }
     
@@ -3185,7 +3191,7 @@ int cmd_register_oid(const char* args) {
     }
     
     if(!(oid_classes[i].className)) {
-        printf("Unknonwn oid class %s\n",oclass_str);
+        printf("Unknown oid class %s\n",oclass_str);
         return 0;
     }
     
@@ -3208,9 +3214,11 @@ int cmd_register_oid(const char* args) {
     oid[i] = val;
     oid[i+1] = -1;
     
-    new_oident=oid_addent (oid,PROTO_GENERAL,oidclass,oname_str,VAL_DYNAMIC);  
-    if(strcmp(new_oident->desc,oname_str)) {
-        fprintf(stderr,"oid is already named as %s, regristration faild\n",
+    new_oident = oid_addent (oid, PROTO_GENERAL, oidclass, oname_str,
+                            VAL_DYNAMIC);  
+    if(strcmp(new_oident->desc,oname_str))
+    {
+        fprintf(stderr,"oid is already named as %s, registration failed\n",
                 new_oident->desc);
     }
     return 1;  
@@ -3595,19 +3603,21 @@ int cmd_cclparse(const char* arg)
 
 int cmd_set_otherinfo(const char* args)
 {
-    char oid[101], otherinfoString[101];
+    char oidstr[101], otherinfoString[101];
     int otherinfoNo;
     int sscan_res;
     int oidval;
     
-    sscan_res = sscanf (args, "%d %100[^ ] %100s", &otherinfoNo, oid, otherinfoString);
-    if(sscan_res==1) {
+    sscan_res = sscanf (args, "%d %100[^ ] %100s", &otherinfoNo, oidstr, otherinfoString);
+    if (sscan_res==1) {
         /* reset this otherinfo */
         if(otherinfoNo>=maxOtherInfosSupported) {
-            printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
+            printf("Error otherinfo index to large (%d>%d)\n",
+                  otherinfoNo,maxOtherInfosSupported);
         }
         extraOtherInfos[otherinfoNo].oidval = -1;
-        if(extraOtherInfos[otherinfoNo].value) free(extraOtherInfos[otherinfoNo].value);                       
+        if (extraOtherInfos[otherinfoNo].value)
+           free(extraOtherInfos[otherinfoNo].value);                   
         return 0;
     }
     if (sscan_res<3) {
@@ -3615,13 +3625,14 @@ int cmd_set_otherinfo(const char* args)
         return 0;
     }
     
-    if(otherinfoNo>=maxOtherInfosSupported) {
-        printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
+    if (otherinfoNo>=maxOtherInfosSupported) {
+        printf("Error otherinfo index to large (%d>%d)\n",
+              otherinfoNo,maxOtherInfosSupported);
     }
     
-    oidval = oid_getvalbyname (oid);
-    if(oidval == -1 ) {
-        printf("Error in set_otherinfo command unknown oid %s \n",oid);
+    oidval = oid_getvalbyname (oidstr);
+    if (oidval == -1 ) {
+        printf("Error in set_otherinfo command unknown oid %s \n",oidstr);
         return 0;
     }
     extraOtherInfos[otherinfoNo].oidval = oidval;