]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/rpcmisc.c
Add debugging to better detect negotiation of enctype not supported by kernel
[nfs-utils.git] / support / nfs / rpcmisc.c
1 /*
2  * support/nfs/rpcmisc.c
3  *
4  * Miscellaneous functions for RPC startup and shutdown.
5  * This code is partially snarfed from rpcgen -s tcp -s udp,
6  * partly written by Mark Shand, Donald Becker, and Rick 
7  * Sladkey. It was tweaked slightly by Olaf Kirch to be
8  * usable by both unfsd and mountd.
9  *
10  * This software may be used for any purpose provided
11  * the above copyright notice is retained.  It is supplied
12  * as is, with no warranty expressed or implied.
13  */
14
15 #ifdef HAVE_CONFIG_H
16 #include <config.h>
17 #endif
18
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <rpc/rpc.h>
24 #include <rpc/pmap_clnt.h>
25 #include <netinet/in.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <signal.h>
30 #include <fcntl.h>
31 #include <memory.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <time.h>
35 #include "nfslib.h"
36
37 static void     closedown(int sig);
38 int     makesock(int port, int proto);
39
40 #define _RPCSVC_CLOSEDOWN       120
41 int     _rpcpmstart = 0;
42 int     _rpcfdtype = 0;
43 int     _rpcsvcdirty = 0;
44
45 void
46 rpc_init(char *name, int prog, int vers, void (*dispatch)(), int defport)
47 {
48         struct sockaddr_in saddr;
49         SVCXPRT *transp;
50         int     sock;
51         int     asize;
52
53         asize = sizeof(saddr);
54         sock = 0;
55         if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0
56             && saddr.sin_family == AF_INET) {
57                 int ssize = sizeof (int);
58                 int fdtype = 0;
59                 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
60                                 (char *)&fdtype, &ssize) == -1)
61                         xlog(L_FATAL, "getsockopt failed: %s", strerror(errno));
62                 /* inetd passes a UDP socket or a listening TCP socket.
63                  * listen will fail on a connected TCP socket(passed by rsh).
64                  */
65                 if (!(fdtype == SOCK_STREAM && listen(0,5) == -1)) {
66                         _rpcfdtype = fdtype;
67                         _rpcpmstart = 1;
68                 }
69         }
70         if (!_rpcpmstart) {
71                 pmap_unset(prog, vers);
72                 sock = RPC_ANYSOCK;
73         }
74
75         if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
76                 static SVCXPRT *last_transp = NULL;
77  
78                 if (_rpcpmstart == 0) {
79                         if (last_transp
80                             && (!defport || defport == last_transp->xp_port)) {
81                                 transp = last_transp;
82                                 goto udp_transport;
83                         }
84                         if (defport == 0)
85                                 sock = RPC_ANYSOCK;
86                         else if ((sock = makesock(defport, IPPROTO_UDP)) < 0) {
87                                 xlog(L_FATAL, "%s: cannot make a UDP socket\n",
88                                                 name);
89                         }
90                 }
91                 if (sock == RPC_ANYSOCK)
92                         sock = svcudp_socket (prog, 1);
93                 transp = svcudp_create(sock);
94                 if (transp == NULL) {
95                         xlog(L_FATAL, "cannot create udp service.");
96                 }
97       udp_transport:
98                 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_UDP)) {
99                         xlog(L_FATAL, "unable to register (%s, %d, udp).",
100                                         name, vers);
101                 }
102                 last_transp = transp;
103         }
104
105         if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
106                 static SVCXPRT *last_transp = NULL;
107
108                 if (_rpcpmstart == 0) {
109                         if (last_transp
110                             && (!defport || defport == last_transp->xp_port)) {
111                                 transp = last_transp;
112                                 goto tcp_transport;
113                         }
114                         if (defport == 0)
115                                 sock = RPC_ANYSOCK;
116                         else if ((sock = makesock(defport, IPPROTO_TCP)) < 0) {
117                                 xlog(L_FATAL, "%s: cannot make a TCP socket\n",
118                                                 name);
119                         }
120                 }
121                 if (sock == RPC_ANYSOCK)
122                         sock = svctcp_socket (prog, 1);
123                 transp = svctcp_create(sock, 0, 0);
124                 if (transp == NULL) {
125                         xlog(L_FATAL, "cannot create tcp service.");
126                 }
127       tcp_transport:
128                 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
129                         xlog(L_FATAL, "unable to register (%s, %d, tcp).",
130                                         name, vers);
131                 }
132                 last_transp = transp;
133         }
134
135         if (_rpcpmstart) {
136                 signal (SIGALRM, closedown);
137                 alarm (_RPCSVC_CLOSEDOWN);
138         }
139 }
140
141 static void closedown(sig)
142 int sig;
143 {
144         (void) signal(sig, closedown);
145         if (_rpcsvcdirty == 0) {
146                 static int size;
147                 int i, openfd;
148
149                 if (_rpcfdtype == SOCK_DGRAM)
150                         exit(0);
151                 if (size == 0) {
152                         size = getdtablesize();
153                 }
154                 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
155                         if (FD_ISSET(i, &svc_fdset))
156                                 openfd++;
157                 if (openfd <= 1)
158                         exit(0);
159         }
160         (void) alarm(_RPCSVC_CLOSEDOWN);
161 }
162
163 int makesock(int port, int proto)
164 {
165         struct sockaddr_in sin;
166         int     s;
167         int     sock_type;
168         int     val;
169
170         sock_type = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
171         s = socket(AF_INET, sock_type, proto);
172         if (s < 0) {
173                 xlog(L_FATAL, "Could not make a socket: %s\n",
174                                         strerror(errno));
175                 return (-1);
176         }
177         memset((char *) &sin, 0, sizeof(sin));
178         sin.sin_family = AF_INET;
179         sin.sin_addr.s_addr = INADDR_ANY;
180         sin.sin_port = htons(port);
181
182         val = 1;
183         if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)) < 0)
184                 xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
185
186 #if 0
187         /* I was told it didn't work with gigabit ethernet.
188            Don't bothet with it.  H.J. */
189 #ifdef SO_SNDBUF
190         {
191                 int sblen, rblen;
192
193                 /* 1024 for rpc & transport overheads */
194                 sblen = rblen = socksz + 1024;
195                 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sblen, sizeof sblen) < 0 ||
196                     setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rblen, sizeof rblen) < 0)
197                         xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
198         }
199 #endif                          /* SO_SNDBUF */
200 #endif
201
202         if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
203                 xlog(L_FATAL, "Could not bind name to socket: %s\n",
204                                         strerror(errno));
205                 return (-1);
206         }
207         return (s);
208 }
209
210
211 /* Log an incoming call. */
212 void
213 rpc_logcall(struct svc_req *rqstp, char *xname, char *arg)
214 {
215         char            buff[1024];
216         int             buflen=sizeof(buff);
217         int             len;
218         char            *sp;
219         int             i;
220
221         if (!xlog_enabled(D_CALL))
222                 return;
223
224         sp = buff;
225         switch (rqstp->rq_cred.oa_flavor) {
226         case AUTH_NULL:
227                 sprintf(sp, "NULL");
228                 break;
229         case AUTH_UNIX: {
230                 struct authunix_parms *unix_cred;
231                 struct tm *tm;
232
233                 unix_cred = (struct authunix_parms *) rqstp->rq_clntcred;
234                 tm = localtime(&unix_cred->aup_time);
235                 snprintf(sp, buflen, "UNIX %d/%d/%d %02d:%02d:%02d %s %d.%d",
236                         tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
237                         tm->tm_hour, tm->tm_min, tm->tm_sec,
238                         unix_cred->aup_machname,
239                         unix_cred->aup_uid,
240                         unix_cred->aup_gid);
241                 sp[buflen-1] = 0;
242                 len = strlen(sp);
243                 sp += buflen;
244                 buflen -= len;
245                 if ((int) unix_cred->aup_len > 0) {
246                         snprintf(sp, buflen, "+%d", unix_cred->aup_gids[0]);
247                         sp[buflen-1] = 0;
248                         len = strlen(sp);
249                         sp += buflen;
250                         buflen -= len;
251                         for (i = 1; i < unix_cred->aup_len; i++) {
252                                 snprintf(sp, buflen, ",%d", 
253                                         unix_cred->aup_gids[i]);
254                                 sp[buflen-1] = 0;
255                                 len = strlen(sp);
256                                 sp += buflen;
257                                 buflen -= len;
258                         }
259                 }
260                 }
261                 break;
262         default:
263                 sprintf(sp, "CRED %d", rqstp->rq_cred.oa_flavor);
264         }
265         xlog(D_CALL, "%s [%s]\n\t%s\n", xname, buff, arg);
266 }