// @pymethod string|win32wnet|WNetGetUser|Retrieves the current default user name, or the user name used to establish a network connection. static PyObject * PyWNetGetUser(PyObject *self, PyObject *args) { PyObject *ret = NULL; PyObject *obConnection = Py_None; DWORD length = 0; DWORD errcode; TCHAR *szConnection = NULL; TCHAR *buf = NULL; // @pyparm string|connection|None|A string that specifies either the name of a local device that has been redirected to a network resource, or the remote name of a network resource to which a connection has been made without redirecting a local device. // If this parameter is None, the system returns the name of the current user for the process. if (!PyArg_ParseTuple(args, "|O", &obConnection)) return NULL; if (!PyWinObject_AsTCHAR(obConnection, &szConnection, TRUE)) goto done; // get the buffer size { Py_BEGIN_ALLOW_THREADS errcode=WNetGetUser(szConnection, NULL, &length); Py_END_ALLOW_THREADS } if (length==0) { ReturnNetError("WNetGetUser", errcode); goto done; } buf = (TCHAR *)malloc( sizeof( TCHAR) * length); if (buf == NULL){ PyErr_Format(PyExc_MemoryError, "Unable to allocate %d bytes", sizeof(TCHAR)*length); goto done; } Py_BEGIN_ALLOW_THREADS errcode = WNetGetUser(szConnection, buf, &length); Py_END_ALLOW_THREADS if (0 != errcode) { ReturnNetError("WNetGetUser", errcode); goto done; } // length includes the NULL - drop it (safely!) ret = PyWinObject_FromTCHAR(buf, (length > 0) ? length-1 : 0); done: PyWinObject_FreeTCHAR(szConnection); if (buf) free(buf); return ret; }
// The network user name. // If the user name is unknown, TRUE is returned; otherwise, FALSE is returned. BOOL CRemindApp::GetNetworkUserName(LPSTR lpszNetworkUserName) { *lpszNetworkUserName = 0; // Attempt to get the user name from the Windows network driver. // Call WNetGetUser() to get the user name. TRACE0("Call WNetGetUser\n"); int nResult; unsigned long ulBufferSize = 48; if ((nResult = WNetGetUser(NULL, lpszNetworkUserName, &ulBufferSize)) != WN_SUCCESS) { TRACE1("WNetGetUser Failed (%d)\n", nResult); // The call failed, clear the string. *lpszNetworkUserName = 0; } TRACE1("WNetGetUser returned |%s|\n", lpszNetworkUserName); // If we still do not have a name, then use the name // specified by the environment variable MLSUSER. if (*lpszNetworkUserName == 0) { LPSTR pszEnvironmentUserName; if ((pszEnvironmentUserName = getenv("MLSUSER")) != NULL) { strcpy(lpszNetworkUserName, pszEnvironmentUserName); } } if (*lpszNetworkUserName != 0) { return FALSE; } ::LoadString(AfxGetResourceHandle(), IDS_UNKNOWN, lpszNetworkUserName, 48); return TRUE; }
BOOL DoMapShareChange(BOOL removeUnknown) { DRIVEMAPLIST List; TCHAR szMachine[ MAX_PATH],szPath[MAX_PATH]; DWORD rc=28; HANDLE hEnum; LPNETRESOURCE lpnrLocal,lpnr=NULL; DWORD res; DWORD cEntries=-1; DWORD cbBuffer=16384; memset(szMachine, '\0', sizeof(szMachine)); GetClientNetbiosName(szMachine); // Initialize the data structure if (!IsServiceActive()) return TRUE; memset (&List, 0x00, sizeof(DRIVEMAPLIST)); for (size_t ii = 0; ii < 26; ++ii) List.aDriveMap[ii].chDrive = chDRIVE_A + ii; QueryDriveMapList_ReadSubmounts (&List); if ((res=WNetOpenEnum(RESOURCE_CONNECTED,RESOURCETYPE_DISK,RESOURCEUSAGE_CONNECTABLE,lpnr,&hEnum))!=NO_ERROR) return FALSE; lpnrLocal=(LPNETRESOURCE) GlobalAlloc(GPTR,cbBuffer); sprintf(szPath,"\\\\%s\\",szMachine); _strlwr(szPath); do { /* Reset lpnrLocal and cEntries before each call */ memset(lpnrLocal,0,cbBuffer); cEntries = -1; if ((res = WNetEnumResource(hEnum,&cEntries,lpnrLocal,&cbBuffer))==NO_ERROR) { for (DWORD i=0;i<cEntries;i++) { if (strstr(_strlwr(lpnrLocal[i].lpRemoteName),szPath)==NULL) continue; //only look at real afs mappings CHAR * pSubmount=strrchr(lpnrLocal[i].lpRemoteName,'\\')+1; if (lstrcmpi(pSubmount,"all")==0) continue; // do not remove 'all' for (DWORD j=0;j<List.cSubmounts;j++) { if ((List.aSubmounts[j].szSubmount[0]) && (lstrcmpi(List.aSubmounts[j].szSubmount,pSubmount)==0) ) { List.aSubmounts[j].fInUse=TRUE; goto nextname; } } // wasn't on list so lets remove DisMountDOSDrive(pSubmount); nextname:; } } } while (res == NO_ERROR); GlobalFree((HGLOBAL)lpnrLocal); WNetCloseEnum(hEnum); sprintf(szPath,"\\\\%s\\all",szMachine); // Lets connect all submounts that weren't connectd DWORD cbUser=MAXRANDOMNAMELEN-1; CHAR szUser[MAXRANDOMNAMELEN]; CHAR * pUser = NULL; if (WNetGetUser(szPath,(LPSTR)szUser,&cbUser)==NO_ERROR) { if ((pUser=strchr(szUser,'\\'))!=NULL) pUser++; } for (DWORD j=0;j<List.cSubmounts;j++) { if (List.aSubmounts[j].fInUse) continue; DWORD res=MountDOSDrive(0,List.aSubmounts[j].szSubmount,FALSE,pUser); } return TRUE; }
BOOL DoMapShare() { DRIVEMAPLIST List; DWORD rc=28; BOOL bMappedAll=FALSE; // Initialize the data structure DEBUG_EVENT0("AFS DoMapShare"); QueryDriveMapList (&List); DoUnMapShare(TRUE); // All connections have been removed // Lets restore them after making the connection from the random name TCHAR szMachine[ MAX_PATH],szPath[MAX_PATH]; memset(szMachine, '\0', sizeof(szMachine)); GetClientNetbiosName(szMachine); sprintf(szPath,"\\\\%s\\all",szMachine); // Lets connect all submounts that weren't connectd DWORD cbUser=MAXRANDOMNAMELEN-1; CHAR szUser[MAXRANDOMNAMELEN]; CHAR * pUser = NULL; if (WNetGetUser(szPath,(LPSTR)szUser,&cbUser)==NO_ERROR) { if ((pUser=strchr(szUser,'\\'))!=NULL) pUser++; } for (DWORD i=0;i<List.cSubmounts;i++) { if (List.aSubmounts[i].szSubmount[0]) { DWORD res=MountDOSDrive(0,List.aSubmounts[i].szSubmount,FALSE,pUser); if (lstrcmpi("all",List.aSubmounts[i].szSubmount)==0) bMappedAll=TRUE; } } if (!bMappedAll) //make sure all is mapped also { DWORD res=MountDOSDrive(0,"all",FALSE,pUser); if (res==ERROR_SESSION_CREDENTIAL_CONFLICT) { DisMountDOSDrive("all"); MountDOSDrive(0,"all",FALSE,pUser); } } for (TCHAR chDrive = chDRIVE_A; chDrive <= chDRIVE_Z; ++chDrive) { if (List.aDriveMap[chDrive-chDRIVE_A].fActive || ForceMapActive(chDrive)) { TCHAR szSubmount[ MAX_PATH ]; if (List.aDriveMap[chDrive-chDRIVE_A].szSubmount[0]) lstrcpy(szSubmount,List.aDriveMap[chDrive-chDRIVE_A].szSubmount); else if (!PathToSubmount (szSubmount, List.aDriveMap[chDrive-chDRIVE_A].szMapping, NULL, NULL)) continue; BOOL fPersistent = List.aDriveMap[chDrive-chDRIVE_A].fPersistent; DWORD res=MountDOSDrive(chDrive ,szSubmount ,fPersistent,pUser); } } return TRUE; }
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; char szUserName[MAX_PATH] = {0}; 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, szRemoteName, &cchBuff ); if (dwResult == NO_ERROR) { dwResult = WNetGetUser(drivename, (LPSTR) szUserName, &cchBuff); if (dwResult != NO_ERROR) snprintf(szUserName, MAX_PATH, "%s", "Unknown User"); 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; }