RPC 11.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* Copyright 1998 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
*
*  RPC.C  -- Remote procedure call routines for
*              interrogating servers
*
*  02-02-95 INH  Original
*  	    	 Added Transact SWI interface
*  25-07-96      Added GetUserHomeDir
*/

#define OMIT_UNUSED_FNS

/* Standard includes */

#include <stdio.h>
#include <stdlib.h>
Stewart Brodie's avatar
Stewart Brodie committed
31
#include <stddef.h>
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#include <string.h>
#include <ctype.h>
#include "kernel.h"

/* Our includes */

#include "stdtypes.h"
#include "buflib.h"
#include "NetBIOS.h"
#include "smb.h"
#include "xlate.h"  /* For string functions */
#include "omni.h"
#include "lmvars.h"
#include "rpc.h"

/* Globals ---------------------------- */

char RPC_DebugMsg[100];
int  RPC_ErrorCount=0;

/* Debug routine */

static err_t debug_err ( err_t res, char *text, char *name )
{
  if ( res != OK )
  {
    RPC_ErrorCount++;
    sprintf( RPC_DebugMsg, "%s %s: %s",
            text, name, Xlt_Error(res)->errmess );
  }
  return res;
}

/* Parameter-assembly subroutines -------------------- */

static struct TransactParms TP;

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

static void addword ( int value )
{
  BYTE *p = TP.parms_in + TP.parms_in_len;
  p[0] = (value & 0xFF);
  p[1] = (value >> 8 );
  TP.parms_in_len+=2;
}

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

static void addlong ( int value )
{
  BYTE *p = TP.parms_in + TP.parms_in_len;
  p[0] = (value & 0xFF);
  p[1] = (value >> 8 );
  p[2] = (value >> 16 );
  p[3] = (value >> 24 );
  TP.parms_in_len+=4;
}

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

static void addstring ( char *str )
{
  BYTE *p = TP.parms_in + TP.parms_in_len;
  int l = strlen(str)+1;
  memcpy ( p, str, l );
  TP.parms_in_len += l;
}

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

static void StartParams ( int func_code, char *in_format, char *out_format,
                           int ret_param_len )
{
  TP.parms_in = SMB_WorkBuf;
  TP.parms_in_len = 0;
  TP.data_in = NULL;
  TP.data_in_len = 0;

  TP.parms_out_buf = SMB_WorkBuf;
  TP.parms_out_maxlen = min(ret_param_len, SMBWORKBUF_SIZE);
  TP.data_out_buf  = SMB_WorkBuf + TP.parms_out_maxlen;
  TP.data_out_maxlen = SMBWORKBUF_SIZE-TP.parms_out_maxlen;

  addword ( func_code );
  addstring ( in_format );
  addstring ( out_format );
}

/* Parameter-return subroutines ====================== */

static int getword ( BYTE *p )
{
  return ( p[0] + (p[1] << 8));
}

/* ----------------- */
Stewart Brodie's avatar
Stewart Brodie committed
129
#if 0
130 131 132 133 134
static int getlong ( BYTE *p )
{
  return ( p[0] + (p[1] << 8) + (p[2] << 16)+ (p[3] << 24));
}
#endif
Stewart Brodie's avatar
Stewart Brodie committed
135

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
/* ----------------- */

static BYTE *getpointer ( BYTE *p )
{
  int ptrval;

  ptrval = getword(p) + TP.data_out_len - TP.data_out_maxlen;
  if ( ptrval <= 0 || ptrval >= TP.data_out_len )
    return NULL;

  return TP.data_out_buf + ptrval;
}

/* ============================================ */

static bool check_hidden ( char *name )
{
  while ( *name != 0 )
  {
    if ( name[0] == '$' && name[1] == 0 )
      return false;  /* Name is hidden */
    name++;
  }
  return true;
}

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

static err_t RPC_EnumSharesOnConnection ( char drv, char *server )
{
  BYTE *p;
Stewart Brodie's avatar
Stewart Brodie committed
167
  int i, co;
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  err_t res;

  /* Assemble parameters for RPC call */

  StartParams( 0x0000, "WrLeh", "B13BWz", 8 ); /* NetShareEnum */
  addword (0x0001);  /* Detail level */
  addword (TP.data_out_maxlen); /* Return buf size */

  /* Make call */

  res = SMB_Transact ( drv, "\\PIPE\\LANMAN", &TP );
  if ( res != OK )
    return res;

  if ( TP.parms_out_len < 8 )
    return EDATALEN;

  /* Decode returned params */
  p = TP.parms_out_buf;

  if ( getword(p) != 0 )   /* API return code; 0 = success */
    return ERPCERROR;            /* Otherwise, call it 'generic' error */

Stewart Brodie's avatar
Stewart Brodie committed
191 192
  co = getword(p+2);  /* Comment offset adjustment - ( why?? ) */

193 194 195 196 197 198 199 200 201 202 203 204 205
  i = getword(p+4);        /* Number of records returned */
  if ( i*20 > TP.data_out_len )  /* Silly values! */
    return EDATALEN;

  /* Process returned records */

  p = TP.data_out_buf;

  while ( i-- > 0 )
  {
    /* p is the start of the record. The first 13 bytes
       are a share name + null termination. If the share name
       ends in '$', it is hidden and should not be listed.
Stewart Brodie's avatar
Stewart Brodie committed
206 207 208 209
       [sbrodie: ... except if it's an IPC share, I've decided.  Also
       we store the comments too for *lanman:lmls to display.  Why is there
       a mystical word in the returned param block which is subtracted from
       the offset field? Dunno, but SAMBA does it and Windows 98 needs it.]
210 211
    */
    int shrtype = getword(p+14);
Stewart Brodie's avatar
Stewart Brodie committed
212 213
    int commoffset = getword(p+16);
    char *comment = commoffset ? ((char *) TP.data_out_buf + commoffset - co) : 0;
214

Stewart Brodie's avatar
Stewart Brodie committed
215
    if ( shrtype == SHR_IPC || check_hidden( (char *)p) )
216 217
    {
      if ( shrtype == SHR_DISK )
Stewart Brodie's avatar
Stewart Brodie committed
218
        Omni_AddInfo ( OAI_DISK, server, (char *)p, comment );
219
      else if ( shrtype == SHR_PRINTER )
Stewart Brodie's avatar
Stewart Brodie committed
220 221 222 223 224
        Omni_AddInfo ( OAI_PRINTER, server, (char *)p, comment );
      else if ( shrtype == SHR_IPC )
        Omni_AddInfo ( OAI_IPC, server, (char *)p, comment );
      else if ( shrtype == SHR_COMM )
        Omni_AddInfo ( OAI_DEVICE, server, (char *)p, comment );
225 226 227 228 229 230 231 232 233 234 235 236 237
    }
    p += 20;
  }

  return OK;
}

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

static err_t RPC_EnumServersOnConnection ( char drv, char *domain )
{
  err_t res;
  BYTE *p;
Stewart Brodie's avatar
Stewart Brodie committed
238
  int co;
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
  int i;

  /* NetServerEnum2 */
  StartParams ( 0x0068, "WrLehDz", "B16BBDz", 8 );
  addword (0x0001);  /* Detail level */
  addword (TP.data_out_maxlen); /* Return buf size */
  addlong (0xFFFFFFFF); /* Return all server types */
  addstring ( domain ); /* Domain name */

  /* Make call */

  res = SMB_Transact ( drv, "\\PIPE\\LANMAN", &TP );
  if ( res != OK )
    return res;

  if ( TP.parms_out_len < 8 )
    return EDATALEN;

  /* Decode returned params */
  p = TP.parms_out_buf;

  if ( getword(p) != 0 )   /* API return code; 0 = success */
    return ERPCERROR;

Stewart Brodie's avatar
Stewart Brodie committed
263 264
  co = getword(p+2);

265 266 267 268 269 270 271 272 273 274 275 276 277
  i = getword(p+4);        /* Number of records returned */
  if ( i*26 > TP.data_out_len )  /* Silly values! */
    return EDATALEN;

  /* Process returned records */

  p = TP.data_out_buf;

  while ( i-- > 0 )
  {
    /* p is the start of the record. The first 16 bytes
       are a server name + null termination.
    */
Stewart Brodie's avatar
Stewart Brodie committed
278 279
    int commoffset = getword(p+22);
    char *comment = commoffset ? ((char *) TP.data_out_buf + commoffset - co) : 0;
280

Stewart Brodie's avatar
Stewart Brodie committed
281
    Omni_AddInfo ( OAI_SERVER, (char *)p, comment, NULL );
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    p += 26;
  }

  return OK;
}

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

err_t RPC_EnumerateShares ( char *server  )
{
  char  drv;                /* Connection identifier */
  err_t res;

  /* (i) Connect to IPC share */

  res = SMB_CreateShare ( SHR_IPC, CREATE_NORMAL,
                            server, "IPC$", NULL, NULL, &drv );

  if ( res != OK )
    return debug_err( res, "(EnumShares) could not log on to", server );

Stewart Brodie's avatar
Stewart Brodie committed
303
  Omni_AddInfo ( OAI_SERVER, server, SMB_GetConnInfo(drv, GCI_SERVERINFO), NULL );
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492

  res = RPC_EnumSharesOnConnection ( drv, server );

  SMB_DeleteShare ( drv );
  return debug_err( res, "EnumShares call on", server );
}


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

static char *GetMasterBrowser (char *wg_name)
{
  NETNAME MBname;
  struct FindName_res fnr;
  static char namebuf[16];

  NB_FormatName ( ntMBROWSER, wg_name, &MBname );

  /* Have a 1.5-sec timeout */
  if ( NB_FindNames ( &MBname, ntSERVER, &fnr, 1, 150 ) == 0)
    return NULL;

  NB_DecodeName ( &(fnr.name), namebuf );
  return namebuf;
}

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

char *RPC_GetDomainController (char *domain_name)
{
  NETNAME DCname;
  struct FindName_res fnr;
  static char namebuf[16];

  NB_FormatName ( ntPRIMARYDC, domain_name, &DCname );

  /* Have quick 1.5-sec timeout */
  if ( NB_FindNames ( &DCname, ntSERVER, &fnr, 1, 150 ) == 0)
    return NULL;

  NB_DecodeName ( &(fnr.name), namebuf );
  return namebuf;
}


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


err_t RPC_EnumerateServers ( char *workgroup )
{
  char  drv;                /* Connection identifier */
  err_t res;
  char *server;

  /* (i) Connect to IPC share */

  server = GetMasterBrowser ( workgroup );

  if ( server == NULL )
    server = RPC_GetDomainController ( workgroup );

  if ( server == NULL )
    return debug_err( ECANTFINDNAME,
       "Can't find master browser or domain controller for",
                        workgroup );

  res = SMB_CreateShare ( SHR_IPC, CREATE_NORMAL,
               server, "IPC$", NULL, NULL, &drv );

  if ( res != OK )
    return debug_err( res, "(EnumServers) could not log on to", server );

  res = RPC_EnumServersOnConnection ( drv, workgroup );

  SMB_DeleteShare ( drv );
  return debug_err( res, "EnumServers call on", server );
}

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

err_t RPC_LogonUser ( char *server, char *user, char *password,
                         char **pHomeDir )
{
  err_t res;
  char drv;

  /* Trying to connect to the IPC share is as good a method of
     password validation as any */

  res = SMB_CreateShare ( SHR_IPC, CREATE_NEW_USER,
               server, "IPC$", user, password, &drv );

  if ( res != OK )
    return debug_err(res, "LogonUser: connect to DC failed, user name",
                            user );

  /* NetUserGetInfo */
  StartParams ( 0x0038, "zWrLh",
               "B21BzzzWDDzzDDWWzWzDWb21W", 6 );

  addstring (user);
  addword (11);  /* Detail level */
  addword (TP.data_out_maxlen); /* Return buf size */

  /* Make call */

  res = SMB_Transact ( drv, "\\PIPE\\LANMAN", &TP );

  if ( res == OK )
  {
    if ( TP.parms_out_len < 6 )
      res = EDATALEN;
    else
    {
      switch ( getword ( TP.parms_out_buf ) ) /* return code */
      {
        case 0:
          *pHomeDir = (char *)getpointer( TP.data_out_buf+44 );
          res = OK;
          break;
        case 5: case 65: /* Access denied */
          res = ENOACCESS;
          break;
        case 2221: /* User not found */
          res = EUSERUNKNOWN;
          break;
        case 2239: /* Account disabled */
          res = EACCDISABLED;
          break;
        default:
          res = ERPCERROR;
          break;
      }
    }
  }

  SMB_DeleteShare ( drv );

  return debug_err(res, "NetGetUserInfo() call failed, user name", user );
}


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

err_t RPC_NameOp ( int reason, char *name_in, char *buf_out )
{
  char *s;
  /* We assume buf_out can hold a 16-character name including last 0 */

  switch ( reason )
  {
    case NAMEOP_GETLOCAL:
      s = LM_Vars.machinename;
      break;

    case NAMEOP_GETWG:
      s = LM_Vars.workgroup;
      break;

    case NAMEOP_GETBROWSER:
      if ( name_in == NULL ) name_in = LM_Vars.workgroup;

      s = GetMasterBrowser ( name_in );
      if ( s == NULL )
        return ECANTFINDNAME;
      break;

    case NAMEOP_GETDC:
      if ( name_in == NULL ) name_in = LM_Vars.workgroup;

      s = RPC_GetDomainController ( name_in );
      if ( s == NULL )
        return ECANTFINDNAME;
      break;

    default:
      return EBADPARAM;
  }

  strcpy ( buf_out, s );
  return OK;
}

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

err_t RPC_Transact ( char *servername, char *pipename, void *pvParmBlk )
{
  char drv;
  err_t res;
Stewart Brodie's avatar
Stewart Brodie committed
493
  struct TransactParms t;
494 495 496 497 498 499 500 501 502

  /* Connect to IPC share using default user ID/password */

  res = SMB_CreateShare ( SHR_IPC, CREATE_NORMAL,
                            servername, "IPC$", NULL, NULL, &drv );

  if ( res != OK )
    return res;

Stewart Brodie's avatar
Stewart Brodie committed
503 504 505 506 507 508
  memcpy(&t, pvParmBlk, sizeof_TransactParms_external);
#ifdef LONGNAMES
  t.setup_in_len = 0;
  t.setup_out_maxlen = 0;
#endif
  res = SMB_Transact ( drv, pipename, &t );
509 510 511 512 513 514 515 516 517 518 519 520 521 522
  SMB_DeleteShare ( drv );
  return res;
}

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

bool RPC_Init ( void )
{
  RPC_ErrorCount = 0;
  RPC_DebugMsg[0] = 0;
  return true;
}