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