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