112016SGirish.Moodalbail@Sun.COM /*
212016SGirish.Moodalbail@Sun.COM * CDDL HEADER START
312016SGirish.Moodalbail@Sun.COM *
412016SGirish.Moodalbail@Sun.COM * The contents of this file are subject to the terms of the
512016SGirish.Moodalbail@Sun.COM * Common Development and Distribution License (the "License").
612016SGirish.Moodalbail@Sun.COM * You may not use this file except in compliance with the License.
712016SGirish.Moodalbail@Sun.COM *
812016SGirish.Moodalbail@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
912016SGirish.Moodalbail@Sun.COM * or http://www.opensolaris.org/os/licensing.
1012016SGirish.Moodalbail@Sun.COM * See the License for the specific language governing permissions
1112016SGirish.Moodalbail@Sun.COM * and limitations under the License.
1212016SGirish.Moodalbail@Sun.COM *
1312016SGirish.Moodalbail@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
1412016SGirish.Moodalbail@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1512016SGirish.Moodalbail@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
1612016SGirish.Moodalbail@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
1712016SGirish.Moodalbail@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
1812016SGirish.Moodalbail@Sun.COM *
1912016SGirish.Moodalbail@Sun.COM * CDDL HEADER END
2012016SGirish.Moodalbail@Sun.COM */
2112016SGirish.Moodalbail@Sun.COM
2212016SGirish.Moodalbail@Sun.COM /*
2312926SMark.Haywood@Oracle.COM * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2412016SGirish.Moodalbail@Sun.COM */
2512016SGirish.Moodalbail@Sun.COM
2612016SGirish.Moodalbail@Sun.COM /*
2712016SGirish.Moodalbail@Sun.COM * Utility functions used by the ipmgmtd daemon.
2812016SGirish.Moodalbail@Sun.COM */
2912016SGirish.Moodalbail@Sun.COM
3012016SGirish.Moodalbail@Sun.COM #include <stddef.h>
3112016SGirish.Moodalbail@Sun.COM #include <stdlib.h>
3212016SGirish.Moodalbail@Sun.COM #include <stdio.h>
3312016SGirish.Moodalbail@Sun.COM #include <syslog.h>
3412016SGirish.Moodalbail@Sun.COM #include <stdarg.h>
3512926SMark.Haywood@Oracle.COM #include <unistd.h>
3612926SMark.Haywood@Oracle.COM #include <errno.h>
3712016SGirish.Moodalbail@Sun.COM #include "ipmgmt_impl.h"
3812016SGirish.Moodalbail@Sun.COM
3912926SMark.Haywood@Oracle.COM #define IPMGMT_BUFSIZ 1024
4012926SMark.Haywood@Oracle.COM
4112016SGirish.Moodalbail@Sun.COM void
ipmgmt_log(int pri,const char * fmt,...)4212016SGirish.Moodalbail@Sun.COM ipmgmt_log(int pri, const char *fmt, ...)
4312016SGirish.Moodalbail@Sun.COM {
4412016SGirish.Moodalbail@Sun.COM va_list alist;
4512016SGirish.Moodalbail@Sun.COM
4612016SGirish.Moodalbail@Sun.COM va_start(alist, fmt);
4712016SGirish.Moodalbail@Sun.COM vsyslog(pri, fmt, alist);
4812016SGirish.Moodalbail@Sun.COM va_end(alist);
4912016SGirish.Moodalbail@Sun.COM }
5012926SMark.Haywood@Oracle.COM
5112926SMark.Haywood@Oracle.COM /*
5212926SMark.Haywood@Oracle.COM * Copy a source file to a new destination. The source file will be
5312926SMark.Haywood@Oracle.COM * removed if rdonly is false (i.e., used when the source file resides
5412926SMark.Haywood@Oracle.COM * on a read-only file system).
5512926SMark.Haywood@Oracle.COM *
5612926SMark.Haywood@Oracle.COM * Returns 0 on success and errno on failure.
5712926SMark.Haywood@Oracle.COM */
5812926SMark.Haywood@Oracle.COM int
ipmgmt_cpfile(const char * src,const char * dst,boolean_t rdonly)5912926SMark.Haywood@Oracle.COM ipmgmt_cpfile(const char *src, const char *dst, boolean_t rdonly)
6012926SMark.Haywood@Oracle.COM {
6112926SMark.Haywood@Oracle.COM struct stat statbuf;
6212926SMark.Haywood@Oracle.COM FILE *sfp, *dfp;
6312926SMark.Haywood@Oracle.COM char buf[IPMGMT_BUFSIZ];
6412926SMark.Haywood@Oracle.COM int err = 0;
6512926SMark.Haywood@Oracle.COM
66*13125SGirish.Moodalbail@oracle.COM errno = 0;
6712926SMark.Haywood@Oracle.COM /*
6812926SMark.Haywood@Oracle.COM * Attempt to open the destination file first since we
6912926SMark.Haywood@Oracle.COM * want to optimize for the case where it is read-only
7012926SMark.Haywood@Oracle.COM * and will return EROFS.
7112926SMark.Haywood@Oracle.COM */
7212926SMark.Haywood@Oracle.COM if ((dfp = fopen(dst, "w+")) == NULL)
7312926SMark.Haywood@Oracle.COM return (errno);
7412926SMark.Haywood@Oracle.COM
7512926SMark.Haywood@Oracle.COM /*
7612926SMark.Haywood@Oracle.COM * Require that the source file exists.
7712926SMark.Haywood@Oracle.COM */
7812926SMark.Haywood@Oracle.COM if (stat(src, &statbuf) != 0) {
7912926SMark.Haywood@Oracle.COM err = errno;
8012926SMark.Haywood@Oracle.COM (void) fclose(dfp);
8112926SMark.Haywood@Oracle.COM return (err);
8212926SMark.Haywood@Oracle.COM }
8312926SMark.Haywood@Oracle.COM if ((sfp = fopen(src, "r")) == NULL) {
8412926SMark.Haywood@Oracle.COM err = errno;
8512926SMark.Haywood@Oracle.COM (void) fclose(dfp);
8612926SMark.Haywood@Oracle.COM return (err);
8712926SMark.Haywood@Oracle.COM }
8812926SMark.Haywood@Oracle.COM
8912926SMark.Haywood@Oracle.COM /*
9012926SMark.Haywood@Oracle.COM * Copy the file.
9112926SMark.Haywood@Oracle.COM */
92*13125SGirish.Moodalbail@oracle.COM while (fgets(buf, sizeof (buf), sfp) != NULL && errno == 0) {
93*13125SGirish.Moodalbail@oracle.COM (void) fputs(buf, dfp);
9412926SMark.Haywood@Oracle.COM if (errno != 0)
9512926SMark.Haywood@Oracle.COM break;
9612926SMark.Haywood@Oracle.COM }
9712926SMark.Haywood@Oracle.COM if (errno != 0)
9812926SMark.Haywood@Oracle.COM err = errno;
99*13125SGirish.Moodalbail@oracle.COM else if (fflush(dfp) == EOF)
100*13125SGirish.Moodalbail@oracle.COM err = errno;
10112926SMark.Haywood@Oracle.COM
10212926SMark.Haywood@Oracle.COM (void) fclose(sfp);
10312926SMark.Haywood@Oracle.COM (void) fclose(dfp);
10412926SMark.Haywood@Oracle.COM
10512926SMark.Haywood@Oracle.COM /*
10612926SMark.Haywood@Oracle.COM * If any error occurred, then remove the destination file.
10712926SMark.Haywood@Oracle.COM */
10812926SMark.Haywood@Oracle.COM if (err != 0) {
10912926SMark.Haywood@Oracle.COM (void) unlink(dst);
11012926SMark.Haywood@Oracle.COM return (err);
11112926SMark.Haywood@Oracle.COM }
11212926SMark.Haywood@Oracle.COM
11312926SMark.Haywood@Oracle.COM /*
11412926SMark.Haywood@Oracle.COM * Make sure the file attributes are correct.
11512926SMark.Haywood@Oracle.COM */
11612926SMark.Haywood@Oracle.COM if (chmod(dst, IPADM_FILE_MODE) != 0 ||
11712926SMark.Haywood@Oracle.COM chown(dst, UID_NETADM, GID_NETADM) != 0) {
11812926SMark.Haywood@Oracle.COM err = errno;
11912926SMark.Haywood@Oracle.COM (void) unlink(dst);
12012926SMark.Haywood@Oracle.COM return (err);
12112926SMark.Haywood@Oracle.COM }
12212926SMark.Haywood@Oracle.COM
12312926SMark.Haywood@Oracle.COM /*
12412926SMark.Haywood@Oracle.COM * If the source file does not reside on a read-only file system
12512926SMark.Haywood@Oracle.COM * then remove it.
12612926SMark.Haywood@Oracle.COM */
12712926SMark.Haywood@Oracle.COM if (!rdonly)
12812926SMark.Haywood@Oracle.COM (void) unlink(src);
12912926SMark.Haywood@Oracle.COM
13012926SMark.Haywood@Oracle.COM return (0);
13112926SMark.Haywood@Oracle.COM }
132