b994fb1361fd19e25e4296446ee128e2feccaaf2
[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, int socksz);
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                                                         int bufsiz)
45 {
46         struct sockaddr_in saddr;
47         SVCXPRT *transp;
48         int     sock;
49         int     asize;
50
51         asize = sizeof(saddr);
52         sock = 0;
53         _rpcfdtype = 0;
54         if (getsockname(0, (struct sockaddr *) &saddr, &asize) == 0) {
55                 int ssize = sizeof (int);
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 && defport != 0) {
71                         if (last_transp && last_transp->xp_port == defport) {
72                                 transp = last_transp;
73                                 goto udp_transport;
74                         }
75                         if ((sock = makesock(defport, IPPROTO_UDP, bufsiz)) < 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 && defport != 0) {
96                         if (last_transp && last_transp->xp_port == defport) {
97                                 transp = last_transp;
98                                 goto tcp_transport;
99                         }
100                         if ((sock = makesock(defport, IPPROTO_TCP, bufsiz)) < 0) {
101                                 xlog(L_FATAL, "%s: cannot make a TCP socket\n",
102                                                 name);
103                         }
104                 }
105                 transp = svctcp_create(sock, 0, 0);
106                 if (transp == NULL) {
107                         xlog(L_FATAL, "cannot create tcp service.");
108                 }
109       tcp_transport:
110                 if (!svc_register(transp, prog, vers, dispatch, IPPROTO_TCP)) {
111                         xlog(L_FATAL, "unable to register (%s, %d, tcp).",
112                                         name, vers);
113                 }
114                 last_transp = transp;
115         }
116
117         if (_rpcpmstart) {
118                 signal (SIGALRM, closedown);
119                 alarm (_RPCSVC_CLOSEDOWN);
120         }
121 }
122
123 static void closedown(sig)
124 int sig;
125 {
126         (void) signal(sig, closedown);
127         if (_rpcsvcdirty == 0) {
128                 extern fd_set svc_fdset;
129                 static int size;
130                 int i, openfd;
131
132                 if (_rpcfdtype == SOCK_DGRAM)
133                         exit(0);
134                 if (size == 0) {
135                         size = getdtablesize();
136                 }
137                 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
138                         if (FD_ISSET(i, &svc_fdset))
139                                 openfd++;
140                 if (openfd <= 1)
141                         exit(0);
142         }
143         (void) alarm(_RPCSVC_CLOSEDOWN);
144 }
145
146 static int makesock(port, proto, socksz)
147 int port;
148 int proto;
149 int socksz;
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 #ifdef SO_SNDBUF
173         {
174                 int sblen, rblen;
175
176                 /* 1024 for rpc & transport overheads */
177                 sblen = rblen = socksz + 1024;
178                 if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sblen, sizeof sblen) < 0 ||
179                     setsockopt(s, SOL_SOCKET, SO_RCVBUF, &rblen, sizeof rblen) < 0)
180                         xlog(L_ERROR, "setsockopt failed: %s\n", strerror(errno));
181         }
182 #endif                          /* SO_SNDBUF */
183
184         if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
185                 xlog(L_FATAL, "Could not bind name to socket: %s\n",
186                                         strerror(errno));
187                 return (-1);
188         }
189         return (s);
190 }
191
192
193 /* Log an incoming call. */
194 void
195 rpc_logcall(struct svc_req *rqstp, char *xname, char *arg)
196 {
197         char            buff[1024];
198         int             buflen=sizeof(buff);
199         int             len;
200         char            *sp;
201         int             i;
202
203         if (!xlog_enabled(D_CALL))
204                 return;
205
206         sp = buff;
207         switch (rqstp->rq_cred.oa_flavor) {
208         case AUTH_NULL:
209                 sprintf(sp, "NULL");
210                 break;
211         case AUTH_UNIX: {
212                 struct authunix_parms *unix_cred;
213                 struct tm *tm;
214
215                 unix_cred = (struct authunix_parms *) rqstp->rq_clntcred;
216                 tm = localtime(&unix_cred->aup_time);
217                 snprintf(sp, buflen, "UNIX %d/%d/%d %02d:%02d:%02d %s %d.%d",
218                         tm->tm_year, tm->tm_mon + 1, tm->tm_mday,
219                         tm->tm_hour, tm->tm_min, tm->tm_sec,
220                         unix_cred->aup_machname,
221                         unix_cred->aup_uid,
222                         unix_cred->aup_gid);
223                 sp[buflen-1] = 0;
224                 len = strlen(sp);
225                 sp += buflen;
226                 buflen -= len;
227                 if ((int) unix_cred->aup_len > 0) {
228                         snprintf(sp, buflen, "+%d", unix_cred->aup_gids[0]);
229                         sp[buflen-1] = 0;
230                         len = strlen(sp);
231                         sp += buflen;
232                         buflen -= len;
233                         for (i = 1; i < unix_cred->aup_len; i++) {
234                                 snprintf(sp, buflen, ",%d", 
235                                         unix_cred->aup_gids[i]);
236                                 sp[buflen-1] = 0;
237                                 len = strlen(sp);
238                                 sp += buflen;
239                                 buflen -= len;
240                         }
241                 }
242                 }
243                 break;
244         default:
245                 sprintf(sp, "CRED %d", rqstp->rq_cred.oa_flavor);
246         }
247         xlog(D_CALL, "%s [%s]\n\t%s\n", xname, buff, arg);
248 }