idmap.c revision 48258c6b4e17f36ab09fba0bd6307d1fec9dcbce
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * CDDL HEADER START
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * The contents of this file are subject to the terms of the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Common Development and Distribution License (the "License").
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * You may not use this file except in compliance with the License.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * or http://www.opensolaris.org/os/licensing.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * See the License for the specific language governing permissions
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * and limitations under the License.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * When distributing Covered Code, include this CDDL HEADER in each
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
5dbfd19ad5fcc2b779f40f80fa05c1bd28fd0b4eTheo Schlossnagle * If applicable, add the following below this CDDL HEADER, with the
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * fields enclosed by brackets "[]" replaced with your own identifying
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * information: Portions Copyright [yyyy] [name of copyright owner]
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * CDDL HEADER END
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * Use is subject to license terms.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#pragma ident "%Z%%M% %I% %E% SMI"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <stdio.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <stdlib.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <locale.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <strings.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <errno.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <limits.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include <sys/varargs.h>
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include "idmap_engine.h"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#include "idmap_priv.h"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Initialization values for pids/rids: */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define UNDEFINED_UID (uid_t)-1
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define UNDEFINED_GID (gid_t)-1
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define UNDEFINED_RID (idmap_rid_t)-1;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* is_user values */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define I_YES 1
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define I_NO 0
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define I_UNKNOWN -1
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * used in do_show for the type of argument, which can be winname,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * unixname, uid, gid, sid or not given at all:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define TYPE_SID 0x010 /* sid */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define TYPE_USID 0x011 /* usid */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define TYPE_GSID 0x012 /* gsid */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define TYPE_WN 0x110 /* winname */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define TYPE_WU 0x111 /* winuser */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define TYPE_WG 0x112 /* wingroup */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define TYPE_UID 0x001 /* uid */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define TYPE_GID 0x002 /* gid */
89b9271284be1a4e3e3053d7bc12f9bbf8145b06Robert Mustacchi#define TYPE_PID 0x000 /* pid */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define TYPE_UN 0x100 /* unixname */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define TYPE_UU 0x101 /* unixuser */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define TYPE_UG 0x102 /* unixgroup */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define IS_WIN 0x010 /* mask for the windows types */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define IS_NAME 0x100 /* mask for string name types */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define IS_USER 0x001 /* mask for user types */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define IS_GROUP 0x002 /* mask for group types */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Identity type strings */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define ID_WINNAME "winname"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define ID_UNIXNAME "unixname"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define ID_UNIXUSER "unixuser"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define ID_UNIXGROUP "unixgroup"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define ID_WINUSER "winuser"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define ID_WINGROUP "wingroup"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define ID_USID "usid"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define ID_GSID "gsid"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define ID_SID "sid"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define ID_UID "uid"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define ID_GID "gid"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define INHIBITED(str) (str == NULL || *str == 0 || strcmp(str, "\"\"") == 0)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowetypedef struct {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *identity;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int code;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe} id_code_t;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweid_code_t identity2code[] = {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {ID_WINNAME, TYPE_WN},
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {ID_UNIXNAME, TYPE_UN},
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {ID_UNIXUSER, TYPE_UU},
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {ID_UNIXGROUP, TYPE_UG},
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {ID_WINUSER, TYPE_WU},
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {ID_WINGROUP, TYPE_WG},
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {ID_USID, TYPE_USID},
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {ID_GSID, TYPE_GSID},
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {ID_SID, TYPE_SID},
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {ID_UID, TYPE_UID},
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {ID_GID, TYPE_GID}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe};
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Flags */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define f_FLAG 'f'
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define t_FLAG 't'
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define d_FLAG 'd'
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define F_FLAG 'F'
ce83b99835cc4643ab0fefd88dea62427d9ced5eRobert Mustacchi#define a_FLAG 'a'
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define n_FLAG 'n'
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define c_FLAG 'c'
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define v_FLAG 'v'
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* used in the function do_import */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe#define MAX_INPUT_LINE_SZ 2047
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowetypedef struct {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int is_user;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int is_wuser;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int direction;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe boolean_t is_nt4;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *unixname;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *winname;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *windomain;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *sidprefix;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe idmap_rid_t rid;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe uid_t pid;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe} name_mapping_t;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Formats of the output:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Idmap reads/prints mappings in several formats: ordinary mappings,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * name mappings in Samba username map format (smbusers), Netapp
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * usermap.cfg.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * DEFAULT_FORMAT are in fact the idmap subcommands suitable for
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * piping to idmap standart input. For example
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * add -d winuser:bob@foo.com unixuser:fred
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * add -d winuser:bob2bar.com unixuser:fred
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * SMBUSERS is the format of Samba username map (smbusers). For full
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * documentation, search for "username map" in smb.conf manpage.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * The format is for example
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * fred = bob@foo.com bob2@bar.com
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * USERMAP_CFG is the format of Netapp usermap.cfg file. Search
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * http://www.netapp.com/ for more documentation. IP qualifiers are not
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * supported.
296749875bd503e7a14e25b4c57d3142cb496df1Joshua M. Clulow * The format is for example
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * bob@foo.com => fred
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * "Bob With Spaces"@bar.com => fred #comment
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * The previous formats were for name rules. MAPPING_NAME and
d2b9ba291ef0d1dc8807b6d46996674c723924d0Robert Mustacchi * MAPPING_ID are for the actual mappings, as seen in show/dump
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * commands. MAPPING_NAME prefers the string names of the user over
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * their numerical identificators. MAPPING_ID prints just the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * identificators.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Example of the MAPPING_NAME:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * winname:bob@foo.com -> unixname:fred
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
b65dd972486b1f5913d705d2a0cb9c3fb189a9e0Robert Mustacchi * Example of the MAPPING_ID:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * sid:S-1-2-3-4 -> uid:5678
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowetypedef enum {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe UNDEFINED_FORMAT = -1,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe DEFAULT_FORMAT = 0,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe MAPPING_ID,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe MAPPING_NAME,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe USERMAP_CFG,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe SMBUSERS
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe} format_t;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowetypedef struct {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe format_t format;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe FILE *file;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe name_mapping_t *last;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe} print_handle_t;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * idmap_api batch related variables:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * idmap can operate in two modes. It the batch mode, the idmap_api
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * batch is committed at the end of a batch of several
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * commands. At the end of input file, typically. This mode is used
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * for processing input from a file.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * In the non-batch mode, each command is committed immediately. This
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * mode is used for tty input.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Are we in the batch mode? */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int batch_mode = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Self describing stricture for positions */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestruct pos_sds {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int size;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int last;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe cmd_pos_t *pos[1];
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe};
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic struct pos_sds *positions;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Handles for idmap_api batch */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic idmap_handle_t *handle = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic idmap_udt_handle_t *udt = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Do we need to commit the udt batch at the end? */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int udt_used;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Command handlers */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int do_show_mapping(flag_t *f, int argc, char **argv, cmd_pos_t *pos);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int do_dump(flag_t *f, int argc, char **argv, cmd_pos_t *pos);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int do_import(flag_t *f, int argc, char **argv, cmd_pos_t *pos);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int do_list_name_mappings(flag_t *f, int argc, char **argv,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe cmd_pos_t *pos);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int do_add_name_mapping(flag_t *f, int argc, char **argv,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe cmd_pos_t *pos);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int do_remove_name_mapping(flag_t *f, int argc, char **argv,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe cmd_pos_t *pos);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int do_exit(flag_t *f, int argc, char **argv, cmd_pos_t *pos);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int do_export(flag_t *f, int argc, char **argv, cmd_pos_t *pos);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int do_help(flag_t *f, int argc, char **argv, cmd_pos_t *pos);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Command names and their hanlers to be passed to idmap_engine */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic cmd_ops_t commands[] = {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "show",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "c(create)v(verbose)",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe do_show_mapping
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe },
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "dump",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "n(names)v(verbose)",
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore do_dump
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe },
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "import",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "F(flush)f:(file)",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe do_import
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe },
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "export",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "f:(file)",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe do_export
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe },
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "list",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe do_list_name_mappings
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe },
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "add",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "d(directional)",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe do_add_name_mapping
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe },
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
462453d2d0c563559a4caf186db76954e563bd1aMatthew Ahrens "remove",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "a(all)t(to)f(from)d(directional)",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe do_remove_name_mapping
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe },
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "exit",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe do_exit
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe },
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "help",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe do_help
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe};
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Print error message, possibly with a position */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* printflike */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic void
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweprint_error(cmd_pos_t *pos, const char *format, ...)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe size_t length;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe va_list ap;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe va_start(ap, format);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (pos != NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe length = strlen(pos->line);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* Skip newlines etc at the end: */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe while (length > 0 && isspace(pos->line[length - 1]))
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe length--;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fprintf(stderr,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe gettext("Error at line %d: %.*s\n"),
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe pos->linenum,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe length,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe pos->line);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) vfprintf(stderr, format, ap);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe va_end(ap);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Inits positions sds. 0 means everything went OK, -1 for errors */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweinit_positions()
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int init_size = 32; /* Initial size of the positions array */
ad0ef8fd06d1ac28108685495a9ba1244a20a5caRobert Mustacchi
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe positions = (struct pos_sds *) malloc(sizeof (struct pos_sds) +
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (init_size - 1) * sizeof (cmd_pos_t *));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (positions == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL, gettext("Not enough memory.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
ad0ef8fd06d1ac28108685495a9ba1244a20a5caRobert Mustacchi
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe positions->size = init_size;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe positions->last = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Free the positions array */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic void
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowefini_positions()
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int i;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe for (i = 0; i < positions->last; i++) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (positions->pos[i] == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe continue;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(positions->pos[i]->line);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(positions->pos[i]);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(positions);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe positions = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Add another position to the positions array. 0 means everything
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * went OK, -1 for errors
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowepositions_add(cmd_pos_t *pos)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (positions->last >= positions->size) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe positions->size *= 2;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe positions = (struct pos_sds *)realloc(positions,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe sizeof (struct pos_sds) +
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (positions->size - 1) * sizeof (cmd_pos_t *));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (positions == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe goto nomemory;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (pos == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe positions->pos[positions->last] = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe positions->pos[positions->last] = (cmd_pos_t *)calloc(1,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe sizeof (cmd_pos_t));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (positions->pos[positions->last] == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe goto nomemory;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *positions->pos[positions->last] = *pos;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe positions->pos[positions->last]->line = strdup(pos->line);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (positions->pos[positions->last]->line == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe goto nomemory;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe positions->last++;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowenomemory:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL, gettext("Not enough memory.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Compare two strings just like strcmp, but stop before the end of
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * the s2
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestrcmp_no0(const char *s1, const char *s2)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (strncmp(s1, s2, strlen(s2)));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Print help message */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic void
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowehelp()
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fprintf(stderr,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "idmap\n"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "idmap -f command-file\n"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "idmap show [-c] [-v] identity [targettype]\n"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "idmap dump [-n] [-v]\n"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "idmap add [-d] name1 name2\n"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "idmap remove -a\n"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "idmap remove [-f|-t] name\n"
d3864341aacc6a2ecc95960d23ac0e49f1f538faRobert Mustacchi "idmap remove [-d] name1 name2\n"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "idmap list\n"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "idmap import [-F] [-f file] format\n"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "idmap export [-f file] format\n"
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "idmap help\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* The handler for the "help" command. */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore/* LINTED E_FUNC_ARG_UNUSED */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowedo_help(flag_t *f, int argc, char **argv, cmd_pos_t *pos)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe help();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Initialization of the idmap api batch */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweinit_batch()
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe idmap_stat stat;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe stat = idmap_init(&handle);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (stat < 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe gettext("Connection not established (%s)\n"),
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe idmap_stat2string(NULL, stat));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Initialization common to all commands */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweinit_command()
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (batch_mode)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (init_batch());
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Finalization common to all commands */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic void
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowefini_command()
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (batch_mode)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) idmap_fini(handle);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe handle = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Initialization of the commands which perform write operations */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweinit_udt_batch()
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe idmap_stat stat;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (init_batch())
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore stat = idmap_udt_create(handle, &udt);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (stat < 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe gettext("Error initiating transaction (%s)"),
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe idmap_stat2string(handle, stat));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (init_positions() < 0)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore/* Finalization of the write commands */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweinit_udt_command()
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe udt_used = 1;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (batch_mode)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (init_udt_batch());
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amore
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* If everythings is OK, send the udt batch to idmapd */
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amorestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowefini_udt_command(int ok, cmd_pos_t *pos)
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amore{
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amore int rc = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int64_t failpos;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe idmap_stat stat, stat1;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe cmd_pos_t *reported_pos;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (batch_mode)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (udt == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(pos,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe gettext("Internal error: uninitiated batch.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (ok && udt_used) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe stat = idmap_udt_commit(udt);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (stat == IDMAP_SUCCESS)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe goto out;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe rc = -1;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe stat1 = idmap_udt_get_error_index(udt, &failpos);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (stat1 != IDMAP_SUCCESS) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe gettext("Error diagnosing transaction (%s)\n"),
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe idmap_stat2string(handle, stat1));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe goto out;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (failpos < 0)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe reported_pos = pos;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe reported_pos = positions->pos[failpos];
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(reported_pos,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe gettext("Error commiting transaction (%s)\n"),
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe idmap_stat2string(handle, stat));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweout:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe idmap_udt_destroy(udt);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe udt = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe udt_used = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe fini_command();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe fini_positions();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (rc);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Convert numeric expression of the direction to it's string form */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic char *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowedirection2string(int direction)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe switch (direction) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case IDMAP_DIRECTION_BI:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return ("==");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case IDMAP_DIRECTION_W2U:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return ("=>");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case IDMAP_DIRECTION_U2W:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return ("<=");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe default:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* This can never happen: */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe gettext("Internal error: invalid direction.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return ("");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* never reached */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Returns 1 if c is a shell-meta-character requiring quoting, 0
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * otherwise.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * We don't quote '*' and ':' because they cannot do any harm
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * a) they have no meaning to idmap_engine b) even ifsomebody copy &
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * paste idmap output to a shell commandline, there is the identity
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * type string in front of them. On the other hand, '*' and ':' are
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * everywhere.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweis_shell_special(char c)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (isspace(c))
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (strchr("&^{}#;'\"\\`!$()[]><|~", c) != NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Returns 1 if c is a shell-meta-character requiring quoting even
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * inside double quotes, 0 otherwise. It means \, " and $ .
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * This set of characters is a subset of those in is_shell_special().
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweis_dq_special(char c)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (strchr("\\\"$", c) != NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Quote any shell meta-characters in the given string. If 'quote' is
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * true then use double-quotes to quote the whole string, else use
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * back-slash to quote each individual meta-character.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * The resulting string is placed in *res. Callers must free *res if the
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * return value isn't 0 (even if the given string had no meta-chars).
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * If there are any errors this returns -1, else 0.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweshell_app(char **res, char *string, int quote)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int i, j;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe uint_t noss = 0; /* Number Of Shell Special chars in the input */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe uint_t noqb = 0; /* Number Of Quotes and Backslahes in the input */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *out;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe size_t len_orig = strlen(string);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe size_t len;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (INHIBITED(string)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe out = strdup("\"\"");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (out == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL, gettext("Not enough memory.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *res = out;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* First, let us count how many characters we need to quote: */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe for (i = 0; i < len_orig; i++) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (is_shell_special(string[i])) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe noss++;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (is_dq_special(string[i]))
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe noqb++;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* Do we need to quote at all? */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (noss == 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe out = strdup(string);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (out == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL, gettext("Not enough memory.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *res = out;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* What is the length of the result? */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (quote)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe len = strlen(string) + 2 + noqb + 1; /* 2 for quotation marks */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe len = strlen(string) + noss + 1;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe out = (char *)malloc(len);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (out == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL, gettext("Not enough memory.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore j = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (quote)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe out[j++] = '"';
820218f30a3ad84d92aa2970dcac9eb5cf69aaa9Robert Mustacchi
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe for (i = 0; i < len_orig; i++) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* Quote the dangerous chars by a backslash */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (quote && is_dq_special(string[i]) ||
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (!quote && is_shell_special(string[i]))) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe out[j++] = '\\';
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe out[j++] = string[i];
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (quote)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe out[j++] = '"';
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe out[j] = '\0';
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *res = out;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Assemble string form sid */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic char *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowesid_format(name_mapping_t *nm)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *to;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe size_t len;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *typestring;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe switch (nm->is_wuser) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case I_YES:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe typestring = ID_USID;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case I_NO:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe typestring = ID_GSID;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe default:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe typestring = ID_SID;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* 'usid:' + sidprefix + '-' + rid + '\0' */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe len = strlen(nm->sidprefix) + 7 + 3 * sizeof (nm->rid);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe to = (char *)malloc(len);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (to == NULL)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (NULL);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) snprintf(to, len, "%s:%s-%u", typestring, nm->sidprefix,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nm->rid);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (to);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Assemble string form uid or gid */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic char *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowepid_format(uid_t from, int is_user)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *to;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe size_t len;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* ID_UID ":" + uid + '\0' */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe len = 5 + 3 * sizeof (uid_t);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe to = (char *)malloc(len);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (to == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (NULL);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) snprintf(to, 16, "%s:%u", is_user ? ID_UID : ID_GID, from);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (to);
e232d9863a8486cf94eaa4bc06c2e9ff52bf3140Robert Mustacchi}
e232d9863a8486cf94eaa4bc06c2e9ff52bf3140Robert Mustacchi
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Assemble winname, e.g. "winuser:bob@foo.sun.com", from name_mapping_t */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowenm2winqn(name_mapping_t *nm, char **winqn)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *out;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe size_t length = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int is_domain = 1;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore char *prefix;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* Sometimes there are no text names. Return a sid, then. */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm->winname == NULL && nm->sidprefix != NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *winqn = sid_format(nm);
e232d9863a8486cf94eaa4bc06c2e9ff52bf3140Robert Mustacchi return (0);
e232d9863a8486cf94eaa4bc06c2e9ff52bf3140Robert Mustacchi }
e232d9863a8486cf94eaa4bc06c2e9ff52bf3140Robert Mustacchi
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe switch (nm->is_wuser) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case I_YES:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe prefix = ID_WINUSER ":";
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case I_NO:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe prefix = ID_WINGROUP ":";
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case I_UNKNOWN:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe prefix = ID_WINNAME ":";
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe length = strlen(prefix);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm->winname != NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe length += strlen(nm->winname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* Windomain is not mandatory: */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm->windomain == NULL || INHIBITED(nm->winname))
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe is_domain = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe length += strlen(nm->windomain) + 1;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe out = (char *)malloc(length + 1);
296749875bd503e7a14e25b4c57d3142cb496df1Joshua M. Clulow if (out == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe gettext("Not enough memory.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) strcpy(out, prefix);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* LINTED E_NOP_IF_STMT */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm->winname == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else if (!is_domain)
d2b9ba291ef0d1dc8807b6d46996674c723924d0Robert Mustacchi (void) strcat(out, nm->winname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else if (nm->is_nt4) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) strcat(out, nm->windomain);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) strcat(out, "\\");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) strcat(out, nm->winname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) strcat(out, nm->winname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) strcat(out, "@");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) strcat(out, nm->windomain);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *winqn = out;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Assemble a text unixname, e.g. unixuser:fred. Use only for
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * mapping, not namerules - there an empty name means inhibited
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * mappings, while here pid is printed if there is no name.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorestatic
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amoreint
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowenm2unixname(name_mapping_t *nm, char **unixname)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe size_t length = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *out, *it, *prefix;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* Sometimes there is no name, just pid: */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm->unixname == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm->pid == UNDEFINED_UID)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *unixname = pid_format(nm->pid, nm->is_user);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (shell_app(&it, nm->unixname, 0))
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe switch (nm->is_user) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore case I_YES:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe prefix = ID_UNIXUSER ":";
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case I_NO:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe prefix = ID_UNIXGROUP ":";
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case I_UNKNOWN:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe prefix = ID_UNIXNAME ":";
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe length = strlen(prefix) + strlen(it);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe out = (char *)malloc(length + 1);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (out == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore gettext("Not enough memory.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(it);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore (void) strcpy(out, prefix);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore (void) strcat(out, it);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(it);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore *unixname = out;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore}
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Allocate a new name_mapping_t and initialize the values. */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorestatic name_mapping_t *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowename_mapping_init()
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore{
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore name_mapping_t *nm = (name_mapping_t *)malloc(sizeof (name_mapping_t));
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (nm == NULL) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore print_error(NULL, gettext("Not enough memory.\n"));
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (NULL);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->winname = nm->windomain = nm->unixname = nm->sidprefix = NULL;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->rid = UNDEFINED_RID;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->is_nt4 = B_FALSE;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->is_user = I_UNKNOWN;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->is_wuser = I_UNKNOWN;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nm->direction = IDMAP_DIRECTION_UNDEF;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->pid = UNDEFINED_UID;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (nm);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore/* Free name_mapping_t */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorestatic void
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amorename_mapping_fini(name_mapping_t *nm)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(nm->winname);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore free(nm->windomain);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(nm->unixname);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore free(nm->sidprefix);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(nm);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowename_mapping_cpy(name_mapping_t *to, name_mapping_t *from)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(to->winname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(to->windomain);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(to->unixname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(to->sidprefix);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) memcpy(to, from, sizeof (name_mapping_t));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe to->winname = to->windomain = to->unixname = to->sidprefix = NULL;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (from->winname != NULL) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore to->winname = strdup(from->winname);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (to->winname == NULL) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore print_error(NULL, gettext("Not enough memory.\n"));
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (-1);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (from->windomain != NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe to->windomain = strdup(from->windomain);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (to->windomain == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL, gettext("Not enough memory.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (from->unixname != NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe to->unixname = strdup(from->unixname);
f6ed5ca267d42d2a060f8447acdeb647ef077b6dRobert Mustacchi if (to->unixname == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL, gettext("Not enough memory.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
1fcc078ae7c0a359a9274d2a5a90547aceb213a6Robert Mustacchi
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (from->sidprefix != NULL) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov to->sidprefix = strdup(from->sidprefix);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (to->sidprefix == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL, gettext("Not enough memory.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowename_mapping_format(name_mapping_t *nm, char **out)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *winname = NULL;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore char *winname1 = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *unixname = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int maxlen;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *out = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm2winqn(nm, &winname1) < 0)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (shell_app(&winname, winname1, 1)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(winname1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(winname1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm2unixname(nm, &unixname)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(winname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* 10 is strlen("add -d\t\t\n") + 1 */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe maxlen = 10 + strlen(unixname) + strlen(winname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe *out = (char *)malloc(maxlen);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm->direction == IDMAP_DIRECTION_U2W) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) snprintf(*out, maxlen, "add -d\t%s\t%s\n",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe unixname, winname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) snprintf(*out, maxlen, "add %s\t%s\t%s\n",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nm->direction == IDMAP_DIRECTION_BI? "" : "-d",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe winname, unixname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(winname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(unixname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Initialize print_mapping variables. Must be called before print_mapping */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic print_handle_t *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweprint_mapping_init(format_t f, FILE *fi)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_handle_t *out;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe out = (print_handle_t *)malloc(sizeof (print_handle_t));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (out == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL, gettext("Not enough memory.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (NULL);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe out->format = f;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe out->file = fi;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe out->last = name_mapping_init();
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (out->last == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (NULL);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (out);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/* Finalize print_mapping. */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweprint_mapping_fini(print_handle_t *pnm)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *out = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int rc = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe switch (pnm->format) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case SMBUSERS:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (pnm->last->unixname != NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fprintf(pnm->file, "\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case DEFAULT_FORMAT:
ad0ef8fd06d1ac28108685495a9ba1244a20a5caRobert Mustacchi if (pnm->last->unixname == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe rc = name_mapping_format(pnm->last, &out);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (rc >= 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fprintf(pnm->file, "%s", out);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(out);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe default:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe ;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe name_mapping_fini(pnm->last);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(pnm);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (rc);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic char *
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweusermap_cfg_string(char *in)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe int len;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *out;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (INHIBITED(in))
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (strdup("\"\""));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe len = strlen(in);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (len == strcspn(in, " \t#"))
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (strdup(in));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe out = malloc(len + 3);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (out == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (NULL);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) snprintf(out, len + 3, "\"%s\"", in);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (out);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Compare two possibly NULL strings
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestrcmp_null(char *a, char *b)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (a == NULL && b == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (a == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (b == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (strcmp(a, b));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * This prints both name rules and ordinary mappings, based on the pnm_format
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * set in print_mapping_init().
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweprint_mapping(print_handle_t *pnm, name_mapping_t *nm)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *dirstring;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *winname = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *windomain = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *unixname = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe FILE *f = pnm->file;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe switch (pnm->format) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case MAPPING_NAME:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm2winqn(nm, &winname) < 0)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm2unixname(nm, &unixname) < 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(winname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* LINTED E_CASE_FALLTHRU */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case MAPPING_ID:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (pnm->format == MAPPING_ID) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm->sidprefix == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL,
b65dd972486b1f5913d705d2a0cb9c3fb189a9e0Robert Mustacchi gettext("SID not given.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe winname = sid_format(nm);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (winname == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe unixname = pid_format(nm->pid, nm->is_user);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (unixname == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(winname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe dirstring = direction2string(nm->direction);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fprintf(f, "%s\t%s\t%s\n", winname, dirstring,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe unixname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case SMBUSERS:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm->is_user != I_YES || nm->is_wuser != I_YES) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe gettext("Group rule: "));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe f = stderr;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else if (nm->direction == IDMAP_DIRECTION_U2W) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe gettext("Opposite direction of the mapping: "));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe f = stderr;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else if (INHIBITED(nm->winname) || INHIBITED(nm->unixname)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL, gettext("Inhibited rule: "));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe f = stderr;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
ead9bb4b1be81d7bbf8ed86ee41d6c1e58b069a3Yuri Pankov
ead9bb4b1be81d7bbf8ed86ee41d6c1e58b069a3Yuri Pankov if (shell_app(&winname, nm->winname, 1))
ead9bb4b1be81d7bbf8ed86ee41d6c1e58b069a3Yuri Pankov return (-1);
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi unixname = INHIBITED(nm->unixname) ? "\"\"" : nm->unixname;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (pnm->file != f) {
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi (void) fprintf(f, "%s=%s\n", unixname, winname);
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi } else if (pnm->last->unixname != NULL &&
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe strcmp(pnm->last->unixname, unixname) == 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fprintf(f, " %s", winname);
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi } else {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (pnm->last->unixname != NULL) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore (void) fprintf(f, "\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(pnm->last->unixname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe pnm->last->unixname = strdup(unixname);
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi if (pnm->last->unixname == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore gettext("Not enough memory.\n"));
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fprintf(f, "%s=%s", unixname, winname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore unixname = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case USERMAP_CFG:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm->is_user != I_YES || nm->is_wuser != I_YES) {
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi print_error(NULL,
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi gettext("Group rule: "));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe f = stderr;
195b26986e3c19e916bf0991a1af7ae87d43010bRobert Mustacchi }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore dirstring = direction2string(nm->direction);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if ((winname = usermap_cfg_string(nm->winname)) == NULL ||
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (unixname = usermap_cfg_string(nm->unixname)) == NULL ||
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (windomain = usermap_cfg_string(nm->windomain)) == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL, gettext("Not enough memory.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(winname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(unixname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(windomain);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm->windomain == NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fprintf(f, "%s\t%s\t%s\n",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe winname, dirstring, unixname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fprintf(f, nm->is_nt4 ?
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "%s\\%s\t%s\t%s\n" :
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe "%2$s@%1$s\t%3$s\t%4$s\n",
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe windomain, winname, dirstring, unixname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* This is a format for namerules */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case DEFAULT_FORMAT:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /*
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * If nm is the same as the last one except is_wuser, we combine
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * winuser & wingroup to winname
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm->direction == pnm->last->direction &&
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nm->is_user == pnm->last->is_user &&
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore strcmp_null(pnm->last->unixname, nm->unixname) == 0 &&
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore strcmp_null(pnm->last->winname, nm->winname) == 0 &&
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore strcmp_null(pnm->last->windomain, nm->windomain) == 0) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore pnm->last->is_wuser = I_UNKNOWN;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore } else {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (pnm->last->unixname != NULL ||
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe pnm->last->winname != NULL) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *out = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (name_mapping_format(pnm->last, &out) < 0)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) fprintf(f, "%s", out);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(out);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (name_mapping_cpy(pnm->last, nm) < 0)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe default:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* This can never happen: */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe gettext("Internal error: invalid print format.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(winname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(unixname);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe free(windomain);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweprint_how(idmap_how *how)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe idmap_namerule *rule;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe name_mapping_t nm;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char *rule_text;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe switch (how->map_type) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case IDMAP_MAP_TYPE_DS_AD:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) printf(gettext("Method:\tAD Directory\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) printf(gettext("DN:\t%s\n"),
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe how->idmap_how_u.ad.dn);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) printf(gettext("Attribute:\t%s=%s\n"),
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe how->idmap_how_u.ad.attr,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe how->idmap_how_u.ad.value);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case IDMAP_MAP_TYPE_DS_NLDAP:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) printf(gettext("Method:\tNative LDAP Directory\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) printf(gettext("DN:\t%s\n"),
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe how->idmap_how_u.nldap.dn);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) printf(gettext("Attribute:\t%s=%s\n"),
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe how->idmap_how_u.nldap.attr,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe how->idmap_how_u.nldap.value);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case IDMAP_MAP_TYPE_RULE_BASED:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) printf(gettext("Method:\tName Rule\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe rule = &how->idmap_how_u.rule;
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amore /*
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * The name rules as specified by the user can have a
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amore * "winname", "winuser" or "wingroup". "Winname" rules are
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * decomposed to a "winuser" and "wingroup" rules by idmap.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * Currently is_wuser is a boolean. Due to these reasons
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * the returned is_wuser does not represent the original rule.
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * It is therefore better set is_wuser to unknown.
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nm.is_user = rule->is_user;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nm.is_wuser = I_UNKNOWN;
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amore nm.direction = rule->direction;
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amore nm.winname = rule->winname;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nm.windomain = rule->windomain;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nm.unixname = rule->unixname;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nm.is_nt4 = rule->is_nt4;
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amore if (name_mapping_format(&nm, &rule_text) == 0) {
538aa54d819fa7751ca82bcc30d4ed8c57ec2ef2Garrett D'Amore (void) printf(gettext("Rule:\t%s"), rule_text);
538aa54d819fa7751ca82bcc30d4ed8c57ec2ef2Garrett D'Amore free(rule_text);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
538aa54d819fa7751ca82bcc30d4ed8c57ec2ef2Garrett D'Amore
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case IDMAP_MAP_TYPE_EPHEMERAL:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) printf(gettext("Method:\tEphemeral\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case IDMAP_MAP_TYPE_LOCAL_SID:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) printf(gettext("Method:\tLocal SID\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore case IDMAP_MAP_TYPE_KNOWN_SID:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore (void) printf(gettext("Method:\tWell-Known mapping\n"));
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore break;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore}
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowevoid
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Loweprint_info(idmap_info *info)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (info->how.map_type != IDMAP_MAP_TYPE_UNKNOWN) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe switch (info->src) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case IDMAP_MAP_SRC_NEW:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) printf(gettext("Source:\tNew\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case IDMAP_MAP_SRC_CACHE:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) printf(gettext("Source:\tCache\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case IDMAP_MAP_SRC_HARD_CODED:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) printf(gettext("Source:\tHard Coded\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe case IDMAP_MAP_SRC_ALGORITHMIC:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe (void) printf(gettext("Source:\tAlgorithmic\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_how(&info->how);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovvoid
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovprint_error_info(idmap_info *info)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_how *how = &info->how;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_namerule *rule;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov name_mapping_t nm;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *rule_text;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov switch (how->map_type) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov case IDMAP_MAP_TYPE_DS_AD:
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) fprintf(stderr,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Failed Method:\tAD Directory\n"));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) fprintf(stderr, gettext("DN:\t%s\n"),
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov how->idmap_how_u.ad.dn);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) fprintf(stderr, gettext("Attribute:\t%s=%s\n"),
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov how->idmap_how_u.ad.attr,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov how->idmap_how_u.ad.value);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov break;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov case IDMAP_MAP_TYPE_DS_NLDAP:
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) fprintf(stderr,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Failed Method:\tNative LDAP Directory\n"));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) fprintf(stderr, gettext("DN:\t%s\n"),
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov how->idmap_how_u.nldap.dn);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) fprintf(stderr, gettext("Attribute:\t%s=%s\n"),
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov how->idmap_how_u.nldap.attr,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov how->idmap_how_u.nldap.value);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov break;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov case IDMAP_MAP_TYPE_RULE_BASED:
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) fprintf(stderr, gettext("Failed Method:\tName Rule\n"));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov rule = &how->idmap_how_u.rule;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /*
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * The name rules as specified by the user can have a
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * "winname", "winuser" or "wingroup". "Winname" rules are
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * decomposed to a "winuser" and "wingroup" rules by idmap.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * Currently is_wuser is a boolean. Due to these reasons
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * the returned is_wuser does not represent the original rule.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * It is therefore better to set is_wuser to unknown.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm.is_user = rule->is_user;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm.is_wuser = I_UNKNOWN;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm.direction = rule->direction;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm.winname = rule->winname;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm.windomain = rule->windomain;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm.unixname = rule->unixname;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm.is_nt4 = rule->is_nt4;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (name_mapping_format(&nm, &rule_text) == 0) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) fprintf(stderr, gettext("Rule:\t%s"), rule_text);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov free(rule_text);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov break;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov case IDMAP_MAP_TYPE_EPHEMERAL:
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) fprintf(stderr, gettext("Failed Method:\tEphemeral\n"));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov break;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov case IDMAP_MAP_TYPE_LOCAL_SID:
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) fprintf(stderr, gettext("Failed Method:\tLocal SID\n"));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov break;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov case IDMAP_MAP_TYPE_KNOWN_SID:
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) fprintf(stderr,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Failed Method:\tWell-Known mapping\n"));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov break;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/* dump command handler */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic int
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/* LINTED E_FUNC_ARG_UNUSED */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovdo_dump(flag_t *f, int argc, char **argv, cmd_pos_t *pos)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_stat stat;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_iter_t *ihandle;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov int rc = 0;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov boolean_t is_user;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov boolean_t is_wuser;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_handle_t *ph;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov int flag = 0;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_info info;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (init_command())
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov ph = print_mapping_init(f[n_FLAG] != NULL ? MAPPING_NAME : MAPPING_ID,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov stdout);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (ph == NULL)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (f[v_FLAG] != NULL)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov flag = IDMAP_REQ_FLG_MAPPING_INFO;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov stat = idmap_iter_mappings(handle, &ihandle, flag);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (stat < 0) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Iteration handle not obtained (%s)\n"),
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_stat2string(handle, stat));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov rc = -1;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov goto cleanup;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov do {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov name_mapping_t *nm = name_mapping_init();
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (nm == NULL) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov rc = -1;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov goto cleanup;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov stat = idmap_iter_next_mapping(ihandle,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov &nm->sidprefix, &nm->rid, &nm->pid,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov &nm->winname, &nm->windomain,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov &nm->unixname, &is_user, &is_wuser,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov &nm->direction, &info);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->is_user = is_user ? I_YES : I_NO;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->is_wuser = is_wuser ? I_YES : I_NO;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (stat >= 0) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) print_mapping(ph, nm);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) print_how(&info.how);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_info_free(&info);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov name_mapping_fini(nm);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov } while (stat > 0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* IDMAP_ERR_NOTFOUND indicates end of the list */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (stat < 0 && stat != IDMAP_ERR_NOTFOUND) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore gettext("Error during iteration (%s)\n"),
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore idmap_stat2string(handle, stat));
820218f30a3ad84d92aa2970dcac9eb5cf69aaa9Robert Mustacchi rc = -1;
820218f30a3ad84d92aa2970dcac9eb5cf69aaa9Robert Mustacchi goto cleanup;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_iter_destroy(ihandle);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovcleanup:
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) print_mapping_fini(ph);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov fini_command();
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (rc);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/*
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * The same as strdup, but length chars is duplicated, no matter on
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * '\0'. The caller must guarantee "length" chars in "from".
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic char *
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstrndup(char *from, size_t length)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *out = (char *)malloc(length + 1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (out == NULL) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(NULL, gettext("Not enough memory\n"));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (NULL);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) strncpy(out, from, length);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov out[length] = '\0';
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (out);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/*
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * Convert pid from string to it's numerical representation. If it is
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * a valid string, i.e. number of a proper length, return 1. Otherwise
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * print an error message and return 0.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic int
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovpid_convert(char *string, uid_t *number, int type, cmd_pos_t *pos)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore int i;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov long long ll;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore char *type_string;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov size_t len = strlen(string);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (type == TYPE_GID)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov type_string = ID_GID;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore else if (type == TYPE_UID)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov type_string = ID_UID;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore else
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov for (i = 0; i < len; i++) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (!isdigit(string[i])) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore gettext("\"%s\" is not a valid %s: the non-digit"
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov " character '%c' found.\n"), string,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore type_string, string[i]);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov ll = atoll(string);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* Isn't it too large? */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (type == TYPE_UID && (uid_t)ll != ll ||
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov type == TYPE_GID && (gid_t)ll != ll) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("%llu: too large for a %s.\n"), ll,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov type_string);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov *number = (uid_t)ll;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/*
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * Convert SID from string to prefix and rid. If it has a valid
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * format, i.e. S(\-\d+)+, return 1. Otherwise print an error
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * message and return 0.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic int
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovsid_convert(char *from, char **prefix, idmap_rid_t *rid, cmd_pos_t *pos)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov int i, j;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *cp;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *ecp;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *prefix_end;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov u_longlong_t a;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov unsigned long r;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (strcmp_no0(from, "S-1-") != 0) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Invalid %s \"%s\": it doesn't start "
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov "with \"%s\".\n"), ID_SID, from, "S-1-");
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (strlen(from) <= strlen("S-1-")) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Invalid %s \"%s\": the authority and RID parts are"
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov " missing.\n"),
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov ID_SID, from);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* count '-'s */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov for (j = 0, cp = strchr(from, '-');
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov cp != NULL;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov j++, cp = strchr(cp + 1, '-')) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* can't end on a '-' */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (*(cp + 1) == '\0') {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Invalid %s \"%s\": '-' at the end.\n"),
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov ID_SID, from);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov } else if (*(cp + 1) == '-') {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Invalid %s \"%s\": double '-'.\n"),
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov ID_SID, from);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* check that we only have digits and '-' */
e232d9863a8486cf94eaa4bc06c2e9ff52bf3140Robert Mustacchi i = strspn(from + 1, "0123456789-") + 1;
e232d9863a8486cf94eaa4bc06c2e9ff52bf3140Robert Mustacchi if (i < strlen(from)) {
e232d9863a8486cf94eaa4bc06c2e9ff52bf3140Robert Mustacchi print_error(pos,
e232d9863a8486cf94eaa4bc06c2e9ff52bf3140Robert Mustacchi gettext("Invalid %s \"%s\": invalid character '%c'.\n"),
e232d9863a8486cf94eaa4bc06c2e9ff52bf3140Robert Mustacchi ID_SID, from, from[i]);
e232d9863a8486cf94eaa4bc06c2e9ff52bf3140Robert Mustacchi return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov cp = from + strlen("S-1-");
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* 64-bit safe parsing of unsigned 48-bit authority value */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov errno = 0;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov a = strtoull(cp, &ecp, 10);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* errors parsing the authority or too many bits */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (cp == ecp || (a == 0 && errno == EINVAL)) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Invalid %s \"%s\": unable to parse the "
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov "authority \"%.*s\".\n"), ID_SID, from, ecp - cp,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov cp);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if ((a == ULLONG_MAX && errno == ERANGE) ||
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (a & 0x0000ffffffffffffULL) != a) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Invalid %s \"%s\": the authority "
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov "\"%.*s\" is too large.\n"), ID_SID, from,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov ecp - cp, cp);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov cp = ecp;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (j < 3) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Invalid %s \"%s\": must have at least one RID.\n"),
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov ID_SID, from);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov for (i = 2; i < j; i++) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (*cp++ != '-') {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* Should never happen */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Invalid %s \"%s\": internal error:"
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov " '-' missing.\n"),
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov ID_SID, from);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* 32-bit safe parsing of unsigned 32-bit RID */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov errno = 0;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov r = strtoul(cp, &ecp, 10);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* errors parsing the RID */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (cp == ecp || (r == 0 && errno == EINVAL)) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* should never happen */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Invalid %s \"%s\": internal error: "
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov "unable to parse the RID "
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov "after \"%.*s\".\n"), ID_SID,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov from, cp - from, from);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (r == ULONG_MAX && errno == ERANGE) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Invalid %s \"%s\": the RID \"%.*s\""
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov " is too large.\n"), ID_SID,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov from, ecp - cp, cp);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov prefix_end = cp;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov cp = ecp;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* check that all of the string SID has been consumed */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (*cp != '\0') {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* Should never happen */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Invalid %s \"%s\": internal error: "
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov "something is still left.\n"),
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov ID_SID, from);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
d2b9ba291ef0d1dc8807b6d46996674c723924d0Robert Mustacchi *rid = (idmap_rid_t)r;
d2b9ba291ef0d1dc8807b6d46996674c723924d0Robert Mustacchi
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* -1 for the '-' at the end: */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov *prefix = strndup(from, prefix_end - from - 1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (*prefix == NULL) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
b65dd972486b1f5913d705d2a0cb9c3fb189a9e0Robert Mustacchi gettext("Not enough memory.\n"));
b65dd972486b1f5913d705d2a0cb9c3fb189a9e0Robert Mustacchi return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/* Does the line start with USERMAP_CFG IP qualifier? */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic int
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovucp_is_IP_qualifier(char *line)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *it;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov it = line + strcspn(line, " \t\n#:");
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (*(it + 1) == ':' ? 1 : 0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/*
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * returns interior of quotation marks in USERMAP_CFG. In this format,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * there cannot be a protected quotation mark inside.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic char *
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovucp_qm_interior(char **line, cmd_pos_t *pos)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *out;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *qm = strchr(*line + 1, '"');
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (qm == NULL) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Unclosed quotations\n"));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (NULL);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov out = strndup(*line + 1, qm - *line - 1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov *line = qm + 1;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (out);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/*
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * Grab next token from the line in USERMAP_CFG format. terminators,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * the 3rd parameter, contains all the characters which can terminate
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * the token. line_num is the line number of input used for error
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * reporting.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic char *
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovucp_grab_token(char **line, cmd_pos_t *pos, const char *terminators)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *token;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (**line == '"')
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov token = ucp_qm_interior(line, pos);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov else {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov int length = strcspn(*line, terminators);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov token = strndup(*line, length);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov *line += length;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (token);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore/*
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * Convert a line in usermap.cfg format to name_mapping.
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore *
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * Return values: -1 for error, 0 for empty line, 1 for a mapping
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * found.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic int
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovucp_line2nm(char *line, cmd_pos_t *pos, name_mapping_t *nm)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *it;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *token;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *token2;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char separator;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov int is_direction = 0;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov it = line + strspn(line, " \t\n");
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* empty or comment lines are OK: */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (*it == '\0' || *it == '#')
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* We do not support network qualifiers */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (ucp_is_IP_qualifier(it)) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Unable to handle network qualifier.\n"));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore /* The windows name: */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore token = ucp_grab_token(&it, pos, " \t#\\\n@=<");
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (token == NULL)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov separator = *it;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* Didn't we bump to the end of line? */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (separator == '\0' || separator == '#') {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov free(token);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore print_error(pos,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore gettext("UNIX_name not found.\n"));
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (-1);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* Do we have a domainname? */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (separator == '\\' || separator == '@') {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore it ++;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore token2 = ucp_grab_token(&it, pos, " \t\n#");
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (token2 == NULL) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore free(token);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore } else if (*it == '\0' || *it == '#') {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov free(token);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore free(token2);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore gettext("UNIX_name not found.\n"));
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (separator == '\\') {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->windomain = token;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->winname = token2;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->is_nt4 = 1;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore } else {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->windomain = token2;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nm->winname = token;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->is_nt4 = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->windomain = NULL;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->winname = token;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->is_nt4 = 0;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov it = it + strspn(it, " \t\n");
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* Direction string is optional: */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (strncmp(it, "==", 2) == 0) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->direction = IDMAP_DIRECTION_BI;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe is_direction = 1;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore } else if (strncmp(it, "<=", 2) == 0) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->direction = IDMAP_DIRECTION_U2W;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore is_direction = 1;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore } else if (strncmp(it, "=>", 2) == 0) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->direction = IDMAP_DIRECTION_W2U;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore is_direction = 1;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore } else {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->direction = IDMAP_DIRECTION_BI;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore is_direction = 0;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (is_direction) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore it += 2;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore it += strspn(it, " \t\n");
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (*it == '\0' || *it == '#') {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("UNIX_name not found.\n"));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* Now unixname: */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov it += strspn(it, " \t\n");
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov token = ucp_grab_token(&it, pos, " \t\n#");
f6ed5ca267d42d2a060f8447acdeb647ef077b6dRobert Mustacchi
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (token == NULL)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* nm->winname to be freed by name_mapping_fini */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
1fcc078ae7c0a359a9274d2a5a90547aceb213a6Robert Mustacchi
1fcc078ae7c0a359a9274d2a5a90547aceb213a6Robert Mustacchi /* Neither here we support IP qualifiers */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (ucp_is_IP_qualifier(token)) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Unable to handle network qualifier.\n"));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov free(token);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->unixname = token;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov it += strspn(it, " \t\n");
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* Does something remain on the line */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (*it != '\0' && *it != '#') {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe gettext("Unrecognized parameters \"%s\".\n"), it);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/*
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * Parse SMBUSERS line to name_mapping_t. if line is NULL, then
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * pasrsing of the previous line is continued. line_num is input line
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * number used for error reporting.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * Return values:
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * rc -1: error
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * rc = 0: mapping found and the line is finished,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * rc = 1: mapping found and there remains other on the line
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amoresup_line2nm(char *line, cmd_pos_t *pos, name_mapping_t *nm)
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore{
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore static char *ll = NULL;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore static char *unixname = NULL;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore static size_t unixname_l = 0;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *token;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (line != NULL) {
5dbfd19ad5fcc2b779f40f80fa05c1bd28fd0b4eTheo Schlossnagle ll = line;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe unixname = ll += strspn(ll, " \t");
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (*ll == '\0' || *ll == '#')
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe unixname_l = strcspn(ll, " \t:=#\n");
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov ll += unixname_l;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (*ll == '\0'|| *ll == '#')
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov ll += strspn(ll, " \t:=#\n");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (*ll == '\0'|| *ll == '#')
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe token = ucp_grab_token(&ll, pos, " \t\n");
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (token == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nm->is_nt4 = 0;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->direction = IDMAP_DIRECTION_W2U;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->windomain = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nm->winname = token;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->unixname = strndup(unixname, unixname_l);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (nm->unixname == NULL)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov ll += strspn(ll, " \t\n");
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/* Parse line to name_mapping_t. Basicaly just a format switch. */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic int
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovline2nm(char *line, cmd_pos_t *pos, name_mapping_t *nm, format_t f)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov switch (f) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov case USERMAP_CFG:
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (line == NULL)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov else
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (ucp_line2nm(line, pos, nm));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov case SMBUSERS:
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (sup_line2nm(line, pos, nm));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov default:
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* This can never happen */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Internal error: invalid line format.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/* Examine -f flag and return the appropriate format_t */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic format_t
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovff2format(char *ff, int is_mandatory)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (ff == NULL && is_mandatory) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(NULL, gettext("Format not given.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (UNDEFINED_FORMAT);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (ff == NULL)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (DEFAULT_FORMAT);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (strcasecmp(ff, "usermap.cfg") == 0)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (USERMAP_CFG);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (strcasecmp(ff, "smbusers") == 0)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (SMBUSERS);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(NULL,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe gettext("The only known formats are: \"usermap.cfg\" and "
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov "\"smbusers\".\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (UNDEFINED_FORMAT);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/* Delete all namerules of the given type */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovflush_nm(boolean_t is_user, cmd_pos_t *pos)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_stat stat;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov stat = idmap_udt_flush_namerules(udt);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (stat < 0) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov is_user ? gettext("Unable to flush users (%s).\n")
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe : gettext("Unable to flush groups (%s).\n"),
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_stat2string(handle, stat));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (positions_add(pos) < 0)
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/* import command handler */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowestatic int
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/* LINTED E_FUNC_ARG_UNUSED */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowedo_import(flag_t *f, int argc, char **argv, cmd_pos_t *pos)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe name_mapping_t *nm;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov cmd_pos_t pos2;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe char line[MAX_INPUT_LINE_SZ];
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov format_t format;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov int rc = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe idmap_stat stat;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov FILE *file = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (batch_mode) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Import is not allowed in the batch mode.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
ad0ef8fd06d1ac28108685495a9ba1244a20a5caRobert Mustacchi format = ff2format(argv[0], 1);
ad0ef8fd06d1ac28108685495a9ba1244a20a5caRobert Mustacchi if (format == UNDEFINED_FORMAT)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (init_udt_command())
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe /* We don't flush groups in the usermap.cfg nor smbusers format */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (f[F_FLAG] != NULL &&
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe flush_nm(B_TRUE, pos) < 0 &&
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (format == USERMAP_CFG || format == SMBUSERS ||
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe flush_nm(B_FALSE, pos) < 0)) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov rc = -1;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe goto cleanup;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* Where we import from? */
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (f[f_FLAG] == NULL)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov file = stdin;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov file = fopen(f[f_FLAG], "r");
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (file == NULL) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov perror(f[f_FLAG]);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe goto cleanup;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov pos2.linenum = 0;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe pos2.line = line;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov while (fgets(line, MAX_INPUT_LINE_SZ, file)) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *line2 = line;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe pos2.linenum++;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /*
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * In SMBUSERS format there can be more mappings on
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * each line. So we need the internal cycle for each line.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov do {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm = name_mapping_init();
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (nm == NULL) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov rc = -1;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov goto cleanup;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov rc = line2nm(line2, &pos2, nm, format);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov line2 = NULL;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (rc < 1) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe name_mapping_fini(nm);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov break;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe stat = idmap_udt_add_namerule(udt, nm->windomain,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->is_user ? B_TRUE : B_FALSE,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->is_wuser ? B_TRUE : B_FALSE,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->winname,
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe nm->unixname, nm->is_nt4, nm->direction);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (stat < 0) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(&pos2,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Transaction error (%s)\n"),
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_stat2string(handle, stat));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov rc = -1;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (rc >= 0)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov rc = positions_add(&pos2);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov name_mapping_fini(nm);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov } while (rc >= 0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (rc < 0) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe print_error(NULL,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Import canceled.\n"));
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe break;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovcleanup:
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (fini_udt_command((rc < 0 ? 0 : 1), pos))
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov rc = -1;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (file != NULL && file != stdin)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) fclose(file);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (rc);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe/*
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * List name mappings in the format specified. list_users /
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe * list_groups determine which type to list. The output goes to the
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * file fi.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic int
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovlist_name_mappings(format_t format, FILE *fi)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe idmap_stat stat;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_iter_t *ihandle;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe name_mapping_t *nm;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov boolean_t is_user;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe boolean_t is_wuser;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_handle_t *ph;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov stat = idmap_iter_namerules(handle, NULL, 0, 0, NULL, NULL, &ihandle);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (stat < 0) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(NULL,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Iteration handle not obtained (%s)\n"),
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_stat2string(handle, stat));
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_iter_destroy(ihandle);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov ph = print_mapping_init(format, fi);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (ph == NULL)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe do {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm = name_mapping_init();
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (nm == NULL) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_iter_destroy(ihandle);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov stat = idmap_iter_next_namerule(ihandle, &nm->windomain,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov &nm->winname, &nm->unixname, &is_user, &is_wuser,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov &nm->is_nt4, &nm->direction);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (stat >= 0) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->is_user = is_user ? I_YES : I_NO;
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->is_wuser = is_wuser ? I_YES : I_NO;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) print_mapping(ph, nm);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore name_mapping_fini(nm);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov } while (stat > 0);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) print_mapping_fini(ph);
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi if (stat < 0 && stat != IDMAP_ERR_NOTFOUND) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(NULL,
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore gettext("Error during iteration (%s)\n"),
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi idmap_stat2string(handle, stat));
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi idmap_iter_destroy(ihandle);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov idmap_iter_destroy(ihandle);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi/* Export command handler */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic int
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/* LINTED E_FUNC_ARG_UNUSED */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovdo_export(flag_t *f, int argc, char **argv, cmd_pos_t *pos)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore int rc;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov format_t format;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov FILE *fi;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi format = ff2format(argv[0], 1);
bad51a906c423d0d7ab33fcc1a4e317d789e3c49Robert Mustacchi if (format == UNDEFINED_FORMAT)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
195b26986e3c19e916bf0991a1af7ae87d43010bRobert Mustacchi
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* Where do we output to? */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (f[f_FLAG] == NULL)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov fi = stdout;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov else {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov fi = fopen(f[f_FLAG], "w");
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (fi == NULL) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov perror(f[f_FLAG]);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (init_command() < 0) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore rc = -1;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov goto cleanup;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* List the requested types: */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov rc = list_name_mappings(format, fi);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov fini_command();
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovcleanup:
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (fi != NULL && fi != stdout)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) fclose(fi);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (rc);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore/* List command handler */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic int
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/* LINTED E_FUNC_ARG_UNUSED */
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amoredo_list_name_mappings(flag_t *f, int argc, char **argv, cmd_pos_t *pos)
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov int rc;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (init_command()) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* List the requested types: */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov rc = list_name_mappings(DEFAULT_FORMAT, stdout);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov fini_command();
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (rc);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/* This is just a debug function for dumping flags */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic void
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovprint_flags(flag_t *f)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov int c;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov for (c = 0; c < FLAG_ALPHABET_SIZE; c++) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (f[c] == FLAG_SET)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) printf("FLAG: -%c, VALUE: %p\n", c,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void *) f[c]);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov else if (f[c])
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) printf("FLAG: -%c, VALUE: %s\n", c, f[c]);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore}
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore/*
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * Split argument to its identity code and a name part
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * return values:
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * -1 for unknown identity
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * 0 for no identity
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * <TYPE_XXX> for known identity
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovstatic int
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankovget_identity(char *arg, char **name, cmd_pos_t *pos)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov int i;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *it;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov int code;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if ((it = strchr(arg, ':')) == NULL) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov *name = arg;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi *it = '\0';
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi for (i = 0, code = 0;
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi i < sizeof (identity2code) / sizeof (id_code_t);
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi i++) {
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi if (strcmp(identity2code[i].identity, arg) == 0) {
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi code = identity2code[i].code;
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi break;
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi }
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi }
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi /* restore the original string: */
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi *it = ':';
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi
cb66c7814563eb32e20c1be88ae738ad8d63079dRobert Mustacchi if (!code) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov print_error(pos,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov gettext("Error: invalid identity type \"%.*s\"\n"),
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov it - arg, arg);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov *name = it + 1;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (code);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov}
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov/*
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * This function splits name to the relevant pieces: is_user, winname,
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * windomain unixname. E.g. for winname, it strdups nm->winname and possibly
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * nm->windomain and return TYPE_WN.
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov *
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov * If there is already one of the text fields allocated, it is OK.
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amore * Return values:
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amore * -1 ... syntax error
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * 0 ... it wasnt possible to determine
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore * <TYPE_XXX> otherwise
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amore */
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amore
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amorestatic int
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amorename2parts(char *name, name_mapping_t *nm, cmd_pos_t *pos)
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore{
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov char *it;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov int code;
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amore
f07f0fb66492a2792d4da5e0a6f9a92b4c581ab3Garrett D'Amore code = get_identity(name, &it, pos);
538aa54d819fa7751ca82bcc30d4ed8c57ec2ef2Garrett D'Amore
538aa54d819fa7751ca82bcc30d4ed8c57ec2ef2Garrett D'Amore switch (code) {
538aa54d819fa7751ca82bcc30d4ed8c57ec2ef2Garrett D'Amore case -1:
538aa54d819fa7751ca82bcc30d4ed8c57ec2ef2Garrett D'Amore /* syntax error: */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov case 0:
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* autodetection: */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (nm->winname != NULL && nm->is_wuser != I_UNKNOWN)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov code = nm->is_wuser == I_YES ? TYPE_UU : TYPE_UG;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov else if (nm->unixname != NULL ||
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov strchr(name, '@') != NULL ||
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov strchr(name, '\\') != NULL)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* btw, nm->is_user can never be I_UNKNOWN here */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov code = TYPE_WN;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov else
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (0);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov /* If the code was guessed succesfully, we are OK. */
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov break;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov default:
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov name = it;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (code & IS_WIN) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (code & IS_USER)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->is_wuser = I_YES;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov else if (code & IS_GROUP)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->is_wuser = I_NO;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov } else {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (code & IS_USER)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->is_user = I_YES;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov else if (code & IS_GROUP)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->is_user = I_NO;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov }
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (code & IS_WIN && code & IS_NAME) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if (nm->winname != NULL || nm->windomain != NULL)
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (code);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov if ((it = strchr(name, '@')) != NULL) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov int length = it - name + 1;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->winname = (char *)malloc(length);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) strncpy(nm->winname, name, length - 1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->winname[length - 1] = '\0';
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->windomain = strdup(it + 1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov } else if ((it = strrchr(name, '\\')) != NULL) {
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov int length = it - name + 1;
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->windomain = (char *)malloc(length);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov (void) strncpy(nm->windomain, name, length - 1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->windomain[length - 1] = '\0';
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->winname = strdup(it + 1);
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov nm->is_nt4 = B_TRUE;
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe } else
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->winname = strdup(name);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (code);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (!(code & IS_WIN) && code & IS_NAME) {
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (nm->unixname != NULL)
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (code);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore if (strlen(name) == 0)
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->unixname = strdup("\"\"");
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore else
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore nm->unixname = strdup(name);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore return (code);
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore }
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
2d08521bd15501c8370ba2153b9cca4f094979d0Garrett D'Amore
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (code & IS_WIN && !(code & IS_NAME)) {
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe if (!sid_convert(name, &nm->sidprefix, &nm->rid, pos))
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (-1);
c10c16dec587a0662068f6e2991c29ed3a9db943Richard Lowe else
a9478106a12424322498e53cf7cd75bd8a4d6004Yuri Pankov return (code);
}
/*
* it is (!(code & TYPE_WIN) && !(code & TYPE_NAME)) here - the other
* possiblities are exhausted.
*/
if (!pid_convert(name, &nm->pid, code, pos))
return (-1);
else
return (code);
}
/*
* Cycle through add/remove arguments until they are identified or found
* invalid.
*/
static
int
args2nm(name_mapping_t *nm, int *is_first_win, int argc, char **argv,
cmd_pos_t *pos)
{
int code;
int i;
for (i = 0; i < 2 * argc - 1; i++) {
code = name2parts(argv[i % 2], nm, pos);
switch (code) {
case -1:
return (-1);
case 0:
if (i > 0) {
print_error(pos,
gettext("Missing identity type"
" cannot be determined for %s.\n"),
argv[i % 2]);
return (-1);
}
break;
default:
if (!(code & IS_NAME)) {
print_error(pos,
gettext("%s is not a valid name\n"),
argv[i % 2]);
return (-1);
}
}
}
if (argc == 2 && nm->winname == NULL) {
print_error(pos, gettext("No windows identity found.\n"));
return (-1);
}
if (argc == 2 && nm->unixname == NULL) {
print_error(pos, gettext("No unix identity found.\n"));
return (-1);
}
*is_first_win = code & IS_WIN;
return (0);
}
/* add command handler. */
static int
do_add_name_mapping(flag_t *f, int argc, char **argv, cmd_pos_t *pos)
{
name_mapping_t *nm;
int rc = 0;
int is_first_win;
idmap_stat stat;
int is_wuser;
print_handle_t *ph;
/* Exactly two arguments must be specified */
if (argc < 2) {
print_error(pos, gettext("Not enough arguments.\n"));
return (-1);
} else if (argc > 2) {
print_error(pos, gettext("Too many arguments.\n"));
return (-1);
}
nm = name_mapping_init();
if (nm == NULL)
return (-1);
if (args2nm(nm, &is_first_win, argc, argv, pos) < 0) {
name_mapping_fini(nm);
return (-1);
}
if (f[d_FLAG] != NULL)
nm->direction = is_first_win
? IDMAP_DIRECTION_W2U
: IDMAP_DIRECTION_U2W;
else
nm->direction = IDMAP_DIRECTION_BI;
/* Now let us write it: */
if (init_udt_command()) {
name_mapping_fini(nm);
return (-1);
}
for (is_wuser = I_YES; is_wuser >= I_NO; is_wuser--) {
/* nm->is_wuser can be I_YES, I_NO or I_UNKNOWN */
if ((is_wuser == I_YES && nm->is_wuser == I_NO) ||
(is_wuser == I_NO && nm->is_wuser == I_YES))
continue;
stat = idmap_udt_add_namerule(udt, nm->windomain,
nm->is_user ? B_TRUE : B_FALSE,
is_wuser ? B_TRUE : B_FALSE,
nm->winname, nm->unixname, nm->is_nt4, nm->direction);
}
/* We echo the mapping */
ph = print_mapping_init(DEFAULT_FORMAT, stdout);
if (ph == NULL) {
rc = -1;
goto cleanup;
}
(void) print_mapping(ph, nm);
(void) print_mapping_fini(ph);
if (stat < 0) {
print_error(pos,
gettext("Mapping not created (%s)\n"),
idmap_stat2string(handle, stat));
rc = -1;
}
if (rc == 0)
rc = positions_add(pos);
cleanup:
name_mapping_fini(nm);
if (fini_udt_command(1, pos))
rc = -1;
return (rc);
}
/* remove command handler */
static int
do_remove_name_mapping(flag_t *f, int argc, char **argv, cmd_pos_t *pos)
{
name_mapping_t *nm;
int rc = 0;
idmap_stat stat;
int is_first_win;
int is_wuser;
/* "-a" means we flush all of them */
if (f[a_FLAG] != NULL) {
if (argc) {
print_error(pos,
gettext("Too many arguments.\n"));
return (-1);
}
if (init_udt_command())
return (-1);
rc = flush_nm(B_TRUE, pos);
if (rc >= 0)
rc = flush_nm(B_FALSE, pos);
if (fini_udt_command(rc ? 0 : 1, pos))
rc = -1;
return (rc);
}
/* Contrary to add_name_mapping, we can have only one argument */
if (argc < 1) {
print_error(pos, gettext("Not enough arguments.\n"));
return (-1);
} else if (argc > 2) {
print_error(pos, gettext("Too many arguments.\n"));
return (-1);
} else if (
/* both -f and -t: */
f[f_FLAG] != NULL && f[t_FLAG] != NULL ||
/* -d with a single argument: */
argc == 1 && f[d_FLAG] != NULL ||
/* -f or -t with two arguments: */
argc == 2 && (f[f_FLAG] != NULL || f[t_FLAG] != NULL)) {
print_error(pos,
gettext("Direction ambiguous.\n"));
return (-1);
}
/*
* Similar to do_add_name_mapping - see the comments
* there. Except we may have only one argument here.
*/
nm = name_mapping_init();
if (nm == NULL)
return (-1);
if (args2nm(nm, &is_first_win, argc, argv, pos) < 0) {
name_mapping_fini(nm);
return (-1);
}
/*
* If the direction is not specified by a -d/-f/-t flag, then it
* is IDMAP_DIRECTION_UNDEF, because in that case we want to
* remove any mapping. If it was IDMAP_DIRECTION_BI, idmap_api would
* delete a bidirectional one only.
*/
if (f[d_FLAG] != NULL || f[f_FLAG] != NULL)
nm->direction = is_first_win
? IDMAP_DIRECTION_W2U
: IDMAP_DIRECTION_U2W;
else if (f[t_FLAG] != NULL)
nm->direction = is_first_win
? IDMAP_DIRECTION_U2W
: IDMAP_DIRECTION_W2U;
else
nm->direction = IDMAP_DIRECTION_UNDEF;
if (init_udt_command()) {
name_mapping_fini(nm);
return (-1);
}
for (is_wuser = I_YES; is_wuser >= I_NO; is_wuser--) {
if ((is_wuser == I_YES && nm->is_wuser == I_NO) ||
(is_wuser == I_NO && nm->is_wuser == I_YES))
continue;
stat = idmap_udt_rm_namerule(udt,
nm->is_user ? B_TRUE : B_FALSE,
is_wuser ? B_TRUE : B_FALSE,
nm->windomain, nm->winname, nm->unixname, nm->direction);
if (stat < 0) {
print_error(pos,
gettext("Mapping not deleted (%s)\n"),
idmap_stat2string(handle, stat));
rc = -1;
break;
}
}
if (rc == 0)
rc = positions_add(pos);
cleanup:
name_mapping_fini(nm);
if (fini_udt_command(1, pos))
rc = -1;
return (rc);
}
/* exit command handler */
static int
/* LINTED E_FUNC_ARG_UNUSED */
do_exit(flag_t *f, int argc, char **argv, cmd_pos_t *pos)
{
return (0);
}
/* debug command handler: just print the parameters */
static int
/* LINTED E_STATIC_UNUSED */
debug_print_params(flag_t *f, int argc, char **argv, cmd_pos_t *pos)
{
int i;
#if 0
char *leaktest = (char *)malloc(100);
#endif
print_flags(f);
for (i = 0; i < argc; i++) {
(void) printf("Argument %d: %s\n", i, argv[i]);
}
(void) fflush(stdout);
return (0);
}
/*
* From name_mapping_t, asseble a string containing identity of the
* given type.
*/
static int
nm2type(name_mapping_t *nm, int type, char **to)
{
switch (type) {
case TYPE_SID:
case TYPE_USID:
case TYPE_GSID:
if (nm->sidprefix == NULL)
return (-1);
*to = sid_format(nm);
return (0);
case TYPE_WN:
case TYPE_WU:
case TYPE_WG:
return (nm2winqn(nm, to));
case TYPE_UID:
case TYPE_GID:
case TYPE_PID:
*to = pid_format(nm->pid, nm->is_user);
if (*to == NULL)
return (-1);
else
return (0);
case TYPE_UN:
case TYPE_UU:
case TYPE_UG:
return (nm2unixname(nm, to));
default:
/* This can never happen: */
print_error(NULL,
gettext("Internal error: invalid name type.\n"));
return (-1);
}
/* never reached */
}
/* show command handler */
static int
do_show_mapping(flag_t *f, int argc, char **argv, cmd_pos_t *pos)
{
idmap_stat stat = 0;
int flag;
idmap_stat map_stat = 0;
int type_from;
int type_to;
name_mapping_t *nm = NULL;
char *fromname;
char *toname;
idmap_info info;
(void) memset(&info, 0, sizeof (info));
if (argc == 0) {
print_error(pos,
gettext("No identity given\n"));
return (-1);
} else if (argc > 2) {
print_error(pos,
gettext("Too many arguments.\n"));
return (-1);
}
flag = f[c_FLAG] != NULL ? 0 : IDMAP_REQ_FLG_NO_NEW_ID_ALLOC;
flag |= f[v_FLAG] == NULL ? 0 : IDMAP_REQ_FLG_MAPPING_INFO;
if (init_command())
return (-1);
nm = name_mapping_init();
if (nm == NULL)
goto cleanup;
type_from = name2parts(argv[0], nm, pos);
if (type_from <= 0) {
stat = IDMAP_ERR_ARG;
goto cleanup;
}
/* Second, determine type_to: */
if (argc < 2) {
type_to = type_from & IS_WIN ? TYPE_PID : TYPE_SID;
if (type_from & IS_NAME)
type_to |= IS_NAME;
} else {
int i;
for (i = 0, type_to = 0;
i < sizeof (identity2code) / sizeof (id_code_t);
i++) {
if (strcmp(identity2code[i].identity, argv[1]) == 0) {
type_to = identity2code[i].code;
break;
}
}
if (!type_to) {
print_error(pos,
gettext("Error: invalid target type \"%s\"\n"),
argv[1]);
stat = IDMAP_ERR_ARG;
goto cleanup;
}
}
if (type_to & IS_WIN) {
if (type_to & IS_USER)
nm->is_wuser = I_YES;
else if (type_to & IS_GROUP)
nm->is_wuser = I_NO;
else
nm->is_wuser = I_UNKNOWN;
} else {
if (type_to & IS_USER)
nm->is_user = I_YES;
else if (type_to & IS_GROUP)
nm->is_user = I_NO;
}
/* Are both arguments the same OS side? */
if (!(type_from & IS_WIN ^ type_to & IS_WIN)) {
print_error(pos,
gettext("Direction ambiguous.\n"));
stat = IDMAP_ERR_ARG;
goto cleanup;
}
/*
* We have two interfaces for retrieving the mappings:
* idmap_get_sidbyuid & comp (the batch interface) and
* idmap_get_w2u_mapping & comp. We want to use both of them, because
* the former mimicks kernel interface better and the later offers the
* string names. In the batch case, our batch has always size 1.
*
* Btw, type_from cannot be IDMAP_PID, because there is no type string
* for it.
*/
if (type_from & IS_NAME || type_to & IS_NAME ||
type_from == TYPE_GSID || type_from == TYPE_USID ||
type_to == TYPE_GSID || type_to == TYPE_USID) {
if (type_from & IS_WIN) {
map_stat = idmap_get_w2u_mapping(handle,
nm->sidprefix,
&nm->rid,
nm->winname,
nm->windomain,
flag,
&nm->is_user, &nm->is_wuser,
&nm->pid,
&nm->unixname,
&nm->direction,
&info);
} else {
map_stat = idmap_get_u2w_mapping(handle,
&nm->pid,
nm->unixname,
flag,
nm->is_user, &nm->is_wuser,
&nm->sidprefix,
&nm->rid,
&nm->winname,
&nm->windomain,
&nm->direction,
&info);
}
} else {
/* batch handle */
idmap_get_handle_t *ghandle = NULL;
/* To be passed to idmap_get_uidbysid */
gid_t gid = UNDEFINED_GID;
/* To be passed to idmap_get_gidbysid */
uid_t uid = UNDEFINED_UID;
/* Create an in-memory structure for all the batch: */
stat = idmap_get_create(handle, &ghandle);
if (stat < 0) {
print_error(pos,
gettext("Unable to create handle for communicating"
" with idmapd(1M) (%s)\n"),
idmap_stat2string(handle, stat));
idmap_get_destroy(ghandle);
goto cleanup;
}
/* Schedule the request: */
if (type_to == TYPE_UID) {
stat = idmap_getext_uidbysid(ghandle,
nm->sidprefix,
nm->rid,
flag,
&uid,
&info,
&map_stat);
} else if (type_to == TYPE_GID) {
stat = idmap_getext_gidbysid(ghandle,
nm->sidprefix,
nm->rid,
flag,
&gid,
&info,
&map_stat);
} else if (type_to == TYPE_PID) {
stat = idmap_getext_pidbysid(ghandle,
nm->sidprefix,
nm->rid,
flag,
&nm->pid,
&nm->is_user,
&info,
&map_stat);
} else if (type_from == TYPE_UID) {
stat = idmap_getext_sidbyuid(ghandle,
nm->pid,
flag,
&nm->sidprefix,
&nm->rid,
&info,
&map_stat);
} else if (type_from == TYPE_GID) {
stat = idmap_getext_sidbygid(ghandle,
(gid_t)nm->pid,
flag,
&nm->sidprefix,
&nm->rid,
&info,
&map_stat);
} else {
/* This can never happen: */
print_error(pos,
gettext("Internal error in show.\n"));
exit(1);
}
if (stat < 0) {
print_error(pos,
gettext("Request for %.3s not sent (%s)\n"),
argv[0], idmap_stat2string(handle, stat));
idmap_get_destroy(ghandle);
goto cleanup;
}
/* Send the batch to idmapd and obtain results: */
stat = idmap_get_mappings(ghandle);
if (stat < 0) {
print_error(pos,
gettext("Mappings not obtained because of"
" RPC problem (%s)\n"),
idmap_stat2string(handle, stat));
idmap_get_destroy(ghandle);
goto cleanup;
}
/* Destroy the batch handle: */
idmap_get_destroy(ghandle);
if (type_to == TYPE_UID)
nm->pid = uid;
else if (type_to == TYPE_GID)
nm->pid = (uid_t)gid;
}
/*
* If there was -c flag, we do output whatever we can even in
* the case of error:
*/
if (map_stat < 0 && flag & IDMAP_REQ_FLG_NO_NEW_ID_ALLOC)
goto errormsg;
/*
* idmapd returns fallback uid/gid in case of errors. However
* it uses special sentinel value i.e 4294967295 (or -1) to
* indicate that falbback pid is not available either. In such
* case idmap(1M) should not display the mapping because there
* is no fallback mapping.
*/
if ((type_to == TYPE_UID || type_to == TYPE_GID ||
type_to == TYPE_PID) && nm->pid == UNDEFINED_UID)
goto errormsg;
if (nm2type(nm, type_from, &fromname) < 0)
goto errormsg;
if (nm2type(nm, type_to, &toname) < 0) {
if (!(flag & IDMAP_REQ_FLG_NO_NEW_ID_ALLOC))
(void) printf("%s -> %s:%u\n",
fromname,
type_to & IS_GROUP ? ID_GID : ID_UID,
UID_NOBODY);
free(fromname);
} else {
(void) printf("%s -> %s\n", fromname, toname);
free(fromname);
free(toname);
}
errormsg:
if (map_stat < 0) {
print_error(pos, gettext("Error:\t%s\n"),
idmap_stat2string(handle, map_stat));
print_error_info(&info);
} else
print_info(&info);
idmap_info_free(&info);
cleanup:
if (nm != NULL)
name_mapping_fini(nm);
fini_command();
return (stat < 0 || map_stat < 0 ? -1 : 0);
}
/* main function. Returns 1 for error, 0 otherwise */
int
main(int argc, char *argv[])
{
int rc;
/* set locale and domain for internationalization */
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
/* idmap_engine determines the batch_mode: */
rc = engine_init(sizeof (commands) / sizeof (cmd_ops_t),
commands,
argc - 1,
argv + 1,
&batch_mode);
if (rc < 0) {
(void) engine_fini();
if (rc == IDMAP_ENG_ERROR_SILENT)
help();
return (1);
}
udt_used = 0;
if (batch_mode) {
if (init_udt_batch() < 0)
return (1);
}
rc = run_engine(argc - 1, argv + 1);
if (batch_mode) {
batch_mode = 0;
if (fini_udt_command(rc == 0 ? 1 : 0, NULL))
rc = -1;
}
(void) engine_fini();
return (rc == 0 ? 0 : 1);
}