コード例 #1
0
ファイル: print_generic.c プロジェクト: hef/samba
/****************************************************************************
get the current list of queued jobs
****************************************************************************/
static int generic_queue_get(const char *printer_name,
                             enum printing_types printing_type,
                             char *lpq_command,
                             print_queue_struct **q,
                             print_status_struct *status)
{
    char **qlines;
    int fd;
    int numlines, i, qcount;
    print_queue_struct *queue = NULL;

    /* never do substitution when running the 'lpq command' since we can't
       get it rigt when using the background update daemon.  Make the caller
       do it before passing off the command string to us here. */

    print_run_command(-1, printer_name, False, lpq_command, &fd, NULL);

    if (fd == -1) {
        DEBUG(5,("generic_queue_get: Can't read print queue status for printer %s\n",
                 printer_name ));
        return 0;
    }

    numlines = 0;
    qlines = fd_lines_load(fd, &numlines,0,NULL);
    close(fd);

    /* turn the lpq output into a series of job structures */
    qcount = 0;
    ZERO_STRUCTP(status);
    if (numlines && qlines) {
        queue = SMB_MALLOC_ARRAY(print_queue_struct, numlines+1);
        if (!queue) {
            TALLOC_FREE(qlines);
            *q = NULL;
            return 0;
        }
        memset(queue, '\0', sizeof(print_queue_struct)*(numlines+1));

        for (i=0; i<numlines; i++) {
            /* parse the line */
            if (parse_lpq_entry(printing_type,qlines[i],
                                &queue[qcount],status,qcount==0)) {
                qcount++;
            }
        }
    }

    TALLOC_FREE(qlines);
    *q = queue;
    return qcount;
}
コード例 #2
0
bool map_username(TALLOC_CTX *ctx, const char *user_in, char **p_user_out)
{
	XFILE *f;
	char *mapfile = lp_username_map(talloc_tos());
	char *s;
	char buf[512];
	bool mapped_user = False;
	char *cmd = lp_username_map_script(talloc_tos());

	*p_user_out = NULL;

	if (!user_in)
		return false;

	/* Initially make a copy of the incoming name. */
	*p_user_out = talloc_strdup(ctx, user_in);
	if (!*p_user_out) {
		return false;
	}

	if (strequal(user_in,get_last_to()))
		return false;

	if (strequal(user_in,get_last_from())) {
		DEBUG(3,("Mapped user %s to %s\n",user_in,get_last_to()));
		TALLOC_FREE(*p_user_out);
		*p_user_out = talloc_strdup(ctx, get_last_to());
		return true;
	}

	if (fetch_map_from_gencache(ctx, user_in, p_user_out)) {
		return true;
	}

	/* first try the username map script */

	if ( *cmd ) {
		char **qlines;
		char *command = NULL;
		int numlines, ret, fd;

		command = talloc_asprintf(ctx,
					"%s \"%s\"",
					cmd,
					user_in);
		if (!command) {
			return false;
		}

		DEBUG(10,("Running [%s]\n", command));
		ret = smbrun(command, &fd);
		DEBUGADD(10,("returned [%d]\n", ret));

		TALLOC_FREE(command);

		if ( ret != 0 ) {
			if (fd != -1)
				close(fd);
			return False;
		}

		numlines = 0;
		qlines = fd_lines_load(fd, &numlines, 0, ctx);
		DEBUGADD(10,("Lines returned = [%d]\n", numlines));
		close(fd);

		/* should be either no lines or a single line with the mapped username */

		if (numlines && qlines) {
			DEBUG(3,("Mapped user %s to %s\n", user_in, qlines[0] ));
			set_last_from_to(user_in, qlines[0]);
			store_map_in_gencache(ctx, user_in, qlines[0]);
			TALLOC_FREE(*p_user_out);
			*p_user_out = talloc_strdup(ctx, qlines[0]);
			if (!*p_user_out) {
				return false;
			}
		}

		TALLOC_FREE(qlines);

		return numlines != 0;
	}

	/* ok.  let's try the mapfile */
	if (!*mapfile)
		return False;

	f = x_fopen(mapfile,O_RDONLY, 0);
	if (!f) {
		DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) ));
		return False;
	}

	DEBUG(4,("Scanning username map %s\n",mapfile));

	while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) {
		char *unixname = s;
		char *dosname = strchr_m(unixname,'=');
		char **dosuserlist;
		bool return_if_mapped = False;

		if (!dosname)
			continue;

		*dosname++ = 0;

		unixname = skip_space(unixname);

		if ('!' == *unixname) {
			return_if_mapped = True;
			unixname = skip_space(unixname+1);
		}

		if (!*unixname || strchr_m("#;",*unixname))
			continue;

		{
			int l = strlen(unixname);
			while (l && isspace((int)unixname[l-1])) {
				unixname[l-1] = 0;
				l--;
			}
		}

		/* skip lines like 'user = '******'*') ||
		    user_in_list(ctx, user_in, (const char **)dosuserlist)) {
			DEBUG(3,("Mapped user %s to %s\n",user_in,unixname));
			mapped_user = True;

			set_last_from_to(user_in, unixname);
			store_map_in_gencache(ctx, user_in, unixname);
			TALLOC_FREE(*p_user_out);
			*p_user_out = talloc_strdup(ctx, unixname);
			if (!*p_user_out) {
				TALLOC_FREE(dosuserlist);
				x_fclose(f);
				return false;
			}

			if ( return_if_mapped ) {
				TALLOC_FREE(dosuserlist);
				x_fclose(f);
				return True;
			}
		}

		TALLOC_FREE(dosuserlist);
	}

	x_fclose(f);

	/*
	 * If we didn't successfully map a user in the loop above,
	 * setup the last_from and last_to as an optimization so
	 * that we don't scan the file again for the same user.
	 */
	if (!mapped_user) {
		DEBUG(8, ("The user '%s' has no mapping. "
			  "Skip it next time.\n", user_in));
		set_last_from_to(user_in, user_in);
		store_map_in_gencache(ctx, user_in, user_in);
	}

	return mapped_user;
}
コード例 #3
0
ファイル: vfs_shell_snap.c プロジェクト: DanilKorotenko/samba
static NTSTATUS shell_snap_create(struct vfs_handle_struct *handle,
				  TALLOC_CTX *mem_ctx,
				  const char *base_volume,
				  time_t *tstamp,
				  bool rw,
				  char **base_path,
				  char **snap_path)
{
	const char *cmd;
	char *cmd_run;
	char **qlines;
	int numlines, ret;
	int fd = -1;
	TALLOC_CTX *tmp_ctx;
	NTSTATUS status;

	cmd = lp_parm_const_string(handle->conn->params->service,
				   "shell_snap", "create command", "");
	if ((cmd == NULL) || (strlen(cmd) == 0)) {
		DEBUG(1, ("\"shell_snap:create command\" not configured\n"));
		status = NT_STATUS_NOT_SUPPORTED;
		goto err_out;
	}

	tmp_ctx = talloc_new(mem_ctx);
	if (tmp_ctx == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto err_out;
	}

	/* add base vol argument */
	cmd_run = talloc_asprintf(tmp_ctx, "%s %s", cmd, base_volume);
	if (cmd_run == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto err_tmp_free;
	}

	ret = smbrun(cmd_run, &fd);
	talloc_free(cmd_run);
	if (ret != 0) {
		if (fd != -1) {
			close(fd);
		}
		status = NT_STATUS_UNSUCCESSFUL;
		goto err_tmp_free;
	}

	numlines = 0;
	qlines = fd_lines_load(fd, &numlines, PATH_MAX + 1, tmp_ctx);
	close(fd);

	/* script must return the snapshot path as a single line */
	if ((numlines == 0) || (qlines == NULL) || (qlines[0] == NULL)) {
		status = NT_STATUS_UNSUCCESSFUL;
		goto err_tmp_free;
	}

	*base_path = talloc_strdup(mem_ctx, base_volume);
	if (*base_path == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto err_tmp_free;
	}
	*snap_path = talloc_strdup(mem_ctx, qlines[0]);
	if (*snap_path == NULL) {
		status = NT_STATUS_NO_MEMORY;
		talloc_free(*base_path);
		goto err_tmp_free;
	}

	status = NT_STATUS_OK;
err_tmp_free:
	talloc_free(tmp_ctx);
err_out:
	return status;
}