Beispiel #1
0
/*
 *Register
 */
int register_user(packet *in_pkt, int fd) {
   int i = 0;
   char *args[16];
   char cpy[BUFFERSIZE];
   char *tmp = cpy;
   strcpy(tmp, in_pkt->buf);

   args[i] = strsep(&tmp, " \t");
   while ((i < sizeof(args) - 1) && (args[i] != '\0')) {
       args[++i] = strsep(&tmp, " \t");
   }
   // Check there are enough arguements to safely inspect them
   if (i > 3) {
      // Ensure requested username is valid
      if (!validUsername(args[1], fd)) { return 0; }
      // Check if the requested username is unique
      if(strcmp(get_real_name(&registered_users_list, args[1], registered_users_mutex), "ERROR") !=0 || \
                              !(strcmp(SERVER_NAME, args[1])) || \
                              strcmp(args[2], args[3]) != 0) {
         sendError("Username unavailable.", fd);
         return 0;
      }
      // Ensure password requested is valid
      if (!validPassword(args[2], args[3], fd)) { return 0; }

      // Allocate memory space for new user node, populate node with new user data
      User *user = (User *)malloc(sizeof(User));
      strcpy(user->username, args[1]);
      strcpy(user->real_name, args[1]);
      // Hash password
      SHA256_CTX sha256;
      SHA256_Init(&sha256);
      SHA256_Update(&sha256, args[2], strlen(args[2]));
      SHA256_Final(user->password, &sha256);
      user->sock = fd;
      user->next = NULL;
      
      // Insert user as registered user, write new user data to file
      insertUser(&registered_users_list, user, registered_users_mutex);
      writeUserFile(&registered_users_list, USERS_FILE, registered_users_mutex);

      // Reform packet as valid login, pass new user data to login
      memset(&in_pkt->buf, 0, sizeof(in_pkt->buf));
      sprintf(in_pkt->buf, "/login %s %s", args[1], args[2]);
      return login(in_pkt, fd);
   }
   // There were not enough arguements received to correctly read them
   else {
      printf("%s --- %sError:%s Malformed reg packet received from %s on %d, ignoring.\n", \
             WHITE, RED, NORMAL, args[1], fd);
   }
   return 0;
}
Beispiel #2
0
/*
 * 若是"Enter Chat Room"按钮被按下:
 * {
 *   若昵称不合法,显示警告信息并清空昵称输入框
 *   反之:
 *   {
 *     记录临时昵称
 *     隐藏此时显示的所有部件,然后显示聊天时该有的所有部件
 *     在聊天内容文本快上添加"我加入了"的消息记录
 *     将自己的昵称和ip加入到昵称表和ip表中
 *     广播"我来了"的消息
 *   }
 * }
 * 若是"Submit"按钮被按下 且 输入的聊天内容不为空:
 * {
 *   在聊天文本快上添加"自己发送了xxx信息"
 *   清空聊天输入框
 *   广播"我发送了xxx"的消息
 * }
*/
bool ChatWindow::eventFilter(QObject *obj, QEvent *event)
{
    if(obj==_enterChat && event->type()==QEvent::MouseButtonPress && _inputUser->text()!="")
    {//若是按下 进入聊天室 按钮
        if(validUsername(_inputUser->text()))  //若昵称合法
        {
            _username=_inputUser->text();      //记录临时昵称
            //隐藏 未进入聊天室时候的 所有ui部件
            _zb->hide();
            _userLabel->hide();
            _inputUser->hide();
            _enterChat->hide();
            //显示 进入聊天室后的 所有ui部件
            _label1->setVisible(true);
            _label2->setVisible(true);
            _room->setVisible(true);
            _allContents->setVisible(true);
            _inputContent->setVisible(true);
            _submit->setVisible(true);
            //在聊天内容文本块中添加"我来了"的消息
            updateAllContents("",QString("!report: ").append(_username).append(" join"));
            //将自己的昵称和ip加入到昵称表和ip表中
            addOne(_username,getIPv4());
            //广播"我来了"的消息
            QByteArray bytes=pack_Come(_username,getIPv4());
            _sender->writeDatagram(bytes,bytes.length(),QHostAddress::Broadcast,_port);
        }else{
            //给出提示
            _userLabel->setText("only allow a-z,A-Z,0-9!");
            _userLabel->setStyleSheet("color:#880000");
            //清理输入框内的内容
            _inputUser->clear();
        }
        return true;
    }
    if(obj==_submit && event->type()==QEvent::MouseButtonPress && _inputContent->toPlainText()!="")
    {//若是按下 提交聊天输入 按钮
        //在聊天文本快上添加"自己发送了xxx信息"
        QString str=_inputContent->toPlainText();
        updateAllContents("[self]",str);
        //清空聊天输入框
        _inputContent->clear();
        //广播"我发送了xxx"的消息
        QByteArray bytes=pack_Talk(_username,str);
        _sender->writeDatagram(bytes,bytes.length(),QHostAddress::Broadcast,_port);
        return true;
    }
    return QWidget::eventFilter(obj,event);
}
Beispiel #3
0
void LoginComponent::sendLoginRequest()
{    
    QString username = ui->usernameEdit->text();
    QString password = ui->passwordEdit->text();

    if (!validUsername(username)) {
        if (username == "")
            initialize("Error: empty username given.");
        else
            initialize("Error: illegal username, " + username + ".");
    }
    else if (!validPassword(password))
        initialize("Error: illegal password.");
    else {
        m_manager->requestLogin(username, password);
    }
}
int
main(int argc, char **argv)
{
    char buf[1024];
    char *user, *passwd;
    char *ldapServer = NULL;
    LDAP *ld = NULL;
    int tryagain;
    int port = LDAP_PORT;

    setbuf(stdout, NULL);

    while (argc > 1 && argv[1][0] == '-') {
	const char *value = "";
	char option = argv[1][1];
	switch (option) {
	case 'P':
	case 'R':
	case 'z':
	case 'Z':
	case 'd':
	case 'O':
	    break;
	default:
	    if (strlen(argv[1]) > 2) {
		value = argv[1] + 2;
	    } else if (argc > 2) {
		value = argv[2];
		argv++;
		argc--;
	    } else
		value = "";
	    break;
	}
	argv++;
	argc--;
	switch (option) {
	case 'H':
#if !HAS_URI_SUPPORT
	    fprintf(stderr, "ERROR: Your LDAP library does not have URI support\n");
	    exit(1);
#endif
	    /* Fall thru to -h */
	case 'h':
	    if (ldapServer) {
		int len = strlen(ldapServer) + 1 + strlen(value) + 1;
		char *newhost = malloc(len);
		snprintf(newhost, len, "%s %s", ldapServer, value);
		free(ldapServer);
		ldapServer = newhost;
	    } else {
		ldapServer = strdup(value);
	    }
	    break;
	case 'b':
	    basedn = value;
	    break;
	case 'f':
	    searchfilter = value;
	    break;
	case 'u':
	    userattr = value;
	    break;
	case 'U':
	    passwdattr = value;
	    break;
	case 's':
	    if (strcmp(value, "base") == 0)
		searchscope = LDAP_SCOPE_BASE;
	    else if (strcmp(value, "one") == 0)
		searchscope = LDAP_SCOPE_ONELEVEL;
	    else if (strcmp(value, "sub") == 0)
		searchscope = LDAP_SCOPE_SUBTREE;
	    else {
		fprintf(stderr, PROGRAM_NAME ": ERROR: Unknown search scope '%s'\n", value);
		exit(1);
	    }
	    break;
	case 'E':
#if defined(NETSCAPE_SSL)
	    sslpath = value;
	    if (port == LDAP_PORT)
		port = LDAPS_PORT;
#else
	    fprintf(stderr, PROGRAM_NAME " ERROR: -E unsupported with this LDAP library\n");
	    exit(1);
#endif
	    break;
	case 'c':
	    connect_timeout = atoi(value);
	    break;
	case 't':
	    timelimit = atoi(value);
	    break;
	case 'a':
	    if (strcmp(value, "never") == 0)
		aliasderef = LDAP_DEREF_NEVER;
	    else if (strcmp(value, "always") == 0)
		aliasderef = LDAP_DEREF_ALWAYS;
	    else if (strcmp(value, "search") == 0)
		aliasderef = LDAP_DEREF_SEARCHING;
	    else if (strcmp(value, "find") == 0)
		aliasderef = LDAP_DEREF_FINDING;
	    else {
		fprintf(stderr, PROGRAM_NAME ": ERROR: Unknown alias dereference method '%s'\n", value);
		exit(1);
	    }
	    break;
	case 'D':
	    binddn = value;
	    break;
	case 'w':
	    bindpasswd = value;
	    break;
	case 'W':
	    readSecret(value);
	    break;
	case 'P':
	    persistent = !persistent;
	    break;
	case 'O':
	    bind_once = !bind_once;
	    break;
	case 'p':
	    port = atoi(value);
	    break;
	case 'R':
	    noreferrals = !noreferrals;
	    break;
#ifdef LDAP_VERSION3
	case 'v':
	    switch (atoi(value)) {
	    case 2:
		version = LDAP_VERSION2;
		break;
	    case 3:
		version = LDAP_VERSION3;
		break;
	    default:
		fprintf(stderr, "Protocol version should be 2 or 3\n");
		exit(1);
	    }
	    break;
	case 'Z':
	    if (version == LDAP_VERSION2) {
		fprintf(stderr, "TLS (-Z) is incompatible with version %d\n",
		    version);
		exit(1);
	    }
	    version = LDAP_VERSION3;
	    use_tls = 1;
	    break;
#endif
	case 'd':
	    debug++;
	    break;
	default:
	    fprintf(stderr, PROGRAM_NAME ": ERROR: Unknown command line option '%c'\n", option);
	    exit(1);
	}
    }

    while (argc > 1) {
	char *value = argv[1];
	if (ldapServer) {
	    int len = strlen(ldapServer) + 1 + strlen(value) + 1;
	    char *newhost = malloc(len);
	    snprintf(newhost, len, "%s %s", ldapServer, value);
	    free(ldapServer);
	    ldapServer = newhost;
	} else {
	    ldapServer = strdup(value);
	}
	argc--;
	argv++;
    }
    if (!ldapServer)
	ldapServer = strdup("localhost");

    if (!basedn) {
	fprintf(stderr, "Usage: " PROGRAM_NAME " -b basedn [options] [ldap_server_name[:port]]...\n\n");
	fprintf(stderr, "\t-b basedn (REQUIRED)\tbase dn under which to search\n");
	fprintf(stderr, "\t-f filter\t\tsearch filter to locate user DN\n");
	fprintf(stderr, "\t-u userattr\t\tusername DN attribute\n");
	fprintf(stderr, "\t-s base|one|sub\t\tsearch scope\n");
	fprintf(stderr, "\t-D binddn\t\tDN to bind as to perform searches\n");
	fprintf(stderr, "\t-w bindpasswd\t\tpassword for binddn\n");
	fprintf(stderr, "\t-W secretfile\t\tread password for binddn from file secretfile\n");
#if HAS_URI_SUPPORT
	fprintf(stderr, "\t-H URI\t\t\tLDAPURI (defaults to ldap://localhost)\n");
#endif
	fprintf(stderr, "\t-h server\t\tLDAP server (defaults to localhost)\n");
	fprintf(stderr, "\t-p port\t\t\tLDAP server port\n");
	fprintf(stderr, "\t-P\t\t\tpersistent LDAP connection\n");
#if defined(NETSCAPE_SSL)
	fprintf(stderr, "\t-E sslcertpath\t\tenable LDAP over SSL\n");
#endif
	fprintf(stderr, "\t-c timeout\t\tconnect timeout\n");
	fprintf(stderr, "\t-t timelimit\t\tsearch time limit\n");
	fprintf(stderr, "\t-R\t\t\tdo not follow referrals\n");
	fprintf(stderr, "\t-a never|always|search|find\n\t\t\t\twhen to dereference aliases\n");
#ifdef LDAP_VERSION3
	fprintf(stderr, "\t-v 2|3\t\t\tLDAP version\n");
	fprintf(stderr, "\t-Z\t\t\tTLS encrypt the LDAP connection, requires LDAP version 3\n");
#endif
	fprintf(stderr, "\n");
	fprintf(stderr, "\tIf no search filter is specified, then the dn <userattr>=user,basedn\n\twill be used (same as specifying a search filter of '<userattr>=',\n\tbut quicker as as there is no need to search for the user DN)\n\n");
	fprintf(stderr, "\tIf you need to bind as a user to perform searches then use the\n\t-D binddn -w bindpasswd or -D binddn -W secretfile options\n\n");
	exit(1);
    }
/* On Windows ldap_start_tls_s is available starting from Windows XP, 
 * so we need to bind at run-time with the function entry point
 */
#ifdef _SQUID_MSWIN_
    if (use_tls) {

	HMODULE WLDAP32Handle;

	WLDAP32Handle = GetModuleHandle("wldap32");
	if ((Win32_ldap_start_tls_s = (PFldap_start_tls_s) GetProcAddress(WLDAP32Handle, LDAP_START_TLS_S)) == NULL) {
	    fprintf(stderr, PROGRAM_NAME ": ERROR: TLS (-Z) not supported on this platform.\n");
	    exit(1);
	}
    }
#endif

    while (fgets(buf, sizeof(buf), stdin) != NULL) {
	user = strtok(buf, " \r\n");
	passwd = strtok(NULL, "\r\n");

	if (!user || !passwd || !passwd[0]) {
	    printf("ERR\n");
	    continue;
	}
	rfc1738_unescape(user);
	rfc1738_unescape(passwd);
	if (!validUsername(user)) {
	    printf("ERR No such user\n");
	    continue;
	}
	tryagain = (ld != NULL);
      recover:
	if (ld == NULL && persistent)
	    ld = open_ldap_connection(ldapServer, port);
	if (checkLDAP(ld, user, passwd, ldapServer, port) != 0) {
	    if (tryagain && squid_ldap_errno(ld) != LDAP_INVALID_CREDENTIALS) {
		tryagain = 0;
		ldap_unbind(ld);
		ld = NULL;
		goto recover;
	    }
	    printf("ERR %s\n", ldap_err2string(squid_ldap_errno(ld)));
	} else {
	    printf("OK\n");
	}
	if (ld && (squid_ldap_errno(ld) != LDAP_SUCCESS && squid_ldap_errno(ld) != LDAP_INVALID_CREDENTIALS)) {
	    ldap_unbind(ld);
	    ld = NULL;
	}
    }
    if (ld)
	ldap_unbind(ld);
    return 0;
}
Beispiel #5
0
int main(int argc, char** argv) {
    if (argc == 4) {
        Transaction* session = new Transaction(argv[1], argv[2], argv[3]);

        string input, username, type, event, seller, balance, price;
        int ticketNum;

        while (getline(cin, input)) {
            //cout << "Enter your command." << endl;
            //input = rtrim(input);
            input = Poco::trimRight(input);

            if (!session->isLoggedIn()) {
                if (input.compare(LOGIN) == 0) {
                    // Log in stuff.
                    cout << ENTER_USERNAME << endl;
                    getline(cin, input);

                    if (!validUsername(input)) {
                        cout << INVALID_USERNAME_EXIST << endl;
                    }
                    else if (session->login(input)) {
                        cout << LOGIN_SUCCESS << endl;
                    }
                }
                else if (input.compare(QUIT) == 0) {
                    session->quit();
                    break;
                }
                else if (input.compare(LOGOUT) == 0
                        || input.compare(BUY) == 0
                        || input.compare(CREATE) == 0
                        || input.compare(SELL) == 0
                        || input.compare(REFUND) == 0
                        || input.compare(ADDCREDIT) == 0
                        || input.compare(DELETE) == 0) {
                    cout << USER_NOT_LOGGED_IN << endl;
                }
                else {
                    cout << INVALID_COMMAND << endl;
                }
            }
            else if (input.compare(LOGIN) == 0) {
                cout << USER_ALREADY_LOGGED << endl;
            }
            else if (input.compare(LOGOUT) == 0) {
                session->logout();
                cout << LOGOUT_SUCCESS << endl;
            }
            else if (input.compare(CREATE) == 0) {
                cout << CREATE_ENTER_USERNAME << endl;
                getline(cin, username);

                if (!validUsername(username)) {
                    cout << INVALID_USERNAME << endl;
                }
                else {
                    cout << ENTER_ACCOUNT_TYPE << endl;
                    getline(cin, type);

                    if (!validAccountType(type)) {
                        cout << INVALID_ACCOUNT_TYPE << endl;
                    }
                    else {
                        cout << ENTER_ACCOUT_BALANCE << endl;
                        getline(cin, balance);
                        //cin >> balance;

                        if (!validBalance(balance)) {
                            cout << INVALID_ACCOUNT_BALANCE << endl;
                        }
                        else {
                            double validBalance;
                            Poco::DynamicAny(balance).convert(validBalance);

                            if (session->create(username, type, validBalance)) {
                                cout << CREATE_SUCCESS << endl;
                            }
                        }
                    }
                }
            }
            else if (input.compare(DELETE) == 0) {
                //if (session->isAdmin()) {
                    cout << ENTER_USERNAME << endl;
                    getline(cin, username);

                    if (!validUsername(username)) {
                        cout << INVALID_USERNAME << endl;
                    }
                    else if (session->removeUser(username)) {
                        cout << DELETE_SUCCESS << endl;
                    }
                //}
            }
            else if (input.compare(ADDCREDIT) == 0) {
                if (session->isAdmin()) {
                    cout << ENTER_USERNAME << endl;
                    getline(cin, username);

                    if (!validUsername(username)) {
                        cout << INVALID_USERNAME << endl;
                    }
                    //else if (session->getFileIO()->findUser(username) != -1) {
                    else {
                        cout << ENTER_CREDIT_AMOUNT << endl;
                        getline(cin, balance);
                        //cin >> balance;

                        if (!validBalance(balance)) {
                            cout << INVALID_CREDIT_AMOUNT << endl;
                        }
                        else {
                            double validBalance;
                            Poco::DynamicAny(balance).convert(validBalance);

                            if (session->addcredit(username, validBalance)) {
                                //cout << "new balance: $" << session->getFileIO()->getAccountList()->at(session->getCurrentUser()).getBalance() << endl;
                                cout << ADDCREDIT_SUCCESS << endl;
                            }
                        }
                    }
                }
                else {
                    cout << ENTER_CREDIT_AMOUNT << endl;
                    //getline(cin, balance);
                    //cin >> balance;

                    if (getline(cin, balance) && !validBalance(balance)) {
                        cout << INVALID_CREDIT_AMOUNT << endl;
                    }
                    else {
                        double validBalance;
                        Poco::DynamicAny(balance).convert(validBalance);

                        if (session->addcredit(validBalance)) {
                            //cout << "new balance: $" << session->getFileIO()->getAccountList()->at(session->getCurrentUser()).getBalance() << endl;
                            cout << ADDCREDIT_SUCCESS << endl;
                        }
                    }
                }
            }
            else if (input.compare(SELL) == 0) {
                cout << ENTER_EVENT << endl;
                getline(cin, event);

                if (!validEventName(event)) {
                    cout << INVALID_EVENT_TITLE << endl;
                }
                else {
                    cout << ENTER_PRICE << endl;
                    getline(cin, price);
                    //cin >> price;

                    if (!validPrice(price)) {
                        cout << INVALID_EVENT_PRICE << endl;
                    }
                    else {
                        double validPrice;
                        Poco::DynamicAny(price).convert(validPrice);

                        cout << ENTER_TICKET_NUMBER << endl;
                        //getline(cin, ticketNum);
                        cin >> ticketNum;

                        bool fail = cin.fail();
                        cin.clear();

                        // Skip to next line
                        string dummy;
                        getline(cin, dummy);

                        if (fail || !validTicketNumber(ticketNum)) {
                            cout << INVALID_TICKET_NUMBER << endl;
                        }
                        else if (session->sell(event, validPrice, ticketNum)) {
                            cout << SELL_SUCCESS << endl;
                        }
                    }
                }
            }
            else if (input.compare(BUY) == 0) {