char *bftpd_cwd_mappath(char *path) { char *result = malloc(strlen(path) + strlen(cwd) + 16); char *path2; char *tmp; if (! result) return NULL; path2 = strdup(path); if (! path2) { free(result); return NULL; } if (path[0] == '/') strcpy(result, "/"); else strcpy(result, cwd); while (strchr(path2, '/')) { tmp = strdup(path2); *strchr(tmp, '/') = '\0'; cutto(path2, strlen(tmp) + 1); appendpath(result, tmp); free(tmp); } appendpath(result, path2); free(path2); return result; }
int admin_parsecmd(char *str) { int i; char *p, *pp; int string_length; string_length = strlen(str) - 2; if (string_length < 0) string_length = 0; str[string_length] = '\0'; /* Remove \r\n */ p = pp = str; /* Remove garbage in the string */ while (*p) if ((unsigned char) *p < 32) p++; else *pp++ = *p++; *pp++ = 0; for (i = 0; admin_commands[i].name; i++) { /* Parse command */ if (!strncasecmp(str, admin_commands[i].name, strlen(admin_commands[i].name))) { cutto(str, strlen(admin_commands[i].name)); p = str; while ((*p) && ((*p == ' ') || (*p == '\t'))) p++; memmove(str, p, strlen(str) - (p - str) + 1); admin_commands[i].function(str); return 0; } } control_printf(SL_FAILURE, "500 Unknown command: \"%s\"", str); return 1; }
void expand_groups() { char foo[USERLEN + 1]; struct passwd *temp; struct group_of_users *grp; struct group *grpinfo; struct list_of_struct_passwd *endp = NULL; struct list_of_struct_group *endg = NULL; uid_t uid; int i; if ((grp = config_groups)) { do { strcat(grp->temp_members, ","); while (strchr(grp->temp_members, ',')) { sscanf(grp->temp_members, "%[^,]", foo); cutto(grp->temp_members, strlen(foo) + 1); if (foo[0] == '@') { if (sscanf(foo + 1, "%i", &uid)) { if (!((grpinfo = getgrgid(uid)))) continue; } else if (!((grpinfo = getgrnam(foo + 1)))) continue; if (grp->groups) endg = endg->next = malloc(sizeof(struct list_of_struct_group)); else grp->groups = endg = malloc(sizeof(struct list_of_struct_group)); endg->grp.gr_name = strdup(grpinfo->gr_name); endg->grp.gr_passwd = strdup(grpinfo->gr_passwd); endg->grp.gr_gid = grpinfo->gr_gid; for (i = 0; grpinfo->gr_mem[i]; i++); endg->grp.gr_mem = malloc((i + 1) * sizeof(char *)); for (i = 0; grpinfo->gr_mem[i]; i++) endg->grp.gr_mem[i] = strdup(grpinfo->gr_mem[i]); endg->grp.gr_mem[i] = NULL; } if (sscanf(foo, "%i", &uid)) { if (!((temp = getpwuid(uid)))) continue; } else if (!((temp = getpwnam(foo)))) continue; if (grp->users) endp = endp->next = malloc(sizeof(struct list_of_struct_passwd)); else grp->users = endp = malloc(sizeof(struct list_of_struct_passwd)); /* This is ugly, but you can't just use memcpy()! */ endp->pwd.pw_name = strdup(temp->pw_name); endp->pwd.pw_passwd = strdup(temp->pw_passwd); endp->pwd.pw_uid = temp->pw_uid; endp->pwd.pw_gid = temp->pw_gid; endp->pwd.pw_gecos = strdup(temp->pw_gecos); endp->pwd.pw_dir = strdup(temp->pw_dir); endp->pwd.pw_shell = strdup(temp->pw_shell); } free(grp->temp_members); } while ((grp = grp->next)); } }
void config_init() { FILE *configfile; char *str; struct group_of_users *grp = NULL; struct user *usr = NULL; config_global.options = NULL; config_global.directories = NULL; if (!configpath) return; configfile = fopen(configpath, "r"); if (!configfile) { control_printf(SL_FAILURE, "421 Unable to open configuration file."); exit(1); } while ((str = config_read_line(configfile))) { if (strchr(str, '{')) { replace(str, " {", "{"); replace(str, "{ ", "{"); replace(str, " }", "}"); replace(str, "} ", "}"); if (!strcasecmp(str, "global{\n")) { create_options(configfile, &(config_global.options), &(config_global.directories)); } else if (strstr(str, "user ") == str) { if (usr) { usr = usr->next = malloc(sizeof(struct user)); } else { config_users = usr = malloc(sizeof(struct user)); } usr->name = strdup(str + 5); *strchr(usr->name, '{') = 0; create_options(configfile, &(usr->options), &(usr->directories)); } else if (strstr(str, "group ") == str) { if (grp) { grp = grp->next = malloc(sizeof(struct group_of_users)); } else { config_groups = grp = malloc(sizeof(struct group_of_users)); } cutto(str, 6); *strchr(str, '{') = 0; grp->users = NULL; grp->next = NULL; grp->temp_members = strdup(str); create_options(configfile, &(grp->options), &(grp->directories)); } } } fclose(configfile); }
int mygetpwnam(char *name, FILE * file) { char _name[USERLEN + 1]; char foo[256]; int uid, i; if (file) { rewind(file); while (fscanf(file, "%255s%*[^\n]\n", foo) != EOF) { if ((foo[0] == '#') || (!strchr(foo, ':')) || (strchr(foo, ':') > foo + USERLEN - 1)) continue; i = strchr(foo, ':') - foo; strncpy(_name, foo, i); _name[i] = 0; sscanf(strchr(foo + i + 1, ':') + 1, "%i", &uid); if (_name[0] == '\n') cutto(_name, 1); if (!strcmp(name, _name)) return uid; } } return -1; }
char *mygetpwuid(int uid, FILE * file, char *name) { int _uid; char foo[256]; int i; if (file) { rewind(file); while (fscanf(file, "%255s%*[^\n]\n", foo) != EOF) { if ((foo[0] == '#') || (!strchr(foo, ':')) || (strchr(foo, ':') > foo + USERLEN - 1)) continue; i = strchr(foo, ':') - foo; strncpy(name, foo, i); name[i] = 0; sscanf(strchr(foo + i + 1, ':') + 1, "%i", &_uid); if (_uid == uid) { if (name[0] == '\n') cutto(name, 1); return name; } } } sprintf(name, "%i", uid); return name; }