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