⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 systemwindows.cpp

📁 Pegasus is an open-source implementationof the DMTF CIM and WBEM standards. It is designed to be por
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                             //not sure what flags we want here                             DS_DIRECTORY_SERVICE_REQUIRED,                             &DomainControllerInfo);            if (rc == ERROR_SUCCESS && DomainControllerInfo)            {                strcpy(mDomainName, DomainControllerInfo->DomainName);                NetApiBufferFree(DomainControllerInfo);                if (!MultiByteToWideChar(                        CP_ACP, 0, mDomainName, -1, wDomainName,                        strlen(mDomainName) + 1))                {                    return false;                }            }        }        */    }    //get user info    nStatus = NetUserGetInfo(wDomainName,                             wUserName,                             dwLevel,                             (LPBYTE *)&pUserInfo);    if (nStatus == NERR_Success)    {        isSystemUser = true;    }    if (pComputerName != NULL)    {        NetApiBufferFree(pComputerName);    }    if (pUserInfo != NULL)    {        NetApiBufferFree(pUserInfo);    }    return isSystemUser;}Boolean System::isPrivilegedUser(const String& userName){    Boolean isPrivileged = false;    char mUserName[UNLEN+1];    char mDomainName[UNLEN+1];    wchar_t wUserName[UNLEN+1];    wchar_t wDomainName[UNLEN+1];    char* pbs;    char userStr[UNLEN+1];    bool usingDomain = false;    LPBYTE pComputerName=NULL;    DWORD dwLevel = 1;    LPUSER_INFO_1 pUserInfo = NULL;    NET_API_STATUS nStatus = NULL;    //get the username in the correct format    strcpy(userStr, (const char*)userName.getCString());    //separate the domain and user name if both are present.    if (NULL != (pbs = strchr(userStr, '\\')))    {        *pbs = '\0';        strcpy(mDomainName, userStr);        strcpy(mUserName, pbs+1);        usingDomain = true;    }    else if ((NULL != (pbs = (strchr(userStr, '@')))) ||             (NULL != (pbs = (strchr(userStr, '.')))))    {        *pbs = '\0';        strcpy(mDomainName, pbs+1);        strcpy(mUserName, userStr);        usingDomain = true;    }    else    {        strcpy(mDomainName, ".");        strcpy(mUserName, userStr);    }    //convert domain name to unicode    if (!MultiByteToWideChar(            CP_ACP, 0, mDomainName, -1, wDomainName, strlen(mDomainName) + 1))    {        return false;    }    //convert username to unicode    if (!MultiByteToWideChar(            CP_ACP, 0, mUserName, -1, wUserName, strlen(mUserName) + 1))    {        return false;    }    if (usingDomain)    {        //get domain controller        DWORD rc = NetGetDCName(NULL, wDomainName, &pComputerName);        if (rc == NERR_Success)        {            // this is automatically prefixed with "\\"            wcscpy(wDomainName, (LPWSTR) pComputerName);        }        /*        else        {            // failover            // ATTN: This is commented out until there is resolution on            // Bugzilla 2236. -hns 2/2005            // This needs to be more thoroughly tested when we uncomment it out.            PDOMAIN_CONTROLLER_INFO DomainControllerInfo = NULL;            //this function does not take wide strings            rc = DsGetDcName(NULL,                             mDomainName,                             NULL,                             NULL,                             // not sure what flags we want here                             DS_DIRECTORY_SERVICE_REQUIRED,                             &DomainControllerInfo);            if (rc == ERROR_SUCCESS && DomainControllerInfo)            {                strcpy(mDomainName, DomainControllerInfo->DomainName);                NetApiBufferFree(DomainControllerInfo);                if (!MultiByteToWideChar(                        CP_ACP, 0, mDomainName, -1, wDomainName,                        strlen(mDomainName) + 1))                {                    return false;                }            }        }        */    }    //get privileges    nStatus = NetUserGetInfo(wDomainName,                             wUserName,                             dwLevel,                             (LPBYTE *)&pUserInfo);    if ((nStatus == NERR_Success) &&        (pUserInfo != NULL) &&        (pUserInfo->usri1_priv == USER_PRIV_ADMIN))    {        isPrivileged = true;    }    if (pComputerName != NULL)    {        NetApiBufferFree(pComputerName);    }    if (pUserInfo != NULL)    {        NetApiBufferFree(pUserInfo);    }    return isPrivileged;}String System::getPrivilegedUserName(){    // ATTN-NB-03-20000304: Implement better way to get the privileged    // user on the system.    return String("Administrator");}Boolean System::isGroupMember(const char* userName, const char* groupName){    Boolean retVal = false;    LPLOCALGROUP_USERS_INFO_0 pBuf = NULL;    DWORD dwLevel = 0;    DWORD dwFlags = LG_INCLUDE_INDIRECT ;    DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;    DWORD dwEntriesRead = 0;    DWORD dwTotalEntries = 0;    NET_API_STATUS nStatus;    wchar_t wcUserName[UNLEN+1];    wchar_t wcGroupName[UNLEN+1];    //Convert user name to unicode    if (!MultiByteToWideChar(CP_ACP,0,userName, -1, wcUserName,         strlen(userName)+1))    {        return false;    }        //Convert group name to unicode    if (!MultiByteToWideChar(CP_ACP, 0, groupName, -1, wcGroupName,         strlen(groupName)+1))    {        return false;    }    //    // Call the NetUserGetLocalGroups function    // specifying information level 0.    //    // The LG_INCLUDE_INDIRECT flag specifies that the    // function should also return the names of the local    // groups in which the user is indirectly a member.    //    nStatus = NetUserGetLocalGroups(        NULL,           (LPCWSTR)wcUserName,        dwLevel,        dwFlags,        (LPBYTE *) &pBuf,        dwPrefMaxLen,        &dwEntriesRead,        &dwTotalEntries);    //    // If the call succeeds,    //    if (nStatus == NERR_Success)    {        LPLOCALGROUP_USERS_INFO_0 pTmpBuf;        DWORD i;        DWORD dwTotalCount = 0;        if ((pTmpBuf = pBuf) != NULL)        {            //            // Loop through the local groups that the user belongs            // and find the matching group name.            //            for (i = 0; i < dwEntriesRead; i++)            {                //                // Compare the user's group name to groupName.                //                if (wcscmp(pTmpBuf->lgrui0_name, wcGroupName) == 0)                {                    // User is a member of the group.                    retVal = true;                    break;                }                pTmpBuf++;                dwTotalCount++;            }        }    }    //    // Free the allocated memory.    //    if (pBuf != NULL)        NetApiBufferFree(pBuf);    //    // If the given user and group are not found in the local group    // then try on the global groups.    //    if (!retVal)    {        LPGROUP_USERS_INFO_0 pBuf = NULL;        dwLevel = 0;        dwPrefMaxLen = MAX_PREFERRED_LENGTH;        dwEntriesRead = 0;        dwTotalEntries = 0;        //        // Call the NetUserGetGroups function, specifying level 0.        //        nStatus = NetUserGetGroups(            NULL,            (LPCWSTR)wcUserName,            dwLevel,            (LPBYTE*)&pBuf,            dwPrefMaxLen,            &dwEntriesRead,            &dwTotalEntries);                //        // If the call succeeds,        //        if (nStatus == NERR_Success)        {            LPGROUP_USERS_INFO_0 pTmpBuf;            DWORD i;            DWORD dwTotalCount = 0;            if ((pTmpBuf = pBuf) != NULL)            {                //                // Loop through the global groups to which the user belongs                // and find the matching group name.                //                for (i = 0; i < dwEntriesRead; i++)                {                    //                    // Compare the user's group name to groupName.                    //                    if (wcscmp(pTmpBuf->grui0_name, wcGroupName) == 0)                    {                        // User is a member of the group.                        retVal = true;                        break;                    }                    pTmpBuf++;                    dwTotalCount++;                }            }        }        //        // Free the allocated buffer.        //        if (pBuf != NULL)            NetApiBufferFree(pBuf);    }    return retVal;}Boolean System::lookupUserId(    const char* userName,    PEGASUS_UID_T& uid,    PEGASUS_GID_T& gid){    // ATTN: Implement this method to look up the specified user    return false;}Boolean System::changeUserContext(    const PEGASUS_UID_T& uid,    const PEGASUS_GID_T& gid){    // ATTN: Implement this method to change the process user context to the    //       specified user    return false;}Uint32 System::getPID(){    return _getpid();}Boolean System::truncateFile(    const char* path,    size_t newSize){    Boolean rv = false;    int fd = open(path, O_RDWR);    if (fd != -1)    {        if (chsize(fd, newSize) == 0)        {            rv = true;        }        close(fd);    }    return rv;}// Is absolute path?Boolean System::is_absolute_path(const char *path){    char full[_MAX_PATH];    char path_slash[_MAX_PATH];    char *p;    strncpy(path_slash, path, _MAX_PATH);    path_slash[_MAX_PATH-1] = '\0';    for (p = path_slash; p < path_slash + strlen(path_slash); p++)      if (*p == '/')          *p = '\\';    return (strcasecmp(        _fullpath(full, path_slash, _MAX_PATH), path_slash) == 0);}// Changes file permissions on the given file.Boolean System::changeFilePermissions(const char* path, mode_t mode){    // ATTN: File permissions are not currently defined in Windows    return true;}Boolean System::verifyFileOwnership(const char* path){    // ATTN: Implement this to check that the owner of the specified file is    //       the same as the effective user for this process.    return true;}void System::syslog(const String& ident, Uint32 severity, const char* message){    // Not implemented}void System::openlog(const char *ident, int logopt, int facility){    // Not implemented}void System::closelog(){    // Not implemented}// System ID constants for Logger::put and Logger::traceconst String System::CIMSERVER = "cimserver";  // Server system ID// check if a given IP address is defined on the local network interfacesBoolean System::isIpOnNetworkInterface(Uint32 inIP){    SOCKET sock;    int interfaces = 0;    int errcode;    if ( SOCKET_ERROR != ( sock  = WSASocket(AF_INET,                         SOCK_RAW, 0, NULL, 0, 0) ) )    {        unsigned long *bytes_returned=0;        char *output_buf = (char *)calloc(1, 256);        int buf_size = 256;        if ( 0 == (errcode = WSAIoctl(sock,                                      SIO_ADDRESS_LIST_QUERY,                                      NULL,                                      0,                                      output_buf,                                      256,                                      bytes_returned,                                      NULL,                                      NULL)) )        {            SOCKET_ADDRESS_LIST *addr_list;            SOCKET_ADDRESS *addr;            Uint32 ip;            struct sockaddr_in *sin;            addr_list = (SOCKET_ADDRESS_LIST *)output_buf;            addr = addr_list->Address;            sin = (struct sockaddr_in *)addr->lpSockaddr;            for ( ; interfaces < addr_list->iAddressCount; interfaces++)            {                ip = sin->sin_addr.s_addr;                addr++;                sin = (struct sockaddr_in *)addr->lpSockaddr;                if (ip == inIP)                {                    free(output_buf);                    closesocket(sock);                    return true;                }            }        }        else        {            free(output_buf);            return false;        }        free(output_buf);        closesocket(sock);    }    return false;}///////////////////////////////////////////////////////////////////////////////// AutoFileLock class///////////////////////////////////////////////////////////////////////////////AutoFileLock::AutoFileLock(const char* fileName){    // ATTN: Not implemented}AutoFileLock::~AutoFileLock(){    // ATTN: Not implemented}PEGASUS_NAMESPACE_END

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -