int main(int argc, char **argv) { char *output; if (argv[1]==NULL) output = str2md5("hello", strlen("hello")); else output = str2md5(argv[1], strlen(argv[1])); printf("%s\n", output); free(output); return 0; }
int get_safe_cache_file_path(const char* path, char* file_path_safe, char* temp_dir) { char tmp_path[PATH_MAX]; strncpy(tmp_path, path, PATH_MAX); char* pch; while ((pch = strchr(tmp_path, '/'))) * pch = '.'; char file_path[PATH_MAX] = ""; //temp file name had process pid in it, removed as on restart files are left in cache (pid changes) snprintf(file_path, PATH_MAX, TEMP_FILE_NAME_FORMAT, temp_dir, tmp_path); //fixme check if sizeof or strlen is suitable int file_path_len = sizeof(file_path); //the file path name using this format can go beyond NAME_MAX size and will generate error on fopen //solution: cap file length to NAME_MAX, use a prefix from original path for debug purposes and add md5 id char* md5_path = str2md5(file_path, file_path_len); int md5len = strlen(md5_path); size_t safe_len_prefix = min(NAME_MAX - md5len, file_path_len); strncpy(file_path_safe, file_path, safe_len_prefix); strncpy(file_path_safe + safe_len_prefix, md5_path, md5len); //sometimes above copy process produces longer strings that NAME_MAX, force a null terminated string file_path_safe[safe_len_prefix + md5len - 1] = '\0'; free(md5_path); return strlen(file_path_safe); }
void str_cli(FILE *fp, int sockfd) { int i, j, maxfdp1, n, logined; fd_set rset, wset; char sendline[MAXLINE], recvline[MAXLINE], username[21], password[34], npassword[34]; short int permission; FD_ZERO(&rset); FD_ZERO(&wset); logined = 0; for ( ; ; ) { if (logined == 0) { FD_SET(sockfd, &wset); } else { FD_SET(fileno(fp), &rset); FD_SET(sockfd, &rset); } maxfdp1 = ( (fileno(fp) > sockfd)?fileno(fp):sockfd ) + 1; select(maxfdp1, &rset, &wset, NULL, NULL); memset(recvline, '\0', MAXLINE); memset(sendline, '\0', MAXLINE); if (FD_ISSET(sockfd, &rset)) { /* socket is readable */ memset(recvline, '\0', MAXLINE); if ( read(sockfd, recvline, MAXLINE) == 0 ) { fputs("Service is not avaliable.", stdout); exit(0); } if (recvline[0] == '\0') continue; fputs(recvline, stdout); fflush(stdout); } if (FD_ISSET(fileno(fp), &rset)) { /* input is readable */ read(fileno(fp), sendline, MAXLINE ); printf("fp: %s", sendline); /* Change passwd */ if( !strcmp(sendline, "passwd\n") ) { memset(password, '\0', 34); memset(npassword, '\0', 34); memset(sendline, '\0', MAXLINE); printf("New password: "******"\nRetype new password: "******"\n:: Change password complete!\n"); sprintf(sendline, "passwd %s\n", password); //printf("[%s]\n", sendline); if ( write(sockfd, sendline, strlen(sendline)) <= 0 ){ printf("server is dead.\n"); exit(1); } } else { printf("\n:: Change password failed, please try again.\n> "); } memset(password, '\0', 34); memset(npassword, '\0', 34); memset(sendline, '\0', MAXLINE); //continue; } /* Create Account */ else if( !strcmp(sendline, "newaccount\n") ) { memset(username, '\0', 21); memset(password, '\0', 34); memset(sendline, '\0', MAXLINE); memset(recvline, '\0', MAXLINE); permission = 0; printf("Username: "******"Password: "******"\nPermission: "); scanf("%hd", &permission); // Encrypt password. strcpy(password, str2md5(password, strlen(password))); sprintf(sendline, "newaccount %s %s %hd\n", username, password, permission); //printf("[%s]\n", sendline); if ( write(sockfd, sendline, strlen(sendline)) <= 0 ){ printf("server is dead.\n"); exit(1); } if ( read(sockfd, recvline, MAXLINE) == 0 ) { fputs("Service is not avaliable.", stdout); exit(1); } fputs(recvline, stdout); fflush(stdout); memset(password, '\0', 34); memset(username, '\0', 21); memset(sendline, '\0', MAXLINE); memset(recvline, '\0', MAXLINE); //continue; } for(i = 0; i < strlen(sendline); i++) { if(sendline[i] != '\n') continue; sendline[i+1] = '\0'; } if(strlen(sendline) > 0) FD_SET(sockfd, &wset); } if (FD_ISSET(sockfd, &wset)) { //printf("sock is writeable!\n"); if (logined == 0) { printf("Username: "******"Password: "******"lg %s %s\n", username, password); //printf("[%s]\n", sendline); if ( write(sockfd, sendline, strlen(sendline)) <= 0 ){ printf("server is dead.\n"); exit(1); } if ( read(sockfd, recvline, MAXLINE) == 0 ) { fputs("Service is not avaliable.", stdout); exit(0); } fputs(recvline, stdout); fflush(stdout); if ( strncmp(recvline, "\nLogin Success!\n", 16) == 0) { logined = 1; FD_SET(fileno(fp), &rset); FD_SET(sockfd, &rset); FD_CLR(sockfd, &wset); } memset(sendline, '\0', MAXLINE); memset(username, '\0', 21); memset(password, '\0', 34); } else if ( write(sockfd, sendline, strlen(sendline)) <= 0 ){ printf("server is dead.\n"); exit(1); } FD_CLR(sockfd, &wset); } } }
void set_stage(struct shared_data *data, char value[100]){ strcpy(data->stage, str2md5(value, strlen(value))); data->update_time = time(NULL); list_config(data); }
void set_default_config (struct shared_data *data) { strcpy(data->stage, str2md5("default", 7)); data->recording = 0; data->update_time = time(NULL); list_config(data); }