int initial_one_var_file_in_mount_path(const char *const account, const char *const mount_path) { FILE *fp; char var_file[256]; int result, i; int sh_num = 0; char **folder_list = NULL; int samba_right, ftp_right, dms_right; if (account == NULL || strlen(account) <= 0) { return -1; } if (mount_path == NULL || strlen(mount_path) <= 0) { return -1; } // get the folder number and folder_list result = get_all_folder_in_mount_path(mount_path, &sh_num, &folder_list); if (result != 0) { free_2_dimension_list(&sh_num, &folder_list); return -1; } // get the samba right and ftp right if (strcmp(account, FTP_ANONYMOUS_USER) == 0) { samba_right = 1; ftp_right = 1; dms_right = 1; } else { samba_right = DEFAULT_SAMBA_RIGHT; ftp_right = DEFAULT_FTP_RIGHT; dms_right = DEFAULT_DMS_RIGHT; } // write the default content in the var file snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", mount_path, account); if ((fp = fopen(var_file, "w")) == NULL) { free_2_dimension_list(&sh_num, &folder_list); return -1; } for (i = 0; i < sh_num; ++i) { fprintf(fp, "*"); fprintf(fp, "%s", folder_list[i]); fprintf(fp, "=%d%d%d\n", samba_right, ftp_right, dms_right); } fclose(fp); free_2_dimension_list(&sh_num, &folder_list); return 0; }
int create_if_no_var_files(const char *const mount_path) { int acc_num = 0; char **account_list = NULL; int i; char var_file[256]; // process aninymous user snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", mount_path, FTP_ANONYMOUS_USER); if (!test_if_file(var_file)) initial_one_var_file_in_mount_path(FTP_ANONYMOUS_USER, mount_path); else modify_if_exist_new_folder(FTP_ANONYMOUS_USER, mount_path); // get the account number and account_list get_account_list(&acc_num, &account_list); for (i = 0; i < acc_num; ++i) { snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", mount_path, account_list[i]); if (!test_if_file(var_file)) initial_one_var_file_in_mount_path(account_list[i], mount_path); else modify_if_exist_new_folder(account_list[i], mount_path); } free_2_dimension_list(&acc_num, &account_list); return 0; }
int initial_all_var_file_in_mount_path(const char *const mount_path) { int i; int acc_num = 0; char **account_list = NULL; if (mount_path == NULL || strlen(mount_path) <= 0) { return -1; } // delete all var files doSystem("rm -f /%s/.__*.txt", mount_path); // initial the var file for ftp anonymous initial_one_var_file_in_mount_path(FTP_ANONYMOUS_USER, mount_path); // get the account number and account_list get_account_list(&acc_num, &account_list); // initial the var file for (i = 0; i < acc_num; ++i) initial_one_var_file_in_mount_path(account_list[i], mount_path); free_2_dimension_list(&acc_num, &account_list); // initial the folder list initial_folder_list_in_mount_path(mount_path); return 0; }
int initial_folder_list_in_mount_path(const char *const mount_path) { int sh_num = 0; char **folder_list = NULL; FILE *fp; char *list_file; int result, len, i; if (mount_path == NULL || strlen(mount_path) <= 0) { return -1; } // 2. get the folder number and folder_list result = get_all_folder_in_mount_path(mount_path, &sh_num, &folder_list); if (result != 0) { free_2_dimension_list(&sh_num, &folder_list); return -1; } // 3. get the list_file len = strlen(mount_path)+strlen("/.__folder_list.txt"); list_file = (char *)malloc(sizeof(char)*(len+1)); if (list_file == NULL) { free_2_dimension_list(&sh_num, &folder_list); return -1; } sprintf(list_file, "%s/.__folder_list.txt", mount_path); list_file[len] = 0; // 4. write the folder info fp = fopen(list_file, "w"); if (fp == NULL) { free_2_dimension_list(&sh_num, &folder_list); free(list_file); return -1; } free(list_file); fprintf(fp, "sh_num=%d\n", sh_num); for (i = 0; i < sh_num; ++i) fprintf(fp, "sh_name%d=%s\n", i, folder_list[i]); fclose(fp); free_2_dimension_list(&sh_num, &folder_list); return 0; }
int test_if_exist_folder_in_mount_path(const char *const mount_path, const char *const folder) { int sh_num = 0; char **folder_list = NULL; int result, i; result = get_folder_list_in_mount_path(mount_path, &sh_num, &folder_list); if (result != 0) { free_2_dimension_list(&sh_num, &folder_list); return -1; } result = 0; for (i = 0; i < sh_num; ++i) if (!upper_strcmp(folder, folder_list[i])) { result = 1; break; } free_2_dimension_list(&sh_num, &folder_list); return result; }
int test_if_exist_account(const char *const account) { int acc_num = 0; char **account_list = NULL; int result, i; if (!strcmp(account, FTP_ANONYMOUS_USER)) return 1; get_account_list(&acc_num, &account_list); result = 0; for (i = 0; i < acc_num; ++i) if (!strcmp(account, account_list[i])) { result = 1; break; } free_2_dimension_list(&acc_num, &account_list); return result; }
int main(int argc, char *argv[]) { FILE *fp; int n=0, sh_num=0; disk_info_t *follow_disk, *disks_info = NULL; partition_info_t *follow_partition; /* st_webdav_mpde = 1 => share mode (default) st_webdav_mode = 2 => account mode */ /* write /tmp/lighttpd.conf */ if ((fp=fopen(WEBDAV_CONF, "r"))) { fclose(fp); unlink(WEBDAV_CONF); } fp = fopen(WEBDAV_CONF, "w"); if (fp==NULL) return; /* Load modules */ fprintf(fp, "server.modules+=(\"mod_alias\")\n"); fprintf(fp, "server.modules+=(\"mod_userdir\")\n"); fprintf(fp, "server.modules+=(\"mod_aidisk_access\")\n"); fprintf(fp, "server.modules+=(\"mod_sharelink\")\n"); fprintf(fp, "server.modules+=(\"mod_create_captcha_image\")\n"); fprintf(fp, "server.modules+=(\"mod_webdav\")\n"); fprintf(fp, "server.modules+=(\"mod_smbdav\")\n"); fprintf(fp, "server.modules+=(\"mod_redirect\")\n"); fprintf(fp, "server.modules+=(\"mod_compress\")\n"); fprintf(fp, "server.modules+=(\"mod_usertrack\")\n"); fprintf(fp, "server.modules+=(\"mod_rewrite\")\n"); if (nvram_match("st_webdav_mode", "2")){ fprintf(fp, "server.modules+=(\"mod_access\")\n"); fprintf(fp, "server.modules+=(\"mod_auth\")\n"); } /* Basic setting */ fprintf(fp, "server.port=%s\n",get_webdav_http_port()); // defult setting, but no use // fprintf(fp, "server.port=8999\n"); // defult setting, but no use // fprintf(fp, "server.document-root=\"/mnt/\"\n"); fprintf(fp, "server.document-root=\"/tmp/lighttpd/www\"\n"); fprintf(fp, "server.upload-dirs=(\"/tmp/lighttpd/uploads\")\n"); fprintf(fp, "server.errorlog=\"/tmp/lighttpd/err.log\"\n"); fprintf(fp, "server.pid-file=\"/tmp/lighttpd/lighttpd.pid\"\n"); fprintf(fp, "server.arpping-interface=\"br0\"\n"); fprintf(fp, "server.errorfile-prefix=\"/usr/css/status-\"\n"); fprintf(fp, "dir-listing.activate=\"enable\"\n"); fprintf(fp, "server.syslog=\"/tmp/lighttpd/syslog.log\"\n"); // **** Minetype setting **** // fprintf(fp, "mimetype.assign = (\n"); fprintf(fp, "\".html\" => \"text/html\",\n"); fprintf(fp, "\".htm\" => \"text/html\",\n"); fprintf(fp, "\".css\" => \"text/css\",\n"); fprintf(fp, "\".js\" => \"text/javascript\",\n"); fprintf(fp, "\".swf\" => \"application/x-shockwave-flash\",\n"); //fprintf(fp, "\".txt\" => \"text/plain\",\n"); //fprintf(fp, "\".jpg\" => \"image/jpeg\",\n"); //fprintf(fp, "\".gif\" => \"image/gif\",\n"); //fprintf(fp, "\".png\" => \"image/png\",\n"); //fprintf(fp, "\".pdf\" => \"application/pdf\",\n"); //fprintf(fp, "\".mp4\" => \"video/mp4\",\n"); //fprintf(fp, "\".m4v\" => \"video/mp4\",\n"); //fprintf(fp, "\".wmv\" => \"video/wmv\",\n"); //fprintf(fp, "\".mp3\" => \"audio/mpeg\",\n"); //fprintf(fp, "\".avi\" => \"video/avi\",\n"); //fprintf(fp, "\".mov\" => \"video/mov\""); fprintf(fp, "\"\" => \"application/x-octet-stream\""); fprintf(fp, ")\n"); // **** Index file names **** // fprintf(fp, "index-file.names = ( \"index.php\", \"index.html\",\n"); fprintf(fp, "\"index.htm\", \"default.htm\",\n"); fprintf(fp, " \" index.lighttpd.html\" )\n"); // **** url access deny fprintf(fp, " url.access-deny = ( \"~\", \".inc\" )\n"); // **** static-file.exclude extensions fprintf(fp," static-file.exclude-extensions = ( \".php\", \".pl\", \".fcgi\" )\n"); // **** fprintf(fp, "compress.cache-dir = \"/tmp/lighttpd/compress/\"\n"); fprintf(fp, "compress.filetype = ( \"application/x-javascript\", \"text/css\", \"text/html\", \"text/plain\" )\n"); // **** SambaDav setting fprintf(fp, "$SERVER[\"socket\"]==\":%s\"{\n", get_webdav_http_port()); // fprintf(fp, "$SERVER[\"socket\"]==\":8999\"{\n"); // fprintf(fp, " alias.url=(\"/webdav\"=>\"/mnt/\")\n"); // fprintf(fp, " $HTTP[\"url\"]=~\"^/usbdisk($|/)\"{\n"); fprintf(fp, " $HTTP[\"url\"]=~\"^/%s($|/)\"{\n",get_productid()); fprintf(fp, " server.document-root = \"/\"\n"); // fprintf(fp, " alias.url=(\"/usbdisk\"=>\"/mnt\")\n"); fprintf(fp, " alias.url=(\"/%s\"=>\"/mnt\")\n", get_productid()); fprintf(fp, " webdav.activate=\"enable\"\n"); fprintf(fp, " webdav.is-readonly=\"disable\"\n"); fprintf(fp, " webdav.sqlite-db-name=\"/tmp/lighttpd/webdav.db\"\n"); fprintf(fp, " }\n"); fprintf(fp, " else $HTTP[\"url\"]=~\"^/smb($|/)\"{\n"); fprintf(fp, " server.document-root = \"/\"\n"); fprintf(fp, " alias.url=(\"/smb\"=>\"/usr\")\n"); fprintf(fp, " smbdav.auth_ntlm = (\"Microsoft-WebDAV\",\"xxBitKinex\",\"WebDrive\")\n"); fprintf(fp, " webdav.activate=\"enable\"\n"); fprintf(fp, " webdav.is-readonly=\"disable\"\n"); // fprintf(fp, " webdav.sqlite-db-name=\"/tmp/lighttpd/webdav.db\"\n"); fprintf(fp, " }\n"); fprintf(fp, " else $HTTP[\"url\"] =~ \"^/favicon.ico$\"{\n"); fprintf(fp, " server.document-root = \"/\"\n"); fprintf(fp, " alias.url = ( \"/favicon.ico\" => \"/usr/css/favicon.ico\" ) \n"); fprintf(fp, " webdav.activate = \"enable\" \n"); fprintf(fp, " webdav.is-readonly = \"enable\"\n"); fprintf(fp, " }\n"); fprintf(fp, " else $HTTP[\"url\"] !~ \"^/smb($|/)\" { \n"); fprintf(fp, " server.document-root = \"smb://\" \n"); fprintf(fp, " smbdav.activate = \"enable\" \n"); fprintf(fp, " smbdav.is-readonly = \"disable\" \n"); fprintf(fp, " smbdav.always-auth = \"enable\" \n"); fprintf(fp, " smbdav.sqlite-db-name = \"/tmp/lighttpd/smbdav.db\" \n"); fprintf(fp, " usertrack.cookie-name = \"SMBSESSID\" \n"); fprintf(fp, " }\n"); fprintf(fp, "}\n"); #if 0 /*** Webdav_setting ***/ /* default : http://192.168.1.1:8082/webdav */ fprintf(fp, "$SERVER[\"socket\"]==\":8082\"{\n"); fprintf(fp, " alias.url=(\"/webdav\"=>\"/mnt/\")\n"); //mount point fprintf(fp, " $HTTP[\"url\"]=~\"^/webdav($|/)\"{\n"); fprintf(fp, " webdav.activate=\"enable\"\n"); fprintf(fp, " webdav.is-readonly=\"disable\"\n"); fprintf(fp, " webdav.sqlite-db-name=\"/tmp/lighttpd/webdav.db\"\n"); fprintf(fp, " }\n"); fprintf(fp, " auth.backend=\"plain\"\n"); fprintf(fp, " auth.backend.plain.userfile=\"/tmp/lighttpd/permissions\"\n"); fprintf(fp, " auth.require= (\"\"=>(\"method\"=>\"basic\",\n"); fprintf(fp, " \"realm\"=>\"webdav\",\n"); fprintf(fp, " \"require\"=>\"valid-user\"))\n"); disks_info = read_disk_data(); if (disks_info == NULL) { //printf("[webdav] fail in read disk data!!\n"); usb_dbg("Couldn't get disk list when writing smb.conf!\n"); // goto confpage; goto WEBDAV_SETTING; } /* account permissions */ if (nvram_match("st_webdav_mode", "2")){ disks_info = read_disk_data(); if (disks_info == NULL) { usb_dbg("Couldn't get disk list when writing lighttpd.conf!\n"); // goto confpage; goto WEBDAV_SETTING; } int acc_num; char **account_list; /* folder permissions */ if (get_account_list(&acc_num, &account_list) < 0) { usb_dbg("Can't read the account list.\n"); //printf("[webdav] fail in get account list\n"); free_2_dimension_list(&acc_num, &account_list); // goto confpage; goto WEBDAV_SETTING; } for (follow_disk = disks_info; follow_disk != NULL; follow_disk = follow_disk->next) { for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) { if (follow_partition->mount_point == NULL) continue; char **folder_list; int i; char tmp1[32]; char *tmp2, *tmp_pre, *tmp_aft; strcpy(tmp1, follow_partition->mount_point); tmp2 = tmp1; for (i=0; i<3; i++){ tmp_pre = strsep(&tmp2, "/"); tmp_aft = tmp2; } //printf("[webdav] tmp1=%s,tmp2=%s,tmp_pre=%s,tmp_aft=%s\n", tmp1, tmp2, tmp_pre, tmp_aft); // 1. get the folder list if (get_folder_list(follow_partition->mount_point, &sh_num, &folder_list) < 0) { //printf("[webdav] fail in get folder list\n"); usb_dbg("Can't read the folder list in %s.\n", follow_partition->mount_point); free_2_dimension_list(&sh_num, &folder_list); continue; } /* right = 0 => NO right = 1 => R right = 3 => R/W (default) */ // 2. start to get every share for (n = 0; n < sh_num; ++n) { int i, right; for (i = 0; i < acc_num; ++i) { right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "webdav"); //printf("[webdav] (%d,%d) : right=%d, account=%s, folder=%s, mount=%s, tmp_aft=%s\n", n, i, right, account_list[i], folder_list[n], follow_partition->mount_point, tmp_aft); if(right == 0){ /* forbid to access the folder */ fprintf(fp, " $HTTP[\"url\"]=~\"^/webdav/%s/%s($|/)\"{\n", tmp_aft, folder_list[n]); fprintf(fp, " url.access-deny=(\"\")\n"); fprintf(fp, " }\n"); } else if (right == 1){ fprintf(fp, " $HTTP[\"url\"]=~\"^/webdav/%s/%s($|/)\"{\n", tmp_aft, folder_list[n]); fprintf(fp, " webdav.is-readonly=\"enable\"\n"); fprintf(fp, " }\n"); } } } free_2_dimension_list(&sh_num, &folder_list); } } /* folder permissions */ free_2_dimension_list(&acc_num, &account_list); }/* account permissions */ WEBDAV_SETTING: fprintf(fp, "}\n"); /*** Webdav_setting ***/ #endif /*** Webdav_SSL ***/ /* default : https://192.168.1.1:443/webdav */ fprintf(fp, "$SERVER[\"socket\"]==\":%s\"{\n",get_webdav_https_port()); fprintf(fp, " ssl.pemfile=\"/tmp/lighttpd/server.pem\"\n"); fprintf(fp, " ssl.engine=\"enable\"\n"); //fprintf(fp, " alias.url=(\"/webdav\"=>\"/mnt/\")\n"); // fprintf(fp, " $HTTP[\"url\"]=~\"^/usbdisk($|/)\"{\n"); fprintf(fp, " $HTTP[\"url\"]=~\"^/%s($|/)\"{\n", get_productid()); fprintf(fp, " server.document-root = \"/\"\n"); // fprintf(fp, " alias.url=(\"/usbdisk\"=>\"/mnt\")\n"); fprintf(fp, " alias.url=(\"/%s\"=>\"/mnt\")\n", get_productid()); fprintf(fp, " webdav.activate=\"enable\"\n"); fprintf(fp, " webdav.is-readonly=\"disable\"\n"); fprintf(fp, " webdav.sqlite-db-name=\"/tmp/lighttpd/webdav.db\"\n"); fprintf(fp, " }\n"); fprintf(fp, " else $HTTP[\"url\"]=~\"^/smb($|/)\"{\n"); fprintf(fp, " server.document-root = \"/\"\n"); fprintf(fp, " alias.url=(\"/smb\"=>\"/usr\")\n"); fprintf(fp, " smbdav.auth_ntlm = (\"Microsoft-WebDAV\",\"xxBitKinex\",\"WebDrive\")\n"); fprintf(fp, " webdav.activate=\"enable\"\n"); fprintf(fp, " webdav.is-readonly=\"disable\"\n"); // fprintf(fp, " webdav.sqlite-db-name=\"/tmp/lighttpd/webdav.db\"\n"); fprintf(fp, " }\n"); fprintf(fp, " else $HTTP[\"url\"] =~ \"^/favicon.ico$\"{\n"); fprintf(fp, " server.document-root = \"/\"\n"); fprintf(fp, " alias.url = ( \"/favicon.ico\" => \"/usr/css/favicon.ico\" ) \n"); fprintf(fp, " webdav.activate = \"enable\" \n"); fprintf(fp, " webdav.is-readonly = \"enable\"\n"); fprintf(fp, " }\n"); fprintf(fp, " else $HTTP[\"url\"] !~ \"^/smb($|/)\" { \n"); fprintf(fp, " server.document-root = \"smb://\" \n"); fprintf(fp, " smbdav.activate = \"enable\" \n"); fprintf(fp, " smbdav.is-readonly = \"disable\" \n"); fprintf(fp, " smbdav.always-auth = \"enable\" \n"); fprintf(fp, " smbdav.sqlite-db-name = \"/tmp/lighttpd/smbdav.db\" \n"); fprintf(fp, " usertrack.cookie-name = \"SMBSESSID\" \n"); fprintf(fp, " }\n"); #if 0 /* account permissions */ if (nvram_match("st_webdav_mode", "2")){ fprintf(stderr,"=================================================>st mod =2"); disks_info = read_disk_data(); if (disks_info == NULL) { usb_dbg("Couldn't get disk list when writing lighttpd.conf!\n"); fprintf(stderr, "=========================================================>error1"); goto confpage; } int acc_num; char **account_list; fprintf(fp, " auth.backend=\"plain\"\n"); fprintf(fp, " auth.backend.plain.userfile=\"/tmp/lighttpd/permissions\"\n"); fprintf(fp, " auth.require= (\"\"=>(\"method\"=>\"basic\",\n"); fprintf(fp, " \"realm\"=>\"webdav\",\n"); fprintf(fp, " \"require\"=>\"valid-user\"))\n"); /* folder permissions */ if (get_account_list(&acc_num, &account_list) < 0) { //usb_dbg("Can't read the account list.\n"); //printf("[webdav] fail in get account list\n"); free_2_dimension_list(&acc_num, &account_list); fprintf(stderr, "================================================================>error2"); goto confpage; } for (follow_disk = disks_info; follow_disk != NULL; follow_disk = follow_disk->next) { for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) { if (follow_partition->mount_point == NULL) continue; char **folder_list; int i; //charles: dont init the size of tmp1, the name of share foler may exceed the buffer. // char tmp1[32]; char *tmp1; char *tmp2, *tmp_pre, *tmp_aft; int tmp1_size=0; tmp1_size = strlen(follow_partition->mount_point)+1; tmp1 = malloc(tmp1_size); memset(tmp1, 0, tmp1_size); strcpy(tmp1, follow_partition->mount_point); tmp2 = tmp1; for (i=0; i<3; i++){ tmp_pre = strsep(&tmp2, "/"); tmp_aft = tmp2; } // 1. get the folder list if (get_folder_list(follow_partition->mount_point, &sh_num, &folder_list) < 0) { //printf("[webdav] fail in get folder list\n"); usb_dbg("Can't read the folder list in %s.\n", follow_partition->mount_point); free_2_dimension_list(&sh_num, &folder_list); if(tmp1) free(tmp1); fprintf(stderr,"====================================================================>get folder list error"); continue; } // 2. start to get every share for (n = 0; n < sh_num; ++n) { int i, right; for (i = 0; i < acc_num; ++i) { right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "webdav"); fprintf(stderr,"[webdav] (%d,%d) : right=%d, account=%s, folder=%s, mount=%s, tmp_aft=%s\n", n, i, right, account_list[i], folder_list[n], follow_partition->mount_point, tmp_aft); if(right == 0){ /* forbid to access the folder */ fprintf(fp, " $HTTP[\"url\"]=~\"^/webdav/%s/%s($|/)\"{\n", tmp_aft, folder_list[n]); fprintf(fp, " url.access-deny=(\"\")\n"); fprintf(fp, " }\n"); } else if (right == 1){ fprintf(fp, " $HTTP[\"url\"]=~\"^/webdav/%s/%s($|/)\"{\n", tmp_aft, folder_list[n]); fprintf(fp, " webdav.is-readonly=\"enable\"\n"); fprintf(fp, " }\n"); } } } free_2_dimension_list(&sh_num, &folder_list); if(tmp1) free(tmp1); } } /* folder permissions */ free_2_dimension_list(&acc_num, &account_list); }/* account permissions */ #endif goto confpage; confpage: fprintf(fp, "}\n"); /*** Webdav_SSL ***/ /* debugging */ fprintf(fp, "debug.log-request-header=\"disable\"\n"); fprintf(fp, "debug.log-response-header=\"disable\"\n"); fprintf(fp, "debug.log-request-handling=\"disable\"\n"); fprintf(fp, "debug.log-file-not-found=\"disable\"\n"); fprintf(fp, "debug.log-condition-handling=\"disable\"\n"); fclose(fp); free_disk_data(&disks_info); return 0; }
int get_folder_list_in_mount_path(const char *const mount_path, int *sh_num, char ***folder_list) { char **tmp_folder_list, target[16]; int len, i; char *list_file, *list_info; char *follow_info, *follow_info_end, backup; // 1. get list file len = strlen(mount_path)+strlen("/.__folder_list.txt"); list_file = (char *)malloc(sizeof(char)*(len+1)); if (list_file == NULL) { return -1; } sprintf(list_file, "%s/.__folder_list.txt", mount_path); list_file[len] = 0; // 2. read if the list file is existed if (!test_if_file(list_file)) initial_folder_list_in_mount_path(mount_path); list_info = read_whole_file(list_file); if (list_info == NULL) { free(list_file); return -1; } free(list_file); // 3. find sh_num follow_info = strstr(list_info, "sh_num="); if (follow_info == NULL) { free(list_info); return -1; } follow_info += strlen("sh_num="); follow_info_end = follow_info; while (*follow_info_end != 0 && *follow_info_end != '\n') ++follow_info_end; if (*follow_info_end == 0) { free(list_info); return -1; } backup = *follow_info_end; *follow_info_end = 0; *sh_num = atoi(follow_info); *follow_info_end = backup; if (*sh_num <= 0) return 0; // 4. get folder list from the folder list file tmp_folder_list = (char **)malloc(sizeof(char *)*((*sh_num)+1)); if (tmp_folder_list == NULL) { free(list_info); return -1; } for (i = 0; i < *sh_num; ++i) { // 5. get folder name memset(target, 0, 16); sprintf(target, "\nsh_name%d=", i); follow_info = strstr(list_info, target); if (follow_info == NULL) { free(list_info); free_2_dimension_list(sh_num, &tmp_folder_list); return -1; } follow_info += strlen(target); follow_info_end = follow_info; while (*follow_info_end != 0 && *follow_info_end != '\n') ++follow_info_end; if (*follow_info_end == 0) { free(list_info); free_2_dimension_list(sh_num, &tmp_folder_list); return -1; } backup = *follow_info_end; *follow_info_end = 0; len = strlen(follow_info); tmp_folder_list[i] = (char *)malloc(sizeof(char)*(len+1)); if (tmp_folder_list == NULL) { *follow_info_end = backup; free(list_info); free_2_dimension_list(sh_num, &tmp_folder_list); return -1; } strcpy(tmp_folder_list[i], follow_info); tmp_folder_list[i][len] = 0; *follow_info_end = backup; } if (*sh_num > 0) *folder_list = tmp_folder_list; return 0; }
int add_folder(const char *const mount_path, const char *const folder) { int result, i, len; int acc_num = 0; char **account_list = NULL; char var_file[256]; char *target, *var_info; FILE *fp; int samba_right, ftp_right, dms_right; char *full_path; if (mount_path == NULL || strlen(mount_path) <= 0) { return -1; } if (folder == NULL || strlen(folder) <= 0) { return -1; } // test if creatting the folder result = test_if_exist_folder_in_mount_path(mount_path, folder); if (result != 0) { return -1; } // create the folder len = strlen(mount_path)+strlen("/")+strlen(folder); full_path = (char *)malloc(sizeof(char)*(len+1)); if (full_path == NULL) { return -1; } sprintf(full_path, "%s/%s", mount_path, folder); full_path[len] = 0; umask(0000); result = mkdir(full_path, 0777); free(full_path); if (result != 0) { return -1; } // get the samba right, ftp right and target samba_right = DEFAULT_SAMBA_RIGHT; ftp_right = DEFAULT_FTP_RIGHT; dms_right = DEFAULT_DMS_RIGHT; len = strlen("*")+strlen(folder)+strlen("="); target = (char *)malloc(sizeof(char)*(len+1)); if (target == NULL) { return -1; } sprintf(target, "*%s=", folder); target[len] = 0; // add folder's right to every var file get_account_list(&acc_num, &account_list); for (i = 0; i < acc_num; ++i) { // check if the created target is exist in the var file snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", mount_path, account_list[i]); var_info = read_whole_file(var_file); if (var_info == NULL) { ; } else if (strstr(var_info, target) != NULL) { free(var_info); continue; } else free(var_info); // 7. add the folder's info in the var file fp = fopen(var_file, "a+"); if (fp) { fprintf(fp, "%s%d%d%d\n", target, samba_right, ftp_right, dms_right); fclose(fp); } } free_2_dimension_list(&acc_num, &account_list); // get the var_file for anonymous ftp snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", mount_path, FTP_ANONYMOUS_USER); fp = fopen(var_file, "a+"); if (fp) { fprintf(fp, "%s%d%d%d\n", target, 1, 1, 1); fclose(fp); } free(target); // 9. add the folder's info in the folder list initial_folder_list_in_mount_path(mount_path); return 0; }
// "new_account" can be the same with "account"! int mod_account(const char *const account, const char *const new_account, const char *const new_password) { disk_info_t *disk_list, *follow_disk; partition_info_t *follow_partition; int i, acc_num = 0; char **account_list = NULL; char nvram_name[16]; int account_order = -1; char var_file[256], new_var_file[256]; if (account == NULL || strlen(account) <= 0) { return -1; } if ((new_account == NULL || strlen(new_account) <= 0) && (new_password == NULL || strlen(new_password) <= 0)) { return -1; } // 1. check if can modify the account get_account_list(&acc_num, &account_list); for (i = 0; i < acc_num; ++i) { if (new_account != NULL && strcmp(new_account, account) && strlen(new_account) > 0) { if (!strcmp(account_list[i], new_account)) { free_2_dimension_list(&acc_num, &account_list); return -1; } } if (!strcmp(account, account_list[i])) account_order = i; } free_2_dimension_list(&acc_num, &account_list); if (account_order == -1) { return -1; } if (!strcmp(new_account, FTP_ANONYMOUS_USER)) { return -1; } // 2. modify nvram value about the new account if (new_account != NULL && strcmp(new_account, account) && strlen(new_account) > 0) { sprintf(nvram_name, "acc_username%d", account_order); nvram_set(nvram_name, new_account); } if (new_password != NULL && strlen(new_password) > 0) { sprintf(nvram_name, "acc_password%d", account_order); nvram_set(nvram_name, new_password); } nvram_commit_safe(); // 3. find every pool if (new_account == NULL || strlen(new_account) <= 0/* || (new_account != NULL && !strcmp(new_account, account))*/) return 0; if ((new_account != NULL && !strcmp(new_account, account))) goto rerun; disk_list = read_disk_data(); if (!disk_list) return 0; for (follow_disk = disk_list; follow_disk != NULL; follow_disk = follow_disk->next) { for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) { if (follow_partition->mount_point == NULL) continue; // get the var_file and new_var_file snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", follow_partition->mount_point, account); snprintf(new_var_file, sizeof(new_var_file), "%s/.__%s_var.txt", follow_partition->mount_point, new_account); // rename the var file rename(var_file, new_var_file); } } rerun: // 6. re-run ftp and samba system("/sbin/run_ftpsamba"); return 0; }
int del_account(const char *const account) { disk_info_t *disk_list, *follow_disk; partition_info_t *follow_partition; int acc_num = 0, target; char **account_list = NULL; int result, i; char nvram_name[16], nvram_value[128]; char var_file[256]; if (account == NULL || strlen(account) <= 0) { return -1; } // 1. check if can create the account get_account_list(&acc_num, &account_list); if (acc_num <= 0) return -1; result = 0; for (i = 0; i < acc_num; ++i) if (!strcmp(account_list[i], account)) { result = 1; target = i; break; } if (result == 0) { free_2_dimension_list(&acc_num, &account_list); return -1; } // 2. delete the nvram value about the deleted account --acc_num; nvram_set_int("acc_num", acc_num); for (i = target; i < acc_num; ++i) { sprintf(nvram_name, "acc_username%d", i); nvram_set(nvram_name, account_list[i+1]); sprintf(nvram_value, "acc_password%d", i+1); sprintf(nvram_name, "acc_password%d", i); nvram_set(nvram_name, nvram_safe_get(nvram_value)); } // 3. change to the share mode when no account if (acc_num < 1) { int i_mode; i_mode = nvram_get_int("st_samba_mode"); if (i_mode != 1 && i_mode != 3) nvram_set_int("st_samba_mode", 1); i_mode = nvram_get_int("st_ftp_mode"); if (i_mode != 1 && i_mode != 3) nvram_set_int("st_ftp_mode", 1); } nvram_commit_safe(); free_2_dimension_list(&acc_num, &account_list); // 4. find every pool disk_list = read_disk_data(); if (!disk_list) return 0; for (follow_disk = disk_list; follow_disk != NULL; follow_disk = follow_disk->next) { for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) { if (follow_partition->mount_point == NULL) continue; // 5. delete the var file of the deleted account snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", follow_partition->mount_point, account); unlink(var_file); } } free_disk_data(disk_list); // 6. re-run ftp and samba system("/sbin/run_ftpsamba"); return 0; }
int mod_folder(const char *const mount_path, const char *const folder, const char *const new_folder) { int result, i, len; int acc_num = 0; char **account_list = NULL; char var_file[256]; char *target, *new_target, *var_info; FILE *fp; char *follow_info, backup; char *full_path, *new_full_path; if (mount_path == NULL || strlen(mount_path) <= 0) { return -1; } if (folder == NULL || strlen(folder) <= 0) { return -1; } if (new_folder == NULL || strlen(new_folder) <= 0) { return -1; } // 1. test if modifying the folder len = strlen(mount_path)+strlen("/")+strlen(folder); full_path = (char *)malloc(sizeof(char)*(len+1)); if (full_path == NULL) { return -1; } sprintf(full_path, "%s/%s", mount_path, folder); full_path[len] = 0; len = strlen(mount_path)+strlen("/")+strlen(new_folder); new_full_path = (char *)malloc(sizeof(char)*(len+1)); if (new_full_path == NULL) { return -1; } sprintf(new_full_path, "%s/%s", mount_path, new_folder); new_full_path[len] = 0; result = test_if_exist_folder_in_mount_path(mount_path, folder); if (result == 0) { result = test_if_dir(full_path); if (result != 1) { free(full_path); free(new_full_path); return -1; } // the folder is existed but not in .__folder_list.txt add_folder(mount_path, folder); } // modify the folder result = rename(full_path, new_full_path); free(full_path); free(new_full_path); if (result != 0) { return -1; } len = strlen("*")+strlen(folder)+strlen("="); target = (char *)malloc(sizeof(char)*(len+1)); if (target == NULL) { return -1; } sprintf(target, "*%s=", folder); target[len] = 0; len = strlen("*")+strlen(new_folder)+strlen("="); new_target = (char *)malloc(sizeof(char)*(len+1)); if (new_target == NULL) { free(target); return -1; } sprintf(new_target, "*%s=", new_folder); new_target[len] = 0; // 3. add folder's right to every var file get_account_list(&acc_num, &account_list); for (i = 0; i < acc_num; ++i) { // check if the created target is exist in the var file snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", mount_path, account_list[i]); var_info = read_whole_file(var_file); if (var_info == NULL) { continue; } if ((follow_info = strstr(var_info, target)) == NULL) { free(var_info); continue; } // 7. modify the folder's info in the var file fp = fopen(var_file, "w"); if (fp) { // write the info before target backup = *follow_info; *follow_info = 0; fprintf(fp, "%s", var_info); *follow_info = backup; // write the info before new_target fprintf(fp, "%s", new_target); // write the info after target follow_info += strlen(target); fprintf(fp, "%s", follow_info); fclose(fp); } free(var_info); } free_2_dimension_list(&acc_num, &account_list); // get the var_file for anonymous ftp snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", mount_path, FTP_ANONYMOUS_USER); var_info = read_whole_file(var_file); if (var_info) { if ((follow_info = strstr(var_info, target))) { fp = fopen(var_file, "w"); if (fp) { // write the info before target backup = *follow_info; *follow_info = 0; fprintf(fp, "%s", var_info); *follow_info = backup; // write the info before new_target fprintf(fp, "%s", new_target); // write the info after target follow_info += strlen(target); fprintf(fp, "%s", follow_info); fclose(fp); } } free(var_info); } free(target); free(new_target); // 9. modify the folder's info in the folder list initial_folder_list_in_mount_path(mount_path); return 0; }
int main(int argc, char *argv[]) { FILE *fp; char *nv; int n=0; char *p_computer_name = NULL; disk_info_t *follow_disk, *disks_info = NULL; partition_info_t *follow_partition; char *mount_folder; int result, node_layer, samba_right; int sh_num; char **folder_list = NULL; int acc_num; char **account_list; int dup, same_m_pt = 0; char unique_share_name[PATH_MAX]; unlink("/var/log.samba"); if ((fp=fopen(SAMBA_CONF, "r"))) { fclose(fp); unlink(SAMBA_CONF); } if((fp = fopen(SAMBA_CONF, "w")) == NULL) goto confpage; fprintf(fp, "[global]\n"); if (nvram_safe_get("st_samba_workgroup")) fprintf(fp, "workgroup = %s\n", nvram_safe_get("st_samba_workgroup")); #if 0 if (nvram_safe_get("computer_name")) { fprintf(fp, "netbios name = %s\n", nvram_safe_get("computer_name")); fprintf(fp, "server string = %s\n", nvram_safe_get("computer_name")); } #else p_computer_name = nvram_get("computer_name") && is_valid_netbios_name(nvram_get("computer_name")) ? nvram_get("computer_name") : get_productid(); if (p_computer_name) { fprintf(fp, "netbios name = %s\n", p_computer_name); fprintf(fp, "server string = %s\n", p_computer_name); } #endif fprintf(fp, "unix charset = UTF8\n"); // ASUS add fprintf(fp, "display charset = UTF8\n"); // ASUS add fprintf(fp, "log file = /var/log.samba\n"); fprintf(fp, "log level = 0\n"); fprintf(fp, "max log size = 5\n"); // account mode if(nvram_match("st_samba_mode", "2") || nvram_match("st_samba_mode", "4") || (nvram_match("st_samba_mode", "1") && nvram_get("st_samba_force_mode") == NULL) ){ fprintf(fp, "security = USER\n"); fprintf(fp, "guest ok = no\n"); fprintf(fp, "map to guest = Bad User\n"); } // share mode else if (nvram_match("st_samba_mode", "1") || nvram_match("st_samba_mode", "3")) { #if 0 //#if defined(RTCONFIG_TFAT) || defined(RTCONFIG_TUXERA_NTFS) || defined(RTCONFIG_TUXERA_HFS) if(nvram_get_int("enable_samba_tuxera") == 1){ fprintf(fp, "auth methods = guest\n"); fprintf(fp, "guest account = admin\n"); fprintf(fp, "map to guest = Bad Password\n"); fprintf(fp, "guest ok = yes\n"); } else{ fprintf(fp, "security = SHARE\n"); fprintf(fp, "guest only = yes\n"); } #else fprintf(fp, "security = SHARE\n"); fprintf(fp, "guest only = yes\n"); #endif } else{ usb_dbg("samba mode: no\n"); goto confpage; } fprintf(fp, "encrypt passwords = yes\n"); fprintf(fp, "pam password change = no\n"); fprintf(fp, "null passwords = yes\n"); // ASUS add #ifdef RTCONFIG_SAMBA_MODERN if (nvram_get_int("smbd_enable_smb2")) fprintf(fp, "max protocol = SMB2\n"); else fprintf(fp, "max protocol = NT1\n"); fprintf(fp, "passdb backend = smbpasswd\n"); fprintf(fp, "smb encrypt = disabled\n"); fprintf(fp, "smb passwd file = /etc/samba/smbpasswd\n"); #endif #if 0 #ifdef RTCONFIG_RECVFILE fprintf(fp, "use recvfile = yes\n"); #endif #endif fprintf(fp, "force directory mode = 0777\n"); fprintf(fp, "force create mode = 0777\n"); /* max users */ if (strcmp(nvram_safe_get("st_max_user"), "") != 0) fprintf(fp, "max connections = %s\n", nvram_safe_get("st_max_user")); /* remove socket options due to NIC compatible issue */ if(!nvram_get_int("stop_samba_speedup")){ #ifdef RTCONFIG_BCMARM #ifdef RTCONFIG_BCM_7114 fprintf(fp, "socket options = IPTOS_LOWDELAY TCP_NODELAY SO_RCVBUF=131072 SO_SNDBUF=131072\n"); #endif #else fprintf(fp, "socket options = TCP_NODELAY SO_KEEPALIVE SO_RCVBUF=65536 SO_SNDBUF=65536\n"); #endif } fprintf(fp, "obey pam restrictions = no\n"); fprintf(fp, "use spnego = no\n"); // ASUS add fprintf(fp, "client use spnego = no\n"); // ASUS add // fprintf(fp, "client use spnego = yes\n"); // ASUS add fprintf(fp, "disable spoolss = yes\n"); // ASUS add fprintf(fp, "host msdfs = no\n"); // ASUS add fprintf(fp, "strict allocate = No\n"); // ASUS add // fprintf(fp, "mangling method = hash2\n"); // ASUS add fprintf(fp, "bind interfaces only = yes\n"); // ASUS add #ifndef RTCONFIG_BCMARM fprintf(fp, "interfaces = lo br0 %s\n", (is_routing_enabled() && nvram_get_int("smbd_wanac")) ? nvram_safe_get("wan0_ifname") : ""); #else fprintf(fp, "interfaces = br0 %s/%s %s\n", nvram_safe_get("lan_ipaddr"), nvram_safe_get("lan_netmask"), (is_routing_enabled() && nvram_get_int("smbd_wanac")) ? nvram_safe_get("wan0_ifname") : ""); #endif #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36) fprintf(fp, "use sendfile = no\n"); #else fprintf(fp, "use sendfile = yes\n"); #endif if (!strcmp(nvram_safe_get("smbd_wins"), "1")) { fprintf(fp, "wins support = yes\n"); } if (!strcmp(nvram_safe_get("smbd_master"), "1")) { fprintf(fp, "os level = 255\n"); fprintf(fp, "domain master = yes\n"); fprintf(fp, "local master = yes\n"); fprintf(fp, "preferred master = yes\n"); } fprintf(fp, "map archive = no\n"); fprintf(fp, "map hidden = no\n"); fprintf(fp, "map read only = no\n"); fprintf(fp, "map system = no\n"); fprintf(fp, "store dos attributes = yes\n"); fprintf(fp, "dos filemode = yes\n"); fprintf(fp, "oplocks = yes\n"); fprintf(fp, "level2 oplocks = yes\n"); fprintf(fp, "kernel oplocks = no\n"); fprintf(fp, "wide links = no\n"); // If we only want name services then skip share definition if (nvram_match("enable_samba", "0")) goto confpage; disks_info = read_disk_data(); if (disks_info == NULL) { usb_dbg("Couldn't get disk list when writing smb.conf!\n"); goto confpage; } /* share */ if (nvram_match("st_samba_mode", "0") || !strcmp(nvram_safe_get("st_samba_mode"), "")) { ; } else if (nvram_match("st_samba_mode", "1") && nvram_match("st_samba_force_mode", "1")) { usb_dbg("samba mode: share\n"); for (follow_disk = disks_info; follow_disk != NULL; follow_disk = follow_disk->next) { for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) { if (follow_partition->mount_point == NULL) continue; strcpy(unique_share_name, follow_partition->mount_point); do { dup = check_mount_point_icase(disks_info, follow_partition, follow_disk, follow_partition->partition_order, unique_share_name); if (dup) sprintf(unique_share_name, "%s(%d)", follow_partition->mount_point, ++same_m_pt); } while (dup); mount_folder = strrchr(unique_share_name, '/')+1; fprintf(fp, "[%s]\n", mount_folder); fprintf(fp, "comment = %s's %s\n", follow_disk->tag, mount_folder); fprintf(fp, "veto files = /.__*.txt*/asus_lighttpdpasswd/\n"); fprintf(fp, "path = %s\n", follow_partition->mount_point); fprintf(fp, "writeable = yes\n"); fprintf(fp, "dos filetimes = yes\n"); fprintf(fp, "fake directory create times = yes\n"); } } } else if (nvram_match("st_samba_mode", "2")) { usb_dbg("samba mode: share\n"); for (follow_disk = disks_info; follow_disk != NULL; follow_disk = follow_disk->next) { for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) { if (follow_partition->mount_point == NULL) continue; strcpy(unique_share_name, follow_partition->mount_point); do { dup = check_mount_point_icase(disks_info, follow_partition, follow_disk, follow_partition->partition_order, unique_share_name); if (dup) sprintf(unique_share_name, "%s(%d)", follow_partition->mount_point, ++same_m_pt); } while (dup); mount_folder = strrchr(unique_share_name, '/')+1; node_layer = get_permission(NULL, follow_partition->mount_point, NULL, "cifs"); if(node_layer == 3){ fprintf(fp, "[%s]\n", mount_folder); fprintf(fp, "comment = %s's %s\n", follow_disk->tag, mount_folder); fprintf(fp, "path = %s\n", follow_partition->mount_point); fprintf(fp, "writeable = yes\n"); fprintf(fp, "dos filetimes = yes\n"); fprintf(fp, "fake directory create times = yes\n"); } else{ //result = get_all_folder(follow_partition->mount_point, &sh_num, &folder_list); result = get_folder_list(follow_partition->mount_point, &sh_num, &folder_list); if (result < 0){ free_2_dimension_list(&sh_num, &folder_list); continue; } for (n = 0; n < sh_num; ++n){ samba_right = get_permission(NULL, follow_partition->mount_point, folder_list[n], "cifs"); if (samba_right < 0 || samba_right > 3) samba_right = DEFAULT_SAMBA_RIGHT; if(samba_right > 0){ int count = get_list_strings_count(folder_list, sh_num, folder_list[n]); if ((!strcmp(nvram_safe_get("smbd_simpler_naming"), "1")) && (count <= 1)) { fprintf(fp, "[%s]\n", folder_list[n]); } else { fprintf(fp, "[%s (at %s)]\n", folder_list[n], mount_folder); } fprintf(fp, "comment = %s's %s in %s\n", mount_folder, folder_list[n], follow_disk->tag); fprintf(fp, "path = %s/%s\n", follow_partition->mount_point, folder_list[n]); if(samba_right == 3) fprintf(fp, "writeable = yes\n"); else fprintf(fp, "writeable = no\n"); fprintf(fp, "dos filetimes = yes\n"); fprintf(fp, "fake directory create times = yes\n"); } } free_2_dimension_list(&sh_num, &folder_list); } } } } else if (nvram_match("st_samba_mode", "3")) { usb_dbg("samba mode: user\n"); // get the account list if (get_account_list(&acc_num, &account_list) < 0) { usb_dbg("Can't read the account list.\n"); free_2_dimension_list(&acc_num, &account_list); goto confpage; } for (follow_disk = disks_info; follow_disk != NULL; follow_disk = follow_disk->next) { for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) { if (follow_partition->mount_point == NULL) continue; mount_folder = strrchr(follow_partition->mount_point, '/')+1; // 1. get the folder list if (get_folder_list(follow_partition->mount_point, &sh_num, &folder_list) < 0) { free_2_dimension_list(&sh_num, &folder_list); } // 2. start to get every share for (n = -1; n < sh_num; ++n) { int i, first; if(n == -1){ fprintf(fp, "[%s]\n", mount_folder); fprintf(fp, "comment = %s's %s\n", follow_disk->tag, mount_folder); fprintf(fp, "path = %s\n", follow_partition->mount_point); } else{ int count = get_list_strings_count(folder_list, sh_num, folder_list[n]); if ((!strcmp(nvram_safe_get("smbd_simpler_naming"), "1")) && (count <= 1)) { fprintf(fp, "[%s]\n", folder_list[n]); } else { fprintf(fp, "[%s (at %s)]\n", folder_list[n], mount_folder); } fprintf(fp, "comment = %s's %s in %s\n", mount_folder, folder_list[n], follow_disk->tag); fprintf(fp, "path = %s/%s\n", follow_partition->mount_point, folder_list[n]); } fprintf(fp, "dos filetimes = yes\n"); fprintf(fp, "fake directory create times = yes\n"); fprintf(fp, "valid users = "); first = 1; for (i = 0; i < acc_num; ++i) { if(n == -1) samba_right = get_permission(account_list[i], follow_partition->mount_point, NULL, "cifs"); else samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); fprintf(fp, "invalid users = "); first = 1; for (i = 0; i < acc_num; ++i) { if(n == -1) samba_right = get_permission(account_list[i], follow_partition->mount_point, NULL, "cifs"); else samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (samba_right >= 1) continue; if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); fprintf(fp, "read list = "); first = 1; for (i = 0; i < acc_num; ++i) { if(n == -1) samba_right = get_permission(account_list[i], follow_partition->mount_point, NULL, "cifs"); else samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (samba_right < 1) continue; if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); fprintf(fp, "write list = "); first = 1; for (i = 0; i < acc_num; ++i) { if(n == -1) samba_right = get_permission(account_list[i], follow_partition->mount_point, NULL, "cifs"); else samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (samba_right < 2) continue; if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); } free_2_dimension_list(&sh_num, &folder_list); } } free_2_dimension_list(&acc_num, &account_list); } else if (nvram_match("st_samba_mode", "4") || (nvram_match("st_samba_mode", "1") && nvram_get("st_samba_force_mode") == NULL) ) { usb_dbg("samba mode: user\n"); // get the account list if (get_account_list(&acc_num, &account_list) < 0) { usb_dbg("Can't read the account list.\n"); free_2_dimension_list(&acc_num, &account_list); goto confpage; } for (follow_disk = disks_info; follow_disk != NULL; follow_disk = follow_disk->next) { for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) { if (follow_partition->mount_point == NULL) continue; mount_folder = strrchr(follow_partition->mount_point, '/')+1; // 1. get the folder list if (get_folder_list(follow_partition->mount_point, &sh_num, &folder_list) < 0) { free_2_dimension_list(&sh_num, &folder_list); } // 2. start to get every share for (n = 0; n < sh_num; ++n) { int i, first; int count = get_list_strings_count(folder_list, sh_num, folder_list[n]); if ((!strcmp(nvram_safe_get("smbd_simpler_naming"), "1")) && (count <= 1)) { fprintf(fp, "[%s]\n", folder_list[n]); } else { fprintf(fp, "[%s (at %s)]\n", folder_list[n], mount_folder); } fprintf(fp, "comment = %s's %s in %s\n", mount_folder, folder_list[n], follow_disk->tag); fprintf(fp, "path = %s/%s\n", follow_partition->mount_point, folder_list[n]); fprintf(fp, "dos filetimes = yes\n"); fprintf(fp, "fake directory create times = yes\n"); fprintf(fp, "valid users = "); first = 1; for (i = 0; i < acc_num; ++i) { if(n == -1) samba_right = get_permission(account_list[i], follow_partition->mount_point, NULL, "cifs"); else samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); fprintf(fp, "invalid users = "); first = 1; for (i = 0; i < acc_num; ++i) { samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (samba_right >= 1) continue; if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); fprintf(fp, "read list = "); first = 1; for (i = 0; i < acc_num; ++i) { samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (samba_right < 1) continue; if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); fprintf(fp, "write list = "); first = 1; for (i = 0; i < acc_num; ++i) { samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (samba_right < 2) continue; if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); } free_2_dimension_list(&sh_num, &folder_list); } } free_2_dimension_list(&acc_num, &account_list); } confpage: if(fp != NULL) { append_custom_config("smb.conf", fp); fclose(fp); use_custom_config("smb.conf", SAMBA_CONF); run_postconf("smb", SAMBA_CONF); } free_disk_data(&disks_info); return 0; }
int write_smb_conf(void) { FILE *fp; int i_maxuser, i_smb_mode; disk_info_t *follow_disk, *disks_info = NULL; partition_info_t *follow_partition; i_smb_mode = nvram_get_int("st_samba_mode"); fp = write_smb_conf_header(); if (!fp) return -1; /* share mode */ if (i_smb_mode == 1 || i_smb_mode == 3) { char *rootnm = nvram_safe_get("http_username"); if (!(*rootnm)) rootnm = "admin"; fprintf(fp, "security = %s\n", "SHARE"); fprintf(fp, "guest ok = %s\n", "yes"); fprintf(fp, "guest only = yes\n"); fprintf(fp, "guest account = %s\n", rootnm); } else if (i_smb_mode == 4) { fprintf(fp, "security = %s\n", "USER"); fprintf(fp, "guest ok = %s\n", "no"); fprintf(fp, "map to guest = Bad User\n"); fprintf(fp, "hide unreadable = yes\n"); } else { goto confpage; } fprintf(fp, "writeable = yes\n"); fprintf(fp, "directory mode = 0777\n"); fprintf(fp, "create mask = 0777\n"); fprintf(fp, "force directory mode = 0777\n"); /* max users */ i_maxuser = nvram_get_int("st_max_user"); if (i_maxuser < 1) i_maxuser = 1; if (i_maxuser > MAX_CLIENTS_NUM) i_maxuser = MAX_CLIENTS_NUM; fprintf(fp, "max connections = %d\n", i_maxuser); fprintf(fp, "use spnego = no\n"); // ASUS add fprintf(fp, "client use spnego = no\n"); // ASUS add fprintf(fp, "null passwords = yes\n"); // ASUS add fprintf(fp, "strict allocate = no\n"); // ASUS add fprintf(fp, "use sendfile = yes\n"); fprintf(fp, "dos filemode = yes\n"); fprintf(fp, "dos filetimes = yes\n"); fprintf(fp, "dos filetime resolution = yes\n"); fprintf(fp, "\n"); disks_info = read_disk_data(); if (!disks_info) { goto confpage; } /* share */ if (i_smb_mode == 1) { for (follow_disk = disks_info; follow_disk != NULL; follow_disk = follow_disk->next) { for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) { char *mount_folder; if (follow_partition->mount_point == NULL) continue; mount_folder = strrchr(follow_partition->mount_point, '/')+1; fprintf(fp, "[%s]\n", mount_folder); fprintf(fp, "comment = %s's %s\n", follow_disk->tag, mount_folder); fprintf(fp, "path = %s\n", follow_partition->mount_point); fprintf(fp, "guest ok = yes\n"); fprintf(fp, "writeable = yes\n"); fprintf(fp, "directory mode = 0777\n"); fprintf(fp, "create mask = 0777\n"); fprintf(fp, "map archive = no\n"); fprintf(fp, "map hidden = no\n"); fprintf(fp, "map read only = no\n"); fprintf(fp, "map system = no\n"); fprintf(fp, "store dos attributes = yes\n\n"); } } } else if (i_smb_mode == 3) { fprintf(fp, "[%s]\n", "Media"); fprintf(fp, "comment = %s\n", "Root share for all media"); fprintf(fp, "path = %s\n", POOL_MOUNT_ROOT); fprintf(fp, "guest ok = yes\n"); fprintf(fp, "writeable = yes\n"); fprintf(fp, "directory mode = 0777\n"); fprintf(fp, "create mask = 0777\n"); fprintf(fp, "map archive = no\n"); fprintf(fp, "map hidden = no\n"); fprintf(fp, "map read only = no\n"); fprintf(fp, "map system = no\n"); fprintf(fp, "store dos attributes = yes\n\n"); } else { int n, acc_num = 0, sh_num=0; char **account_list; // get the account list if (get_account_list(&acc_num, &account_list) < 0) { usb_dbg("Can't read the account list.\n"); free_2_dimension_list(&acc_num, &account_list); goto confpage; } for (follow_disk = disks_info; follow_disk != NULL; follow_disk = follow_disk->next) { for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) { if (follow_partition->mount_point == NULL) continue; char **folder_list; // 1. get the folder list if (get_folder_list_in_mount_path(follow_partition->mount_point, &sh_num, &folder_list) < 0) { usb_dbg("Can't read the folder list in %s.\n", follow_partition->mount_point); free_2_dimension_list(&sh_num, &folder_list); continue; } // 2. start to get every share for (n = 0; n < sh_num; ++n) { int i, right, first; char share[256]; memset(share, 0, 256); strcpy(share, folder_list[n]); fclose(fp); if(check_existed_share(share)){ i = 1; memset(share, 0, 256); sprintf(share, "%s(%d)", folder_list[n], i); while(check_existed_share(share)){ ++i; memset(share, 0, 256); sprintf(share, "%s(%d)", folder_list[n], i); } } if((fp = fopen(SAMBA_CONF, "a")) == NULL) goto confpage; fprintf(fp, "[%s]\n", share); fprintf(fp, "comment = %s\n", folder_list[n]); fprintf(fp, "path = %s/%s\n", follow_partition->mount_point, folder_list[n]); fprintf(fp, "writeable = no\n"); fprintf(fp, "valid users = "); first = 1; for (i = 0; i < acc_num; ++i) { right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); fprintf(fp, "invalid users = "); first = 1; for (i = 0; i < acc_num; ++i) { right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (right >= 1) continue; if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); fprintf(fp, "read list = "); first = 1; for (i = 0; i < acc_num; ++i) { right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (right < 1) continue; if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); fprintf(fp, "write list = "); first = 1; for (i = 0; i < acc_num; ++i) { right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (right < 2) continue; if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); } free_2_dimension_list(&sh_num, &folder_list); } } free_2_dimension_list(&acc_num, &account_list); } confpage: fclose(fp); free_disk_data(disks_info); return 0; }
int modify_if_exist_new_folder(const char *const account, const char *const mount_path) { int sh_num = 0; char **folder_list = NULL, *target; int result, i, len; char var_file[256]; FILE *fp; int samba_right, ftp_right, dms_right; // 1. get all folder in mount_path result = get_all_folder_in_mount_path(mount_path, &sh_num, &folder_list); if (result != 0) { free_2_dimension_list(&sh_num, &folder_list); return -1; } for (i = 0; i < sh_num; ++i) { result = test_if_exist_folder_in_mount_path(mount_path, folder_list[i]); if (result != 0) { continue; } // get the target len = strlen("*")+strlen(folder_list[i])+strlen("="); target = (char *)malloc(sizeof(char)*(len+1)); if (target == NULL) { free_2_dimension_list(&sh_num, &folder_list); return -1; } sprintf(target, "*%s=", folder_list[i]); target[len] = 0; // get the samba right and ftp right if (strcmp(account, FTP_ANONYMOUS_USER) == 0) { samba_right = 1; ftp_right = 1; dms_right = 1; } else { samba_right = DEFAULT_SAMBA_RIGHT; ftp_right = DEFAULT_FTP_RIGHT; dms_right = DEFAULT_DMS_RIGHT; } // add the information of the new folder snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", mount_path, account); fp = fopen(var_file, "a+"); if (fp == NULL) { free_2_dimension_list(&sh_num, &folder_list); free(target); return -1; } fprintf(fp, "%s%d%d%d\n", target, samba_right, ftp_right, dms_right); free(target); fclose(fp); } free_2_dimension_list(&sh_num, &folder_list); return 0; }
int main(int argc, char *argv[]){ char *command; int i, num; char **list; int layer, right; char *mount_path, *share; int result; if((command = rindex(argv[0], '/')) != NULL) ++command; else command = argv[0]; if(!strcmp(command, "get_account_list")){ if(get_account_list(&num, &list) <= 0) usb_dbg("Can't get account list.\n"); else{ for(i = 0; i < num; ++i) usb_dbg("%dth account: %s.\n", i+1, list[i]); free_2_dimension_list(&num, &list); } } else if(!strcmp(command, "get_folder_list")){ if(argc != 2) usb_dbg("Usage: get_folder_list mount_path\n"); else if(get_folder_list(argv[1], &num, &list) < 0) usb_dbg("Can't get folder list.\n"); else{ for(i = 0; i < num; ++i) usb_dbg("%dth folder: %s.\n", i+1, list[i]); free_2_dimension_list(&num, &list); } } else if(!strcmp(command, "get_all_folder")){ if(argc != 2) usb_dbg("Usage: get_all_folder mount_path\n"); else if(get_all_folder(argv[1], &num, &list) < 0) usb_dbg("Can't get all folder.\n"); else{ for(i = 0; i < num; ++i) usb_dbg("%dth folder: %s.\n", i+1, list[i]); free_2_dimension_list(&num, &list); } } else if(!strcmp(command, "get_var_file_name")){ char *file_name; if(argc != 3) usb_dbg("Usage: get_var_file_name account mount_path\n"); else if(get_var_file_name(argv[1], argv[2], &file_name)) usb_dbg("Can't get the var file name with %s in %s.\n", argv[1], argv[2]); else{ usb_dbg("done: %s.\n", file_name); free(file_name); } } else if(!strcmp(command, "initial_folder_list")){ if(argc != 2) usb_dbg("Usage: initial_folder_list mount_path\n"); else if(initial_folder_list(argv[1]) < 0) usb_dbg("Can't initial folder list in %s.\n", argv[1]); else usb_dbg("done.\n"); } else if(!strcmp(command, "initial_var_file")){ char *target_acc = NULL; if(argc != 3){ usb_dbg("Usage: initial_var_file account mount_path\n"); return -1; } if(strcmp(argv[1], "NULL")) target_acc = argv[1]; if(initial_var_file(target_acc, argv[2]) < 0){ if(target_acc == NULL) usb_dbg("Can't initial share's var file in %s.\n", argv[2]); else usb_dbg("Can't initial %s's var file in %s.\n", target_acc, argv[2]); } else usb_dbg("done.\n"); } else if(!strcmp(command, "initial_all_var_file")){ if(argc != 2) usb_dbg("Usage: initial_all_var_file mount_path\n"); else if(initial_all_var_file(argv[1]) < 0) usb_dbg("Can't initial all var file in %s.\n", argv[1]); else usb_dbg("done.\n"); } else if(!strcmp(command, "test_of_var_files")){ if(argc != 2) usb_dbg("Usage: test_of_var_files mount_path\n"); else if(test_of_var_files(argv[1]) < 0) usb_dbg("Can't test_of_var_files in %s.\n", argv[1]); else usb_dbg("done.\n"); } else if(!strcmp(command, "create_if_no_var_files")){ if(argc != 2) usb_dbg("Usage: create_if_no_var_files mount_path\n"); else if(create_if_no_var_files(argv[1]) < 0) usb_dbg("Can't create var files in %s.\n", argv[1]); else usb_dbg("done.\n"); } else if(!strcmp(command, "modify_if_exist_new_folder")){ if(argc != 3) usb_dbg("Usage: modify_if_exist_new_folder account mount_path\n"); else if(modify_if_exist_new_folder(argv[1], argv[2]) < 0) usb_dbg("Can't fix %s's var files in %s.\n", argv[1], argv[2]); else usb_dbg("done.\n"); } else if(!strcmp(command, "get_permission")){ if(argc != 5) usb_dbg("Usage: get_permission account mount_path folder [cifs|ftp|dms]\n"); else if((right = get_permission(argv[1], argv[2], argv[3], argv[4])) < 0) usb_dbg("%s can't get %s's %s permission in %s.\n", argv[1], argv[3], argv[4], argv[2]); else usb_dbg("done: %d.\n", right); } else if(!strcmp(command, "set_permission")){ if(argc != 6) usb_dbg("Usage: set_permission account mount_path folder [cifs|ftp|dms] [0~3]\n"); else if(set_permission(argv[1], argv[2], argv[3], argv[4], atoi(argv[5])) < 0) usb_dbg("%s can't set %s's %s permission to be %s in %s.\n", argv[1], argv[3], argv[4], argv[5], argv[2]); else usb_dbg("done.\n"); } else if(!strcmp(command, "add_account")){ if(argc != 3) usb_dbg("Usage: add_account account password\n"); else if(add_account(argv[1], argv[2]) < 0) usb_dbg("Can't add account(%s:%s).\n", argv[1], argv[2]); else usb_dbg("done.\n"); } else if(!strcmp(command, "del_account")){ if(argc != 2) usb_dbg("Usage: del_account account\n"); else if(del_account(argv[1]) < 0) usb_dbg("Can't del account(%s).\n", argv[1]); else usb_dbg("done.\n"); } else if(!strcmp(command, "mod_account")){ if(argc != 4) usb_dbg("Usage: mod_account account new_account new_password\n"); else if(mod_account(argv[1], argv[2], argv[3]) < 0) usb_dbg("Can't mod account(%s) to (%s:%s).\n", argv[1], argv[2], argv[3]); else usb_dbg("done.\n"); } else if(!strcmp(command, "test_if_exist_account")){ if(argc != 2) usb_dbg("Usage: test_if_exist_account account\n"); else if(test_if_exist_account(argv[1]) < 0) usb_dbg("Can't test if %s is existed.\n", argv[1]); else usb_dbg("done.\n"); } else if(!strcmp(command, "add_folder")){ if(argc != 4){ usb_dbg("Usage: add_folder account mount_path folder\n"); return 0; } if(!strcmp(argv[1], "NULL")) result = add_folder(NULL, argv[2], argv[3]); else result = add_folder(argv[1], argv[2], argv[3]); if(result < 0) usb_dbg("Can't add folder(%s) in %s.\n", argv[3], argv[2]); else usb_dbg("done.\n"); } else if(!strcmp(command, "del_folder")){ if(argc != 3) usb_dbg("Usage: del_folder mount_path folder\n"); else if(del_folder(argv[1], argv[2]) < 0) usb_dbg("Can't del folder(%s) in %s.\n", argv[2], argv[1]); else usb_dbg("done.\n"); } else if(!strcmp(command, "mod_folder")){ if(argc != 4) usb_dbg("Usage: mod_folder mount_path folder new_folder\n"); else if(mod_folder(argv[1], argv[2], argv[3]) < 0) usb_dbg("Can't mod folder(%s) to (%s) in %s.\n", argv[2], argv[3], argv[1]); else usb_dbg("done.\n"); } else if(!strcmp(command, "test_if_exist_share")){ if(argc != 3) usb_dbg("Usage: test_if_exist_share mount_path folder\n"); else if(test_if_exist_share(argv[1], argv[2])) usb_dbg("%s is existed in %s.\n", argv[2], argv[1]); else usb_dbg("%s is NOT existed in %s.\n", argv[2], argv[1]); } else if(!strcmp(command, "how_many_layer")){ if(argc != 2) usb_dbg("Usage: how_many_layer path\n"); else if((layer = how_many_layer(argv[1], &mount_path, &share)) < 0) usb_dbg("Can't count the layer with %s.\n", argv[1]); else usb_dbg("done: %d layers, share=%s, mount_path=%s.\n", layer, share, mount_path); } else usb_dbg("test_share(ver. %d): Need to link the command name from test_share first.\n", VERSION); return 0; }
int add_account(const char *const account, const char *const password) { disk_info_t *disk_list, *follow_disk; partition_info_t *follow_partition; int i, acc_num = 0; char **account_list = NULL; char nvram_name[32]; if (account == NULL || strlen(account) <= 0) { return -1; } if (password == NULL || strlen(password) <= 0) { return -1; } // 1. check if can create the account get_account_list(&acc_num, &account_list); if (acc_num >= MAX_ACCOUNT_NUM) { free_2_dimension_list(&acc_num, &account_list); return -1; } for (i = 0; i < acc_num; ++i) if (!strcmp(account, account_list[i])) { free_2_dimension_list(&acc_num, &account_list); return -1; } if (!strcmp(account, FTP_ANONYMOUS_USER)) { free_2_dimension_list(&acc_num, &account_list); return -1; } // 2. create nvram value about the new account nvram_set_int("acc_num", acc_num+1); sprintf(nvram_name, "acc_username%d", acc_num); nvram_set(nvram_name, account); sprintf(nvram_name, "acc_password%d", acc_num); nvram_set(nvram_name, password); nvram_commit_safe(); free_2_dimension_list(&acc_num, &account_list); // 3. find every pool disk_list = read_disk_data(); if (!disk_list) return 0; for (follow_disk = disk_list; follow_disk != NULL; follow_disk = follow_disk->next) { for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) { if (follow_partition->mount_point == NULL) continue; // 4. initial the var file of the account initial_one_var_file_in_mount_path(account, follow_partition->mount_point); } } free_disk_data(disk_list); // 6. re-run samba system("/sbin/run_samba"); return 0; }
int main(int argc, char *argv[]) { FILE *fp; int n=0; char *p_computer_name = NULL; disk_info_t *follow_disk, *disks_info = NULL; partition_info_t *follow_partition; char *mount_folder; int result, node_layer, samba_right; int sh_num; char **folder_list = NULL; int acc_num; char **account_list; char tmp[128]; unlink("/var/log.samba"); if ((fp=fopen(SAMBA_CONF, "r"))) { fclose(fp); unlink(SAMBA_CONF); } if((fp = fopen(SAMBA_CONF, "w")) == NULL) goto confpage; fprintf(fp, "[global]\n"); //if (nvram_safe_get("st_samba_workgroup")) if(!tcapi_get("Samba_Entry", "st_samba_workgroup", tmp)) //fprintf(fp, "workgroup = %s\n", nvram_safe_get("st_samba_workgroup")); fprintf(fp, "workgroup = %s\n", tmp); #if 0 if (nvram_safe_get("computer_name")) { fprintf(fp, "netbios name = %s\n", nvram_safe_get("computer_name")); fprintf(fp, "server string = %s\n", nvram_safe_get("computer_name")); } #else //p_computer_name = nvram_get("computer_name") && is_valid_hostname(nvram_get("computer_name")) ? nvram_get("computer_name") : nvram_safe_get("productid"); tcapi_get("Samba_Entry", "NetBiosName", tmp); //check NetBiosName in web page p_computer_name = tmp; if (p_computer_name) { fprintf(fp, "netbios name = %s\n", p_computer_name); fprintf(fp, "server string = %s\n", p_computer_name); } #endif fprintf(fp, "unix charset = UTF8\n"); // ASUS add fprintf(fp, "display charset = UTF8\n"); // ASUS add fprintf(fp, "log file = /var/log.samba\n"); fprintf(fp, "log level = 0\n"); fprintf(fp, "max log size = 5\n"); /* share mode */ tcapi_get("Samba_Entry", "st_samba_mode", tmp); if (!strcmp(tmp, "1") || !strcmp(tmp, "3")) { //if (!strcmp(nvram_safe_get("st_samba_mode"), "1") || !strcmp(nvram_safe_get("st_samba_mode"), "3")) { fprintf(fp, "security = SHARE\n"); fprintf(fp, "guest only = yes\n"); } //else if (!strcmp(nvram_safe_get("st_samba_mode"), "2") || !strcmp(nvram_safe_get("st_samba_mode"), "4")) { else if (!strcmp(tmp, "2") || !strcmp(tmp, "4")) { fprintf(fp, "security = USER\n"); fprintf(fp, "guest ok = no\n"); fprintf(fp, "map to guest = Bad User\n"); } else{ usb_dbg("samba mode: no\n"); goto confpage; } fprintf(fp, "encrypt passwords = yes\n"); fprintf(fp, "pam password change = no\n"); fprintf(fp, "null passwords = yes\n"); // ASUS add fprintf(fp, "force directory mode = 0777\n"); fprintf(fp, "force create mode = 0777\n"); /* max users */ if (!tcapi_get("Samba_Entry", "st_max_user", tmp)) fprintf(fp, "max connections = %s\n", tmp); //if (strcmp(nvram_safe_get("st_max_user"), "") != 0) //fprintf(fp, "max connections = %s\n", nvram_safe_get("st_max_user")); fprintf(fp, "socket options = TCP_NODELAY SO_KEEPALIVE SO_RCVBUF=65536 SO_SNDBUF=65536\n"); fprintf(fp, "use sendfile = no\n"); fprintf(fp, "obey pam restrictions = no\n"); fprintf(fp, "use spne go = no\n"); // ASUS add fprintf(fp, "client use spnego = no\n"); // ASUS add fprintf(fp, "disable spoolss = yes\n"); // ASUS add fprintf(fp, "host msdfs = no\n"); // ASUS add fprintf(fp, "strict allocate = No\n"); // ASUS add // fprintf(fp, "mangling method = hash2\n"); // ASUS add fprintf(fp, "bind interfaces only = yes\n"); // ASUS add fprintf(fp, "interfaces = lo br0\n"); //fprintf(fp, "interfaces = lo br0 %s\n", (!nvram_match("sw_mode", "3") ? nvram_safe_get("wan0_ifname") : "")); // fprintf(fp, "dns proxy = no\n"); // J-- // fprintf(fp, "domain master = no\n"); // J++ // fprintf(fp, "wins support = no\n"); // J++ // fprintf(fp, "printable = no\n"); // J++ // fprintf(fp, "browseable = yes\n"); // J++ // fprintf(fp, "security mask = 0777\n"); // J++ // fprintf(fp, "force security mode = 0\n"); // J++ // fprintf(fp, "directory security mask = 0777\n"); // J++ // fprintf(fp, "force directory security mode = 0\n"); // J++ fprintf(fp, "map archive = no\n"); fprintf(fp, "map hidden = no\n"); fprintf(fp, "map read only = no\n"); fprintf(fp, "map system = no\n"); fprintf(fp, "store dos attributes = yes\n"); fprintf(fp, "dos filemode = yes\n"); fprintf(fp, "dos filetimes = yes\n"); fprintf(fp, "dos filetime resolution = yes\n"); disks_info = read_disk_data(); if (disks_info == NULL) { usb_dbg("Couldn't get disk list when writing smb.conf!\n"); goto confpage; } /* share */ tcapi_get("Samba_Entry", "st_samba_mode", tmp); if (!strcmp(tmp, "0")) { //if (!strcmp(nvram_safe_get("st_samba_mode"), "0") || !strcmp(nvram_safe_get("st_samba_mode"), "")) { ; } else if (!strcmp(tmp, "1")) { //else if (!strcmp(nvram_safe_get("st_samba_mode"), "1")) { usb_dbg("samba mode: share\n"); for (follow_disk = disks_info; follow_disk != NULL; follow_disk = follow_disk->next) { for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) { if (follow_partition->mount_point == NULL) continue; mount_folder = strrchr(follow_partition->mount_point, '/')+1; fprintf(fp, "[%s]\n", mount_folder); fprintf(fp, "comment = %s's %s\n", follow_disk->tag, mount_folder); fprintf(fp, "veto files = /.__*.txt*/asusware*/asus_lighttpdpasswd/\n"); fprintf(fp, "path = %s\n", follow_partition->mount_point); fprintf(fp, "writeable = yes\n"); } } } else if (!strcmp(tmp, "2")) { //else if (!strcmp(nvram_safe_get("st_samba_mode"), "2")) { usb_dbg("samba mode: share\n"); for (follow_disk = disks_info; follow_disk != NULL; follow_disk = follow_disk->next) { for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) { if (follow_partition->mount_point == NULL) continue; mount_folder = strrchr(follow_partition->mount_point, '/')+1; node_layer = get_permission(NULL, follow_partition->mount_point, NULL, "cifs"); if(node_layer == 3){ fprintf(fp, "[%s]\n", mount_folder); fprintf(fp, "comment = %s's %s\n", follow_disk->tag, mount_folder); fprintf(fp, "path = %s\n", follow_partition->mount_point); fprintf(fp, "writeable = yes\n"); } else{ //result = get_all_folder(follow_partition->mount_point, &sh_num, &folder_list); result = get_folder_list(follow_partition->mount_point, &sh_num, &folder_list); if (result < 0){ free_2_dimension_list(&sh_num, &folder_list); continue; } for (n = 0; n < sh_num; ++n){ samba_right = get_permission(NULL, follow_partition->mount_point, folder_list[n], "cifs"); if (samba_right < 0 || samba_right > 3) samba_right = DEFAULT_SAMBA_RIGHT; if(samba_right > 0){ fprintf(fp, "[%s (at %s)]\n", folder_list[n], mount_folder); fprintf(fp, "comment = %s's %s in %s\n", mount_folder, folder_list[n], follow_disk->tag); fprintf(fp, "path = %s/%s\n", follow_partition->mount_point, folder_list[n]); if(samba_right == 3) fprintf(fp, "writeable = yes\n"); else fprintf(fp, "writeable = no\n"); } } free_2_dimension_list(&sh_num, &folder_list); } } } } else if (!strcmp(tmp, "3")) { //else if (!strcmp(nvram_safe_get("st_samba_mode"), "3")) { usb_dbg("samba mode: user\n"); // get the account list if (get_account_list(&acc_num, &account_list) < 0) { usb_dbg("Can't read the account list.\n"); free_2_dimension_list(&acc_num, &account_list); goto confpage; } for (follow_disk = disks_info; follow_disk != NULL; follow_disk = follow_disk->next) { for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) { if (follow_partition->mount_point == NULL) continue; mount_folder = strrchr(follow_partition->mount_point, '/')+1; // 1. get the folder list if (get_folder_list(follow_partition->mount_point, &sh_num, &folder_list) < 0) { free_2_dimension_list(&sh_num, &folder_list); } // 2. start to get every share for (n = -1; n < sh_num; ++n) { int i, first; if(n == -1){ fprintf(fp, "[%s]\n", mount_folder); fprintf(fp, "comment = %s's %s\n", follow_disk->tag, mount_folder); fprintf(fp, "path = %s\n", follow_partition->mount_point); } else{ fprintf(fp, "[%s (at %s)]\n", folder_list[n], mount_folder); fprintf(fp, "comment = %s's %s in %s\n", mount_folder, folder_list[n], follow_disk->tag); fprintf(fp, "path = %s/%s\n", follow_partition->mount_point, folder_list[n]); } fprintf(fp, "valid users = "); first = 1; for (i = 0; i < acc_num; ++i) { if(n == -1) samba_right = get_permission(account_list[i], follow_partition->mount_point, NULL, "cifs"); else samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); fprintf(fp, "invalid users = "); first = 1; for (i = 0; i < acc_num; ++i) { if(n == -1) samba_right = get_permission(account_list[i], follow_partition->mount_point, NULL, "cifs"); else samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (samba_right >= 1) continue; if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); fprintf(fp, "read list = "); first = 1; for (i = 0; i < acc_num; ++i) { if(n == -1) samba_right = get_permission(account_list[i], follow_partition->mount_point, NULL, "cifs"); else samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (samba_right < 1) continue; if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); fprintf(fp, "write list = "); first = 1; for (i = 0; i < acc_num; ++i) { if(n == -1) samba_right = get_permission(account_list[i], follow_partition->mount_point, NULL, "cifs"); else samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (samba_right < 2) continue; if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); } free_2_dimension_list(&sh_num, &folder_list); } } free_2_dimension_list(&acc_num, &account_list); } else if (!strcmp(tmp, "4")) { //else if (!strcmp(nvram_safe_get("st_samba_mode"), "4")) { usb_dbg("samba mode: user\n"); // get the account list if (get_account_list(&acc_num, &account_list) < 0) { usb_dbg("Can't read the account list.\n"); free_2_dimension_list(&acc_num, &account_list); goto confpage; } for (follow_disk = disks_info; follow_disk != NULL; follow_disk = follow_disk->next) { for (follow_partition = follow_disk->partitions; follow_partition != NULL; follow_partition = follow_partition->next) { if (follow_partition->mount_point == NULL) continue; mount_folder = strrchr(follow_partition->mount_point, '/')+1; // 1. get the folder list if (get_folder_list(follow_partition->mount_point, &sh_num, &folder_list) < 0) { free_2_dimension_list(&sh_num, &folder_list); } // 2. start to get every share for (n = 0; n < sh_num; ++n) { int i, first; fprintf(fp, "[%s (at %s)]\n", folder_list[n], mount_folder); fprintf(fp, "comment = %s's %s in %s\n", mount_folder, folder_list[n], follow_disk->tag); fprintf(fp, "path = %s/%s\n", follow_partition->mount_point, folder_list[n]); fprintf(fp, "valid users = "); first = 1; for (i = 0; i < acc_num; ++i) { if(n == -1) samba_right = get_permission(account_list[i], follow_partition->mount_point, NULL, "cifs"); else samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); fprintf(fp, "invalid users = "); first = 1; for (i = 0; i < acc_num; ++i) { samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (samba_right >= 1) continue; if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); fprintf(fp, "read list = "); first = 1; for (i = 0; i < acc_num; ++i) { samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (samba_right < 1) continue; if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); fprintf(fp, "write list = "); first = 1; for (i = 0; i < acc_num; ++i) { samba_right = get_permission(account_list[i], follow_partition->mount_point, folder_list[n], "cifs"); if (samba_right < 2) continue; if (first == 1) first = 0; else fprintf(fp, ", "); fprintf(fp, "%s", account_list[i]); } fprintf(fp, "\n"); } free_2_dimension_list(&sh_num, &folder_list); } } free_2_dimension_list(&acc_num, &account_list); } confpage: if(fp != NULL) fclose(fp); free_disk_data(&disks_info); return 0; }
int del_folder(const char *const mount_path, const char *const folder) { int result, i, len; int acc_num = 0; char **account_list = NULL; char var_file[256]; char *follow_info, backup; char *target, *var_info; FILE *fp; char *full_path; if (mount_path == NULL || strlen(mount_path) <= 0) { return -1; } if (folder == NULL || strlen(folder) <= 0) { return -1; } // 1. test if deleting the folder len = strlen(mount_path)+strlen("/")+strlen(folder); full_path = (char *)malloc(sizeof(char)*(len+1)); if (full_path == NULL) { return -1; } sprintf(full_path, "%s/%s", mount_path, folder); full_path[len] = 0; result = test_if_exist_folder_in_mount_path(mount_path, folder); if (result == 0) { result = test_if_dir(full_path); if (result != 1) { free(full_path); return -1; } } // 2. delete the folder result = rmdir(full_path); free(full_path); if (result != 0) { return -1; } // 4. get the target which is deleted in every var file len = strlen("*")+strlen(folder)+strlen("="); target = (char *)malloc(sizeof(char)*(len+1)); if (target == NULL) { return -1; } sprintf(target, "*%s=", folder); target[len] = 0; // 3. del folder's right to every var file get_account_list(&acc_num, &account_list); for (i = 0; i < acc_num; ++i) { // delete the content about the folder snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", mount_path, account_list[i]); var_info = read_whole_file(var_file); if (var_info == NULL) { continue; } follow_info = upper_strstr(var_info, target); if (follow_info == NULL) { free(var_info); continue; } backup = *follow_info; *follow_info = 0; fp = fopen(var_file, "w"); if (fp == NULL) { *follow_info = backup; free(var_info); continue; } fprintf(fp, "%s", var_info); *follow_info = backup; while (*follow_info != 0 && *follow_info != '\n') ++follow_info; if (*follow_info != 0 && *(follow_info+1) != 0) { ++follow_info; fprintf(fp, "%s", follow_info); } fclose(fp); free(var_info); } free_2_dimension_list(&acc_num, &account_list); // get the var_file for anonymous ftp snprintf(var_file, sizeof(var_file), "%s/.__%s_var.txt", mount_path, FTP_ANONYMOUS_USER); var_info = read_whole_file(var_file); if (var_info == NULL) { goto MOD_FOLDER_END; } follow_info = upper_strstr(var_info, target); if (follow_info == NULL) { free(var_info); goto MOD_FOLDER_END; } backup = *follow_info; *follow_info = 0; fp = fopen(var_file, "w"); if (fp == NULL) { *follow_info = backup; free(var_info); goto MOD_FOLDER_END; } fprintf(fp, "%s", var_info); *follow_info = backup; while (*follow_info != 0 && *follow_info != '\n') ++follow_info; if (*follow_info != 0 && *(follow_info+1) != 0) { ++follow_info; fprintf(fp, "%s", follow_info); } fclose(fp); free(var_info); MOD_FOLDER_END: free(target); // 9. modify the folder's info in the folder list initial_folder_list_in_mount_path(mount_path); return 0; }