Exemplo n.º 1
0
static int mavis_parse_in(mavis_ctx * mcx, struct sym *sym)
{
    u_int line;
    char *env_name;
    size_t len;
    struct stat st;

    while (1) {
	switch (sym->code) {
	case S_script:
	    mavis_script_parse(mcx, sym);
	    continue;
	case S_userid:
	    parse_userid(sym, &mcx->uid, &mcx->gid);
	    continue;
	case S_groupid:
	    parse_groupid(sym, &mcx->gid);
	    continue;
	case S_home:
	    sym_get(sym);
	    parse(sym, S_equal);
	    strset(&mcx->home, sym->buf);
	    sym_get(sym);
	    continue;
	case S_childs:
	    sym_get(sym);
	    switch (sym->code) {
	    case S_min:
		sym_get(sym);
		parse(sym, S_equal);
		mcx->child_min = parse_int(sym);
		continue;
	    case S_max:
		sym_get(sym);
		parse(sym, S_equal);
		mcx->child_max = parse_int(sym);
		continue;
	    default:
		parse_error_expect(sym, S_min, S_max, S_unknown);
	    }

	case S_setenv:
	    sym_get(sym);
	    env_name = alloca(strlen(sym->buf) + 1);
	    strcpy(env_name, sym->buf);
	    sym_get(sym);
	    parse(sym, S_equal);
	    len = strlen(env_name) + strlen(sym->buf) + 2;
	    mcx->env = Xrealloc(mcx->env, (mcx->envcount + 2) * sizeof(char *));
	    mcx->env[mcx->envcount] = Xcalloc(1, len);
	    snprintf(mcx->env[mcx->envcount++], len, "%s=%s", env_name, sym->buf);
	    mcx->env[mcx->envcount] = NULL;
	    sym_get(sym);
	    continue;

	case S_exec:{
		char buf[MAX_INPUT_LINE_LEN];
		sym_get(sym);
		parse(sym, S_equal);
		mcx->argv = calloc(1, sizeof(char *));
		line = sym->line;
		ostypef(sym->buf, buf, sizeof(buf));
		if (stat(buf, &st))
		    parse_error(sym, "%s: %s", buf, strerror(errno));
		strset(&mcx->path, buf);
		sym_get(sym);
		while (sym->line == line) {
		    mcx->argv = realloc(mcx->argv, (mcx->argc + 2) * sizeof(char *));
		    mcx->argv[mcx->argc] = strdup(sym->buf);
		    mcx->argc++;
		    mcx->argv[mcx->argc] = NULL;
		    sym_get(sym);
		}
		if (!mcx->argv[0]) {
		    mcx->argv = realloc(mcx->argv, 2 * sizeof(char *));
		    mcx->argv[0] = strdup(mcx->path);
		    mcx->argv[1] = NULL;
		}
		continue;
	    }
	case S_eof:
	case S_closebra:
	    if (!mcx->argv)
		parse_error(sym, "Missing \"exec\" declaration.");
	    return MAVIS_CONF_OK;
	default:
	    parse_error_expect(sym, S_script, S_userid, S_groupid, S_home, S_childs, S_setenv, S_exec, S_closebra, S_unknown);
	}
    }
}
Exemplo n.º 2
0
static int mavis_parse_in(mavis_ctx * mcx, struct sym *sym)
{
    while (1) {
	switch (sym->code) {
	case S_script:
	    mavis_script_parse(mcx, sym);
	    continue;
	case S_userid:
	    parse_userid(sym, &mcx->uid, &mcx->gid);
	    continue;
	case S_groupid:
	    parse_groupid(sym, &mcx->gid);
	    continue;
	case S_home:
	    sym_get(sym);
	    parse(sym, S_equal);
	    strset(&mcx->home, sym->buf);
	    sym_get(sym);
	    continue;
	case S_root:
	    sym_get(sym);
	    parse(sym, S_equal);
	    strset(&mcx->root, sym->buf);
	    sym_get(sym);
	    continue;
	case S_upload:
	    sym_get(sym);
	    parse(sym, S_equal);
	    strset(&mcx->incoming, sym->buf);
	    sym_get(sym);
	    continue;
	case S_eof:
	case S_closebra:
	    {
		int bye = 0;
		if (!mcx->uid || !mcx->gid || !mcx->root) {
		    struct passwd *pw;

		    pw = getpwnam("ftp");
		    if (pw) {
			if (!mcx->uid)
			    mcx->uid = pw->pw_uid;
			if (!mcx->gid)
			    mcx->gid = pw->pw_gid;
			if (!mcx->root)
			    mcx->root = Xstrdup(pw->pw_dir);
		    }
		}

		if (!mcx->uid) {
		    logmsg("%s: Fatal: anonymous ftp uid not set!", MAVIS_name);
		    bye++;
		}
		if (!mcx->gid) {
		    logmsg("%s: Fatal: anonymous ftp gid not set!", MAVIS_name);
		    bye++;
		}
		if (!mcx->root) {
		    logmsg("%s: Fatal: anonymous ftp root not set!", MAVIS_name);
		    bye++;
		}
		if (bye)
		    return -1;

		if (!mcx->home)
		    mcx->home = Xstrdup("/");

		return MAVIS_CONF_OK;
	    }
	default:
	    parse_error_expect(sym, S_script, S_userid, S_groupid, S_path, S_mode, S_closebra, S_unknown);
	}
    }
}
Exemplo n.º 3
0
bool get_wrap_data(char *wrapid, char *handler, char *cgi, t_wrap *wrap_data, char *user_directory) {
	FILE *fp;
	char line[257], *item, *rest, *pipe;
	struct passwd *pwd;
	bool wrap_oke = false, handler_oke;
	size_t len;

	handler_oke = (handler == NULL);

	wrap_data->chroot = NULL;

	/* WrapID is local userid? */
	if (*wrapid == '~') {
		if ((pwd = getpwnam(wrapid + 1)) == NULL) {
			return false;
		}
		if ((wrap_data->uid = pwd->pw_uid) == 0) {
			return false;
		}
		len = strlen(pwd->pw_dir);
		if ((wrap_data->cgiroot = (char*)malloc(len + strlen(user_directory) + 1)) == NULL) {
			return false;
		}
		memcpy(wrap_data->cgiroot, pwd->pw_dir, len);
		strcpy(wrap_data->cgiroot + len, user_directory);
		if (strncmp(wrap_data->cgiroot, cgi, strlen(wrap_data->cgiroot)) != 0) {
			return false;
		}
		if (lookup_group_ids(wrap_data->uid, &(wrap_data->gid), &(wrap_data->groups)) == -1) {
			return false;
		}

		if (handler_oke) {
			return true;
		}

		wrap_oke = true;
	} else {
		wrap_data->cgiroot = NULL;
	}

	/* Read CGI wrapper configuration */
	if ((fp = fopen(CONFIG_DIR"/cgi-wrapper.conf", "r")) != NULL) {
		line[256] = '\0';

		while (fgets(line, 256, fp) != NULL) {
			rest = uncomment(line);
			if (*rest == '\0') {
				continue;
			}

			if (split_configline(rest, &item, &rest) == 0) {
				strlower(item);
				if (strcmp(item, "cgihandler") == 0) {
					/* CGI handler */
					if (handler_oke) {
						continue;
					}
					do {
						split_string(rest, &item, &rest, ',');
						if (strcmp(handler, item) == 0) {
							handler_oke = true;
							break;
						}
					} while (rest != NULL);
				} else if (strcmp(item, "wrap") == 0) {
					/* Wrap entry */
					if (wrap_oke) {
						continue;
					}

					/* Wrap Id */
					if (split_string(rest, &item, &rest, DELIMITER) == -1) {
						break;
					}
					if (strcmp(item, wrapid) != 0) {
						continue;
					}

					/* Homedirectory */
					if (split_string(rest, &item, &rest, DELIMITER) == -1) {
						break;
					}
					if (*item == '/') {
						/* chroot directory */
						if ((pipe = strchr(item, '|')) != NULL) {
							*pipe = '\0';
							len = pipe - item + 1;
							if ((wrap_data->chroot = (char*)malloc(len)) == NULL) {
								break;
							}
							memcpy(wrap_data->chroot, item, len);
							*pipe = '/';
						}

						if ((len = strlen(item)) == 0) {
							break;
						}
						if ((strncmp(item, cgi, len) != 0) || (*(cgi + len) != '/')) {
							log_error("CGI not in WebsiteRoot");
							break;
						}

						if (pipe != NULL) {
							cgi += (pipe - item);
							item = pipe;
						}

						wrap_data->cgiroot = strdup(item);
					} else if (*item == '~') {
						if ((pwd = getpwnam(item + 1)) == NULL) {
							log_error("invalid username");
							break;
						}
						len = strlen(pwd->pw_dir);
						if ((wrap_data->cgiroot = (char*)malloc(len + strlen(user_directory) + 1)) == NULL) {
							break;
						}
						memcpy(wrap_data->cgiroot, pwd->pw_dir, len);
						strcpy(wrap_data->cgiroot + len, user_directory);
						if (strncmp(wrap_data->cgiroot, cgi, strlen(wrap_data->cgiroot)) != 0) {
							log_error("CGI not in user directory");
							break;
						}
					} else {
						log_error("invalid CGI root");
						break;
					}

					/* User Id */
					split_string(rest, &item, &rest, ':');
					if (parse_userid(item, &(wrap_data->uid)) != 1) {
						log_error("invalid userid");
						break;
					}

					/* Group id */
					if (rest != NULL) {
						if (parse_groups(rest, &(wrap_data->gid), &(wrap_data->groups)) != 1) {
							log_error("syntax error in groupid");
							break;
						}
					} else {
						if (lookup_group_ids(wrap_data->uid, &(wrap_data->gid), &(wrap_data->groups)) != 1) {
							log_error("invalid group (user member of root?)");
							break;
						}
					}

					wrap_oke = true;
				} else {
					/* Crap in configurationfile */
					log_error("syntax error in configurationfile");
					break;
				}
				if (wrap_oke && handler_oke) {
					break;
				}
			} else {
				/* split_string() error */
				break;
			}
		}
		fclose(fp);

		if (wrap_oke == false) {
			log_error("no valid Wrap found");
		}
		if (handler_oke == false) {
			log_error("no valid CGIhandler found");
		}

		return (wrap_oke && handler_oke);
	} else {
		return false;
	}
}