home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dsstlkt5.zip / dssos2tk / dss / ALIAS32.C < prev    next >
C/C++ Source or Header  |  1998-05-08  |  11KB  |  335 lines

  1. /****************************************************************************/
  2. /*
  3.  *    PROGRAM NAME: ALIAS32
  4.  *    ------------
  5.  *
  6.  *    This program executes on a DSS client connected to a DSS cell.
  7.  *    It does not support connection to an LS domain.
  8.  *
  9.  *    What this program does: Creates an alias for a files resource;
  10.  *    shares it; attempts to add a directory space limit to the resource
  11.  *    (but only if directory space limits are enabled).       
  12.  *
  13.  *    This progam does not set access control on the server resource.
  14.  *    DCE sec_acl APIs could be used to provide this.
  15.  *
  16.  *    Execution of this program requires administrator rights in the
  17.  *    resource domain where the alias is created and on the server of
  18.  *    of the alias resource.
  19.  *
  20.  *    SYNTAX:
  21.  *    ------
  22.  *    ALIAS32 aliasname \\servername resourcename  [//resourcedomainname]
  23.  *       where:
  24.  *        - aliasname is the alias that the resource will be known by
  25.  *          (an alias cannot be longer than 8 bytes)
  26.  *        - \\servername is the name of the server where the resource
  27.  *          physically resides. 
  28.  *        - resourcename is a fully qualified path (beginning with a
  29.  *          drive letter).
  30.  *        - //resourcedomainname is optional and specifies the resource
  31.  *          domain name of the cell where the alias is to reside.  If not
  32.  *          specified, the resource domain of the server is used.
  33.  *
  34.  *        Note that adding an alias via this sample program will require
  35.  *        some cleanup after the program runs: an administrator will need
  36.  *        to: delete the alias, delete the share, and remove the
  37.  *        directory limit, if one was added.  These tasks may be done
  38.  *        with the following NET commands:
  39.  *        - NET ALIAS <aliasname> /DEL  /DOMAIN:resourcedomainname
  40.  *        - NET SHARE <netname (which is aliasname)> /DEL
  41.  *          or, if the shared resource is on a remote server,
  42.  *          NET ADMIN \\servername /C NET SHARE <aliasname> /DEL
  43.  *        - NET DASD <resourcename> /DEL
  44.  *          or
  45.  *          NET ADMIN \\servername /C NET DASD <resourcename> /DEL
  46.  *          if the resource resides on a remote server.
  47.  *
  48.  *    REQUIRED FILES:
  49.  *    --------------
  50.  *    ALIAS32.C        -  Source code for this program
  51.  *
  52.  *    REQUIRED LIBRARIES:
  53.  *    ------------------
  54.  *    NETAPI32.LIB     -  Netapi library (in \IBMLAN\NETSRC\LIB directory)
  55.  *
  56.  *    NetAPI functions used in this program:
  57.  *    -------------------------------------
  58.  *    Net32ServerGetInfo
  59.  *    Net32AliasAdd
  60.  *    Net32AliasDel
  61.  *    Net32ShareAdd
  62.  *    Net32DASDAdd
  63.  *
  64.  *    HOW TO COMPILE THIS PROGRAM:
  65.  *    ---------------------------
  66.  *       see sample makefile
  67.  */
  68. /****************************************************************************/
  69.  
  70. /*------- OS/2 include files -----------------------------------------------*/
  71. #define INCL_DOSMEMMGR
  72. #define INCL_DOSPROCESS
  73. #include <os2.h>
  74.  
  75. /*------- NET APIs include files -------------------------------------------*/
  76. #include <neterr.h>
  77. #include <netcons.h>
  78. #include <dasd.h>
  79. #include <dcdb.h>
  80. #include <server.h>
  81. #include <shares.h>
  82.  
  83. /*------- C include files --------------------------------------------------*/
  84. #include <stdio.h>
  85. #include <string.h>
  86.  
  87. /*------- Function declaration ---------------------------------------------*/
  88. VOID Syntax(VOID);
  89. VOID Error_Message(USHORT, PSZ);
  90.  
  91.  
  92. /*------- Main program -----------------------------------------------------*/
  93. VOID
  94. main(int argc, char *argv[])
  95. {
  96.     USHORT                      usRc = 0,
  97.                                 usCount;
  98.     ULONG                       ulBytesAvailable,
  99.                                 ulEntriesRead;
  100.     CHAR                        ServerName[UNCLEN+1];
  101.     CHAR                        ResDomName[256];
  102.     PCHAR                       pBuffer;
  103.     BOOL                        AccessAdded = FALSE;
  104.     struct alias_info_2        *pAliasInfo;
  105.     struct share_info_2        *pShareInfo;
  106.     struct dasd_info_0         *pDasdInfo;
  107.     struct server_info_10      *pSrvInfo;
  108.     ULONG Level =101L;  /* Level needed for LSE */
  109.  
  110.  
  111.     /* There must be either 4 or 5 command line arguments */
  112.     if (argc < 4 || argc > 5)
  113.         Syntax();
  114.  
  115.  
  116.     /*
  117.      * Verify that the \\servername argument begins with two backslashes,
  118.      * and that it's not too long.
  119.      */
  120.     if ( *(argv[2]) != '\\' || *(argv[2]+1) != '\\' ||
  121.          strlen(argv[2]) > UNCLEN )
  122.     {
  123.          Syntax();
  124.     }
  125.     else
  126.     {
  127.             strcpy(ServerName, argv[2]);
  128.     }
  129.  
  130.     /*
  131.      * Check for the resource domain name and verify it begins with two
  132.      * forward slashes and that it's not too long.
  133.      */
  134.     if (argc == 5)
  135.     {
  136.         if ( *(argv[4]) != '/' || *(argv[4]+1) != '/' ||
  137.              strlen(argv[2]) > 256 )
  138.         {
  139.             Syntax();
  140.         } 
  141.         else
  142.         {
  143.             strcpy(ResDomName, argv[4]);
  144.         }
  145.     } 
  146.     
  147.  
  148.     /* Make sure that the length of the alias name is acceptable. */
  149.     if (strlen(argv[1]) > ALIAS_LEN)
  150.         Syntax();
  151.  
  152.     /* allocate a buffer for the API calls */
  153.     usRc = DosAllocMem((PPVOID)&pBuffer,
  154.                        4096,
  155.                        PAG_WRITE | PAG_READ | PAG_COMMIT);
  156.  
  157.     if (usRc)
  158.     {
  159.         Error_Message(usRc, "DosAllocMem");
  160.         DosExit(EXIT_PROCESS, (ULONG)usRc);
  161.     }
  162.  
  163.     /* Set the buffer to all zeroes. */
  164.     memset(pBuffer, 0, 4096);
  165.  
  166.     /*
  167.      * If no resource domain name specified, then interrogate the
  168.      * server for it.
  169.      */
  170.  
  171.     if (argc != 5)
  172.     {
  173.        pSrvInfo = (struct server_info_10 *)(pBuffer +3072);
  174.         usRc =  Net32ServerGetInfo(ServerName,
  175.                                    10,
  176.                                    (unsigned char *)pSrvInfo,
  177.                                    1024,
  178.                              &ulBytesAvailable);
  179.  
  180.         if (usRc)
  181.         {
  182.             Error_Message(usRc, "NetServerGetInfo");
  183.             (void)DosFreeMem((PVOID)pBuffer);
  184.             DosExit(EXIT_PROCESS, (ULONG)usRc);
  185.         }
  186.         else
  187.         {
  188.            strcpy(ResDomName, "//");
  189.            strcat(ResDomName, pSrvInfo -> sv10_resdom_name);
  190.         }
  191.     }
  192.  
  193.     /*
  194.      * Set up the buffer for the call to Net32AliasAdd.
  195.      * This will be an internal files alias that is shared
  196.      * by administrator action.  The maximum number of
  197.      * simultaneous users of this alias will be 10.
  198.      */
  199.     pAliasInfo = (struct alias_info_2 *)pBuffer;
  200.  
  201.     /*
  202.      * Note that the ai2_queue, ai2_priority, and ai2_device_pool
  203.      * elements of the alias_info_2 structure do not apply to files
  204.      * aliases, so these elements are not initialized here.  Since
  205.      * the buffer has been memset to 0, these elements all have a
  206.      * value of 0.
  207.      */
  208.     strcpy(pAliasInfo -> ai2_alias, argv[1]);
  209.     pAliasInfo -> ai2_remark = "Alias created by ALIAS sample program.";
  210.     pAliasInfo -> ai2_type = ALIAS_TYPE_FILE;
  211.     pAliasInfo -> ai2_location = ALIAS_LOCATION_INTERNAL;
  212.     strcpy(pAliasInfo -> ai2_server, ServerName+2);
  213.     pAliasInfo -> ai2_mode = ALIAS_MODE_BYADMIN;
  214.     pAliasInfo -> ai2_maxuses = 10;
  215.     pAliasInfo -> ai2_path = argv[3];
  216.  
  217.     /* The Net32AliasAdd api must be remoted to the domain controller. */
  218.     usRc = Net32AliasAdd(ResDomName,
  219.                          2,
  220.                          (unsigned char *)pAliasInfo,
  221.                          sizeof(struct alias_info_2),
  222.                          NULL);
  223.  
  224.     if (usRc)
  225.     {
  226.         Error_Message(usRc, "Net32AliasAdd");
  227.         (void)DosFreeMem((PVOID)pBuffer);
  228.         DosExit(EXIT_PROCESS, (ULONG)usRc);
  229.     }
  230.     else
  231.     {
  232.         printf("The alias has been added successfully.\n");
  233.     }
  234.  
  235.  
  236.     /*
  237.      * Set up the buffer to get the alias shared.
  238.      */
  239.     pShareInfo = (struct share_info_2 *)pBuffer;
  240.  
  241.     strcpy(pShareInfo -> shi2_netname, argv[1]);/* netname == aliasname */
  242.     pShareInfo -> shi2_type = STYPE_DISKTREE;
  243.     pShareInfo -> shi2_remark = "Share added by ALIAS32 sample program.";
  244.     pShareInfo -> shi2_max_uses = 10;
  245.     if (argc == 3)
  246.         pShareInfo -> shi2_path = argv[2];
  247.     else
  248.         pShareInfo -> shi2_path = argv[3];
  249.     *pShareInfo -> shi2_passwd = '\0';
  250.     pShareInfo -> shi2_permissions = 0;
  251.     pShareInfo -> shi2_current_uses = 0;
  252.  
  253.     usRc = Net32ShareAdd(ServerName,
  254.                          2,
  255.                          pBuffer,
  256.                          4096);
  257.  
  258.     if (usRc)
  259.     {
  260.         Error_Message(usRc, "Net32ShareAdd");
  261.         printf("Deleting alias definition for %s...\n", argv[1]);
  262.         usRc = Net32AliasDel(ResDomName, argv[1], 0L, NULL);
  263.         if (usRc)
  264.         {
  265.             printf("Deletion of alias %s failed.\n", argv[1]);
  266.             Error_Message(usRc, "Net32AliasDel");
  267.         }
  268.         else
  269.         {
  270.             printf("Alias %s was deleted successfully.\n", argv[1]);
  271.         }
  272.         (void)DosFreeMem((PVOID)pBuffer);
  273.         DosExit(EXIT_PROCESS, (ULONG)usRc);
  274.     }
  275.     else
  276.     {
  277.         printf("The alias was shared successfully.\n");
  278.     }
  279.  
  280.     /*
  281.      * Finally, attempt to add a directory limit to the resource.
  282.      * We arbitrarily set the limit at 1MB and specify that alerts
  283.      * are to be generated when the directory is 90% full.
  284.      */
  285.     pDasdInfo = (struct dasd_info_0 *)pBuffer;
  286.  
  287.     pDasdInfo -> d0_resource_name = argc == 3 ? argv[2] : argv[3];
  288.     pDasdInfo -> d0_max = 1024;         /* 1024KB == 1Meg */
  289.     pDasdInfo -> d0_use = 0;
  290.     pDasdInfo -> d0_flag = DASD_VALIDATE_LIMIT_ON;
  291.     pDasdInfo -> d0_thresh = 90;
  292.     pDasdInfo -> d0_delta = 0;
  293.  
  294.     usRc = Net32DASDAdd(ServerName,
  295.                         0,
  296.                         pBuffer,
  297.                         4096);
  298.  
  299.     if (usRc == NERR_Success)
  300.     {
  301.         printf("A directory limit of 1MB was set for directory %s.\n",
  302.                 argc == 3 ? argv[2] : argv[3]);
  303.     }
  304.     else
  305.     {
  306.         Error_Message(usRc, "Net32DASDAdd");
  307.         (void)DosFreeMem((PVOID)pBuffer);
  308.     }
  309.  
  310.     DosExit(EXIT_PROCESS, (ULONG)usRc);
  311. }
  312.  
  313. /*
  314.  * Display syntax information and exit.
  315.  */
  316. VOID
  317. Syntax()
  318. {
  319.     printf("USAGE:\nThe ALIAS32 sample program adds a files alias, shares it, and\n");
  320.     printf("adds a directory limit to the resource.\n\n");
  321.     printf("Syntax:\n");
  322.     printf("   ALIAS32 aliasname \\servername resourcename\n");
  323.     printf("      OR\n");
  324.     printf("   ALIAS32 aliasname \\\\servername resourcename //resourcedomainname \n");
  325.     printf(" where:\n");
  326.     printf("   - aliasname is an 8-byte name that is not currently in use.\n");
  327.     printf("   - \\\\servername specifies the server that the alias resource\n");
  328.     printf("     resides on.\n");
  329.     printf("   - resourcename is a fully qualified path name.\n");
  330.     printf("   - //resourcedomainname is the resource resource domain name \n");
  331.     printf("     of the cell where the alias is to reside.  If not specified, \n");
  332.     printf("     the resource doamin of the server is used. \n");
  333.     DosExit(EXIT_PROCESS, 1);
  334. }
  335.