summaryrefslogtreecommitdiffstats
path: root/sysconfig/network-scripts/network-functions
blob: 4d937ac53fad0de02b8da99b89c76de5107fd384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# -*-Shell-script-*-
#
# This file is not a stand-alone shell script; it provides functions 
# to network scripts that source it.

need_config()
{
    [ -f "${CONFIG}" ] || \
       CONFIG=../networking/default/${1}
    [ -f "${CONFIG}" ] || \
       CONFIG=../networking/default/ifcfg-${1}
    [ -f "${CONFIG}" ] || \
       CONFIG="ifcfg-${1}"
}

source_config ()
{
    DEVNAME=`basename $CONFIG | sed 's/^ifcfg-//g'`

    if basename $CONFIG | grep '[^g]-' >/dev/null 2>&1 ; then
	PARENTCONFIG=`echo $CONFIG | sed 's/-[^-]*$//g'`
	PARENTDEVNAME=`basename $PARENTCONFIG | sed 's/^ifcfg-//g'`
	[ -f $PARENTCONFIG ] || {
	    echo $"Missing config file $PARENTCONFIG." >&2
	    exit 1
	}
	. $PARENTCONFIG
    fi
    . $CONFIG
}

expand_config ()
{
    if [ -z "${NETMASK}" ]; then
	eval `/bin/ipcalc --netmask ${IPADDR}`
    fi

    if [ -z "${PREFIX}" ]; then
	eval `/bin/ipcalc --prefix ${IPADDR} ${NETMASK}`
    fi

    if [ -z "${BROADCAST}" ]; then
	eval `/bin/ipcalc --broadcast ${IPADDR} ${NETMASK}`
    fi

    if [ -z "${NETWORK}" ]; then
	eval `/bin/ipcalc --network ${IPADDR} ${NETMASK}`
    fi
}

toggle_value()
{
	if [ -z "$2" ]
	then
		echo ''
	elif [ "$2" = "yes"  -o "$2" = "YES" ] ; then
		echo "$1 on"
	elif [ "$2" = "no"  -o "$2" = "NO" ] ; then
		echo "$1 off"
	else
		echo ''
	fi
}
 
do_netreport ()
{
  # Notify programs that have requested notification
  ( cd /var/run/netreport || exit
    for i in * ; do
      if [ -f $i ]; then
        OWNER=`ls -l $i | awk '{ print $3 }'`
	
        if [ "`id -u`" = "0" ]; then
	  su $OWNER -c "kill -SIGIO $i >/dev/null 2>&1 || rm -f $i >/dev/null 2>&1" > /dev/null 2>&1
	else
	  kill -SIGIO $i >/dev/null 2>&1 || rm -f $i >/dev/null 2>&1
	fi
      fi
    done
  )
}

is_available ()
{
 LC_ALL= LANG= ip -o link | grep -q $1
 if [ "$?" = "1" ]; then
    alias=`modprobe -c | awk "/^alias $1 / { print \\$3 }"`
    if [ -z "$alias" -o "$alias" = "off" ]; then
        return 2
    fi
    modprobe $alias >/dev/null 2>&1|| {
      return 1
    }
 else
        return 0
 fi
}

need_hostname()
{
    if [ "`hostname`" = "(none)" -o "`hostname`" = "localhost" -o \
	 "`hostname`" = "localhost.localdomain" ]; then
	 return 0
    else
	 return 1
    fi
}

set_hostname()
{
    hostname $1
    if ! grep search /etc/resolv.conf >/dev/null 2>&1; then
	domain=`echo $1 | sed 's/^[^\.]*\.//'`
	echo "search $domain" >> /etc/resolv.conf
    fi
}

check_device_down ()
{
    if echo $1 | grep -q ':' ; then
     if LC_ALL=C ifconfig -a 2>/dev/null | grep -q $1 ; then
	  retcode=1
     else
	  retcode=0
     fi
    else 
     if LC_ALL=C ip -o link ls dev $1 2>/dev/null | grep ",UP" >/dev/null 2>&1 ; then
	retcode=1
     else
	retcode=0
     fi
    fi
    return $retcode
}

check_link_down ()
{
    if [ -x /sbin/mii-tool ]; then
        LC_ALL=C ip link show $1 2>/dev/null| grep -q UP || ip link set $1 up >/dev/null 2>&1
	sleep 2
	/sbin/mii-tool >/dev/null 2>&1 || return 1
	output=`LC_ALL=C /sbin/mii-tool $1 2>&1`
	if echo $output | grep -q "Operation not supported"; then
	    return 1
	elif echo $output | grep -q "Invalid argument"; then
	    return 1
	elif echo $output | grep -q "link ok"; then
	    return 1
	elif echo $output | grep -q "No MII transceiver present"; then
	    return 1
	else
	    return 0
        fi
    fi
    return 0
}

check_default_route ()
{
   LC_ALL=C ip route | grep -q default
}

find_gateway_dev ()
{
    . /etc/sysconfig/network
    if [ "${GATEWAY}" != "" -a "${GATEWAY}" != "none" ] ; then
        export GATEWAY
        networks=`LC_ALL=C /sbin/route -n|awk '{print $1, ENVIRON["GATEWAY"],$3,$8}'|awk '{system("ipcalc --silent --network "$2" "$3" 2>/dev/null")}'|sed s^NETWORK=^^g`
        for net in $networks; do
            dev=`LC_ALL=C /sbin/route -n|grep ^$net|awk '{print $NF}'`
            if [ "$dev" != "" ] ; then
                GATEWAYDEV=$dev
            fi
        done
    fi
}

add_default_route ()
{
    . /etc/sysconfig/network
    find_gateway_dev
    if [ "${GATEWAY}" != "" -a \
 	 "${GATEWAY}" != "none" -a \
	 "${GATEWAYDEV}" != "" ] ; then
        if ! check_device_down $1; then
            if [ "$GATEWAY" = "0.0.0.0" ]; then
                /sbin/ip route add default dev ${GATEWAYDEV}
            else
                /sbin/ip route add default via ${GATEWAY}
            fi
        fi
    else
    	if [ -f /etc/default-route ]; then
		/sbin/ip route add default via `cat /etc/default-route`
		rm -f /etc/default-route
	fi
    fi
}

is_wireless_device ()
{
  if [ -x /sbin/iwconfig ]; then
    LC_ALL=C iwconfig $1 2>&1 | grep -q "no wireless extensions" || return 0
  fi
  return 1
}
href='#n363'>363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
/*
   SSSD

   tools_utils.c

   Copyright (C) Jakub Hrozek <jhrozek@redhat.com>        2009

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <talloc.h>
#include <tevent.h>
#include <popt.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>

#include "config.h"
#ifdef HAVE_SELINUX
#include <selinux/selinux.h>
#endif

#include "util/util.h"
#include "confdb/confdb.h"
#include "db/sysdb.h"
#include "tools/tools_util.h"
#include "tools/sss_sync_ops.h"

static int setup_db(struct tools_ctx *ctx)
{
    char *confdb_path;
    int ret;

    /* Create the event context */
    ctx->ev = tevent_context_init(ctx);
    if (ctx->ev == NULL) {
        DEBUG(1, ("Could not create event context\n"));
        return EIO;
    }

    confdb_path = talloc_asprintf(ctx, "%s/%s", DB_PATH, CONFDB_FILE);
    if (confdb_path == NULL) {
        return ENOMEM;
    }

    /* Connect to the conf db */
    ret = confdb_init(ctx, &ctx->confdb, confdb_path);
    if (ret != EOK) {
        DEBUG(1, ("Could not initialize connection to the confdb\n"));
        return ret;
    }

    ret = confdb_get_domain(ctx->confdb, "local", &ctx->local);
    if (ret != EOK) {
        DEBUG(1, ("Could not get 'local' domain: [%d] [%s]\n", ret, strerror(ret)));
        return ret;
    }

    /* open 'local' sysdb at default path */
    ret = sysdb_domain_init(ctx, ctx->ev, ctx->local, DB_PATH, &ctx->sysdb);
    if (ret != EOK) {
        DEBUG(1, ("Could not initialize connection to the sysdb\n"));
        return ret;
    }

    talloc_free(confdb_path);
    return EOK;
}

/*
 * Print poptUsage as well as our error message
 */
void usage(poptContext pc, const char *error)
{
    poptPrintUsage(pc, stderr, 0);
    if (error) fprintf(stderr, "%s", error);
}

int parse_groups(TALLOC_CTX *mem_ctx, const char *optstr, char ***_out)
{
    char **out;
    char *orig, *n, *o;
    char delim = ',';
    unsigned int tokens = 1;
    int i;

    orig = talloc_strdup(mem_ctx, optstr);
    if (!orig) return ENOMEM;

    n = orig;
    tokens = 1;
    while ((n = strchr(n, delim))) {
        n++;
        tokens++;
    }

    out = talloc_array(mem_ctx, char *, tokens+1);
    if (!out) {
        talloc_free(orig);
        return ENOMEM;
    }

    n = o = orig;
    for (i = 0; i < tokens; i++) {
        o = n;
        n = strchr(n, delim);
        if (!n) {
            break;
        }
        *n = '\0';
        n++;
        out[i] = talloc_strdup(out, o);
    }
    out[tokens-1] = talloc_strdup(out, o);
    out[tokens] = NULL;

    talloc_free(orig);
    *_out = out;
    return EOK;
}

int parse_group_name_domain(struct tools_ctx *tctx,
                            char **groups)
{
    int i;
    int ret;
    char *name = NULL;
    char *domain = NULL;

    if (!groups) {
        return EOK;
    }

    for (i = 0; groups[i]; ++i) {
        ret = sss_parse_name(tctx, tctx->snctx, groups[i], &domain, &name);

        /* If FQDN is specified, it must be within the same domain as user */
        if (domain) {
            if (strcmp(domain, tctx->octx->domain->name) != 0) {
                return EINVAL;
            }

            /* Use only groupname */
            talloc_zfree(groups[i]);
            groups[i] = talloc_strdup(tctx, name);
            if (groups[i] == NULL) {
                return ENOMEM;
            }
        }

        talloc_zfree(name);
        talloc_zfree(domain);
    }

    talloc_zfree(name);
    talloc_zfree(domain);
    return EOK;
}

int parse_name_domain(struct tools_ctx *tctx,
                      const char *fullname)
{
    int ret;
    char *domain = NULL;

    ret = sss_parse_name(tctx, tctx->snctx, fullname, &domain, &tctx->octx->name);
    if (ret != EOK) {