Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / appl / bsd / rcmd_util.c
1 /*
2  * Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include "bsd_locl.h"
35
36 RCSID("$Id: rcmd_util.c,v 1.19.2.1 2000/06/23 02:34:48 assar Exp $");
37
38 int
39 get_login_port(int kerberos, int encryption)
40 {
41   char *service="login";
42   int port=htons(513);
43
44   if(kerberos && encryption){
45     service="eklogin";
46     port=htons(2105);
47   }
48   
49   if(kerberos && !encryption){
50     service="klogin";
51     port=htons(543);
52   }
53   return k_getportbyname (service, "tcp", port);
54 }
55
56 int
57 get_shell_port(int kerberos, int encryption)
58 {
59   char *service="shell";
60   int port=htons(514);
61
62   if(kerberos && encryption){
63     service="ekshell";
64     port=htons(545);
65   }
66   
67   if(kerberos && !encryption){
68     service="kshell";
69     port=htons(544);
70   }
71
72   return k_getportbyname (service, "tcp", port);
73 }
74
75 /* 
76  * On reasonable systems, `cf[gs]et[io]speed' use values of bit/s
77  * directly, and the following functions are just identity functions.
78  * This is however a slower way of doing those
79  * should-be-but-are-not-always idenity functions.  
80  */
81
82 static struct { int speed; int bps; } conv[] = {
83 #ifdef B0
84     {B0, 0},
85 #endif
86 #ifdef B50
87     {B50, 50},
88 #endif
89 #ifdef B75
90     {B75, 75},
91 #endif
92 #ifdef B110
93     {B110, 110},
94 #endif
95 #ifdef B134
96     {B134, 134},
97 #endif
98 #ifdef B150
99     {B150, 150},
100 #endif
101 #ifdef B200
102     {B200, 200},
103 #endif
104 #ifdef B300
105     {B300, 300},
106 #endif
107 #ifdef B600
108     {B600, 600},
109 #endif
110 #ifdef B1200
111     {B1200, 1200},
112 #endif
113 #ifdef B1800
114     {B1800, 1800},
115 #endif
116 #ifdef B2400
117     {B2400, 2400},
118 #endif
119 #ifdef B4800
120     {B4800, 4800},
121 #endif
122 #ifdef B9600
123     {B9600, 9600},
124 #endif
125 #ifdef B19200
126     {B19200, 19200},
127 #endif
128 #ifdef EXTA
129     {EXTA, 19200},
130 #endif
131 #ifdef B38400
132     {B38400, 38400},
133 #endif
134 #ifdef EXTB
135     {EXTB, 38400},
136 #endif
137 #ifdef B57600
138     {B57600, 57600},
139 #endif
140 #ifdef B115200
141     {B115200, 115200},
142 #endif
143 #ifdef B153600
144     {B153600, 153600},
145 #endif
146 #ifdef B230400
147     {B230400, 230400},
148 #endif
149 #ifdef B307200
150     {B307200, 307200},
151 #endif
152 #ifdef B460800
153     {B460800, 460800},
154 #endif
155 };
156
157 #define N (sizeof(conv)/sizeof(*conv))
158
159 int
160 speed_t2int (speed_t s)
161 {
162   int l, r, m;
163
164   l = 0;
165   r = N - 1;
166   while(l <= r) {
167     m = (l + r) / 2;
168     if (conv[m].speed == s)
169       return conv[m].bps;
170     else if(conv[m].speed < s)
171       l = m + 1;
172     else
173       r = m - 1; 
174   }
175   return -1;
176 }
177
178 /*
179  *
180  */
181
182 speed_t
183 int2speed_t (int i)
184 {
185   int l, r, m;
186
187   l = 0;
188   r = N - 1;
189   while(l <= r) {
190     m = (l + r) / 2;
191     if (conv[m].bps == i)
192       return conv[m].speed;
193     else if(conv[m].bps < i)
194       l = m + 1;
195     else
196       r = m - 1;
197   }
198   return -1;
199 }
200
201 /*
202  * If there are any IP options on `sock', die.
203  */
204
205 void
206 ip_options_and_die (int sock, struct sockaddr_in *fromp)
207 {
208 #if defined(IP_OPTIONS) && defined(HAVE_GETSOCKOPT)
209   u_char optbuf[BUFSIZ/3], *cp;
210   char lbuf[BUFSIZ], *lp;
211   int optsize = sizeof(optbuf), ipproto;
212   struct protoent *ip;
213
214   if ((ip = getprotobyname("ip")) != NULL)
215     ipproto = ip->p_proto;
216   else
217     ipproto = IPPROTO_IP;
218   if (getsockopt(sock, ipproto, IP_OPTIONS,
219                  (void *)optbuf, &optsize) == 0 &&
220       optsize != 0) {
221     lp = lbuf;
222     for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
223       snprintf(lp, sizeof(lbuf) - (lp - lbuf), " %2.2x", *cp);
224     syslog(LOG_NOTICE,
225            "Connection received from %s using IP options (dead):%s",
226            inet_ntoa(fromp->sin_addr), lbuf);
227     exit(1);
228   }
229 #endif
230 }
231
232 void
233 warning(const char *fmt, ...)
234 {
235     char *rstar_no_warn = getenv("RSTAR_NO_WARN");
236     va_list args;
237
238     va_start(args, fmt);
239     if (rstar_no_warn == NULL)
240         rstar_no_warn = "";
241     if (strncmp(rstar_no_warn, "yes", 3) != 0) {
242         /* XXX */
243         fprintf(stderr, "%s: warning, using standard ", __progname);
244         vwarnx(fmt, args);
245     }
246     va_end(args);
247 }
248
249 /*
250  * setuid but work-around Linux 2.2.15 bug with setuid and capabilities
251  */
252
253 void
254 paranoid_setuid (uid_t uid)
255 {
256     if (setuid (uid) < 0)
257         err (1, "setuid");
258     if (uid != 0 && setuid (0) == 0) {
259         syslog(LOG_ALERT | LOG_AUTH,
260                "Failed to drop privileges for uid %u", (unsigned)uid);
261         err (1, "setuid");
262     }
263 }