.ad 8
.bm 8
.fm 4
.bt $Copyright by   Software AG, 1993$$Page %$
.tm 12
.hm 6
.hs 3
.tt 1 $NME$Project Distributed Database System$vos03c$
.tt 2 $$$
.tt 3 $R.Roedling$Windows NT Functions$1997-05-14$
***********************************************************
.nf


    ========== licence begin LGPL
    Copyright (C) 2002 SAP AG

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    ========== licence end

.fo
.nf
.sp
Module  :
=========
.sp
Purpose :
.CM *-END-* purpose -------------------------------------
.sp
.cp 3
Define  :

.CM *-END-* define --------------------------------------
.sp;.cp 3
Use     :

.CM *-END-* use -----------------------------------------
.sp;.cp 3
Synonym :

.CM *-END-* synonym -------------------------------------
.sp;.cp 3
Author  : R.Roedling
.sp
.cp 3
Created : 1997-04-21
.sp
.cp 3
Version : 1997-04-21
.sp
.cp 3
Release :  6.2 	 Date : 1997-05-14
.br
.sp
***********************************************************
.sp
.cp 10
.fo
.oc _/1
Specification:

.CM *-END-* specification -------------------------------
.sp 2
***********************************************************
.sp
.cp 10
.fo
.oc _/1
Description:

.CM *-END-* description ---------------------------------
.sp 2
***********************************************************
.sp
.cp 10
.nf
.oc _/1
Structure:

.CM *-END-* structure -----------------------------------
.sp 2
**********************************************************
.sp
.cp 10
.nf
.oc _/1
.CM -lll-
Code    :
/*PRETTY*/


//
//  INCLUDE FILES
//
#include           <lm.h>


//
//  DEFINES
//

#define MOD__  "VOS03C : "
#define MF__   MOD__"UNDEFINED"

#define NET_API_DLL               "NETAPI32.DLL"

//
//  MACROS
//
#define MK_PA(f)       (NET_API_FUNCTION *lp##f)
#define LOAD_FUNC(f)   ((lp##f = sql03c_load_net_func(#f)) == NULL) ? \
                         -1 : NO_ERROR


//
//  LOCAL TYPE AND STRUCT DEFINITIONS
//


//
//  EXTERNAL VARIABLES
//


//
//  EXPORTED VARIABLES
//


//
//  LOCAL VARIABLES
//
static HINSTANCE      hinst = NULL;
static NET_API_STATUS MK_PA(NetGetDCName)     ( LPWSTR   servername,
                                                LPWSTR   domainname,
                                                LPBYTE  *bufptr );
static NET_API_STATUS MK_PA(NetLocalGroupAdd) ( LPWSTR   servername,
                                                DWORD    level,
                                                LPBYTE   buf,
                                                LPDWORD  parm_err );
static NET_API_STATUS MK_PA(NetGroupAdd)      ( LPWSTR   servername,
                                                DWORD    level,
                                                LPBYTE   buf,
                                                LPDWORD  parm_err );

static NET_API_STATUS MK_PA(NetApiBufferFree) ( LPVOID   Buffer );

static NET_API_STATUS MK_PA(NetUserModalsGet) ( LPWSTR   servername,
                                                DWORD    level,
                                                LPBYTE   *bufptr );



//
//  LOCAL FUNCTION PROTOTYPES
//
static VOID*  sql03c_load_net_func              ( CHAR    *szFuncName );


//
// ========================== GLOBAL FUNCTIONS ================================
//

APIRET sql03c_add_ADABAS_operators_group ( VOID )
  {
  #undef  MF__
  #define MF__ MOD__"sql03c_add_ADABAS_operators_group"
  #ifdef CRASH_CLEANUP
   #undef CRASH_CLEANUP
  #endif
  #define CRASH_CLEANUP(_no,_rc)                                             \
      MSGCD(( ERR_ADD_ADABAS_ADMIN_GROUP, _no, _rc ));                       \
      if( lpwszDomainController ) lpNetApiBufferFree ( lpwszDomainController);
  LONG               rc                     = NO_ERROR;
  LPWSTR             lpwszDomainController  = NULL;
  LOCALGROUP_INFO_1  LocalGroupInfo1;

  DBGIN;

  // --- load this net functions by 'LoadLibrary'
  //     because there is no 'NETAPI32.DLL' available
  //     on Windows95 systems
  //
  rc = LOAD_FUNC( NetGetDCName );

  if ( rc == NO_ERROR )
    rc = LOAD_FUNC( NetLocalGroupAdd );

  if ( rc == NO_ERROR )
    rc = LOAD_FUNC( NetGroupAdd );

  if ( rc == NO_ERROR )
    rc = LOAD_FUNC( NetApiBufferFree );

  if ( rc != NO_ERROR )
    return ( rc );


  // --- get domain controller server name
  rc = lpNetGetDCName ( NULL, NULL, ( LPBYTE *) &lpwszDomainController );

  if (( rc != NERR_Success ) && ( rc != NERR_DCNotFound ))
    {
    CRASH_CLEANUP( 1, rc );
    DBGOUT;
    return ( rc );
    }

  if ( rc != NERR_DCNotFound )
    {
    // --- set name & comment
    LocalGroupInfo1.lgrpi1_name    = L_DOMAIN_DATABASE_OPERATOR_GROUP;
    LocalGroupInfo1.lgrpi1_comment = L_DATABASE_OPERATOR_GROUP_DESC;

    rc = lpNetGroupAdd ( lpwszDomainController, 1,
                         (LPBYTE) &LocalGroupInfo1, NULL);

    if (( rc != NERR_Success )       &&
        ( rc != NERR_GroupExists )   && // --- group already there?
        ( rc != ERROR_ALIAS_EXISTS ) && // --- group already there?
        ( rc != ERROR_ACCESS_DENIED ))
      {
      CRASH_CLEANUP( 2, rc );
      DBGOUT;
      return ( rc );
      }

    // --- set name & comment
    LocalGroupInfo1.lgrpi1_name    = L_DATABASE_OPERATOR_GROUP;
    LocalGroupInfo1.lgrpi1_comment = L_DATABASE_OPERATOR_GROUP_DESC;

    rc = lpNetLocalGroupAdd ( lpwszDomainController, 1,
                              (LPBYTE) &LocalGroupInfo1, NULL);

    if (( rc != NERR_Success )       &&
        ( rc != NERR_GroupExists )   && // --- group already there?
        ( rc != ERROR_ALIAS_EXISTS ) && // --- check if group already there
        ( rc != ERROR_ACCESS_DENIED ))
      {
      CRASH_CLEANUP( 3, rc );
      DBGOUT;
      return ( rc );
      }
    }

  // --- set name & comment
  LocalGroupInfo1.lgrpi1_name    = L_DATABASE_OPERATOR_GROUP;
  LocalGroupInfo1.lgrpi1_comment = L_DATABASE_OPERATOR_GROUP_DESC;

  rc = lpNetLocalGroupAdd ( NULL, 1, (LPBYTE) &LocalGroupInfo1, NULL);

  if (( rc != NERR_Success )       &&
      ( rc != NERR_GroupExists )   && // --- group already there?
      ( rc != ERROR_ALIAS_EXISTS ) && // --- check if group already there
      ( rc != ERROR_ACCESS_DENIED ))
    {
    CRASH_CLEANUP( 4, rc );
    DBGOUT;
    return ( rc );
    }

  if ( lpwszDomainController != NULL )
    lpNetApiBufferFree ( lpwszDomainController );

  if (( rc == ERROR_ALIAS_EXISTS ) || ( rc == NERR_GroupExists ))
    rc = NO_ERROR;

  DBGOUT;
  return( rc );
  }

/*------------------------------*/

APIRET sql03c_get_domain_name ( PSZ pszDomainName )
  {
  #undef  MF__
  #define MF__ MOD__"sql03c_get_domain_controller"
  #ifdef CRASH_CLEANUP
   #undef CRASH_CLEANUP
  #endif
  #define CRASH_CLEANUP(_no,_rc)                    \
      MSGCD(( ERR_GET_DOMAIN_NAME, _no, _rc ));     \
      if( pUserModInfo2 ) lpNetApiBufferFree ( pUserModInfo2 );
  LONG                 rc            = NO_ERROR;
  PUSER_MODALS_INFO_2  pUserModInfo2 = NULL;

  DBGIN;

  rc = LOAD_FUNC( NetUserModalsGet );

  if ( rc == NO_ERROR )
    rc = LOAD_FUNC( NetApiBufferFree );

  if ( rc != NO_ERROR )
    return ( rc );


  // --- get domain controller name
  rc = lpNetUserModalsGet(NULL, 2, (LPBYTE *)&pUserModInfo2);

  if ( rc != NERR_Success )
    {
    CRASH_CLEANUP( 1, rc );
    DBGOUT;
    return ( rc );
    }

  WideCharToMultiByte( CP_ACP, 0, pUserModInfo2->usrmod2_domain_name, -1,
                       pszDomainName, MAX_COMPUTERNAME_LENGTH,
                       NULL, NULL );

  pszDomainName[MAX_COMPUTERNAME_LENGTH] = '\0';
  lpNetApiBufferFree ( pUserModInfo2 );

  DBGOUT;
  return( NO_ERROR );
  }


//
// ========================== LOCAL FUNCTIONS =================================
//

static VOID* sql03c_load_net_func( CHAR  *szFuncName )
  {
  #undef  MF__
  #define MF__ MOD__"sql03c_load_net_func"
  LONG             rc    = NO_ERROR;
  FARPROC          pFunc = NULL;

  DBGIN;

  if ( hinst == NULL )
    hinst = LoadLibrary( NET_API_DLL );

  if ( hinst != NULL )
    pFunc = GetProcAddress( hinst, szFuncName);

  if (( hinst == NULL ) || ( pFunc == NULL ))
    {
    rc = GetLastError();
    MSGD(( ERR_STARTING_FUNC_FOM_DLL, szFuncName, NET_API_DLL, rc ));
    }

  DBGOUT;
  return ( pFunc );
  }

//
// =============================== END ========================================
//
.CM *-END-* code ----------------------------------------
.SP 2
***********************************************************
.PA
