paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Fetch ipforward value by reading /proc filesystem. |
| 3 | * Copyright (C) 1997 Kunihiro Ishiguro |
| 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 7 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2, or (at your option) any |
| 10 | * later version. |
| 11 | * |
| 12 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 20 | * 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
| 24 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 25 | #include "log.h" |
| 26 | #include "privs.h" |
| 27 | |
| 28 | extern struct zebra_privs_t zserv_privs; |
| 29 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 30 | char proc_net_snmp[] = "/proc/net/snmp"; |
| 31 | |
| 32 | static void |
| 33 | dropline (FILE *fp) |
| 34 | { |
| 35 | int c; |
| 36 | |
| 37 | while ((c = getc (fp)) != '\n') |
| 38 | ; |
| 39 | } |
| 40 | |
| 41 | int |
| 42 | ipforward () |
| 43 | { |
| 44 | FILE *fp; |
| 45 | int ipforwarding = 0; |
| 46 | char *pnt; |
| 47 | char buf[10]; |
| 48 | |
| 49 | fp = fopen (proc_net_snmp, "r"); |
| 50 | |
| 51 | if (fp == NULL) |
| 52 | return -1; |
| 53 | |
| 54 | /* We don't care about the first line. */ |
| 55 | dropline (fp); |
| 56 | |
| 57 | /* Get ip_statistics.IpForwarding : |
| 58 | 1 => ip forwarding enabled |
| 59 | 2 => ip forwarding off. */ |
| 60 | pnt = fgets (buf, 6, fp); |
| 61 | sscanf (buf, "Ip: %d", &ipforwarding); |
| 62 | |
paul | 44ebf84 | 2003-10-07 05:58:48 +0000 | [diff] [blame] | 63 | fclose(fp); |
| 64 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 65 | if (ipforwarding == 1) |
| 66 | return 1; |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | /* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */ |
| 72 | char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward"; |
| 73 | |
| 74 | int |
| 75 | ipforward_on () |
| 76 | { |
| 77 | FILE *fp; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 78 | |
| 79 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 80 | zlog_err ("Can't raise privileges, %s", safe_strerror (errno) ); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 81 | |
| 82 | fp = fopen (proc_ipv4_forwarding, "w"); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 83 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 84 | if (fp == NULL) { |
| 85 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 86 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 87 | return -1; |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 88 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 89 | |
| 90 | fprintf (fp, "1\n"); |
| 91 | |
| 92 | fclose (fp); |
| 93 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 94 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 95 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 96 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 97 | return ipforward (); |
| 98 | } |
| 99 | |
| 100 | int |
| 101 | ipforward_off () |
| 102 | { |
| 103 | FILE *fp; |
| 104 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 105 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 106 | zlog_err ("Can't raise privileges, %s", safe_strerror (errno)); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 107 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 108 | fp = fopen (proc_ipv4_forwarding, "w"); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 109 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 110 | if (fp == NULL) { |
| 111 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 112 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 113 | return -1; |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 114 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 115 | |
| 116 | fprintf (fp, "0\n"); |
| 117 | |
| 118 | fclose (fp); |
| 119 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 120 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 121 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 122 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 123 | return ipforward (); |
| 124 | } |
| 125 | #ifdef HAVE_IPV6 |
| 126 | |
| 127 | char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding"; |
| 128 | |
| 129 | int |
| 130 | ipforward_ipv6 () |
| 131 | { |
| 132 | FILE *fp; |
| 133 | char buf[5]; |
| 134 | int ipforwarding = 0; |
| 135 | |
| 136 | fp = fopen (proc_ipv6_forwarding, "r"); |
| 137 | |
| 138 | if (fp == NULL) |
| 139 | return -1; |
| 140 | |
| 141 | fgets (buf, 2, fp); |
| 142 | sscanf (buf, "%d", &ipforwarding); |
| 143 | |
hasso | 42a66d7 | 2005-03-07 08:19:44 +0000 | [diff] [blame^] | 144 | fclose (fp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 145 | return ipforwarding; |
| 146 | } |
| 147 | |
| 148 | int |
| 149 | ipforward_ipv6_on () |
| 150 | { |
| 151 | FILE *fp; |
| 152 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 153 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 154 | zlog_err ("Can't raise privileges, %s", safe_strerror (errno)); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 155 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 156 | fp = fopen (proc_ipv6_forwarding, "w"); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 157 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 158 | if (fp == NULL) { |
| 159 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 160 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 161 | return -1; |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 162 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 163 | |
| 164 | fprintf (fp, "1\n"); |
| 165 | |
| 166 | fclose (fp); |
| 167 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 168 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 169 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 170 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 171 | return ipforward_ipv6 (); |
| 172 | } |
| 173 | |
| 174 | int |
| 175 | ipforward_ipv6_off () |
| 176 | { |
| 177 | FILE *fp; |
| 178 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 179 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 180 | zlog_err ("Can't raise privileges, %s", safe_strerror (errno)); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 181 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 182 | fp = fopen (proc_ipv6_forwarding, "w"); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 183 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 184 | if (fp == NULL) { |
| 185 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 186 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 187 | return -1; |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 188 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 189 | |
| 190 | fprintf (fp, "0\n"); |
| 191 | |
| 192 | fclose (fp); |
| 193 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 194 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 195 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 196 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 197 | return ipforward_ipv6 (); |
| 198 | } |
| 199 | #endif /* HAVE_IPV6 */ |