home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Collection - Online Library - January 1996 / CKITOS2196.ISO / diskette / gg244090.dsk / unc.dsk / CHAPTER.09 / CLIENT.C next >
C/C++ Source or Header  |  1993-08-04  |  7KB  |  210 lines

  1. /***************************************************************************/
  2. /*                                                                         */
  3. /* Module:      client.c                                                   */
  4. /*                                                                         */
  5. /* Description: Implements the client for the MessageBox application.      */
  6. /*                                                                         */
  7. /***************************************************************************/
  8.  
  9. # include <string.h>
  10.  
  11. # include "mbox.h"
  12. # include "common.h"
  13.  
  14. # define NO_ERROR        10
  15. # define NO_OPTION      -11
  16. # define NO_PRINCIPAL   -12
  17.  
  18. # ifdef IBMOS2
  19. # define USAGE          "usage: %s [/c] [principal] [message ...]\n"
  20. # define OPTCHAR        '/'
  21. # else
  22. # define USAGE          "usage: %s [-c] [principal] [message ...]\n"
  23. # define OPTCHAR        '-'
  24. # endif
  25.  
  26. # define IF_HANDLE      MessageBox_v2_0_c_ifspec
  27.  
  28. void error( int );
  29.  
  30. char    *prgname   = NULL;
  31. char    *principal = NULL;
  32.  
  33. int main (int argc, char *argv[])
  34. {
  35.         char            msg[MAX_CHAR]   = "";
  36.         int             action          = MBOX_READ;
  37.         int             rc;
  38.         handle_t        bh;
  39.         rpc_ns_handle_t ns_handle;
  40.         error_status_t  status;
  41.  
  42.         /* get application name */
  43.         prgname = strdup(*argv++);
  44.  
  45.         /* parse the commandline for options */
  46.         while ( --argc ) {
  47.                 if ( **argv != OPTCHAR )
  48.                         break;
  49.  
  50.                 while ( *++(*argv) ) {
  51.                         switch ( **argv ) {
  52.                         case 'c':
  53.                                 action = MBOX_CREATE;
  54.                                 break;
  55.                         default:
  56.                                 error(NO_OPTION);
  57.                         }
  58.                 }
  59.                 argv++;
  60.  
  61.         }
  62.  
  63.         /* if there are args, assume the first none option is the principal */
  64.         if ( argc ) {
  65.                 principal = *argv++;
  66.                 if ( action == MBOX_READ )
  67.                         action    = MBOX_APPEND;
  68.  
  69.                 /* concat the rest of the command line to a message */
  70.                 while ( --argc ) {
  71.                         strcat(msg,*argv++);
  72.                         strcat(msg," ");
  73.                 }
  74.         }
  75.  
  76.         /* check for the principal argument */
  77.         if ( ( action == MBOX_CREATE ) && ( principal == "" ) )
  78.                 error( NO_PRINCIPAL );
  79.  
  80.         /* set up for reading bindin information */
  81.         rpc_ns_binding_import_begin(
  82.                 rpc_c_ns_syntax_default,
  83.                 ENTRY_NAME,
  84.                 IF_HANDLE,
  85.                 NULL,
  86.                 &ns_handle,
  87.                 &status
  88.         );
  89.         ERRCHK( status );
  90.  
  91.         /* import first binding information from name space */
  92.         rpc_ns_binding_import_next(
  93.                 ns_handle,
  94.                 &bh,
  95.                 &status
  96.         );
  97.         ERRCHK( status );
  98.  
  99.         /* stop reading binding indormation */
  100.         rpc_ns_binding_import_done(
  101.                 &ns_handle,
  102.                 &status
  103.         );
  104.         ERRCHK( status );
  105.  
  106.         /* determine which authorization info to send based on action */
  107.         if ( action == MBOX_CREATE )
  108.                 rpc_binding_set_auth_info(
  109.                         bh,
  110.                         PRINCIPAL_NAME,
  111.                         rpc_c_protect_level_pkt_integ, /* send checksum       */
  112.                         rpc_c_authn_default,
  113.                         NULL,                          /* use login context   */
  114.                         rpc_c_authz_dce,               /* send PAC            */
  115.                         &status
  116.                 );
  117.         else
  118.                 rpc_binding_set_auth_info(
  119.                         bh,
  120.                         PRINCIPAL_NAME,
  121.                         rpc_c_protect_level_pkt_integ, /* send checksum       */
  122.                         rpc_c_authn_default,
  123.                         NULL,                          /* use login context   */
  124.                         rpc_c_authz_name,              /* send name, not PAC  */
  125.                         &status
  126.                 );
  127.         ERRCHK( status );
  128.  
  129.         /* call the remote procedures for action */
  130.         switch ( action ) {
  131.         case MBOX_CREATE:
  132.                 if ( (rc = mbox_new(bh,principal)) != MBOX_OK )
  133.                         error( rc );
  134.  
  135.                 break;
  136.  
  137.         case MBOX_APPEND:
  138.                 if ( (rc = mbox_append(bh,principal,msg)) != MBOX_OK )
  139.                         error( rc );
  140.                 break;
  141.  
  142.         case MBOX_READ:
  143.                 switch ( rc = mbox_next(bh,msg) ) {
  144.                 case MBOX_OK:
  145.                         printf("%s\n",msg);
  146.                         break;
  147.  
  148.                 case MBOX_NO_MSG:
  149.                         printf("%s: no more messages\n",prgname);
  150.                         break;
  151.                 default:
  152.                         error( rc );
  153.                 }
  154.                 break;
  155.  
  156.         }
  157.  
  158.         exit ( NO_ERROR );
  159. }
  160.  
  161. /***************************************************************************/
  162. /*                                                                         */
  163. /* Name:        function error()                                           */
  164. /*                                                                         */
  165. /* Description: error() implements the error printing routine.             */
  166. /*              All error output is send to stderr. After the error text   */
  167. /*              is printed error() exist the application with a error      */
  168. /*              return value.                                              */
  169. /*                                                                         */
  170. /***************************************************************************/
  171. void error (int msg)
  172. {
  173.         switch ( msg ) {
  174.         case MBOX_PRINCIPAL_EXIST:
  175.                 fprintf(stderr,"%s: mbox for principal %s exists already\n",
  176.                         prgname,principal);
  177.                 break;
  178.  
  179.         case MBOX_PRINCIPAL_NOT_EXIST:
  180.                 fprintf(stderr,"%s: principal %s has no message box\n",
  181.                         prgname,principal);
  182.                 break;
  183.  
  184.         case MBOX_NOT_AUTHORIZED:
  185.                 fprintf(stderr,"%s: you are not authorized\n",
  186.                         prgname);
  187.                 break;
  188.  
  189.         case MBOX_ALL_USED:
  190.                 fprintf(stderr,"%s: no more message boxes available\n",
  191.                         prgname);
  192.                 break;
  193.  
  194.         case NO_PRINCIPAL:
  195.                 fprintf(stderr,"%s: missing principal name\n",prgname);
  196.                 break;
  197.  
  198.         case NO_OPTION:
  199.                 fprintf(stderr,"%s: unknown option\n",prgname);
  200.                 break;
  201.  
  202.         default:
  203.                 fprintf(stderr,"%s: something is wrong\n",prgname);
  204.         }
  205.         fprintf(stderr, USAGE,prgname);
  206.  
  207.         exit( msg );
  208. }
  209.  
  210.