📄 systemposix.cpp
字号:
const size_t MAX_PASS_LEN = 1024; static char buf[MAX_PASS_LEN]; struct termios old, new_val; char *ptr; int c; buf[0] = 0; /* Turn echoing off and fail if we can't. */ if (tcgetattr (fileno (stdin), &old) != 0) return buf; new_val = old; new_val.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); if (tcsetattr (fileno (stdin), TCSAFLUSH, &new_val) != 0) return buf; /* Read the password. */ fputs (prompt, stdin); ptr = buf; while ( (c = getc(stdin)) != EOF && c != '\n') { if (ptr < &buf[MAX_PASS_LEN]) *ptr++ = c; } *ptr = 0; putc('\n', stdin); /* Restore terminal. */ (void) tcsetattr (fileno (stdin), TCSAFLUSH, &old); fclose(stdin); return buf;}#endif /* PEGASUS_OS_LSB */String System::getPassword(const char* prompt){#if defined(PEGASUS_OS_VMS) struct { short int numbuf; char frst_char; char rsv1; long rsv2; } tahead; typedef struct { // I/O status block short i_cond; // Condition value short i_xfer; // Transfer count long i_info; // Device information } iosb; typedef struct { // Terminal characteristics char t_class; // Terminal class char t_type; // Terminal type short t_width; // Terminal width in characters long t_mandl; // Terminal's mode and length long t_extend; // Extended terminal characteristics } termb; termb otermb; termb ntermb; static long ichan; // Gets channel number for TT: register int errorcode; int kbdflgs; // saved keyboard fd flags int kbdpoll; // in O_NDELAY mode int kbdqp = false; // there is a char in kbdq int psize; // size of the prompt const size_t MAX_PASS_LEN = 32; static char buf[MAX_PASS_LEN]; char kbdq; // char we've already read iosb iostatus; static long termset[2] = { 0, 0 }; // No terminator $DESCRIPTOR(inpdev, "TT"); // Terminal to use for input // Get a channel for the terminal buf[0] = 0; errorcode = sys$assign(&inpdev, // Device name &ichan, // Channel assigned 0, // request KERNEL mode access 0); // No mailbox assigned if (errorcode != SS$_NORMAL) { return buf; } // Read current terminal settings errorcode = sys$qiow(0, // Wait on event flag zero ichan, // Channel to input terminal IO$_SENSEMODE, // Function - Sense Mode &iostatus, // Status after operation 0, 0, // No AST service &otermb, // [P1] Address of Char Buffer sizeof (otermb), // [P2] Size of Char Buffer 0, 0, 0, 0); // [P3] - [P6] if (errorcode != SS$_NORMAL) { return buf; } // setup new settings ntermb = otermb; // turn on passthru and nobroadcast ntermb.t_extend |= TT2$M_PASTHRU; ntermb.t_mandl |= TT$M_NOBRDCST; // Write out new terminal settings errorcode = sys$qiow(0, // Wait on event flag zero ichan, // Channel to input terminal IO$_SETMODE, // Function - Set Mode &iostatus, // Status after operation 0, 0, // No AST service &ntermb, // [P1] Address of Char Buffer sizeof (ntermb), // [P2] Size of Char Buffer 0, 0, 0, 0); // [P3] - [P6] if (errorcode != SS$_NORMAL) { return buf; } // Write a prompt, read characters from the terminal, performing no // editing // and doing no echo at all. psize = strlen(prompt); errorcode = sys$qiow(0, // Event flag ichan, // Input channel IO$_READPROMPT | IO$M_NOECHO | IO$M_NOFILTR | IO$M_TRMNOECHO, // Read with prompt, no echo, no translate, no // termination character echo &iostatus, // I/O status block NULL, // AST block (none) 0, // AST parameter &buf, // P1 - input buffer MAX_PASS_LEN, // P2 - buffer length 0, // P3 - ignored (timeout) 0, // P4 - ignored (terminator char set) prompt, // P5 - prompt buffer psize); // P6 - prompt size if (errorcode != SS$_NORMAL) { return buf; } // Write out old terminal settings errorcode = sys$qiow(0, // Wait on event flag zero ichan, // Channel to input terminal IO$_SETMODE, // Function - Set Mode &iostatus, // Status after operation 0, 0, // No AST service &otermb, // [P1] Address of Char Buffer sizeof (otermb), // [P2] Size of Char Buffer 0, 0, 0, 0); // [P3] - [P6] if (errorcode != SS$_NORMAL) { return buf; } // Start new line const int CR = 0x0d; const int LF = 0x0a; fputc(CR, stdout); fputc(LF, stdout); // Remove the termination character psize = strlen(buf); buf[psize - 1] = 0; return buf;#elif defined(PEGASUS_OS_OS400) // Not supported on OS/400, and we don't need it. // 'getpass' is DEPRECATED return String();#elif defined(PEGASUS_OS_LSB) return String(getpassword(prompt));#else /* default */ return String(getpass(prompt));#endif /* default */}String System::getEffectiveUserName(){ String userName = String::EMPTY; struct passwd* pwd = NULL;#if defined(PEGASUS_OS_SOLARIS) || \ defined(PEGASUS_OS_HPUX) || \ defined(PEGASUS_OS_LINUX) || \ defined(PEGASUS_OS_OS400) const unsigned int PWD_BUFF_SIZE = 1024; struct passwd local_pwd; char buf[PWD_BUFF_SIZE]; if (getpwuid_r(geteuid(), &local_pwd, buf, PWD_BUFF_SIZE, &pwd) != 0) { String errorMsg = String("getpwuid_r failure : ") + String(strerror(errno)); PEG_TRACE_STRING(TRC_OS_ABSTRACTION, Tracer::LEVEL2, errorMsg); // L10N TODO - This message needs to be added. //Logger::put(Logger::STANDARD_LOG, "CIMServer", Logger::WARNING, // errorMsg); }#elif defined(PEGASUS_OS_ZOS) char effective_username[9]; __getuserid(effective_username, 9); __etoa_l(effective_username,9); userName.assign(effective_username); return userName;#else // // get the currently logged in user's UID. // pwd = getpwuid(geteuid());#endif if (pwd == NULL) { // L10N TODO - This message needs to be added. //Logger::put(Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING, // "getpwuid_r failure, user may have been removed just after login"); Tracer::trace (TRC_OS_ABSTRACTION, Tracer::LEVEL4, "getpwuid_r failure, user may have been removed just after login"); } else {#if defined(PEGASUS_OS_OS400) EtoA(pwd->pw_name);#endif // // get the user name // userName.assign(pwd->pw_name); } return userName;}String System::encryptPassword(const char* password, const char* salt){#if defined(PEGASUS_OS_VMS) const size_t MAX_PASS_LEN = 1024; char pbBuffer[MAX_PASS_LEN] = {0}; int dwByteCount; char pcSalt[3] = {0}; strncpy(pcSalt, salt, 2); dwByteCount = strlen(password); memcpy(pbBuffer, password, dwByteCount); for (int i=0; (i<dwByteCount) || (i>=MAX_PASS_LEN); i++) { (i%2 == 0) ? pbBuffer[i] ^= pcSalt[1] : pbBuffer[i] ^= pcSalt[0]; } return String(pcSalt) + String((char *)pbBuffer);#elif !defined(PEGASUS_OS_OS400) return String(crypt( password,salt));#else // Not supported on OS400, and we don't need it. return String(password);#endif}Boolean System::isSystemUser(const char* userName){#if defined(PEGASUS_OS_SOLARIS) || \ defined(PEGASUS_OS_HPUX) || \ defined(PEGASUS_OS_LINUX) || \ defined(PEGASUS_OS_OS400) const unsigned int PWD_BUFF_SIZE = 1024; struct passwd pwd; struct passwd *result; char pwdBuffer[PWD_BUFF_SIZE]; if (getpwnam_r(userName, &pwd, pwdBuffer, PWD_BUFF_SIZE, &result) != 0) { String errorMsg = String("getpwnam_r failure : ") + String(strerror(errno)); PEG_TRACE_STRING(TRC_OS_ABSTRACTION, Tracer::LEVEL2, errorMsg); // L10N TODO - This message needs to be added. //Logger::put(Logger::STANDARD_LOG, "CIMServer", Logger::WARNING, // errorMsg); } if (result == NULL) return false; return true;#elif defined(PEGASUS_OS_OS400) AtoE((char*)userName); if (getpwnam(userName) == NULL) { EtoA((char*)userName); return false; } EtoA((char*)userName); return true;#else /* default */ return getpwnam(userName) != NULL;#endif /* default */}Boolean System::isPrivilegedUser(const String& userName){#if !defined(PEGASUS_OS_OS400) struct passwd pwd; struct passwd *result; const unsigned int PWD_BUFF_SIZE = 1024; char pwdBuffer[PWD_BUFF_SIZE]; if (getpwnam_r( userName.getCString(), &pwd, pwdBuffer, PWD_BUFF_SIZE, &result) != 0) { String errorMsg = String("getpwnam_r failure : ") + String(strerror(errno)); PEG_TRACE_STRING(TRC_OS_ABSTRACTION, Tracer::LEVEL2, errorMsg); // L10N TODO - This message needs to be added. //Logger::put(Logger::STANDARD_LOG, "CIMServer", Logger::WARNING, // errorMsg); } // Check if the requested entry was found. If not return false. if ( result != NULL ) { // Check if the uid is 0. if ( pwd.pw_uid == 0 ) { return true; } } return false;#elif defined(PEGASUS_OS_VMS) int retStat; unsigned long int prvPrv = 0; retStat = sys$setprv(0, 0, 0, &prvPrv); if (!$VMS_STATUS_SUCCESS(retStat)) return false; // ATTN-VMS: should this be a bitwise and? return ((PRV$M_SETPRV && prvPrv) == 1);#else /* default */ CString user = userName.getCString(); const char * tmp = (const char *)user; AtoE((char *)tmp); return ycmCheckUserCmdAuthorities(tmp);#endif /* default */}static String _priviledgedUserName;static Once _priviledgedUserNameOnce = PEGASUS_ONCE_INITIALIZER;static void _initPrivilegedUserName(){ struct passwd* pwd = NULL;#if defined(PEGASUS_OS_SOLARIS) || \ defined(PEGASUS_OS_HPUX) || \ defined(PEGASUS_OS_LINUX) || \ defined(PEGASUS_OS_OS400) const unsigned int PWD_BUFF_SIZE = 1024; struct passwd local_pwd; char buf[PWD_BUFF_SIZE]; if (getpwuid_r(0, &local_pwd, buf, PWD_BUFF_SIZE, &pwd) != 0) { String errorMsg = String("getpwuid_r failure : ") + String(strerror(errno)); PEG_TRACE_STRING(TRC_OS_ABSTRACTION, Tracer::LEVEL2, errorMsg); // L10N TODO - This message needs to be added. // Logger::put(Logger::STANDARD_LOG, "CIMServer", Logger::WARNING, // errorMsg); }#else /* default */ pwd = getpwuid(0);#endif /* default */ if ( pwd != NULL ) {#if defined(PEGASUS_OS_OS400) EtoA(pwd->pw_name);#endif _priviledgedUserName.assign(pwd->pw_name); } else { Tracer::trace ( TRC_OS_ABSTRACTION, Tracer::LEVEL4, "Could not find entry."); PEGASUS_ASSERT(0); }}String System::getPrivilegedUserName(){ once(&_priviledgedUserNameOnce, _initPrivilegedUserName); return _priviledgedUserName;}#if !defined(PEGASUS_OS_VMS) || defined(PEGASUS_ENABLE_USERGROUP_AUTHORIZATION)Boolean System::isGroupMember(const char* userName, const char* groupName){ struct group grp; char* member; Boolean retVal = false; const unsigned int PWD_BUFF_SIZE = 1024; const unsigned int GRP_BUFF_SIZE = 1024; struct passwd pwd; struct passwd* result; struct group* grpresult; char pwdBuffer[PWD_BUFF_SIZE]; char grpBuffer[GRP_BUFF_SIZE]; // Search Primary group information. // Find the entry that matches "userName" if (getpwnam_r(userName, &pwd, pwdBuffer, PWD_BUFF_SIZE, &result) != 0) { String errorMsg = String("getpwnam_r failure : ") + String(strerror(errno)); PEG_TRACE_STRING(TRC_OS_ABSTRACTION, Tracer::LEVEL2, errorMsg); Logger::put(Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING, errorMsg); throw InternalSystemError(); } if ( result != NULL ) { // User found, check for group information. gid_t group_id; group_id = pwd.pw_gid; // Get the group name using group_id and compare with group passed. if ( getgrgid_r(group_id, &grp, grpBuffer, GRP_BUFF_SIZE, &grpresult) != 0) { String errorMsg = String("getgrgid_r failure : ") + String(strerror(errno)); PEG_TRACE_STRING(TRC_OS_ABSTRACTION, Tracer::LEVEL2, errorMsg); Logger::put( Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING, errorMsg); throw InternalSystemError(); } // Compare the user's group name to groupName. if (strcmp(grp.gr_name, groupName) == 0) { // User is a member of the group. return true; } } //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -