📄 plugin_common.c
字号:
PARAMERROR(utils); return SASL_BADPARAM; } } return ret;}/* * Retrieve the client realm. */int _plug_get_realm(const sasl_utils_t *utils, const char **availrealms, const char **realm, sasl_interact_t **prompt_need){ int ret = SASL_FAIL; sasl_getrealm_t *realm_cb; void *realm_context; sasl_interact_t *prompt; *realm = NULL; /* see if we were given the result in the prompt */ prompt = _plug_find_prompt(prompt_need, SASL_CB_GETREALM); if (prompt != NULL) { /* We prompted, and got.*/ if (!prompt->result) { SETERROR(utils, "Unexpectedly missing a prompt result"); return SASL_BADPARAM; } *realm = prompt->result; return SASL_OK; } /* Try to get the callback... */ ret = utils->getcallback(utils->conn, SASL_CB_GETREALM, &realm_cb, &realm_context); if (ret == SASL_OK && realm_cb) { ret = realm_cb(realm_context, SASL_CB_GETREALM, availrealms, realm); if (ret != SASL_OK) return ret; if (!*realm) { PARAMERROR(utils); return SASL_BADPARAM; } } return ret;}/* * Make the requested prompts. (prompt==NULL means we don't want it) */int _plug_make_prompts(const sasl_utils_t *utils, sasl_interact_t **prompts_res, const char *user_prompt, const char *user_def, const char *auth_prompt, const char *auth_def, const char *pass_prompt, const char *pass_def, const char *echo_chal, const char *echo_prompt, const char *echo_def, const char *realm_chal, const char *realm_prompt, const char *realm_def){ int num = 1; int alloc_size; sasl_interact_t *prompts; if (user_prompt) num++; if (auth_prompt) num++; if (pass_prompt) num++; if (echo_prompt) num++; if (realm_prompt) num++; if (num == 1) { SETERROR( utils, "make_prompts() called with no actual prompts" ); return SASL_FAIL; } alloc_size = sizeof(sasl_interact_t)*num; prompts = utils->malloc(alloc_size); if (!prompts) { MEMERROR( utils ); return SASL_NOMEM; } memset(prompts, 0, alloc_size); *prompts_res = prompts; if (user_prompt) { (prompts)->id = SASL_CB_USER; (prompts)->challenge = "Authorization Name"; (prompts)->prompt = user_prompt; (prompts)->defresult = user_def; prompts++; } if (auth_prompt) { (prompts)->id = SASL_CB_AUTHNAME; (prompts)->challenge = "Authentication Name"; (prompts)->prompt = auth_prompt; (prompts)->defresult = auth_def; prompts++; } if (pass_prompt) { (prompts)->id = SASL_CB_PASS; (prompts)->challenge = "Password"; (prompts)->prompt = pass_prompt; (prompts)->defresult = pass_def; prompts++; } if (echo_prompt) { (prompts)->id = SASL_CB_ECHOPROMPT; (prompts)->challenge = echo_chal; (prompts)->prompt = echo_prompt; (prompts)->defresult = echo_def; prompts++; } if (realm_prompt) { (prompts)->id = SASL_CB_GETREALM; (prompts)->challenge = realm_chal; (prompts)->prompt = realm_prompt; (prompts)->defresult = realm_def; prompts++; } /* add the ending one */ (prompts)->id = SASL_CB_LIST_END; (prompts)->challenge = NULL; (prompts)->prompt = NULL; (prompts)->defresult = NULL; return SASL_OK;}void _plug_decode_init(decode_context_t *text, const sasl_utils_t *utils, unsigned int in_maxbuf){ memset(text, 0, sizeof(decode_context_t)); text->utils = utils; text->needsize = 4; text->in_maxbuf = in_maxbuf;}/* * Decode as much of the input as possible (possibly none), * using decode_pkt() to decode individual packets. */int _plug_decode(decode_context_t *text, const char *input, unsigned inputlen, char **output, /* output buffer */ unsigned *outputsize, /* current size of output buffer */ unsigned *outputlen, /* length of data in output buffer */ int (*decode_pkt)(void *rock, const char *input, unsigned inputlen, char **output, unsigned *outputlen), void *rock){ unsigned int tocopy; unsigned diff; char *tmp; unsigned tmplen; int ret; *outputlen = 0; while (inputlen) { /* more input */ if (text->needsize) { /* need to get the rest of the 4-byte size */ /* copy as many bytes (up to 4) as we have into size buffer */ tocopy = (inputlen > text->needsize) ? text->needsize : inputlen; memcpy(text->sizebuf + 4 - text->needsize, input, tocopy); text->needsize -= tocopy; input += tocopy; inputlen -= tocopy; if (!text->needsize) { /* we have the entire 4-byte size */ memcpy(&(text->size), text->sizebuf, 4); text->size = ntohl(text->size); if (!text->size) /* should never happen */ return SASL_FAIL; if (text->size > text->in_maxbuf) { text->utils->log(NULL, SASL_LOG_ERR, "encoded packet size too big (%d > %d)", text->size, text->in_maxbuf); return SASL_FAIL; } if (!text->buffer) text->buffer = text->utils->malloc(text->in_maxbuf); if (text->buffer == NULL) return SASL_NOMEM; text->cursize = 0; } else { /* We do NOT have the entire 4-byte size... * wait for more data */ return SASL_OK; } } diff = text->size - text->cursize; /* bytes needed for full packet */ if (inputlen < diff) { /* not a complete packet, need more input */ memcpy(text->buffer + text->cursize, input, inputlen); text->cursize += inputlen; return SASL_OK; } /* copy the rest of the packet */ memcpy(text->buffer + text->cursize, input, diff); input += diff; inputlen -= diff; /* decode the packet (no need to free tmp) */ ret = decode_pkt(rock, text->buffer, text->size, &tmp, &tmplen); if (ret != SASL_OK) return ret; /* append the decoded packet to the output */ ret = _plug_buf_alloc(text->utils, output, outputsize, *outputlen + tmplen + 1); /* +1 for NUL */ if (ret != SASL_OK) return ret; memcpy(*output + *outputlen, tmp, tmplen); *outputlen += tmplen; /* protect stupid clients */ *(*output + *outputlen) = '\0'; /* reset for the next packet */ text->needsize = 4; } return SASL_OK; }void _plug_decode_free(decode_context_t *text){ if (text->buffer) text->utils->free(text->buffer);}/* returns the realm we should pretend to be in */int _plug_parseuser(const sasl_utils_t *utils, char **user, char **realm, const char *user_realm, const char *serverFQDN, const char *input){ int ret; char *r; if(!user || !serverFQDN) { PARAMERROR( utils ); return SASL_BADPARAM; } r = strchr(input, '@'); if (!r) { /* hmmm, the user didn't specify a realm */ if(user_realm && user_realm[0]) { ret = _plug_strdup(utils, user_realm, realm, NULL); } else { /* Default to serverFQDN */ ret = _plug_strdup(utils, serverFQDN, realm, NULL); } if (ret == SASL_OK) { ret = _plug_strdup(utils, input, user, NULL); } } else { r++; ret = _plug_strdup(utils, r, realm, NULL); *--r = '\0'; *user = utils->malloc(r - input + 1); if (*user) { strncpy(*user, input, r - input +1); } else { MEMERROR( utils ); ret = SASL_NOMEM; } *r = '@'; } return ret;}int _plug_make_fulluser(const sasl_utils_t *utils, char **fulluser, const char * useronly, const char *realm){ if(!fulluser || !useronly || !realm) { PARAMERROR( utils ); return (SASL_BADPARAM); } *fulluser = utils->malloc (strlen(useronly) + strlen(realm) + 2); if (*fulluser == NULL) { MEMERROR( utils ); return (SASL_NOMEM); } strcpy (*fulluser, useronly); strcat (*fulluser, "@"); strcat (*fulluser, realm); return (SASL_OK);}char * _plug_get_error_message (const sasl_utils_t *utils,#ifdef WIN32 DWORD error#else int error#endif ){ char * return_value;#ifdef WIN32 LPVOID lpMsgBuf; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */ (LPTSTR) &lpMsgBuf, 0, NULL ); if (_plug_strdup (utils, lpMsgBuf, &return_value, NULL) != SASL_OK) { return_value = NULL; } LocalFree( lpMsgBuf );#else /* !WIN32 */ if (_plug_strdup (utils, strerror(error), &return_value, NULL) != SASL_OK) { return_value = NULL; }#endif /* WIN32 */ return (return_value);}void _plug_snprintf_os_info (char * osbuf, int osbuf_len){#ifdef WIN32 OSVERSIONINFOEX versioninfo; char *sysname;/* : DWORD dwOSVersionInfoSize; DWORD dwMajorVersion; DWORD dwMinorVersion; DWORD dwBuildNumber; TCHAR szCSDVersion[ 128 ];//Only NT SP 6 and later WORD wServicePackMajor; WORD wServicePackMinor; WORD wSuiteMask; BYTE wProductType; */ versioninfo.dwOSVersionInfoSize = sizeof (versioninfo); sysname = "Unknown Windows"; if (GetVersionEx ((OSVERSIONINFO *) &versioninfo) == FALSE) { snprintf(osbuf, osbuf_len, "%s", sysname); goto SKIP_OS_INFO; } switch (versioninfo.dwPlatformId) { case VER_PLATFORM_WIN32s: /* Win32s on Windows 3.1 */ sysname = "Win32s on Windows 3.1";/* I can't test if dwBuildNumber has any meaning on Win32s */ break; case VER_PLATFORM_WIN32_WINDOWS: /* 95/98/ME */ switch (versioninfo.dwMinorVersion) { case 0: sysname = "Windows 95"; break; case 10: sysname = "Windows 98"; break; case 90: sysname = "Windows Me"; break; default: sysname = "Unknown Windows 9X/ME series"; break; }/* Clear the high order word, as it contains major/minor version */ versioninfo.dwBuildNumber &= 0xFFFF; break; case VER_PLATFORM_WIN32_NT: /* NT/2000/XP/.NET */ if (versioninfo.dwMinorVersion > 99) { } else { switch (versioninfo.dwMajorVersion * 100 + versioninfo.dwMinorVersion) { case 351: sysname = "Windows NT 3.51"; break; case 400: sysname = "Windows NT 4.0"; break; case 500: sysname = "Windows 2000"; break; case 501: sysname = "Windows XP/.NET"; /* or Windows .NET Server */ break; default: sysname = "Unknown Windows NT series"; break; } } break; default: break; } snprintf(osbuf, osbuf_len, "%s %s (Build %u)", sysname, versioninfo.szCSDVersion, versioninfo.dwBuildNumber );SKIP_OS_INFO: ;#else /* !WIN32 */ struct utsname os; uname(&os); snprintf(osbuf, osbuf_len, "%s %s", os.sysname, os.release);#endif /* WIN32 */}#if defined(WIN32)unsigned int plug_sleep (unsigned int seconds){ long dwSec = seconds*1000; Sleep (dwSec); return 0;}#endif#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -