187866Ssheldonh/*
287866Ssheldonh * Copyright (c) 2000-2001, Boris Popov
387866Ssheldonh * All rights reserved.
487866Ssheldonh *
587866Ssheldonh * Redistribution and use in source and binary forms, with or without
687866Ssheldonh * modification, are permitted provided that the following conditions
787866Ssheldonh * are met:
887866Ssheldonh * 1. Redistributions of source code must retain the above copyright
987866Ssheldonh *    notice, this list of conditions and the following disclaimer.
1087866Ssheldonh * 2. Redistributions in binary form must reproduce the above copyright
1187866Ssheldonh *    notice, this list of conditions and the following disclaimer in the
1287866Ssheldonh *    documentation and/or other materials provided with the distribution.
1387866Ssheldonh * 3. All advertising materials mentioning features or use of this software
1487866Ssheldonh *    must display the following acknowledgement:
1587866Ssheldonh *    This product includes software developed by Boris Popov.
1687866Ssheldonh * 4. Neither the name of the author nor the names of any co-contributors
1787866Ssheldonh *    may be used to endorse or promote products derived from this software
1887866Ssheldonh *    without specific prior written permission.
1987866Ssheldonh *
2087866Ssheldonh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2187866Ssheldonh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2287866Ssheldonh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2387866Ssheldonh * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2487866Ssheldonh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2587866Ssheldonh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2687866Ssheldonh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2787866Ssheldonh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2887866Ssheldonh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2987866Ssheldonh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3087866Ssheldonh * SUCH DAMAGE.
3187866Ssheldonh *
3288282Ssheldonh * $Id: kiconv.c,v 1.3 2001/08/22 03:31:36 bp Exp $
33118042Speter * $FreeBSD$
3487866Ssheldonh */
3587866Ssheldonh
3687866Ssheldonh#include <sys/types.h>
3787866Ssheldonh#include <sys/iconv.h>
3887866Ssheldonh#include <sys/sysctl.h>
3987866Ssheldonh#include <ctype.h>
4087866Ssheldonh#include <errno.h>
41136700Sobrien#include <string.h>
4287866Ssheldonh
4388282Ssheldonh#ifdef APPLE
4488282Ssheldonh#include <sys/types.h>
4588282Ssheldonhextern uid_t real_uid, eff_uid;
4688282Ssheldonh#endif
4788282Ssheldonh
4887866Ssheldonhint
4987866Ssheldonhkiconv_add_xlat_table(const char *to, const char *from, const u_char *table)
5087866Ssheldonh{
5187866Ssheldonh	struct iconv_add_in din;
5287866Ssheldonh	struct iconv_add_out dout;
53118042Speter	size_t olen;
5487866Ssheldonh
55149415Simura	if (strlen(from) >= ICONV_CSNMAXLEN || strlen(to) >= ICONV_CSNMAXLEN)
5687866Ssheldonh		return EINVAL;
5787866Ssheldonh	din.ia_version = ICONV_ADD_VER;
5887866Ssheldonh	strcpy(din.ia_converter, "xlat");
5987866Ssheldonh	strcpy(din.ia_from, from);
6087866Ssheldonh	strcpy(din.ia_to, to);
6187866Ssheldonh	din.ia_data = table;
6287866Ssheldonh	din.ia_datalen = 256;
6387866Ssheldonh	olen = sizeof(dout);
6488282Ssheldonh#ifdef APPLE
6588282Ssheldonh        seteuid(eff_uid); /* restore setuid root briefly */
6688282Ssheldonh	if (sysctlbyname("net.smb.fs.iconv.add", &dout, &olen, &din, sizeof(din)) == -1) {
6788282Ssheldonh        	seteuid(real_uid); /* and back to real user */
6888282Ssheldonh		return errno;
6988282Ssheldonh	}
7088282Ssheldonh        seteuid(real_uid); /* and back to real user */
7188282Ssheldonh#else
7287866Ssheldonh	if (sysctlbyname("kern.iconv.add", &dout, &olen, &din, sizeof(din)) == -1)
7387866Ssheldonh		return errno;
7488282Ssheldonh#endif
7587866Ssheldonh	return 0;
7687866Ssheldonh}
7787866Ssheldonh
78