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