netuse.c 20.5 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
/*
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <Python.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/cygwin.h>

#include <windows.h>
29 30
#include <lm.h>
#include <winnetwk.h>
31 32 33 34

#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))

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
#define MAX_USERBUFFER_SIZE 1024

static char userinfo[MAX_USERBUFFER_SIZE] = { 0 };
static char * logonuser = NULL;
static char * logondomain = NULL;
static char * logonserver = NULL;

static size_t
wchar2mchar(wchar_t *ws, char *buffer, size_t size)
{
  size_t len;
  len = WideCharToMultiByte(CP_ACP,
                            0,
                            ws,
                            -1,
                            NULL,
                            0,
                            NULL,
                            NULL
                            );
  if (len + 1 > size)
    return -1;
  if (WideCharToMultiByte(CP_ACP,
                          0,
                          ws,
                          -1,
                          buffer,
                          len,
                          NULL,
                          NULL
                          ) == 0)
    return -1;
  return len + 1;
}

static PyObject *
netuse_user_info(PyObject *self, PyObject *args)
{
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
  DWORD dwLevel = 1;
  LPWKSTA_USER_INFO_1 pBuf = NULL;
  NET_API_STATUS nStatus;
  //
  // Call the NetWkstaUserGetInfo function;
  //  specify level 1.
  //
  nStatus = NetWkstaUserGetInfo(NULL,
                                dwLevel,
                                (LPBYTE *)&pBuf);
  //
  // If the call succeeds, print the information
  //  about the logged-on user.
  //
  if (nStatus == NERR_Success) {
    if (pBuf != NULL) {
      size_t size = MAX_USERBUFFER_SIZE;
      size_t len;
      logonuser = userinfo;
      len = wchar2mchar(pBuf->wkui1_username, logonuser, size);
      if (len == -1) {
Marco Mariani's avatar
Marco Mariani committed
94
        PyErr_SetString(PyExc_RuntimeError, "Unicode conversion error");
95 96 97 98 99 100
        return NULL;
      }
      size -= len;
      logondomain = logonuser + len;
      len = wchar2mchar(pBuf->wkui1_logon_domain, logondomain, size);
      if (len == -1) {
Marco Mariani's avatar
Marco Mariani committed
101
        PyErr_SetString(PyExc_RuntimeError, "Unicode conversion error");
102 103 104 105 106 107
        return NULL;
      }
      size -= len;
      logonserver = logondomain + len;
      len = wchar2mchar(pBuf->wkui1_logon_server, logonserver, size);
      if (len == -1) {
Marco Mariani's avatar
Marco Mariani committed
108
        PyErr_SetString(PyExc_RuntimeError, "Unicode conversion error");
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        return NULL;
      }
    }
  }
  // Otherwise, print the system error.
  //
  else {
    PyErr_Format(PyExc_RuntimeError,
                 "A system error has occurred: %ld",
                 nStatus
                 );
    return NULL;
  }
  //
  // Free the allocated memory.
  //
  if (pBuf != NULL) {
    NetApiBufferFree(pBuf);
    return Py_BuildValue("sss", logonserver, logondomain, logonuser);
  }

  PyErr_SetString(PyExc_RuntimeError, "No logon user information");
  return NULL;
}

Jondy Zhao's avatar
Jondy Zhao committed
134
static int
135
wnet_enumerate_netdrive(LPNETRESOURCE lpnr)
136
{
137 138 139 140 141 142
  DWORD dwResult, dwResultEnum;
  HANDLE hEnum;
  DWORD cbBuffer = 16384;     // 16K is a good size
  DWORD cEntries = -1;        // enumerate all possible entries
  LPNETRESOURCE lpnrLocal;    // pointer to enumerated structures
  DWORD i;
Jondy Zhao's avatar
Jondy Zhao committed
143
  dwResult = WNetOpenEnum(RESOURCE_GLOBALNET,
144
                          RESOURCETYPE_DISK,
Jondy Zhao's avatar
Jondy Zhao committed
145
                          0,
146 147 148 149 150 151
                          lpnr,       // NULL first time the function is called
                          &hEnum);    // handle to the resource

  if (dwResult != NO_ERROR) {
    printf("WnetOpenEnum failed with error %ld\n", dwResult);
    return FALSE;
152
  }
153 154 155 156 157 158 159 160 161 162 163
  lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
  if (lpnrLocal == NULL) {
    printf("WnetOpenEnum failed with error %ld\n", dwResult);
    return FALSE;
  }

  do {
    ZeroMemory(lpnrLocal, cbBuffer);
    dwResultEnum = WNetEnumResource(hEnum,     // resource handle
                                    &cEntries, // defined locally as -1
                                    lpnrLocal, // LPNETRESOURCE
Jondy Zhao's avatar
Jondy Zhao committed
164
                                    &cbBuffer);
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    if (dwResultEnum == NO_ERROR) {
      for (i = 0; i < cEntries; i++) {
        printf("NETRESOURCE[%ld] Usage: 0x%ld = ", i, lpnrLocal[i].dwUsage);
        if (lpnrLocal[i].dwUsage & RESOURCEUSAGE_CONNECTABLE)
          printf("connectable ");
        if (lpnrLocal[i].dwUsage & RESOURCEUSAGE_CONTAINER)
          printf("container ");
        printf("\n");

        printf("NETRESOURCE[%ld] Localname: %s\n", i, lpnrLocal[i].lpLocalName);
        printf("NETRESOURCE[%ld] Remotename: %s\n", i, lpnrLocal[i].lpRemoteName);
        printf("NETRESOURCE[%ld] Comment: %s\n", i, lpnrLocal[i].lpComment);
        printf("NETRESOURCE[%ld] Provider: %s\n", i, lpnrLocal[i].lpProvider);
        printf("\n");
        if (RESOURCEUSAGE_CONTAINER == (lpnrLocal[i].dwUsage
                                        & RESOURCEUSAGE_CONTAINER))
          if (!wnet_enumerate_netdrive(&lpnrLocal[i]))
            printf("EnumerateFunc returned FALSE\n");

      }
    }
    else if (dwResultEnum != ERROR_NO_MORE_ITEMS) {
      printf("WNetEnumResource failed with error %ld\n", dwResultEnum);
      break;
    }
  } while (dwResultEnum != ERROR_NO_MORE_ITEMS);

  GlobalFree((HGLOBAL) lpnrLocal);
  dwResult = WNetCloseEnum(hEnum);

  if (dwResult != NO_ERROR) {
    printf("WNetCloseEnum failed with error %ld\n", dwResult);
    return FALSE;
  }
  return TRUE;
}

Jondy Zhao's avatar
Jondy Zhao committed
202
static int
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
reg_enumerate_netdrive(void)
{
  /* LsaEnumerateLogonSessions -> LogonId */
  /* OpenTokenByLogonId ->  TokenHandle */
  /* LsaGetLogonSessionData -> Sid */
  /* ConvertSidToStringSid */
  /* From regkey HKU\SID\NETWORK */
  /* ImpersonateLoggedOnUser Or CreateProcessWithTokenW */

  /* WNetAddConnection */
  return 0;
}

static PyObject *
netuse_list_drive(PyObject *self, PyObject *args)
{
  PyObject *retvalue = NULL;
  PyObject *pobj = NULL;
  char *servername = NULL;
  char *username = NULL;
  char *password = NULL;
  char chdrive = 'A';
  char drivepath[] = { 'A', ':', '\\', 0 };
  char drivename[] = { 'A', ':', 0 };

  char szRemoteName[MAX_PATH];
  DWORD dwResult;
  DWORD cchBuff = MAX_PATH;
Jondy Zhao's avatar
Jondy Zhao committed
231
  char szUserName[MAX_PATH] = {0};
232 233 234 235 236 237 238 239 240 241 242 243 244 245

  if (! PyArg_ParseTuple(args, "|s", &servername)) {
    return NULL;
  }

  retvalue = PyList_New(0);
  if (retvalue == NULL)
    return NULL;

  while (chdrive <= 'Z') {
    drivepath[0] = chdrive;
    drivename[0] = chdrive;

    dwResult = WNetGetConnection(drivename,
Jondy Zhao's avatar
Jondy Zhao committed
246 247 248
                                 szRemoteName,
                                 &cchBuff
                                 );
249
    if (dwResult == NO_ERROR) {
Jondy Zhao's avatar
Jondy Zhao committed
250 251 252 253 254
      dwResult = WNetGetUser(drivename,
                             (LPSTR) szUserName,
                             &cchBuff);
      if (dwResult != NO_ERROR)
        snprintf(szUserName, MAX_PATH, "%s", "Unknown User");
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
      pobj = Py_BuildValue("ssss",
                           drivename,
                           szRemoteName,
                           "OK",
                           szUserName
                           );
      if (PyList_Append(retvalue, pobj) == -1) {
        Py_XDECREF(retvalue);
        return NULL;
      }
    }
    else if (dwResult == ERROR_CONNECTION_UNAVAIL) {
    }
    else if (dwResult == ERROR_NOT_CONNECTED) {
    }
    else if (dwResult == ERROR_BAD_DEVICE) {
    }
    else if (dwResult == ERROR_NO_NET_OR_BAD_PATH) {
    }
    else {
      PyErr_Format(PyExc_RuntimeError,
                   "A system error has occurred in WNetGetConnection: %ld",
                   GetLastError()
                   );
      Py_XDECREF(retvalue);
      return NULL;
    }
    ++ chdrive;
  }
  return retvalue;
}

static int
connect_net_drive(char *remote, char *drive)
{
  DWORD dwRetVal;
  NETRESOURCE nr;
  DWORD dwFlags;
  char *password=NULL;
  char *user=NULL;

  memset(&nr, 0, sizeof (NETRESOURCE));
  nr.dwType = RESOURCETYPE_DISK;
  nr.lpLocalName = drive;
  nr.lpRemoteName = remote;
  nr.lpProvider = NULL;

  dwFlags = CONNECT_REDIRECT;
  dwRetVal = WNetAddConnection2(&nr, password, user, dwFlags);
Jondy Zhao's avatar
Jondy Zhao committed
304
  if (dwRetVal == NO_ERROR)
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
    return 0;
  PyErr_Format(PyExc_RuntimeError,
               "WNetAddConnection2 failed with error: %lu\n",
               dwRetVal
               );
  return dwRetVal;
}

static PyObject *
netuse_auto_connect(PyObject *self, PyObject *args)
{
  Py_RETURN_NONE;
}

static PyObject *
netuse_remove_drive(PyObject *self, PyObject *args)
{
  DWORD dwRetVal;
  char *drive = NULL;
  int force = 1;

  if (! PyArg_ParseTuple(args, "si", &drive, &force))
    return NULL;

  dwRetVal = WNetCancelConnection2(drive, 0, force);
  if (dwRetVal == NO_ERROR)
    Py_RETURN_NONE;
Jondy Zhao's avatar
Jondy Zhao committed
332

333 334 335 336 337
  PyErr_Format(PyExc_RuntimeError,
               "WNetCancelConnection2 failed with error: %lu\n",
               dwRetVal
               );
  return NULL;
338 339
}

340
static PyObject *
341
netuse_map_drive(PyObject *self, PyObject *args)
342
{
343 344 345 346 347
  DWORD dwRetVal;
  NETRESOURCE nr;
  DWORD dwFlags;

  char *remote = NULL;
348
  char *drive = NULL;
349 350
  char *user = NULL;
  char *password = NULL;
351 352 353
  char accessName[MAX_PATH] = {0};
  DWORD dwBufSize = MAX_PATH;
  DWORD dwResult;
354

355
  if (! PyArg_ParseTuple(args, "ss|ss", &remote, &drive, &user, &password))
356 357 358 359 360 361 362 363 364
    return NULL;

  memset(&nr, 0, sizeof (NETRESOURCE));
  nr.dwType = RESOURCETYPE_DISK;
  nr.lpLocalName = drive;
  nr.lpRemoteName = remote;
  nr.lpProvider = NULL;

  dwFlags = CONNECT_UPDATE_PROFILE;
365 366 367 368 369 370 371 372 373 374
  if (drive == NULL)
    dwFlags |= CONNECT_REDIRECT;
  dwRetVal = WNetUseConnection(NULL,
                               &nr,
                               password,
                               user,
                               dwFlags,
                               accessName,
                               &dwBufSize,
                               &dwResult);
375
  if (dwRetVal == NO_ERROR)
376
    return PyString_FromString(accessName);
377 378 379 380
  PyErr_Format(PyExc_RuntimeError,
               "WNetAddConnection2 failed with error: %lu\n",
               dwRetVal
               );
381 382
  return NULL;
}
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

static PyObject *
netuse_usage_report(PyObject *self, PyObject *args)
{
  char *drive = NULL;
  PyObject *pobj = NULL;

  ULARGE_INTEGER lFreeBytesAvailable;
  ULARGE_INTEGER lTotalNumberOfBytes;
  /* ULARGE_INTEGER lTotalNumberOfFreeBytes; */

  if (! PyArg_ParseTuple(args, "s", &drive))
    return NULL;

  if (GetDiskFreeSpaceEx(drive,
                         &lFreeBytesAvailable,
                         &lTotalNumberOfBytes,
                         NULL
                         )) {
    pobj = Py_BuildValue("LL", lFreeBytesAvailable, lTotalNumberOfBytes);
    return pobj;
  }

  PyErr_Format(PyExc_RuntimeError,
               "A system error has occurred in GetDiskFreeSpaceEx(%s): %ld",
               drive,
               GetLastError()
               );
  return NULL;
}

/* Useless code */
#if 0

static char
get_free_drive_letter(void)
{
  DWORD bitmasks = GetLogicalDrives();
  char ch = 'A';
  while (bitmasks) {
    if ((bitmasks & 1L) == 0)
      return ch;
    ++ ch;
    bitmasks >>= 1;
  }
  return (char)0;
}

431
/*
432 433 434 435 436
 * Travel all the mapped drive to check whether there is duplicated
 * shared folder:
 *
 *   Return 1 if current share folder is same or sub-folder of the
 *   mapped folder;
437
 *
438 439 440 441 442 443
 *   Return -1 if unknown exception occurs;
 *
 *   Remove mapped item from list if the mapped folder is sub-folder
 *   of current share folder.
 *
 * Return 0 if it's new share folder.
444
 *
445
 */
446 447 448 449 450 451 452 453 454 455 456
static int
check_duplicate_shared_folder(PyObject *retvalue, const char *folder)
{
  if (!PyList_Check(retvalue))
    return -1;

  Py_ssize_t size = PyList_Size(retvalue);
  int len = strlen(folder);
  int len2;
  PyObject *item;
  char * s;
457

458
  while (size > 0) {
459 460 461 462 463 464 465 466
    size --;
    item = PySequence_GetItem(retvalue, size);
    if (!PySequence_Check(item))
      return -1;
    s = PyString_AsString(PySequence_GetItem(item, 1));
    if (s == NULL)
      return -1;
    len2 = strlen(s);
467

468 469 470 471 472 473 474 475 476 477 478 479 480
    if (strncmp(folder, s, len > len2 ? len : len2) == 0) {
      if (len2 > len) {
        if (PySequence_DelItem(retvalue, size) == -1)
          return -1;
        size --;
      }
      else
        return 1;
    }
  }
  return 0;
}

481
static PyObject *
482
netuse_usage_report_orig(PyObject *self, PyObject *args)
483 484 485 486 487
{
  char * servername = NULL;
  PyObject *retvalue = NULL;
  DWORD bitmasks;
  char chdrive = '@';
Jondy Zhao's avatar
Jondy Zhao committed
488 489
  char  drivepath[] = { 'A', ':', '\\', 0 };
  char  drivename[] = { 'A', ':', 0 };
490 491
  ULARGE_INTEGER lFreeBytesAvailable;
  ULARGE_INTEGER lTotalNumberOfBytes;
492
  /* ULARGE_INTEGER lTotalNumberOfFreeBytes; */
493 494 495 496 497 498 499 500 501

  char szRemoteName[MAX_PATH];
  DWORD dwResult, cchBuff = MAX_PATH;
  DWORD serverlen = 0;

  if (! PyArg_ParseTuple(args, "|s", &servername)) {
    return NULL;
  }

502
  if (servername)
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
    serverlen = strlen(servername);

  bitmasks = GetLogicalDrives();
  if (bitmasks == 0) {
     PyErr_Format(PyExc_RuntimeError,
                  "A system error has occurred in GetLogicalDrives: %ld",
                  GetLastError()
                  );
     return NULL;
  }

  retvalue = PyList_New(0);
  if (retvalue == NULL)
    return NULL;

  while (bitmasks) {
    ++ chdrive;
    drivepath[0] = chdrive;
    drivename[0] = chdrive;

    if ((bitmasks & 1L) == 0) {
      bitmasks >>= 1;
      continue;
    }

    bitmasks >>= 1;
    switch (GetDriveType(drivepath)) {
      case DRIVE_REMOTE:
        break;
532 533
      case DRIVE_FIXED:
        continue;
534 535 536 537
      default:
        continue;
      }

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
    /* If the network connection was made using the Microsoft LAN
     * Manager network, and the calling application is running in a
     * different logon session than the application that made the
     * connection, a call to the WNetGetConnection function for the
     * associated local device will fail. The function fails with
     * ERROR_NOT_CONNECTED or ERROR_CONNECTION_UNAVAIL. This is
     * because a connection made using Microsoft LAN Manager is
     * visible only to applications running in the same logon session
     * as the application that made the connection. (To prevent the
     * call to WNetGetConnection from failing it is not sufficient for
     * the application to be running in the user account that created
     * the connection.)
     *
     * Refer to http://msdn.microsoft.com/en-us/library/windows/desktop/aa385453(v=vs.85).aspx
     *
     */
554 555 556 557
    dwResult = WNetGetConnection(drivename,
                                 szRemoteName,
                                 &cchBuff
                                 );
558 559
    if (dwResult == NO_ERROR || dwResult == ERROR_CONNECTION_UNAVAIL    \
        || dwResult == ERROR_NOT_CONNECTED) {
Jondy Zhao's avatar
Jondy Zhao committed
560
      if (serverlen) {
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
        if ((cchBuff < serverlen + 3) ||
            (strncmp(servername, szRemoteName+2, serverlen) != 0) ||
            (szRemoteName[serverlen + 2] != '\\')
            )
        continue;
      }
    }

    else {
      PyErr_Format(PyExc_RuntimeError,
                   "A system error has occurred in WNetGetConnection: %ld",
                   GetLastError()
                   );
      Py_XDECREF(retvalue);
      return NULL;
    }
577 578 579 580 581 582 583 584 585 586 587

    switch (check_duplicate_shared_folder(retvalue, szRemoteName)) {
    case -1:
      Py_XDECREF(retvalue);
      return NULL;
    case 1:
      continue;
    default:
      break;
    }

588 589 590
    if (GetDiskFreeSpaceEx(drivepath,
                           &lFreeBytesAvailable,
                           &lTotalNumberOfBytes,
591
                           NULL
592
                           )) {
593
      PyObject *pobj = Py_BuildValue("ssLL",
594 595 596 597 598 599 600 601 602 603
                                     drivename,
                                     szRemoteName,
                                     lFreeBytesAvailable,
                                     lTotalNumberOfBytes
                                     );
      if (PyList_Append(retvalue, pobj) == -1) {
        Py_XDECREF(retvalue);
        return NULL;
      }
    }
604 605 606 607 608 609 610 611 612 613 614 615
    else if (dwResult == ERROR_CONNECTION_UNAVAIL || dwResult == ERROR_NOT_CONNECTED) {
      PyObject *pobj = Py_BuildValue("ssLL",
                                     drivename,
                                     szRemoteName,
                                     0L,
                                     0L
                                     );
      if (PyList_Append(retvalue, pobj) == -1) {
        Py_XDECREF(retvalue);
        return NULL;
      }
    }
616
    else {
617 618 619 620 621 622 623 624 625 626 627
     PyErr_Format(PyExc_RuntimeError,
                  "A system error has occurred in GetDiskFreeSpaceEx(%s): %ld",
                  drivepath,
                  GetLastError()
                  );
     Py_XDECREF(retvalue);
     return NULL;
    }
  }
  return retvalue;
}
628
#endif  /* #if 0 */
629

630 631
static PyMethodDef NetUseMethods[] = {
  {
632 633
    "autoConnect",
    netuse_auto_connect,
634 635
    METH_VARARGS,
    (
636 637 638 639
     "autoConnect()\n\n"
     "Create mapped drive from shared folder, it uses the default user\n"
     "name. (provided by the user context for the process.)\n"
     "Raise exception if something is wrong.\n "
640 641 642
     )
  },
  {
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
    "listNetDrive",
    netuse_list_drive,
    METH_VARARGS,
    (
     "listNetDrive()\n\n"
     "List all the net drives visible by current user. Return a list:\n"
     "  [ (drive, remote, status, user), ... ] \n"
     "Not that if the calling application is running in a different logon\n"
     "session than the application that made the connection, it's\n"
     "unvisible for the current application. If the current user has\n"
     "administrator privilege, these connections could be shown, but\n"
     "the status is unavaliable or unconnect.\n"
     "\n"
     "Refer to http://msdn.microsoft.com/en-us/library/windows/desktop/aa363908(v=vs.85).aspx\n"
     "Defining an MS-DOS Device Name\n"
     "Refer to http://msdn.microsoft.com/en-us/library/windows/hardware/ff554302(v=vs.85).aspx\n"
     "Local and Global MS-DOS Device Names\n"
     )
  },
  {
    "mapNetDrive",
664 665 666
    netuse_map_drive,
    METH_VARARGS,
    (
667 668 669 670 671 672 673 674
     "mapNetDrive(remote, drive, user=None, password=None)\n\n"
     "Create net drive from remote folder, and return the assigned\n"
     "drive letter. \n"
     "It uses the default user which initialize this remote connection\n"
     "if user is None. \n"
     "When drive is an empty string, the system will automatically\n"
     "assigns network drive letters, letters are assigned beginning\n"
     "with Z:, then Y:, and ending with C:\n."
Jondy Zhao's avatar
Jondy Zhao committed
675
     "For examples,\n"
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
     "  mapNetDrive(r'\\\\server\\data')\n"
     "  mapNetDrive(r'\\\\server\\data', 'T:')\n"
     "  mapNetDrive(r'\\\\server\\data', 'T:', r'\\\\server\\jack', 'abc')\n"
     "Raise exception if something is wrong.\n"
     )
  },
  {
    "removeNetDrive",
    netuse_remove_drive,
    METH_VARARGS,
    (
     "removeNetDrive(drive, force=True)\n\n"
     "Remove mapped drive specified by drive, For example,\n"
     "  removeNetDrive('X:')\n"
     "Parameter force specifies whether the disconnection should occur\n"
     "if there are open files or jobs on the connection. If this parameter\n"
     "is FALSE, the function fails if there are open files or jobs.\n"
     "Raise exception if something is wrong, otherwise return None.\n"
694 695 696
     )
  },
  {
697
    "usageReport",
698
    netuse_usage_report,
699 700
    METH_VARARGS,
    (
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
     "usageReport(drive)\n\n"
     "Return a tuple to report the usage of the net drive:\n"
     "  (available, total)\n"
     "For examples,\n"
     "  usageReport('Z:')\n"
     "Raise exception if something is wrong.\n"
     )
  },
  {
    "userInfo",
    netuse_user_info,
    METH_VARARGS,
    (
     "userInfo()\n\n"
     "Get the logon user information, return a tuple:\n"
Jondy Zhao's avatar
Jondy Zhao committed
716
     "  (server, domain, user).\n"
717 718 719 720 721
     )
  },
  {NULL, NULL, 0, NULL}
};

722
PyMODINIT_FUNC initnetuse(void)
723 724 725 726
{
  PyObject* module;
  module = Py_InitModule3("netuse",
                          NetUseMethods,
Marco Mariani's avatar
Marco Mariani committed
727
                          "Show information about net resource in Windows."
728 729 730 731 732
                          );

  if (module == NULL)
    return;
}