home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Src / LINEconsole / assoc.c next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  14.0 KB  |  635 lines

  1. /* assoc.c: routines to manage association with qmgr */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Src/LINEconsole/RCS/assoc.c,v 6.0 1991/12/18 20:26:30 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Src/LINEconsole/RCS/assoc.c,v 6.0 1991/12/18 20:26:30 jpo Rel $
  9.  *
  10.  * $Log: assoc.c,v $
  11.  * Revision 6.0  1991/12/18  20:26:30  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16. #include    "console.h"
  17.  
  18. extern char    *host, *actual_host, *cmd_argv[];
  19. extern int    assocD, cmd_argc;
  20. extern Command comm;
  21.  
  22. int    connected = FALSE;
  23. extern int    authorised;
  24.  
  25.  
  26. static IFP            stopfnx = NULLIFP;
  27. static jmp_buf            toplevel;
  28.  
  29. static char    *service = "pp qmgr";
  30. static struct client_dispatch   *channelread_op,
  31.                 *channelcontrol_op,
  32.                 *mtaread_op,
  33.                 *mtacontrol_op,
  34.                 *readchannelmtamessage_op,
  35.                 *msgcontrol_op,
  36.                 *quecontrol_op,
  37.                 *qmgrStatus_op,
  38.                 *quit_op;
  39. static void    acs_advise(), ros_advise();
  40. void        advise();
  41. int    remoteStop();
  42. PE    passwdpeps[1], *passwdpep = passwdpeps;
  43. /* CLIENT OPERATIONS */
  44. extern int do_channelread();
  45. extern int do_channelcontrol();
  46. extern int do_mtaread();
  47. extern int do_mtacontrol();
  48. extern int do_readchannelmtamessage();
  49. extern int do_msgcontrol();
  50. extern int do_quecontrol();
  51. int do_quit();
  52.  
  53. /* CLIENT RESULTS */
  54. extern int channelread_result();
  55. extern int channelcontrol_result();
  56. extern int mtaread_result();
  57. extern int mtacontrol_result();
  58. extern int readchannelmtamessage_result();
  59. extern int msgcontrol_result();
  60. extern int quecontrol_result();
  61. extern int qmgrStatus_result();
  62. /* CLIENT ERRORS */
  63. static int general_error();
  64.  
  65. #define channelread_error    general_error
  66. #define channelcontrol_error    general_error
  67. #define mtaread_error        general_error
  68. #define mtacontrol_error    general_error
  69. #define readchannelmtamessage_error    general_error
  70. #define msgcontrol_error    general_error
  71. #define quecontrol_error    general_error
  72. #define qmgrStatus_error    general_error
  73.  
  74. /* CLIENT STRUCTURE */
  75.  
  76. struct client_dispatch client_dispatches[] = {
  77. {
  78.     "channelread", operation_Qmgr_channelread,
  79. #ifdef PEPSY_VERSION
  80.     do_channelread, &_ZUNIV_mod, _ZUTCTimeUNIV,
  81. #else
  82.     do_channelread, free_UNIV_UTCTime,
  83. #endif
  84.     channelread_result, channelread_error,
  85.     "read information on channels"
  86. },
  87. {
  88.     "channelcontrol", operation_Qmgr_channelcontrol,
  89. #ifdef PEPSY_VERSION
  90.     do_channelcontrol, &_ZQmgr_mod, _ZChannelControlQmgr,
  91. #else
  92.     do_channelcontrol, free_Qmgr_ChannelControl,
  93. #endif
  94.     channelcontrol_result, channelcontrol_error,
  95.     "control channel"
  96. },
  97. {
  98.     "mtaread", operation_Qmgr_mtaread,
  99. #ifdef PEPSY_VERSION
  100.     do_mtaread, &_ZQmgr_mod, _ZMtaReadQmgr,
  101. #else
  102.     do_mtaread, free_Qmgr_MtaRead,
  103. #endif
  104.     mtaread_result, mtaread_error,
  105.     "read information on mtas"
  106. },
  107. {
  108.     "mtacontrol", operation_Qmgr_mtacontrol,
  109. #ifdef PEPSY_VERSION
  110.     do_mtacontrol, &_ZQmgr_mod, _ZMtaControlQmgr,
  111. #else
  112.     do_mtacontrol, free_Qmgr_MtaControl,
  113. #endif
  114.     mtacontrol_result, mtacontrol_error,
  115.     "control mta"
  116. },
  117. {
  118.     "readchannelmtamessage", operation_Qmgr_readChannelMtaMessage,
  119. #ifdef PEPSY_VERSION
  120.     do_readchannelmtamessage, &_ZQmgr_mod, _ZMsgReadQmgr,
  121. #else
  122.     do_readchannelmtamessage, free_Qmgr_MsgRead,
  123. #endif
  124.     readchannelmtamessage_result, readchannelmtamessage_error,
  125.     "read a set of messages"
  126. },
  127. {
  128.     "msgcontrol", operation_Qmgr_msgcontrol,
  129. #ifdef PEPSY_VERSION
  130.     do_msgcontrol, &_ZQmgr_mod, _ZMsgControlQmgr,
  131. #else
  132.     do_msgcontrol, free_Qmgr_MsgControl,
  133. #endif
  134.     msgcontrol_result, msgcontrol_error,
  135.     "control msg"
  136. },
  137. {
  138.     "quecontrol", operation_Qmgr_qmgrControl,
  139. #ifdef PEPSY_VERSION
  140.     do_quecontrol, &_ZQmgr_mod, _ZQMGRControlQmgr,
  141. #else
  142.     do_quecontrol, free_Qmgr_QMGRControl,
  143. #endif
  144.     quecontrol_result, quecontrol_error,
  145.     "control qmgr"
  146. },
  147. {
  148.     "qmgrStatus", operation_Qmgr_qmgrStatus, NULLIFP,
  149. #ifdef PEPSY_VERSION
  150.     &_ZQmgr_mod, _ZQmgrStatusQmgr,
  151. #else
  152.     free_Qmgr_QmgrStatus,
  153. #endif
  154.     qmgrStatus_result, qmgrStatus_error,
  155.     "control qmgr"
  156. },
  157. {
  158.     "quit",     0,      do_quit,
  159. #ifdef PEPSY_VERSION
  160.     NULL, 0, NULLIFP, NULLIFP,
  161. #else
  162.     NULLIFP, NULLIFP, NULLIFP,
  163. #endif
  164.     "terminate the association and exit"
  165. },
  166. {
  167.     NULL
  168. }
  169. };
  170.  
  171. init_assoc()
  172. {
  173.     struct client_dispatch         *ix;
  174.     
  175.     for (ix = client_dispatches; ix->ds_name; ix++){
  176.         if (strcmp(ix -> ds_name, "channelread") == 0)
  177.             channelread_op = ix;
  178.         else if (strcmp(ix->ds_name, "channelcontrol") == 0)
  179.             channelcontrol_op = ix;
  180.         else if (strcmp(ix->ds_name, "quit") == 0)
  181.             quit_op = ix;
  182.         else if (strcmp(ix->ds_name, "mtaread") == 0)
  183.             mtaread_op = ix;
  184.         else if (strcmp(ix->ds_name, "mtacontrol") == 0)
  185.             mtacontrol_op = ix;
  186.         else if (strcmp(ix->ds_name, "readchannelmtamessage") == 0)
  187.             readchannelmtamessage_op = ix;
  188.         else if (strcmp(ix->ds_name, "msgcontrol") == 0)
  189.             msgcontrol_op = ix;
  190.         else if (strcmp(ix->ds_name, "quecontrol") == 0)
  191.             quecontrol_op = ix;
  192.         else if (strcmp(ix->ds_name, "qmgrStatus") == 0)
  193.             qmgrStatus_op = ix;
  194.     }
  195.     stopfnx = remoteStop;
  196. }
  197.  
  198. ConnectProc ()
  199. {
  200.     char    *args;
  201.     args = strdup(host);
  202.     do_connect(&args);
  203.     free(args);
  204. }
  205.  
  206. do_connect(args)
  207. char    **args;
  208. {
  209.     struct SSAPref             sfs;
  210.     register struct SSAPref     *sf;
  211.     register struct PSAPaddr     *pa;
  212.     struct AcSAPconnect         accs;
  213.     register struct AcSAPconnect       *acc = &accs;
  214.     struct AcSAPindication      acis;
  215.     register struct AcSAPindication    *aci = &acis;
  216.     register struct AcSAPabort     *aca = &aci -> aci_abort;
  217.     AEI                    aei;
  218.     OID                    ctx,
  219.                     tmppci;
  220.     struct PSAPctxlist         pcs;
  221.     register struct PSAPctxlist     *pc = &pcs;
  222.     struct RoSAPindication         rois;
  223.     register struct RoSAPindication    *roi = &rois;
  224.     register struct RoSAPpreject     *rop = &roi -> roi_preject;
  225.  
  226.     if (host) free(host);
  227.     host = strdup(*args);
  228.     if (actual_host) free(actual_host);
  229.     actual_host = strdup(host);
  230.     if ((aei = _str2aei (host, service, QMGR_CTX_OID, 
  231.                  0, dap_user, dap_passwd)) == NULLAEI) {
  232.         advise (NULLCP, "%s: unknown entity",
  233.                host);
  234.         return NOTOK;
  235.     }
  236.     if ((pa = aei2addr (aei)) == NULLPA) {
  237.         advise (NULLCP, "%s", "address translation failed");
  238.         return NOTOK;
  239.     }
  240.  
  241.     if ((ctx = oid_cpy (QMGR_AC)) == NULLOID) {
  242.         advise (NULLCP, "%s", "out of memory");
  243.         return NOTOK;
  244.     }
  245.  
  246.     if ((tmppci = oid_cpy (QMGR_PCI)) == NULLOID) {
  247.         advise (NULLCP, "%s", "out of memory"); 
  248.         return NOTOK;
  249.     }
  250.     pc -> pc_nctx = 1;
  251.     pc -> pc_ctx[0].pc_id = 1;
  252.     pc -> pc_ctx[0].pc_asn = tmppci;
  253.     pc -> pc_ctx[0].pc_atn = NULLOID;
  254.  
  255.     if ((sf = addr2ref (PLocalHostName ())) == NULL) {
  256.         sf = &sfs;
  257.         (void) bzero ((char *) sf, sizeof *sf);
  258.     }
  259.  
  260.     fprintf(stdout, "Please wait while attempt to connect to %s...", host);
  261.     fflush(stdout);
  262.  
  263.     if (AcAssocRequest (ctx, NULLAEI, aei, NULLPA,
  264.                 pa, pc, NULLOID, 0, ROS_MYREQUIRE,
  265.                 SERIAL_NONE, 0, sf, passwdpep, *passwdpep ? 1 : 0,
  266.                 NULLQOS, acc, aci) == NOTOK) {
  267.         acs_advise (aca, "A-ASSOCIATE.REQUEST");
  268.         return NOTOK;
  269.     }
  270.     if (acc -> acc_result != ACS_ACCEPT) {
  271.         advise (NULLCP, "Association rejected: [%s]",
  272.             AcErrString (acc->acc_result));
  273.         return NOTOK;
  274.     }
  275.  
  276.     assocD = acc->acc_sd;
  277.     
  278.     set_authmode(acc);
  279.  
  280.     ACCFREE (acc);
  281.  
  282.     if (RoSetService (assocD, RoPService, roi) == NOTOK) {
  283.         ros_advise (rop, "set RO/PS fails");
  284.         return NOTOK;
  285.     }
  286.     fprintf(stdout, "connected\n");
  287.     fflush(stdout);
  288.     connected = TRUE;
  289.     TaiInit();
  290.     return OK;
  291. }
  292.  
  293. DisconnectProc()
  294. {
  295.     my_invoke(disconnect, &host);
  296. }
  297.  
  298. QuitProc()
  299. {
  300.     if (connected == TRUE)
  301.         DisconnectProc();
  302. }
  303.  
  304. static void resetForDisconnect()
  305. {
  306.     connected = FALSE;
  307.     clear_msg_level();
  308.     clear_mta_level();
  309.     clear_channel_level();
  310. }
  311.  
  312. extern time_t    currentTime;
  313.  
  314. static    int invoke (ad, ops, ds, args)
  315. int                ad;
  316. struct RyOperation         ops[];
  317. register struct client_dispatch    *ds;
  318. char                  **args;
  319. {
  320.     int                        result;
  321.     caddr_t                 in;
  322.     struct AcSAPindication  acis;
  323.     struct RoSAPindication  rois;
  324.     register struct RoSAPindication    *roi = &rois;
  325.     register struct RoSAPpreject       *rop = &roi -> roi_preject;
  326.  
  327.     in = NULL;
  328.     time(¤tTime);
  329.  
  330.     if (ds -> ds_argument && (*ds -> ds_argument) (ad, ds, args, &in) == NOTOK)
  331.         return 0;
  332.  
  333.     switch (setjmp (toplevel)) {
  334.     case OK: 
  335.         break;
  336.  
  337.     default: 
  338.         if (stopfnx)
  339.         return (*stopfnx) (ad, (struct AcSAPfinish *) 0);
  340.         break;
  341.     case DONE:
  342.         (void) AcUAbortRequest (ad, NULLPEP, 0, &acis);
  343.         (void) RyLose (ad, roi);
  344.         return NOTOK;
  345.     }
  346.  
  347.     if (connected != TRUE)
  348.         return NOTOK;
  349.  
  350.     switch (result = RyStub (ad, ops, ds -> ds_operation, RyGenID (ad),
  351.                  NULLIP, in, ds -> ds_result, 
  352.                  ds -> ds_error, ROS_SYNC, roi)) {
  353.     case NOTOK:        /* failure */
  354.         if (ROS_FATAL(rop->rop_reason)) {
  355.             ros_advise (rop, "STUB");
  356.             fprintf(stderr,
  357.                 "Disconnected from '%s'\n", host);
  358.             comm = Disconnect;
  359.             resetForDisconnect();
  360.         }
  361.         break;
  362.         
  363.     case OK:        /* got a result/error response */
  364.         break;
  365.         
  366.     case DONE:        /* got RO-END? */
  367.         advise (NULLCP, "%s", "got RO-END.INDICATION");
  368.         connected = FALSE;
  369.         /* NOTREACHED */
  370.  
  371.     default:
  372.         advise (NULLCP, "unknown return from RyStub=%d", result);
  373.         /* NOTREACHED */
  374.     }
  375.  
  376. #ifdef PEPSY_VERSION
  377.     if (ds -> ds_fr_mod && in)
  378.         fre_obj (in,  ds -> ds_fr_mod -> md_dtab[ds -> ds_fr_index],
  379.              ds -> ds_fr_mod);
  380. #else
  381.     if (ds -> ds_free && in)
  382.         (*ds -> ds_free)(in);
  383. #endif
  384.     return OK;
  385. }
  386.  
  387. Operations    currentop;
  388.  
  389. my_invoke(op, args)
  390. Operations    op;
  391. char        **args;
  392. {
  393.     caddr_t    in;
  394.     currentop = op;
  395.     switch (op) {
  396.         case chanread:
  397.         invoke(assocD, table_Qmgr_Operations, 
  398.                channelread_op,(char **) NULL);
  399.         break;
  400.         case chanstop:
  401.         case chanstart:
  402.         case chanclear:
  403.         case chancacheadd:
  404.         invoke(assocD, table_Qmgr_Operations,
  405.                channelcontrol_op, args);
  406.         break;
  407.         case mtaread:
  408.         invoke(assocD, table_Qmgr_Operations,
  409.                mtaread_op, args);
  410.         break;
  411.         case mtastop:
  412.         case mtastart:
  413.         case mtaclear:
  414.         case mtacacheadd:
  415.         invoke(assocD, table_Qmgr_Operations,
  416.                mtacontrol_op, args);
  417.         break;
  418.         case readchannelmtamessage:
  419.         invoke(assocD, table_Qmgr_Operations,
  420.                readchannelmtamessage_op, args);
  421.         break;
  422.         case msgstop:
  423.         case msgstart:
  424.         case msgclear:
  425.         case msgcacheadd:
  426.         invoke(assocD, table_Qmgr_Operations,
  427.                msgcontrol_op, args);
  428.         break;
  429.         case quit:
  430.         invoke(assocD, table_Qmgr_Operations,
  431.                quit_op, (char **) NULL);
  432.         break;
  433.         case connect:
  434.         return do_connect(args);
  435.         case disconnect:
  436.         if (connected == TRUE)
  437.             do_quit(assocD, quit_op, args, &in);
  438.         break;
  439.         case quecontrol:
  440.         invoke(assocD, table_Qmgr_Operations,
  441.                quecontrol_op, args);
  442.         break;
  443.         case qmgrStatus:
  444.         invoke(assocD, table_Qmgr_Operations,
  445.                qmgrStatus_op, args);
  446.         return 0;
  447.         default:
  448.         break;
  449.     }
  450.     return 0;
  451. }
  452.  
  453. /* ARGSUSED */
  454. int  do_quit (ad, ds, args, arg)
  455. int     ad;
  456. struct client_dispatch *ds;
  457. char      **args,
  458.     **arg;
  459. {
  460.     struct AcSAPrelease acrs;
  461.     register struct AcSAPrelease   *acr = &acrs;
  462.     struct AcSAPindication  acis;
  463.     register struct AcSAPindication *aci = &acis;
  464.     register struct AcSAPabort *aca = &aci -> aci_abort;
  465.  
  466.     if (ad >= 0) {
  467. #ifdef    CONNECTING_1
  468.         if (AcRelRequest (ad, ACF_NORMAL, NULLPEP, 0, NOTOK, acr, aci) == NOTOK)
  469. #else
  470.         if (AcRelRequest (ad, ACF_NORMAL, NULLPEP, 0, acr, aci) == NOTOK)
  471. #endif
  472.             acs_advise (aca, "A-RELEASE.REQUEST");
  473.  
  474.         if (!acr -> acr_affirmative) {
  475.             (void) AcUAbortRequest (ad, NULLPEP, 0, aci);
  476.             advise (NULLCP, "Release rejected by peer: %d", acr -> acr_reason);
  477.         }
  478.         ad = -1;
  479.     }
  480.     
  481.     resetForDisconnect();
  482. }
  483.  
  484. /*   */
  485. /* error routine */
  486.  
  487. /* ARGSUSED */
  488. static general_error (ad, id, err, parameter, roi)
  489. int     ad,
  490.     id,
  491.     err;
  492. caddr_t parameter;
  493. struct RoSAPindication *roi;
  494. {
  495.     register struct RyError *rye;
  496.  
  497.     if (err == RY_REJECT) {
  498.         advise (NULLCP, "%s", RoErrString ((int) parameter));
  499.         return OK;
  500.     }
  501.  
  502.     if ((rye = finderrbyerr (table_Qmgr_Errors, err)) != 
  503.         (struct RyError *) NULL)
  504.         advise (NULLCP, "Error: %s", rye -> rye_name);
  505.     else
  506.         advise (NULLCP, "Error: %d", err);
  507.  
  508.     return OK;
  509. }
  510.  
  511. /*   */
  512. /* advising routines */
  513.  
  514. static void    acs_advise (aca, event)
  515. register struct AcSAPabort *aca;
  516. char   *event;
  517. {
  518.     char    buffer[BUFSIZ];
  519.  
  520.     if (aca -> aca_cc > 0)
  521.         (void) sprintf (buffer, "[%s] %*.*s",
  522.                 AcErrString (aca -> aca_reason),
  523.                 aca -> aca_cc, aca -> aca_cc, aca -> aca_data);
  524.     else
  525.         (void) sprintf (buffer, "[%s]",
  526.                 AcErrString (aca -> aca_reason));
  527.  
  528.     advise (NULLCP, "%s: %s (source %d)", event, buffer,
  529.         aca -> aca_source);
  530. }
  531.  
  532.  
  533. static void    ros_advise (rop, event)
  534. register struct RoSAPpreject *rop;
  535. char   *event;
  536. {
  537.     char    buffer[BUFSIZ];
  538.  
  539.     if (rop -> rop_cc > 0)
  540.         (void) sprintf (buffer, "[%s] %*.*s",
  541.                 RoErrString (rop -> rop_reason),
  542.                 rop -> rop_cc, rop -> rop_cc, rop -> rop_data);
  543.     else
  544.         (void) sprintf (buffer, "[%s]",
  545.                 RoErrString (rop -> rop_reason));
  546.  
  547.     advise (NULLCP, "%s: %s", event, buffer);
  548. }
  549.  
  550. #ifndef lint
  551. void advise (va_alist)
  552. va_dcl
  553. {
  554.     va_list ap;
  555.     
  556.     char    buffer[BUFSIZ];
  557.  
  558.     va_start (ap);
  559.     asprintf (buffer, ap);
  560.     fprintf(stdout, "%s\n",buffer);
  561.     va_end (ap);
  562. }
  563. #else
  564. /* VARARGS2 */
  565.  
  566. void advise(what, fmt)
  567. char    *what,
  568.     *fmt;
  569. {
  570.     advise(what,fmt);
  571. }
  572. #endif
  573.  
  574. /*   */
  575.  
  576. /* ARGSUSED */
  577. int remoteStop(fd, dummy)
  578. int    fd;
  579. struct AcSAPfinish    *dummy;
  580. {
  581.     struct AcSAPindication  acis;
  582.     struct RoSAPindication  rois;
  583.     register struct RoSAPindication *roi = &rois;
  584.  
  585.     connected = FALSE;
  586.  
  587.     /* isode release */
  588.     (void) AcUAbortRequest (fd, NULLPEP, 0, &acis);
  589.     (void) RyLose (fd, roi);
  590.     
  591.     comm = Disconnect;
  592.     return NOTOK;
  593. }
  594.  
  595. struct type_Qmgr_BindArgument    *ba;
  596.  
  597. fillin_passwdpep(user, passwd, auth)
  598. char    *user,
  599.     *passwd;
  600. int    auth;
  601. {
  602.     
  603.     if (*passwdpep != NULL) {
  604.         pe_free(*passwdpep);
  605.         free_Qmgr_BindArgument(ba);
  606.         *passwdpep = NULL;
  607.     }
  608.  
  609.     ba = (struct type_Qmgr_BindArgument *) smalloc (sizeof *ba);
  610.     
  611.     if (auth != TRUE) {
  612.         /* no authentication */
  613.         ba -> offset = type_Qmgr_BindArgument_noAuthentication;
  614.     } else {
  615.         ba -> offset = type_Qmgr_BindArgument_weakAuthentication;
  616.         ba -> un.weakAuthentication = 
  617.             (struct type_Qmgr_WeakAuthentication *) 
  618.                 smalloc(sizeof(struct type_Qmgr_WeakAuthentication));
  619.         ba -> un.weakAuthentication->username = str2qb(user, strlen(user), 1);
  620.         if (passwd != NULLCP && *passwd != '\0')
  621.             ba -> un.weakAuthentication->passwd = str2qb(passwd, strlen(passwd), 1);
  622.         else 
  623.             ba -> un.weakAuthentication->passwd = NULL;
  624.     }
  625.         
  626.     if (encode_Qmgr_BindArgument(passwdpep, 1, NULLCP, 0, ba) == NOTOK) {
  627.         PP_LOG(LLOG_EXCEPTIONS,
  628.                ("failed to encode BindArgument [%s]", PY_pepy));
  629.         *passwdpep = NULLPE;
  630.         free_Qmgr_BindArgument(ba);
  631.         exit(1);
  632.     }
  633.     (*passwdpep)->pe_context = 3;
  634. }
  635.