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 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 28 | #include "zebra/ipforward.h" |
| 29 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 30 | extern struct zebra_privs_t zserv_privs; |
| 31 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 32 | char proc_net_snmp[] = "/proc/net/snmp"; |
| 33 | |
| 34 | static void |
| 35 | dropline (FILE *fp) |
| 36 | { |
| 37 | int c; |
| 38 | |
| 39 | while ((c = getc (fp)) != '\n') |
| 40 | ; |
| 41 | } |
| 42 | |
| 43 | int |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 44 | ipforward (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 45 | { |
| 46 | FILE *fp; |
| 47 | int ipforwarding = 0; |
| 48 | char *pnt; |
| 49 | char buf[10]; |
| 50 | |
| 51 | fp = fopen (proc_net_snmp, "r"); |
| 52 | |
| 53 | if (fp == NULL) |
| 54 | return -1; |
| 55 | |
| 56 | /* We don't care about the first line. */ |
| 57 | dropline (fp); |
| 58 | |
| 59 | /* Get ip_statistics.IpForwarding : |
| 60 | 1 => ip forwarding enabled |
| 61 | 2 => ip forwarding off. */ |
| 62 | pnt = fgets (buf, 6, fp); |
| 63 | sscanf (buf, "Ip: %d", &ipforwarding); |
| 64 | |
paul | 44ebf84 | 2003-10-07 05:58:48 +0000 | [diff] [blame] | 65 | fclose(fp); |
| 66 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 67 | if (ipforwarding == 1) |
| 68 | return 1; |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | /* char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/conf/all/forwarding"; */ |
| 74 | char proc_ipv4_forwarding[] = "/proc/sys/net/ipv4/ip_forward"; |
| 75 | |
| 76 | int |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 77 | ipforward_on (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 78 | { |
| 79 | FILE *fp; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 80 | |
| 81 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 82 | zlog_err ("Can't raise privileges, %s", safe_strerror (errno) ); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 83 | |
| 84 | fp = fopen (proc_ipv4_forwarding, "w"); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 85 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 86 | if (fp == NULL) { |
| 87 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 88 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 89 | return -1; |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 90 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 91 | |
| 92 | fprintf (fp, "1\n"); |
| 93 | |
| 94 | fclose (fp); |
| 95 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 96 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 97 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 98 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 99 | return ipforward (); |
| 100 | } |
| 101 | |
| 102 | int |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 103 | ipforward_off (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 104 | { |
| 105 | FILE *fp; |
| 106 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 107 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 108 | zlog_err ("Can't raise privileges, %s", safe_strerror (errno)); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 109 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 110 | fp = fopen (proc_ipv4_forwarding, "w"); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 111 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 112 | if (fp == NULL) { |
| 113 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 114 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 115 | return -1; |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 116 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 117 | |
| 118 | fprintf (fp, "0\n"); |
| 119 | |
| 120 | fclose (fp); |
| 121 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 122 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 123 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 124 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 125 | return ipforward (); |
| 126 | } |
| 127 | #ifdef HAVE_IPV6 |
| 128 | |
| 129 | char proc_ipv6_forwarding[] = "/proc/sys/net/ipv6/conf/all/forwarding"; |
| 130 | |
| 131 | int |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 132 | ipforward_ipv6 (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 133 | { |
| 134 | FILE *fp; |
| 135 | char buf[5]; |
| 136 | int ipforwarding = 0; |
| 137 | |
| 138 | fp = fopen (proc_ipv6_forwarding, "r"); |
| 139 | |
| 140 | if (fp == NULL) |
| 141 | return -1; |
| 142 | |
| 143 | fgets (buf, 2, fp); |
| 144 | sscanf (buf, "%d", &ipforwarding); |
| 145 | |
hasso | 42a66d7 | 2005-03-07 08:19:44 +0000 | [diff] [blame] | 146 | fclose (fp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 147 | return ipforwarding; |
| 148 | } |
| 149 | |
| 150 | int |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 151 | ipforward_ipv6_on (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 152 | { |
| 153 | FILE *fp; |
| 154 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 155 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 156 | zlog_err ("Can't raise privileges, %s", safe_strerror (errno)); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 157 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 158 | fp = fopen (proc_ipv6_forwarding, "w"); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 159 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 160 | if (fp == NULL) { |
| 161 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 162 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 163 | return -1; |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 164 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 165 | |
| 166 | fprintf (fp, "1\n"); |
| 167 | |
| 168 | fclose (fp); |
| 169 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 170 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 171 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 172 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 173 | return ipforward_ipv6 (); |
| 174 | } |
| 175 | |
| 176 | int |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 177 | ipforward_ipv6_off (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 178 | { |
| 179 | FILE *fp; |
| 180 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 181 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 182 | zlog_err ("Can't raise privileges, %s", safe_strerror (errno)); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 183 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 184 | fp = fopen (proc_ipv6_forwarding, "w"); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 185 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 186 | if (fp == NULL) { |
| 187 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 188 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 189 | return -1; |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 190 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 191 | |
| 192 | fprintf (fp, "0\n"); |
| 193 | |
| 194 | fclose (fp); |
| 195 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 196 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 197 | zlog_err ("Can't lower privileges, %s", safe_strerror (errno)); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 198 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 199 | return ipforward_ipv6 (); |
| 200 | } |
| 201 | #endif /* HAVE_IPV6 */ |