Convert to systemd API.
[platform/core/security/security-manager.git] / src / communication / security-server-comm.c
1 /*
2  * security-server
3  *
4  *  Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  *  Contact: Bumjin Im <bj.im@samsung.com>
7  *
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License
19  *
20  */
21
22 #include <sys/poll.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <sys/smack.h>
29 #include <fcntl.h>
30 #include <pwd.h>
31 #include <sys/un.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <sys/stat.h>
35 #include <limits.h>
36 #include <ctype.h>
37
38 #include <systemd/sd-daemon.h>
39
40 #include "security-server-common.h"
41 #include "security-server-comm.h"
42
43 void printhex(const unsigned char *data, int size)
44 {
45     int i;
46     for (i = 0; i < size; i++)
47     {
48         if (data[i] < 0xF)
49             printf("0");
50
51         printf("%X ", data[i]);
52         if (((i + 1) % 16) == 0 && i != 0)
53             printf("\n");
54     }
55     printf("\n");
56 }
57
58 /* Return code in packet is positive integer *
59  * We need to convert them to error code which are negative integer */
60 int return_code_to_error_code(int ret_code)
61 {
62     int ret;
63     switch (ret_code)
64     {
65         case SECURITY_SERVER_RETURN_CODE_SUCCESS:
66         case SECURITY_SERVER_RETURN_CODE_ACCESS_GRANTED:
67             ret = SECURITY_SERVER_SUCCESS;
68             break;
69         case SECURITY_SERVER_RETURN_CODE_BAD_REQUEST:
70             ret = SECURITY_SERVER_ERROR_BAD_REQUEST;
71             break;
72         case SECURITY_SERVER_RETURN_CODE_AUTHENTICATION_FAILED:
73             ret = SECURITY_SERVER_ERROR_AUTHENTICATION_FAILED;
74             break;
75         case SECURITY_SERVER_RETURN_CODE_ACCESS_DENIED:
76             ret = SECURITY_SERVER_ERROR_ACCESS_DENIED;
77             break;
78         case SECURITY_SERVER_RETURN_CODE_NO_SUCH_OBJECT:
79             ret = SECURITY_SERVER_ERROR_NO_SUCH_OBJECT;
80             break;
81         case SECURITY_SERVER_RETURN_CODE_SERVER_ERROR:
82             ret = SECURITY_SERVER_ERROR_SERVER_ERROR;
83             break;
84         case SECURITY_SERVER_RETURN_CODE_NO_SUCH_COOKIE:
85             ret = SECURITY_SERVER_ERROR_NO_SUCH_COOKIE;
86             break;
87         case SECURITY_SERVER_RETURN_CODE_NO_PASSWORD:
88             ret = SECURITY_SERVER_ERROR_NO_PASSWORD;
89             break;
90         case SECURITY_SERVER_RETURN_CODE_PASSWORD_EXIST:
91             ret = SECURITY_SERVER_ERROR_PASSWORD_EXIST;
92             break;
93         case SECURITY_SERVER_RETURN_CODE_PASSWORD_MISMATCH:
94             ret = SECURITY_SERVER_ERROR_PASSWORD_MISMATCH;
95             break;
96         case SECURITY_SERVER_RETURN_CODE_PASSWORD_RETRY_TIMER:
97             ret = SECURITY_SERVER_ERROR_PASSWORD_RETRY_TIMER;
98             break;
99         case SECURITY_SERVER_RETURN_CODE_PASSWORD_EXPIRED:
100             ret = SECURITY_SERVER_ERROR_PASSWORD_EXPIRED;
101             break;
102         case SECURITY_SERVER_RETURN_CODE_PASSWORD_MAX_ATTEMPTS_EXCEEDED:
103             ret = SECURITY_SERVER_ERROR_PASSWORD_MAX_ATTEMPTS_EXCEEDED;
104             break;
105         case SECURITY_SERVER_RETURN_CODE_PASSWORD_REUSED:
106             ret = SECURITY_SERVER_ERROR_PASSWORD_REUSED;
107             break;
108         default:
109             ret = SECURITY_SERVER_ERROR_UNKNOWN;
110             break;
111     }
112     return ret;
113 }
114
115 int check_socket_poll(int sockfd, int event, int timeout)
116 {
117     struct pollfd poll_fd[1];
118     int retval = SECURITY_SERVER_ERROR_POLL;
119
120     poll_fd[0].fd = sockfd;
121     poll_fd[0].events = event;
122     retval = poll(poll_fd, 1, timeout);
123     if (retval < 0)
124     {
125         SEC_SVR_ERR("poll() error. errno=%d", errno);
126         if (errno != EINTR)
127             return SECURITY_SERVER_ERROR_POLL;
128         else
129         {
130             /* Chile process has been closed. Not poll() problem. Call it once again */
131             return check_socket_poll(sockfd, event, timeout);
132         }
133     }
134
135     /* Timed out */
136     if (retval == 0)
137     {
138         return SECURITY_SERVER_ERROR_TIMEOUT;
139     }
140
141     if (poll_fd[0].revents != event)
142     {
143         SEC_SVR_ERR("Something wrong on the peer socket. event=0x%x", poll_fd[0].revents);
144         return SECURITY_SERVER_ERROR_POLL;
145     }
146     return SECURITY_SERVER_SUCCESS;
147 }
148
149 int safe_server_sock_close(int client_sockfd)
150 {
151     struct pollfd poll_fd[1];
152     int retval;
153     retval = SECURITY_SERVER_ERROR_POLL;
154     poll_fd[0].fd = client_sockfd;
155     poll_fd[0].events = POLLRDHUP;
156     retval = poll(poll_fd, 1, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
157     SEC_SVR_DBG("%s", "Server: Closing server socket");
158     close(client_sockfd);
159     return SECURITY_SERVER_SUCCESS;
160 }
161
162 /* Get socket from systemd */
163 int get_socket_from_systemd(int *sockfd)
164 {
165     int n = sd_listen_fds(0);
166     int fd;
167
168     for(fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START+n; ++fd) {
169         if (0 < sd_is_socket_unix(fd, SOCK_STREAM, 1,
170                                   SECURITY_SERVER_SOCK_PATH, 0))
171         {
172             *sockfd = fd;
173             return SECURITY_SERVER_SUCCESS;
174         }
175     }
176     return SECURITY_SERVER_ERROR_SOCKET;
177 }
178
179 /* Create a Unix domain socket and bind */
180 int create_new_socket(int *sockfd)
181 {
182     int retval = 0, localsockfd = 0, flags;
183     struct sockaddr_un serveraddr;
184     mode_t sock_mode;
185
186     /* Deleted garbage Unix domain socket file */
187     retval = remove(SECURITY_SERVER_SOCK_PATH);
188
189     if (retval == -1 && errno != ENOENT) {
190         retval = SECURITY_SERVER_ERROR_UNKNOWN;
191         localsockfd = -1;
192         SECURE_SLOGE("%s", "Unable to remove /tmp/.security_server.sock");
193         goto error;
194     }
195
196     /* Create Unix domain socket */
197     if ((localsockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
198     {
199         retval = SECURITY_SERVER_ERROR_SOCKET;
200         localsockfd = -1;
201         SEC_SVR_ERR("%s", "Socket creation failed");
202         goto error;
203     }
204
205     // If SMACK is present we have to label our sockets regardless of SMACK_ENABLED flag
206     if (smack_runtime_check()) {
207         if (smack_fsetlabel(localsockfd, "@", SMACK_LABEL_IPOUT) != 0)
208         {
209             SEC_SVR_ERR("%s", "SMACK labeling failed");
210             if (errno != EOPNOTSUPP)
211             {
212                 retval = SECURITY_SERVER_ERROR_SOCKET;
213                 close(localsockfd);
214                 localsockfd = -1;
215                 goto error;
216             }
217         }
218         if (smack_fsetlabel(localsockfd, "*", SMACK_LABEL_IPIN) != 0)
219         {   SEC_SVR_ERR("%s", "SMACK labeling failed");
220             if (errno != EOPNOTSUPP)
221             {
222                 retval = SECURITY_SERVER_ERROR_SOCKET;
223                 close(localsockfd);
224                 localsockfd = -1;
225                 goto error;
226             }}
227     }
228     else {
229         SEC_SVR_DBG("SMACK is not available. Sockets won't be labeled.");
230     }
231
232     /* Make socket as non blocking */
233     if ((flags = fcntl(localsockfd, F_GETFL, 0)) < 0 ||
234         fcntl(localsockfd, F_SETFL, flags | O_NONBLOCK) < 0)
235     {
236         retval = SECURITY_SERVER_ERROR_SOCKET;
237         close(localsockfd);
238         localsockfd = -1;
239         SEC_SVR_ERR("%s", "Cannot go to nonblocking mode");
240         goto error;
241     }
242
243     bzero (&serveraddr, sizeof(serveraddr));
244     serveraddr.sun_family = AF_UNIX;
245     strncpy(serveraddr.sun_path, SECURITY_SERVER_SOCK_PATH,
246         strlen(SECURITY_SERVER_SOCK_PATH));
247     serveraddr.sun_path[strlen(SECURITY_SERVER_SOCK_PATH)] = 0;
248
249     /* Bind the socket */
250     if ((bind(localsockfd, (struct sockaddr*)&serveraddr, sizeof(serveraddr))) < 0)
251     {
252         retval = SECURITY_SERVER_ERROR_SOCKET_BIND;
253         SEC_SVR_ERR("%s", "Cannot bind");
254         close(localsockfd);
255         localsockfd = -1;
256         goto error;
257     }
258
259
260     /* Change permission to accept all processes that has different uID/gID */
261     sock_mode = (S_IRWXU | S_IRWXG | S_IRWXO);
262     /* Flawfinder hits this chmod function as level 5 CRITICAL as race condition flaw *
263      * Flawfinder recommends to user fchmod insted of chmod
264      * But, fchmod doesn't work on socket file so there is no other choice at this point */
265     if (chmod(SECURITY_SERVER_SOCK_PATH, sock_mode) < 0)     /* Flawfinder: ignore */
266     {
267         SEC_SVR_ERR("%s", "chmod() error");
268         retval = SECURITY_SERVER_ERROR_SOCKET;
269         close(localsockfd);
270         localsockfd = -1;
271         goto error;
272     }
273
274     retval = SECURITY_SERVER_SUCCESS;
275
276 error:
277     *sockfd = localsockfd;
278     return retval;
279 }
280
281 /* Authenticate peer that it's really security server.
282  * Check UID that is root
283  */
284 int authenticate_server(int sockfd)
285 {
286     int retval;
287     struct ucred cr;
288     unsigned int cl = sizeof(cr);
289 /*      char *exe = NULL;*/
290
291     /* get socket peer credential */
292     if (getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &cr, &cl) != 0)
293     {
294         retval = SECURITY_SERVER_ERROR_SOCKET;
295         SEC_SVR_ERR("%s", "getsockopt() failed");
296         goto error;
297     }
298
299     /* Security server must run as root */
300     if (cr.uid != 0)
301     {
302         retval = SECURITY_SERVER_ERROR_AUTHENTICATION_FAILED;
303         SEC_SVR_ERR("Peer is not root: uid=%d", cr.uid);
304         goto error;
305     }
306     else
307         retval = SECURITY_SERVER_SUCCESS;
308
309     /* Read command line of the PID from proc fs */
310     /* This is commented out because non root process cannot read link of /proc/pid/exe */
311 /*      exe = read_exe_path_from_proc(cr.pid);
312
313     if(strcmp(exe, SECURITY_SERVER_DAEMON_PATH) != 0)
314     {
315         retval = SECURITY_SERVER_ERROR_AUTHENTICATION_FAILED;
316         SEC_SVR_DBG("Executable path is different. auth failed. Exe path=%s", exe);
317     }
318     else
319     {
320         retval = SECURITY_SERVER_SUCCESS;
321         SEC_SVR_DBG("Server authenticatd. %s, sockfd=%d", exe, sockfd);
322     }
323 */
324 error:
325 /*      if(exe != NULL)
326         free(exe);
327 */
328     return retval;
329 }
330
331 /* Create a socket and connect to Security Server */
332 int connect_to_server(int *fd)
333 {
334     struct sockaddr_un clientaddr;
335     int client_len = 0, localsockfd, ret, flags;
336     *fd = -1;
337
338     /* Create a socket */
339     localsockfd = socket(AF_UNIX, SOCK_STREAM, 0);
340     if (localsockfd < 0)
341     {
342         SEC_SVR_ERR("%s", "Error on socket()");
343         return SECURITY_SERVER_ERROR_SOCKET;
344     }
345
346     /* Make socket as non blocking */
347     if ((flags = fcntl(localsockfd, F_GETFL, 0)) < 0 ||
348         fcntl(localsockfd, F_SETFL, flags | O_NONBLOCK) < 0)
349     {
350         close(localsockfd);
351         SEC_SVR_ERR("%s", "Cannot go to nonblocking mode");
352         return SECURITY_SERVER_ERROR_SOCKET;
353     }
354
355     bzero(&clientaddr, sizeof(clientaddr));
356     clientaddr.sun_family = AF_UNIX;
357     strncpy(clientaddr.sun_path, SECURITY_SERVER_SOCK_PATH, strlen(SECURITY_SERVER_SOCK_PATH));
358     clientaddr.sun_path[strlen(SECURITY_SERVER_SOCK_PATH)] = 0;
359     client_len = sizeof(clientaddr);
360
361     ret = connect(localsockfd, (struct sockaddr*)&clientaddr, client_len);
362     if (ret < 0)
363     {
364         if (errno == EINPROGRESS)
365         {
366             SEC_SVR_DBG("%s", "Connection is in progress");
367             ret = check_socket_poll(localsockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
368             if (ret == SECURITY_SERVER_ERROR_POLL)
369             {
370                 SEC_SVR_ERR("%s", "poll() error");
371                 close(localsockfd);
372                 return SECURITY_SERVER_ERROR_SOCKET;
373             }
374             if (ret == SECURITY_SERVER_ERROR_TIMEOUT)
375             {
376                 SEC_SVR_ERR("%s", "poll() timeout");
377                 close(localsockfd);
378                 return SECURITY_SERVER_ERROR_SOCKET;
379             }
380             ret = connect(localsockfd, (struct sockaddr*)&clientaddr, client_len);
381             if (ret < 0)
382             {
383                 SEC_SVR_ERR("%s", "connection failed");
384                 close(localsockfd);
385                 return SECURITY_SERVER_ERROR_SOCKET;
386             }
387         }
388         else
389         {
390             SEC_SVR_ERR("%s", "Connection failed");
391             close(localsockfd);
392             return SECURITY_SERVER_ERROR_SOCKET;
393         }
394     }
395
396     /* Authenticate the peer is actually security server */
397     ret = authenticate_server(localsockfd);
398     if (ret != SECURITY_SERVER_SUCCESS)
399     {
400         close(localsockfd);
401         SEC_SVR_ERR("Authentication failed. %d", ret);
402         return ret;
403     }
404     *fd = localsockfd;
405     return SECURITY_SERVER_SUCCESS;
406 }
407
408 /* Accept a new client connection */
409 int accept_client(int server_sockfd)
410 {
411     /* Call poll() to wait for socket connection */
412     int retval, localsockfd;
413     struct sockaddr_un clientaddr;
414     unsigned int client_len;
415
416     client_len = sizeof(clientaddr);
417
418     /* Check poll */
419     retval = check_socket_poll(server_sockfd, POLLIN, SECURITY_SERVER_ACCEPT_TIMEOUT_MILISECOND);
420     if (retval == SECURITY_SERVER_ERROR_POLL)
421     {
422         SEC_SVR_ERR("%s", "Error on polling");
423         return SECURITY_SERVER_ERROR_SOCKET;
424     }
425
426     /* Timed out */
427     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
428     {
429         /*SEC_SVR_DBG("%s", "accept() timeout");*/
430         return SECURITY_SERVER_ERROR_TIMEOUT;
431     }
432
433     localsockfd = accept(server_sockfd,
434         (struct sockaddr*)&clientaddr,
435         &client_len);
436
437     if (localsockfd < 0)
438     {
439         SEC_SVR_ERR("Cannot accept client. errno=%d", errno);
440         return SECURITY_SERVER_ERROR_SOCKET;
441     }
442     return localsockfd;
443 }
444
445 /* Minimal check of request packet */
446 int validate_header(basic_header hdr)
447 {
448     if (hdr.version != SECURITY_SERVER_MSG_VERSION)
449         return SECURITY_SERVER_ERROR_BAD_REQUEST;
450
451     return SECURITY_SERVER_SUCCESS;
452 }
453
454 /* Send generic response packet to client
455  *
456  * Generic Response Packet Format
457  0                   1                   2                   3
458  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
459 |---------------------------------------------------------------|
460 | version=0x01  |  Message ID   |Message Length (without header)|
461 |---------------------------------------------------------------|
462 |  return code  |
463 -----------------
464 */
465 int send_generic_response (int sockfd, unsigned char msgid, unsigned char return_code)
466 {
467     response_header hdr;
468     int size;
469
470     /* Assemble header */
471     hdr.basic_hdr.version = SECURITY_SERVER_MSG_VERSION;
472     hdr.basic_hdr.msg_id = msgid;
473     hdr.basic_hdr.msg_len = 0;
474     hdr.return_code = return_code;
475
476     /* Check poll */
477     size = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
478     if (size == SECURITY_SERVER_ERROR_POLL)
479     {
480         SEC_SVR_ERR("%s", "poll() error");
481         return SECURITY_SERVER_ERROR_SEND_FAILED;
482     }
483     if (size == SECURITY_SERVER_ERROR_TIMEOUT)
484     {
485         SEC_SVR_ERR("%s", "poll() timeout");
486         return SECURITY_SERVER_ERROR_SEND_FAILED;
487     }
488
489     /* Send to client */
490     size = TEMP_FAILURE_RETRY(write(sockfd, &hdr, sizeof(hdr)));
491
492     if (size < (int)sizeof(hdr))
493         return SECURITY_SERVER_ERROR_SEND_FAILED;
494     return SECURITY_SERVER_SUCCESS;
495 }
496
497 /* Send cookie response to client
498  *
499  * Get Cookie response packet format
500  *  0                   1                   2                   3
501  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
502  *  |---------------------------------------------------------------|
503  *  | version=0x01  |MessageID=0x02 |       Message Length =20      |
504  *  |---------------------------------------------------------------|
505  *  |  return code  |                                               |
506  *  -----------------                                               |
507  *  |                 cookie (20 bytes)                             |
508  *  |---------------------------------------------------------------|
509 */
510 int send_cookie(int sockfd, unsigned char *cookie)
511 {
512     response_header hdr;
513     unsigned char msg[SECURITY_SERVER_COOKIE_LEN + sizeof(hdr)];
514     int ret;
515
516     /* Assemble header */
517     hdr.basic_hdr.version = SECURITY_SERVER_MSG_VERSION;
518     hdr.basic_hdr.msg_id = SECURITY_SERVER_MSG_TYPE_COOKIE_RESPONSE;
519     hdr.basic_hdr.msg_len = SECURITY_SERVER_COOKIE_LEN;
520     hdr.return_code = SECURITY_SERVER_RETURN_CODE_SUCCESS;
521
522     memcpy(msg, &hdr, sizeof(hdr));
523     memcpy(msg + sizeof(hdr), cookie, SECURITY_SERVER_COOKIE_LEN);
524
525     /* Check poll */
526     ret = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
527     if (ret == SECURITY_SERVER_ERROR_POLL)
528     {
529         SEC_SVR_ERR("%s", "poll() error");
530         return SECURITY_SERVER_ERROR_SEND_FAILED;
531     }
532     if (ret == SECURITY_SERVER_ERROR_TIMEOUT)
533     {
534         SEC_SVR_ERR("%s", "poll() timeout");
535         return SECURITY_SERVER_ERROR_SEND_FAILED;
536     }
537
538     ret = TEMP_FAILURE_RETRY(write(sockfd, msg, sizeof(hdr) + SECURITY_SERVER_COOKIE_LEN));
539     if (ret < (int)(sizeof(hdr) + SECURITY_SERVER_COOKIE_LEN))
540     {
541         /* Error on writing */
542         SEC_SVR_ERR("Error on write: %d", ret);
543         ret = SECURITY_SERVER_ERROR_SEND_FAILED;
544         return ret;
545     }
546     return SECURITY_SERVER_SUCCESS;
547 }
548
549 /* Send Object name response *
550  * Get Object name response packet format
551  *  0                   1                   2                   3
552  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
553  * |---------------------------------------------------------------|
554  * | version=0x01  |MessageID=0x06 |       Message Length          |
555  * |---------------------------------------------------------------|
556  * |  return code  |                                               |
557  * -----------------                                               |
558  * |                 object name                                   |
559  * |---------------------------------------------------------------|
560 */
561 int send_object_name(int sockfd, char *obj)
562 {
563     response_header hdr;
564     unsigned char msg[strlen(obj) + sizeof(hdr)];
565     int ret;
566
567     hdr.basic_hdr.version = SECURITY_SERVER_MSG_VERSION;
568     hdr.basic_hdr.msg_id = 0x06;
569     hdr.basic_hdr.msg_len = strlen(obj);
570     hdr.return_code = SECURITY_SERVER_RETURN_CODE_SUCCESS;
571
572     memcpy(msg, &hdr, sizeof(hdr));
573     memcpy(msg + sizeof(hdr), obj, strlen(obj));
574
575     /* Check poll */
576     ret = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
577     if (ret == SECURITY_SERVER_ERROR_POLL)
578     {
579         SEC_SVR_ERR("%s", "poll() error");
580         return SECURITY_SERVER_ERROR_SEND_FAILED;
581     }
582     if (ret == SECURITY_SERVER_ERROR_TIMEOUT)
583     {
584         SEC_SVR_ERR("%s", "poll() timeout");
585         return SECURITY_SERVER_ERROR_SEND_FAILED;
586     }
587
588     ret = TEMP_FAILURE_RETRY(write(sockfd, msg, sizeof(hdr) + strlen(obj)));
589     if (ret < sizeof(hdr) + strlen(obj))
590     {
591         /* Error on writing */
592         SEC_SVR_ERR("Error on write: %d", ret);
593         ret = SECURITY_SERVER_ERROR_SEND_FAILED;
594         return ret;
595     }
596     return SECURITY_SERVER_SUCCESS;
597 }
598
599 /* Send GID response to client
600  *
601  * Get GID response packet format
602  *  0                   1                   2                   3
603  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
604  * |---------------------------------------------------------------|
605  * | version=0x01  |MessageID=0x08 |       Message Length = 4      |
606  * |---------------------------------------------------------------|
607  * |  return code  |           gid (first 3 words)                 |
608  * |---------------------------------------------------------------|
609  * |gid(last word) |
610  * |---------------|
611 */
612 int send_gid(int sockfd, int gid)
613 {
614     response_header hdr;
615     unsigned char msg[sizeof(gid) + sizeof(hdr)];
616     int ret;
617
618     /* Assemble header */
619     hdr.basic_hdr.version = SECURITY_SERVER_MSG_VERSION;
620     hdr.basic_hdr.msg_id = SECURITY_SERVER_MSG_TYPE_GID_RESPONSE;
621     hdr.basic_hdr.msg_len = sizeof(gid);
622     hdr.return_code = SECURITY_SERVER_RETURN_CODE_SUCCESS;
623
624     /* Perpare packet */
625     memcpy(msg, &hdr, sizeof(hdr));
626     memcpy(msg + sizeof(hdr), &gid, sizeof(gid));
627
628     /* Check poll */
629     ret = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
630     if (ret == SECURITY_SERVER_ERROR_POLL)
631     {
632         SEC_SVR_ERR("%s", "poll() error");
633         return SECURITY_SERVER_ERROR_SEND_FAILED;
634     }
635     if (ret == SECURITY_SERVER_ERROR_TIMEOUT)
636     {
637         SEC_SVR_ERR("%s", "poll() timeout");
638         return SECURITY_SERVER_ERROR_SEND_FAILED;
639     }
640
641     /* Send it */
642     ret = TEMP_FAILURE_RETRY(write(sockfd, msg, sizeof(hdr) + sizeof(gid)));
643     if (ret < sizeof(hdr) + sizeof(gid))
644     {
645         /* Error on writing */
646         SEC_SVR_ERR("Error on write(): %d", ret);
647         ret = SECURITY_SERVER_ERROR_SEND_FAILED;
648         return ret;
649     }
650     return SECURITY_SERVER_SUCCESS;
651 }
652
653 /* Send PID response to client
654  *
655  * Get PID response packet format
656  *  0                   1                   2                   3
657  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
658  * |---------------------------------------------------------------|
659  * | version=0x01  |MessageID=0x0a |       Message Length = 4      |
660  * |---------------------------------------------------------------|
661  * |  return code  |           pid (first 3 words)                 |
662  * |---------------------------------------------------------------|
663  * |pid(last word) |
664  * |---------------|
665 */
666 int send_pid(int sockfd, int pid)
667 {
668     response_header hdr;
669     unsigned char msg[sizeof(pid) + sizeof(hdr)];
670     int ret;
671
672     /* Assemble header */
673     hdr.basic_hdr.version = SECURITY_SERVER_MSG_VERSION;
674     hdr.basic_hdr.msg_id = SECURITY_SERVER_MSG_TYPE_PID_RESPONSE;
675     hdr.basic_hdr.msg_len = sizeof(pid);
676     hdr.return_code = SECURITY_SERVER_RETURN_CODE_SUCCESS;
677
678     /* Perpare packet */
679     memcpy(msg, &hdr, sizeof(hdr));
680     memcpy(msg + sizeof(hdr), &pid, sizeof(pid));
681
682     /* Check poll */
683     ret = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
684     if (ret == SECURITY_SERVER_ERROR_POLL)
685     {
686         SEC_SVR_ERR("%s", "poll() error");
687         return SECURITY_SERVER_ERROR_SEND_FAILED;
688     }
689     if (ret == SECURITY_SERVER_ERROR_TIMEOUT)
690     {
691         SEC_SVR_ERR("%s", "poll() timeout");
692         return SECURITY_SERVER_ERROR_SEND_FAILED;
693     }
694
695     /* Send it */
696     ret = TEMP_FAILURE_RETRY(write(sockfd, msg, sizeof(hdr) + sizeof(pid)));
697     if (ret < sizeof(hdr) + sizeof(pid))
698     {
699         /* Error on writing */
700         SEC_SVR_ERR("Error on write(): %d", ret);
701         ret = SECURITY_SERVER_ERROR_SEND_FAILED;
702         return ret;
703     }
704     return SECURITY_SERVER_SUCCESS;
705 }
706
707 /* Send SMACK label to client with lenght N
708  *  0                   1                   2                   3
709  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
710  * |---------------------------------------------------------------|
711  * | version=0x01  |MessageID=0x1e |  Message Length = SMACK_LABEL_LEN + 1
712  * |---------------------------------------------------------------|
713  * |  return code  |           SMACK label byte 0                  |
714  * |---------------------------------------------------------------|
715  * |                      ..................                       |
716  * |---------------------------------------------------------------|
717  * |                      SMACK label byte N                       |
718  * |---------------------------------------------------------------|
719 */
720 int send_smack(int sockfd, char *label)
721 {
722     response_header hdr;
723     //added 1 to the size is for NULL terminating label
724     int LABEL_SIZE = SMACK_LABEL_LEN + 1;
725     int PACKET_SIZE = sizeof(hdr) + LABEL_SIZE;
726     unsigned char msg[PACKET_SIZE];
727     int ret;
728
729     /* Assemble header */
730     hdr.basic_hdr.version = SECURITY_SERVER_MSG_VERSION;
731     hdr.basic_hdr.msg_id = SECURITY_SERVER_MSG_TYPE_SMACK_RESPONSE;
732     hdr.basic_hdr.msg_len = LABEL_SIZE;
733     hdr.return_code = SECURITY_SERVER_RETURN_CODE_SUCCESS;
734
735     /* Perpare packet */
736     memcpy(msg, &hdr, sizeof(hdr));
737     memcpy(msg + sizeof(hdr), label, LABEL_SIZE);
738     memset(msg + sizeof(hdr) + SMACK_LABEL_LEN, 0x00, 1); //adding NULL ad the label end
739
740     /* Check poll */
741     ret = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
742     if (ret == SECURITY_SERVER_ERROR_POLL)
743     {
744         SEC_SVR_ERR("%s", "poll() error");
745         return SECURITY_SERVER_ERROR_SEND_FAILED;
746     }
747     if (ret == SECURITY_SERVER_ERROR_TIMEOUT)
748     {
749         SEC_SVR_ERR("%s", "poll() timeout");
750         return SECURITY_SERVER_ERROR_SEND_FAILED;
751     }
752
753     /* Send it */
754     ret = TEMP_FAILURE_RETRY(write(sockfd, msg, PACKET_SIZE));
755     if (ret < PACKET_SIZE)
756     {
757         /* Error on writing */
758         SEC_SVR_ERR("Error on write(): %d", ret);
759         ret = SECURITY_SERVER_ERROR_SEND_FAILED;
760         return ret;
761     }
762     return SECURITY_SERVER_SUCCESS;
763 }
764
765 /* Send Check password response to client
766  *
767  * Check password response packet format
768  *  0                   1                   2                   3
769  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
770  * |---------------------------------------------------------------|
771  * | version=0x01  |   MessageID   |       Message Length = 12     |
772  * |---------------------------------------------------------------|
773  * |  return code  |           attempts (first 3 words)            |
774  * |---------------------------------------------------------------|
775  * |attempts(rest) |          max_attempts (first 3 words)         |
776  * |---------------|-----------------------------------------------|
777  * | max_attempts  |          expire_in_days (first 3 words)       |
778  * |---------------------------------------------------------------|
779  * |expire_in_days |
780  * |----------------
781  */
782 int send_pwd_response(const int sockfd,
783                       const unsigned char msg_id,
784                       const unsigned char return_code,
785                       const unsigned int current_attempts,
786                       const unsigned int max_attempts,
787                       const unsigned int expire_time)
788 {
789     response_header hdr;
790     unsigned int expire_secs;
791     unsigned char msg[sizeof(hdr) + sizeof(current_attempts) + sizeof(max_attempts) + sizeof(expire_secs)];
792     int ret, ptr = 0;
793
794
795     /* Assemble header */
796     hdr.basic_hdr.version = SECURITY_SERVER_MSG_VERSION;
797     hdr.basic_hdr.msg_id = msg_id;
798     hdr.basic_hdr.msg_len = sizeof(unsigned int) * 3;
799     hdr.return_code = return_code;
800
801     /* Perpare packet */
802     memcpy(msg, &hdr, sizeof(hdr));
803     ptr += sizeof(hdr);
804     memcpy(msg + ptr, &current_attempts, sizeof(current_attempts));
805     ptr += sizeof(current_attempts);
806     memcpy(msg + ptr, &max_attempts, sizeof(max_attempts));
807     ptr += sizeof(max_attempts);
808     memcpy(msg + ptr, &expire_time, sizeof(expire_time));
809     ptr += sizeof(expire_time);
810
811     /* Check poll */
812     ret = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
813     if (ret == SECURITY_SERVER_ERROR_POLL)
814     {
815         SEC_SVR_ERR("%s", "Server: poll() error");
816         return SECURITY_SERVER_ERROR_SEND_FAILED;
817     }
818     if (ret == SECURITY_SERVER_ERROR_TIMEOUT)
819     {
820         SEC_SVR_ERR("%s", "Server: poll() timeout");
821         return SECURITY_SERVER_ERROR_SEND_FAILED;
822     }
823
824     /* Send it */
825     ret = TEMP_FAILURE_RETRY(write(sockfd, msg, ptr));
826     if (ret < ptr)
827     {
828         /* Error on writing */
829         SEC_SVR_ERR("Server: ERROR on write(): %d", ret);
830         ret = SECURITY_SERVER_ERROR_SEND_FAILED;
831         return ret;
832     }
833     return SECURITY_SERVER_SUCCESS;
834 }
835
836 /* Send cookie request packet to security server *
837  *
838  * Message format
839  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
840  * |---------------------------------------------------------------|
841  * | version=0x01  |MessageID=0x01 |       Message Length = 0      |
842  * |---------------------------------------------------------------|
843  */
844 int send_cookie_request(int sock_fd)
845 {
846     basic_header hdr;
847     int retval;
848
849     /* Assemble header */
850     hdr.version = SECURITY_SERVER_MSG_VERSION;
851     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_COOKIE_REQUEST;
852     hdr.msg_len = 0;
853
854     /* Check poll */
855     retval = check_socket_poll(sock_fd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
856     if (retval == SECURITY_SERVER_ERROR_POLL)
857     {
858         SEC_SVR_ERR("%s", "poll() error");
859         return SECURITY_SERVER_ERROR_SEND_FAILED;
860     }
861     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
862     {
863         SEC_SVR_ERR("%s", "poll() timeout");
864         return SECURITY_SERVER_ERROR_SEND_FAILED;
865     }
866
867     /* Send to server */
868     retval = TEMP_FAILURE_RETRY(write(sock_fd, &hdr, sizeof(hdr)));
869     if (retval < sizeof(hdr))
870     {
871         /* Write error */
872         SEC_SVR_ERR("Error on write(): %d", retval);
873         return SECURITY_SERVER_ERROR_SEND_FAILED;
874     }
875     return SECURITY_SERVER_SUCCESS;
876 }
877
878 /* Send GID request message to security server
879  *
880  * Message format
881  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
882  * |---------------------------------------------------------------|
883  * | version=0x01  |MessageID=0x07 |   Message Length = variable   |
884  * |---------------------------------------------------------------|
885  * |                                                               |
886  * |                   Object name (variable)                      |
887  * |                                                               |
888  * |---------------------------------------------------------------|
889  */
890 int send_gid_request(int sock_fd, const char *object)
891 {
892     basic_header hdr;
893     int retval = 0, send_len = 0;
894     unsigned char *buf = NULL;
895
896     if (strlen(object) > SECURITY_SERVER_MAX_OBJ_NAME)
897     {
898         /* Object name is too big*/
899         SEC_SVR_ERR("Object name is too big %dbytes", strlen(object));
900         return SECURITY_SERVER_ERROR_INPUT_PARAM;
901     }
902
903     hdr.version = SECURITY_SERVER_MSG_VERSION;
904     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_GID_REQUEST;
905     hdr.msg_len = strlen(object);
906
907     send_len = sizeof(hdr) + strlen(object);
908
909     buf = malloc(send_len);
910     if (buf == NULL)
911     {
912         SEC_SVR_ERR("%s", "out of memory");
913         return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
914     }
915
916     memcpy(buf, &hdr, sizeof(hdr));
917     memcpy(buf + sizeof(hdr), object, strlen(object));
918
919     /* Check poll */
920     retval = check_socket_poll(sock_fd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
921     if (retval == SECURITY_SERVER_ERROR_POLL)
922     {
923         SEC_SVR_ERR("%s", "poll() error");
924         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
925         goto error;
926     }
927     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
928     {
929         SEC_SVR_ERR("%s", "poll() timeout");
930         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
931         goto error;
932     }
933
934     retval = TEMP_FAILURE_RETRY(write(sock_fd, buf, send_len));
935     if (retval < send_len)
936     {
937         /* Write error */
938         SEC_SVR_ERR("Error on write(): %d. errno=%d, sockfd=%d", retval, errno, sock_fd);
939         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
940     }
941     else
942         retval = SECURITY_SERVER_SUCCESS;
943
944 error:
945     if (buf != NULL)
946         free(buf);
947
948     return retval;
949 }
950
951 /* Send object name request message to security server *
952  *
953  * Message format
954  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
955  * |---------------------------------------------------------------|
956  * | version=0x01  |MessageID=0x05 |       Message Length = 4      |
957  * |---------------------------------------------------------------|
958  * |                               gid                             |
959  * |---------------------------------------------------------------|
960  */
961 int send_object_name_request(int sock_fd, int gid)
962 {
963     basic_header hdr;
964     int retval;
965     unsigned char buf[sizeof(hdr) + sizeof(gid)];
966
967     /* Assemble header */
968     hdr.version = SECURITY_SERVER_MSG_VERSION;
969     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_OBJECT_NAME_REQUEST;
970     hdr.msg_len = sizeof(gid);
971
972     memcpy(buf, &hdr, sizeof(hdr));
973     memcpy(buf + sizeof(hdr), &gid, sizeof(gid));
974
975     /* Check poll */
976     retval = check_socket_poll(sock_fd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
977     if (retval == SECURITY_SERVER_ERROR_POLL)
978     {
979         SEC_SVR_ERR("%s", "poll() error");
980         return SECURITY_SERVER_ERROR_SEND_FAILED;
981     }
982     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
983     {
984         SEC_SVR_ERR("%s", "poll() timeout");
985         return SECURITY_SERVER_ERROR_SEND_FAILED;
986     }
987
988     /* Send to server */
989     retval = TEMP_FAILURE_RETRY(write(sock_fd, buf, sizeof(buf)));
990     if (retval < sizeof(buf))
991     {
992         /* Write error */
993         SEC_SVR_ERR("Error on write(): %d", retval);
994         return SECURITY_SERVER_ERROR_SEND_FAILED;
995     }
996     return SECURITY_SERVER_SUCCESS;
997 }
998
999 /* Send privilege check request message to security server *
1000  *
1001  * Message format
1002  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1003  * |---------------------------------------------------------------|
1004  * | version=0x01  |MessageID=0x03 |      Message Length = 24      |
1005  * |---------------------------------------------------------------|
1006  * |                                                               |
1007  * |                                                               |
1008  * |                      Cookie (20bytes)                         |
1009  * |                                                               |
1010  * |                                                               |
1011  * |---------------------------------------------------------------|
1012  * |                            GID                                |
1013  * |---------------------------------------------------------------|
1014  */
1015 int send_privilege_check_request(int sock_fd, const char *cookie, int gid)
1016 {
1017     basic_header hdr;
1018     int retval;
1019     unsigned char buf[sizeof(hdr) + sizeof(gid) + SECURITY_SERVER_COOKIE_LEN];
1020
1021     /* Assemble header */
1022     hdr.version = SECURITY_SERVER_MSG_VERSION;
1023     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_REQUEST;
1024     hdr.msg_len = sizeof(gid) + SECURITY_SERVER_COOKIE_LEN;
1025
1026     memcpy(buf, &hdr, sizeof(hdr));
1027     memcpy(buf + sizeof(hdr), cookie, SECURITY_SERVER_COOKIE_LEN);
1028     memcpy(buf + sizeof(hdr) + SECURITY_SERVER_COOKIE_LEN, &gid, sizeof(gid));
1029
1030     /* Check poll */
1031     retval = check_socket_poll(sock_fd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1032     if (retval == SECURITY_SERVER_ERROR_POLL)
1033     {
1034         SEC_SVR_ERR("%s", "poll() error");
1035         return SECURITY_SERVER_ERROR_SEND_FAILED;
1036     }
1037     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
1038     {
1039         SEC_SVR_ERR("%s", "poll() timeout");
1040         return SECURITY_SERVER_ERROR_SEND_FAILED;
1041     }
1042
1043     /* Send to server */
1044     retval = TEMP_FAILURE_RETRY(write(sock_fd, buf, sizeof(buf)));
1045     if (retval < sizeof(buf))
1046     {
1047         /* Write error */
1048         SEC_SVR_ERR("Error on write(): %d", retval);
1049         return SECURITY_SERVER_ERROR_SEND_FAILED;
1050     }
1051     return SECURITY_SERVER_SUCCESS;
1052 }
1053
1054 int send_privilege_check_new_request(int sock_fd,
1055                                      const char *cookie,
1056                                      const char *object,
1057                                      const char *access_rights)
1058 {
1059     basic_header hdr;
1060     int retval;
1061     int olen, alen;
1062     int size;
1063
1064     olen = strlen(object);
1065     alen = strlen(access_rights);
1066     if (olen > MAX_OBJECT_LABEL_LEN || alen > MAX_MODE_STR_LEN)
1067     {
1068         return SECURITY_SERVER_ERROR_INPUT_PARAM;
1069     }
1070
1071     unsigned char buf[sizeof(hdr) + SECURITY_SERVER_COOKIE_LEN +
1072                       2 * sizeof(int) + MAX_OBJECT_LABEL_LEN + MAX_MODE_STR_LEN];
1073
1074     /* Assemble header */
1075     hdr.version = SECURITY_SERVER_MSG_VERSION;
1076     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_NEW_REQUEST;
1077     hdr.msg_len = SECURITY_SERVER_COOKIE_LEN + 2 * sizeof(int) + olen + alen;
1078
1079     memcpy(buf, &hdr, sizeof(hdr));
1080     memcpy(buf + sizeof(hdr), cookie, SECURITY_SERVER_COOKIE_LEN);
1081     memcpy(buf + sizeof(hdr) + SECURITY_SERVER_COOKIE_LEN, &olen, sizeof(int));
1082     memcpy(buf + sizeof(hdr) + SECURITY_SERVER_COOKIE_LEN + sizeof(int),
1083         &alen, sizeof(int));
1084     memcpy(buf + sizeof(hdr) + SECURITY_SERVER_COOKIE_LEN + 2 * sizeof(int), object, olen);
1085     memcpy(buf + sizeof(hdr) + SECURITY_SERVER_COOKIE_LEN + 2 * sizeof(int) + olen,
1086         access_rights, alen);
1087
1088     /* Check poll */
1089     retval = check_socket_poll(sock_fd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1090     if (retval == SECURITY_SERVER_ERROR_POLL)
1091     {
1092         SEC_SVR_ERR("%s", "poll() error");
1093         return SECURITY_SERVER_ERROR_SEND_FAILED;
1094     }
1095     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
1096     {
1097         SEC_SVR_ERR("%s", "poll() timeout");
1098         return SECURITY_SERVER_ERROR_SEND_FAILED;
1099     }
1100
1101     size = sizeof(hdr) + SECURITY_SERVER_COOKIE_LEN + 2 * sizeof(int) + olen + alen;
1102     /* Send to server */
1103     retval = TEMP_FAILURE_RETRY(write(sock_fd, buf, size));
1104     if (retval < size)
1105     {
1106         /* Write error */
1107         SEC_SVR_ERR("Error on write(): %d", retval);
1108         return SECURITY_SERVER_ERROR_SEND_FAILED;
1109     }
1110     return SECURITY_SERVER_SUCCESS;
1111 }
1112
1113 /* Send SMACK request message to security server *
1114  *
1115  * Message format
1116  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1117  * |---------------------------------------------------------------|
1118  * | version=0x01  |MessageID=0x1d |      Message Length = 20      |
1119  * |---------------------------------------------------------------|
1120  * |                                                               |
1121  * |                                                               |
1122  * |                      Cookie (20bytes)                         |
1123  * |                                                               |
1124  * |                                                               |
1125  * |---------------------------------------------------------------|
1126  */
1127 int send_smack_request(int sock_fd, const char *cookie)
1128 {
1129     basic_header hdr;
1130     int retval;
1131     unsigned char buf[sizeof(hdr) + SECURITY_SERVER_COOKIE_LEN];
1132
1133     /* Assemble header */
1134     hdr.version = SECURITY_SERVER_MSG_VERSION;
1135     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_SMACK_REQUEST;
1136     hdr.msg_len = SECURITY_SERVER_COOKIE_LEN;
1137
1138     memcpy(buf, &hdr, sizeof(hdr));
1139     memcpy(buf + sizeof(hdr), cookie, SECURITY_SERVER_COOKIE_LEN);
1140
1141     /* Check poll */
1142     retval = check_socket_poll(sock_fd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1143     if (retval == SECURITY_SERVER_ERROR_POLL)
1144     {
1145         SEC_SVR_ERR("%s", "poll() error");
1146         return SECURITY_SERVER_ERROR_SEND_FAILED;
1147     }
1148     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
1149     {
1150         SEC_SVR_ERR("%s", "poll() timeout");
1151         return SECURITY_SERVER_ERROR_SEND_FAILED;
1152     }
1153
1154     /* Send to server */
1155     retval = TEMP_FAILURE_RETRY(write(sock_fd, buf, sizeof(buf)));
1156     if (retval < sizeof(buf))
1157     {
1158         /* Write error */
1159         SEC_SVR_ERR("Error on write(): %d", retval);
1160         return SECURITY_SERVER_ERROR_SEND_FAILED;
1161     }
1162     return SECURITY_SERVER_SUCCESS;
1163 }
1164
1165 //VERSION:      0x01
1166 //MSG_ID:       0x1f (SECURITY_SERVER_MSG_TYPE_CHECK_PID_PRIVILEGE_REQUEST)
1167 //DATA_SIZE:    strlen(object) + 1 + strlen(access_rights) + 1
1168 int send_pid_privilege_request(int sockfd, int pid, const char *object, const char *access_rights)
1169 {
1170     //header structure
1171     basic_header hdr;
1172     int retval;
1173     int message_size;
1174     //buffer for data
1175     char *buff = NULL;
1176     int offset = 0;
1177
1178     if (pid < 0) {
1179         SEC_SVR_ERR("%s", "Error input param");
1180         retval = SECURITY_SERVER_ERROR_INPUT_PARAM;
1181         goto error;
1182     }
1183
1184     if (object == NULL) {
1185         SEC_SVR_ERR("%s", "Error input param");
1186         retval = SECURITY_SERVER_ERROR_INPUT_PARAM;
1187         goto error;
1188     }
1189
1190     //allocate buffer
1191     //+1 for the '\0' at string end
1192
1193     message_size = sizeof(int) + strlen(object) + 1 + strlen(access_rights) + 1;
1194     buff = (char*)malloc(message_size + sizeof(hdr));
1195     if (buff == NULL) {
1196         SEC_SVR_ERR("%s", "malloc() error");
1197         retval = SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
1198         goto error;
1199     }
1200
1201     //clear buffer
1202     bzero(buff, message_size + sizeof(hdr));
1203
1204     //create header
1205     hdr.version = SECURITY_SERVER_MSG_VERSION;
1206     //MSG_ID
1207     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_CHECK_PID_PRIVILEGE_REQUEST;
1208     //set message size without header (data size)
1209     hdr.msg_len = message_size;
1210
1211     //copy message fields to buffer
1212     offset = 0;
1213     memcpy(&buff[offset], &hdr, sizeof(hdr));
1214     offset += sizeof(hdr);
1215     //add PID
1216     memcpy(&buff[offset], &pid, sizeof(pid));
1217     offset += sizeof(pid);
1218     //add *object with NULL at the end
1219     memcpy(&buff[offset], object, strlen(object));
1220     offset += strlen(object);
1221     buff[offset] = 0;
1222     offset += 1;
1223     //add *access_rights with NULL at the end
1224     memcpy(&buff[offset], access_rights, strlen(access_rights));
1225     offset += strlen(access_rights);
1226     buff[offset] = 0;
1227
1228     //check pool
1229     retval = check_socket_poll(sockfd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1230     if (retval == SECURITY_SERVER_ERROR_POLL) {
1231         SEC_SVR_ERR("%s", "poll() error");
1232         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1233         goto error;
1234     }
1235     if (retval == SECURITY_SERVER_ERROR_TIMEOUT) {
1236         SEC_SVR_ERR("%s", "poll() timeout");
1237         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1238         goto error;
1239     }
1240
1241     //send message
1242     retval = TEMP_FAILURE_RETRY(write(sockfd, buff, message_size + sizeof(hdr)));
1243     if (retval < message_size) {
1244         //error on write
1245         SEC_SVR_ERR("Error on write(): %d", retval);
1246         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1247         goto error;
1248     }
1249     retval = SECURITY_SERVER_SUCCESS;
1250 error:
1251     if (buff != NULL)
1252         free(buff);
1253
1254     return retval;
1255 }
1256
1257 /* Send PID check request message to security server *
1258  *
1259  * Message format
1260  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1261  * |---------------------------------------------------------------|
1262  * | version=0x01  |MessageID=0x09 |      Message Length = 20      |
1263  * |---------------------------------------------------------------|
1264  * |                                                               |
1265  * |                                                               |
1266  * |                      Cookie (20bytes)                         |
1267  * |                                                               |
1268  * |                                                               |
1269  * |---------------------------------------------------------------|
1270  */
1271 int send_pid_request(int sock_fd, const char *cookie)
1272 {
1273     basic_header hdr;
1274     int retval;
1275     unsigned char buf[sizeof(hdr) + SECURITY_SERVER_COOKIE_LEN];
1276
1277     /* Assemble header */
1278     hdr.version = SECURITY_SERVER_MSG_VERSION;
1279     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_PID_REQUEST;
1280     hdr.msg_len = SECURITY_SERVER_COOKIE_LEN;
1281
1282     memcpy(buf, &hdr, sizeof(hdr));
1283     memcpy(buf + sizeof(hdr), cookie, SECURITY_SERVER_COOKIE_LEN);
1284
1285     /* Check poll */
1286     retval = check_socket_poll(sock_fd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1287     if (retval == SECURITY_SERVER_ERROR_POLL)
1288     {
1289         SEC_SVR_ERR("%s", "poll() error");
1290         return SECURITY_SERVER_ERROR_SEND_FAILED;
1291     }
1292     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
1293     {
1294         SEC_SVR_ERR("%s", "poll() timeout");
1295         return SECURITY_SERVER_ERROR_SEND_FAILED;
1296     }
1297
1298     /* Send to server */
1299     retval = TEMP_FAILURE_RETRY(write(sock_fd, buf, sizeof(buf)));
1300     if (retval < sizeof(buf))
1301     {
1302         /* Write error */
1303         SEC_SVR_ERR("Error on write(): %d", retval);
1304         return SECURITY_SERVER_ERROR_SEND_FAILED;
1305     }
1306     return SECURITY_SERVER_SUCCESS;
1307 }
1308
1309
1310 /* Send debug tool launch request message to security server *
1311  *
1312  * Message format
1313  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1314  * |---------------------------------------------------------------|
1315  * | version=0x01  |MessageID=0x0b |       Message Length          |
1316  * |---------------------------------------------------------------|
1317  * |                        total # of args                        |
1318  * |---------------------------------------------------------------|
1319  * |                        1st argv length                        |
1320  * |---------------------------------------------------------------|
1321  * |                                                               |
1322  * |                            1st argv                           |
1323  * |                                                               |
1324  * |---------------------------------------------------------------|
1325  * |                        2nd argv length                        |
1326  * |---------------------------------------------------------------|
1327  * |                                                               |
1328  * |                            2nd argv                           |
1329  * |                                                               |
1330  * |---------------------------------------------------------------|
1331  * |                                ...                            |
1332  * |---------------------------------------------------------------|
1333  * |                        nth argv length                        |
1334  * |---------------------------------------------------------------|
1335  * |                                                               |
1336  * |                            nth argv                           |
1337  * |                                                               |
1338  * |---------------------------------------------------------------|
1339  */
1340 int send_launch_tool_request(int sock_fd, int argc, const char **argv)
1341 {
1342     basic_header hdr;
1343     int retval, total_length = 0, ptr, i, tempnum;
1344     unsigned char *buf = NULL;
1345
1346     for (i = 0; i < argc; i++)
1347     {
1348         if (argv[i] == NULL)
1349         {
1350             SEC_SVR_ERR("Error: %dth argv is NULL", i);
1351             return SECURITY_SERVER_ERROR_INPUT_PARAM;
1352         }
1353         total_length += strlen(argv[i]);
1354     }
1355
1356     if (total_length < 1)
1357     {
1358         SEC_SVR_ERR("Error: There is a problem in argv. [%d]", total_length);
1359         return SECURITY_SERVER_ERROR_INPUT_PARAM;
1360     }
1361     total_length += sizeof(hdr) + sizeof(int) + (argc * sizeof(int));
1362
1363     if (total_length > 0xffff)
1364     {
1365         SEC_SVR_ERR("Buffer overflow. too big payload. [%d]", total_length);
1366         return SECURITY_SERVER_ERROR_INPUT_PARAM;
1367     }
1368
1369     buf = malloc(total_length);
1370     if (buf == NULL)
1371     {
1372         SEC_SVR_ERR("%s", "Error: failed to malloc()");
1373         return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
1374     }
1375
1376     /* Assemble header */
1377     hdr.version = SECURITY_SERVER_MSG_VERSION;
1378     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_TOOL_REQUEST;
1379     hdr.msg_len = (unsigned short)total_length;
1380     memcpy(buf, &hdr, sizeof(hdr));
1381     ptr = sizeof(hdr);
1382     memcpy(buf + ptr, &argc, sizeof(int));
1383     ptr += sizeof(hdr);
1384
1385     /* Assemple each argv length and value */
1386     for (i = 0; i < argc; i++)
1387     {
1388         tempnum = strlen(argv[i]);
1389         memcpy(buf + ptr, &tempnum, sizeof(int));
1390         ptr += sizeof(int);
1391         memcpy(buf + ptr, argv[i], tempnum);
1392         ptr += tempnum;
1393     }
1394
1395     /* Check poll */
1396     retval = check_socket_poll(sock_fd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1397     if (retval == SECURITY_SERVER_ERROR_POLL)
1398     {
1399         SEC_SVR_ERR("%s", "poll() error");
1400         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1401         goto error;
1402     }
1403     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
1404     {
1405         SEC_SVR_ERR("%s", "poll() timeout");
1406         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1407         goto error;
1408     }
1409
1410     /* Send to server */
1411     retval = TEMP_FAILURE_RETRY(write(sock_fd, buf, total_length));
1412     if (retval < sizeof(buf))
1413     {
1414         /* Write error */
1415         SEC_SVR_ERR("Error on write(): %d", retval);
1416         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1417         goto error;
1418     }
1419     retval = SECURITY_SERVER_SUCCESS;
1420
1421 error:
1422     if (buf != NULL)
1423         free(buf);
1424     return retval;
1425 }
1426
1427 /* Send validate password request message to security server *
1428  *
1429  * Message format
1430  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1431  * |---------------------------------------------------------------|
1432  * | version=0x01  |MessageID=0x0d |       Message Length          |
1433  * |---------------------------------------------------------------|
1434  */
1435 int send_valid_pwd_request(int sock_fd)
1436 {
1437     basic_header hdr;
1438     int retval;
1439
1440     hdr.version = SECURITY_SERVER_MSG_VERSION;
1441     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_VALID_PWD_REQUEST;
1442     hdr.msg_len = 0;
1443
1444     /* Check poll */
1445     retval = check_socket_poll(sock_fd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1446     if (retval == SECURITY_SERVER_ERROR_POLL)
1447     {
1448         SEC_SVR_ERR("%s", "poll() error");
1449         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1450         goto error;
1451     }
1452     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
1453     {
1454         SEC_SVR_ERR("%s", "poll() timeout");
1455         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1456         goto error;
1457     }
1458
1459     /* Send to server */
1460     retval = TEMP_FAILURE_RETRY(write(sock_fd, &hdr, sizeof(hdr)));
1461     if (retval < sizeof(hdr))
1462     {
1463         /* Write error */
1464         SEC_SVR_ERR("Error on write(): %d", retval);
1465         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1466         goto error;
1467     }
1468     retval = SECURITY_SERVER_SUCCESS;
1469
1470 error:
1471     return retval;
1472 }
1473
1474 /* Send password set request message to security server *
1475  *
1476  * Message format
1477  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1478  * |---------------------------------------------------------------|
1479  * | version=0x01  |MessageID=0x0f |       Message Length          |
1480  * |---------------------------------------------------------------|
1481  * |  cur_pwd_len  |  new_pwd_len  |                               |
1482  * |--------------------------------                               |
1483  * |                            cur pwd                            |
1484  * |---------------------------------------------------------------|
1485  * |                                                               |
1486  * |                            new pwd                            |
1487  * |                                                               |
1488  * |---------------------------------------------------------------|
1489  * |                         max attempts                          |
1490  * |---------------------------------------------------------------|
1491  * |                         valid days                            |
1492  * |---------------------------------------------------------------|
1493  */
1494 int send_set_pwd_request(int sock_fd,
1495                          const char *cur_pwd,
1496                          const char *new_pwd,
1497                          const unsigned int max_challenge,
1498                          const unsigned int valid_period_in_days)
1499 {
1500     basic_header hdr;
1501     int retval, total_length = 0, ptr;
1502     unsigned char *buf = NULL, cur_pwd_len, new_pwd_len;
1503
1504     if (cur_pwd == NULL)
1505         cur_pwd_len = 0;
1506     else
1507         cur_pwd_len = strlen(cur_pwd);
1508     new_pwd_len = strlen(new_pwd);
1509
1510     total_length += sizeof(hdr) + sizeof(char) + sizeof(char) + cur_pwd_len
1511                     + new_pwd_len + sizeof(unsigned int) + sizeof(unsigned int);
1512
1513     buf = malloc(total_length);
1514     if (buf == NULL)
1515     {
1516         SEC_SVR_ERR("%s", "Error: failed to malloc()");
1517         return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
1518     }
1519
1520     /* Assemble header */
1521     hdr.version = SECURITY_SERVER_MSG_VERSION;
1522     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_SET_PWD_REQUEST;
1523     hdr.msg_len = (unsigned short)total_length;
1524     memcpy(buf, &hdr, sizeof(hdr));
1525     ptr = sizeof(hdr);
1526     memcpy(buf + ptr, &cur_pwd_len, sizeof(char));
1527     ptr += sizeof(char);
1528     memcpy(buf + ptr, &new_pwd_len, sizeof(char));
1529     ptr += sizeof(char);
1530     if (cur_pwd != NULL)
1531     {
1532         memcpy(buf + ptr, cur_pwd, cur_pwd_len);
1533         ptr += cur_pwd_len;
1534     }
1535     memcpy(buf + ptr, new_pwd, new_pwd_len);
1536     ptr += new_pwd_len;
1537     memcpy(buf + ptr, &max_challenge, sizeof(unsigned int));
1538     ptr += sizeof(unsigned int);
1539     memcpy(buf + ptr, &valid_period_in_days, sizeof(unsigned int));
1540
1541     /* Check poll */
1542     retval = check_socket_poll(sock_fd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1543     if (retval == SECURITY_SERVER_ERROR_POLL)
1544     {
1545         SEC_SVR_ERR("%s", "poll() error");
1546         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1547         goto error;
1548     }
1549     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
1550     {
1551         SEC_SVR_ERR("%s", "poll() timeout");
1552         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1553         goto error;
1554     }
1555
1556     /* Send to server */
1557     retval = TEMP_FAILURE_RETRY(write(sock_fd, buf, total_length));
1558     if (retval < sizeof(buf))
1559     {
1560         /* Write error */
1561         SEC_SVR_ERR("Error on write(): %d", retval);
1562         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1563         goto error;
1564     }
1565     retval = SECURITY_SERVER_SUCCESS;
1566
1567 error:
1568     if (buf != NULL)
1569         free(buf);
1570     return retval;
1571 }
1572
1573 /* Send password validity change request message to security server *
1574  *
1575  * Message format
1576  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1577  * |---------------------------------------------------------------|
1578  * | version=0x01  |MessageID=0x0f |       Message Length          |
1579  * |---------------------------------------------------------------|
1580  * |                         valid days                            |
1581  * |---------------------------------------------------------------|
1582  */
1583 int send_set_pwd_validity_request(int sock_fd, const unsigned int valid_period_in_days)
1584 {
1585     basic_header hdr;
1586     int retval, total_length = 0, ptr;
1587     unsigned char *buf = NULL;
1588
1589     total_length = sizeof(hdr) + sizeof(unsigned int);
1590
1591     buf = malloc(total_length);
1592     if (buf == NULL)
1593     {
1594         SEC_SVR_ERR("%s", "Error: failed to malloc()");
1595         return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
1596     }
1597
1598     /* Assemble header */
1599     hdr.version = SECURITY_SERVER_MSG_VERSION;
1600     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_SET_PWD_VALIDITY_REQUEST;
1601     hdr.msg_len = (unsigned short)total_length;
1602     memcpy(buf, &hdr, sizeof(hdr));
1603     ptr = sizeof(hdr);
1604     memcpy(buf + ptr, &valid_period_in_days, sizeof(unsigned int));
1605
1606     /* Check poll */
1607     retval = check_socket_poll(sock_fd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1608     if (retval == SECURITY_SERVER_ERROR_POLL)
1609     {
1610         SEC_SVR_ERR("%s", "poll() error");
1611         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1612         goto error;
1613     }
1614     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
1615     {
1616         SEC_SVR_ERR("%s", "poll() timeout");
1617         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1618         goto error;
1619     }
1620
1621     /* Send to server */
1622     retval = TEMP_FAILURE_RETRY(write(sock_fd, buf, total_length));
1623     if (retval < sizeof(buf))
1624     {
1625         /* Write error */
1626         SEC_SVR_ERR("Error on write(): %d", retval);
1627         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1628         goto error;
1629     }
1630     retval = SECURITY_SERVER_SUCCESS;
1631
1632 error:
1633     if (buf != NULL)
1634         free(buf);
1635     return retval;
1636 }
1637
1638 /* Send password max challenge request message to security server *
1639  *
1640  * Message format
1641  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1642  * |---------------------------------------------------------------|
1643  * | version=0x01  |MessageID=0x0f |       Message Length          |
1644  * |---------------------------------------------------------------|
1645  * |                         max challenge                         |
1646  * |---------------------------------------------------------------|
1647  */
1648 int send_set_pwd_max_challenge_request(int sock_fd, const unsigned int max_challenge)
1649 {
1650     basic_header hdr;
1651     int retval, total_length = 0, ptr;
1652     unsigned char *buf = NULL;
1653
1654     total_length = sizeof(hdr) + sizeof(unsigned int);
1655
1656     buf = malloc(total_length);
1657     if (buf == NULL)
1658     {
1659         SEC_SVR_ERR("%s", "Error: failed to malloc()");
1660         return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
1661     }
1662
1663     /* Assemble header */
1664     hdr.version = SECURITY_SERVER_MSG_VERSION;
1665     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_SET_PWD_MAX_CHALLENGE_REQUEST;
1666     hdr.msg_len = (unsigned short)total_length;
1667     memcpy(buf, &hdr, sizeof(hdr));
1668     ptr = sizeof(hdr);
1669     memcpy(buf + ptr, &max_challenge, sizeof(unsigned int));
1670
1671     /* Check poll */
1672     retval = check_socket_poll(sock_fd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1673     if (retval == SECURITY_SERVER_ERROR_POLL)
1674     {
1675         SEC_SVR_ERR("%s", "poll() error");
1676         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1677         goto error;
1678     }
1679     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
1680     {
1681         SEC_SVR_ERR("%s", "poll() timeout");
1682         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1683         goto error;
1684     }
1685
1686     /* Send to server */
1687     retval = TEMP_FAILURE_RETRY(write(sock_fd, buf, total_length));
1688     if (retval < sizeof(buf))
1689     {
1690         /* Write error */
1691         SEC_SVR_ERR("Error on write(): %d", retval);
1692         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1693         goto error;
1694     }
1695     retval = SECURITY_SERVER_SUCCESS;
1696
1697 error:
1698     if (buf != NULL)
1699         free(buf);
1700     return retval;
1701 }
1702
1703 /* Send password reset request message to security server *
1704  *
1705  * Message format
1706  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1707  * |---------------------------------------------------------------|
1708  * | version=0x01  |MessageID=0x11 |       Message Length          |
1709  * |---------------------------------------------------------------|
1710  * |  new_pwd_len  |                                               |
1711  * |---------------------------------------------------------------|
1712  * |                                                               |
1713  * |                            new pwd                            |
1714  * |                                                               |
1715  * |---------------------------------------------------------------|
1716  * |                         max attempts                          |
1717  * |---------------------------------------------------------------|
1718  * |                         valid days                            |
1719  * |---------------------------------------------------------------|
1720  */
1721 int send_reset_pwd_request(int sock_fd,
1722                            const char *new_pwd,
1723                            const unsigned int max_challenge,
1724                            const unsigned int valid_period_in_days)
1725 {
1726     basic_header hdr;
1727     int retval, total_length = 0, ptr;
1728     unsigned char *buf = NULL, new_pwd_len;
1729
1730     new_pwd_len = strlen(new_pwd);
1731
1732     total_length += sizeof(hdr) + sizeof(char) + new_pwd_len + sizeof(unsigned int) +
1733                     sizeof(unsigned int);
1734
1735     buf = malloc(total_length);
1736     if (buf == NULL)
1737     {
1738         SEC_SVR_ERR("%s", "Error: failed to malloc()");
1739         return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
1740     }
1741
1742     /* Assemble header */
1743     hdr.version = SECURITY_SERVER_MSG_VERSION;
1744     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_RESET_PWD_REQUEST;
1745     hdr.msg_len = (unsigned short)total_length;
1746     memcpy(buf, &hdr, sizeof(hdr));
1747     ptr = sizeof(hdr);
1748     memcpy(buf + ptr, &new_pwd_len, sizeof(char));
1749     ptr += sizeof(char);
1750     memcpy(buf + ptr, new_pwd, new_pwd_len);
1751     ptr += new_pwd_len;
1752     memcpy(buf + ptr, &max_challenge, sizeof(unsigned int));
1753     ptr += sizeof(unsigned int);
1754     memcpy(buf + ptr, &valid_period_in_days, sizeof(unsigned int));
1755
1756     /* Check poll */
1757     retval = check_socket_poll(sock_fd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1758     if (retval == SECURITY_SERVER_ERROR_POLL)
1759     {
1760         SEC_SVR_ERR("%s", "poll() error");
1761         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1762         goto error;
1763     }
1764     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
1765     {
1766         SEC_SVR_ERR("%s", "poll() timeout");
1767         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1768         goto error;
1769     }
1770
1771     /* Send to server */
1772     retval = TEMP_FAILURE_RETRY(write(sock_fd, buf, total_length));
1773     if (retval < sizeof(buf))
1774     {
1775         /* Write error */
1776         SEC_SVR_ERR("Error on write(): %d", retval);
1777         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1778         goto error;
1779     }
1780     retval = SECURITY_SERVER_SUCCESS;
1781
1782 error:
1783     if (buf != NULL)
1784         free(buf);
1785     return retval;
1786 }
1787
1788 /* Send password check request message to security server *
1789  *
1790  * Message format
1791  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1792  * |---------------------------------------------------------------|
1793  * | version=0x01  |MessageID=0x13 |       Message Length          |
1794  * |---------------------------------------------------------------|
1795  * | challenge_len |                                               |
1796  * |---------------                                                |
1797  * |                          challenge                            |
1798  * |---------------------------------------------------------------|
1799  */
1800 int send_chk_pwd_request(int sock_fd, const char *challenge)
1801 {
1802     basic_header hdr;
1803     int retval, total_length = 0, ptr;
1804     unsigned char *buf = NULL, challenge_len;
1805
1806     challenge_len = strlen(challenge);
1807
1808     total_length += sizeof(hdr) + sizeof(char) + challenge_len;
1809
1810     buf = malloc(total_length);
1811     if (buf == NULL)
1812     {
1813         SEC_SVR_ERR("%s", "Error: failed to malloc()");
1814         return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
1815     }
1816
1817     /* Assemble header */
1818     hdr.version = SECURITY_SERVER_MSG_VERSION;
1819     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_CHK_PWD_REQUEST;
1820     hdr.msg_len = (unsigned short)total_length;
1821     memcpy(buf, &hdr, sizeof(hdr));
1822     ptr = sizeof(hdr);
1823     memcpy(buf + ptr, &challenge_len, sizeof(char));
1824     ptr += sizeof(char);
1825     memcpy(buf + ptr, challenge, challenge_len);
1826     ptr += sizeof(char);
1827
1828     /* Check poll */
1829     retval = check_socket_poll(sock_fd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1830     if (retval == SECURITY_SERVER_ERROR_POLL)
1831     {
1832         SEC_SVR_ERR("%s", "poll() error");
1833         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1834         goto error;
1835     }
1836     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
1837     {
1838         SEC_SVR_ERR("%s", "poll() timeout");
1839         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1840         goto error;
1841     }
1842
1843     /* Send to server */
1844     retval = TEMP_FAILURE_RETRY(write(sock_fd, buf, total_length));
1845     if (retval < sizeof(buf))
1846     {
1847         /* Write error */
1848         SEC_SVR_ERR("Error on write(): %d", retval);
1849         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1850         goto error;
1851     }
1852     retval = SECURITY_SERVER_SUCCESS;
1853
1854 error:
1855     if (buf != NULL)
1856         free(buf);
1857     return retval;
1858 }
1859
1860 /* Send password history set request message to security server *
1861  *
1862  * Message format
1863  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1864  * |---------------------------------------------------------------|
1865  * | version=0x01  |MessageID=0x15 |       Message Length          |
1866  * |---------------------------------------------------------------|
1867  * | challenge_len |
1868  * |----------------
1869  */
1870 int send_set_pwd_history_request(int sock_fd, int num)
1871 {
1872     basic_header hdr;
1873     int retval, total_length = 0, ptr;
1874     unsigned char history;
1875     unsigned char buf[sizeof(hdr) + sizeof(history)];
1876
1877     total_length = sizeof(hdr) + sizeof(char);
1878     history = (unsigned char) num;
1879
1880     /* Assemble header */
1881     hdr.version = SECURITY_SERVER_MSG_VERSION;
1882     hdr.msg_id = SECURITY_SERVER_MSG_TYPE_SET_PWD_HISTORY_REQUEST;
1883     hdr.msg_len = (unsigned short)total_length;
1884     memcpy(buf, &hdr, sizeof(hdr));
1885     ptr = sizeof(hdr);
1886     memcpy(buf + ptr, &history, sizeof(char));
1887     ptr += sizeof(char);
1888
1889     /* Check poll */
1890     retval = check_socket_poll(sock_fd, POLLOUT, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1891     if (retval == SECURITY_SERVER_ERROR_POLL)
1892     {
1893         SEC_SVR_ERR("%s", "poll() error");
1894         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1895         goto error;
1896     }
1897     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
1898     {
1899         SEC_SVR_ERR("%s", "poll() timeout");
1900         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1901         goto error;
1902     }
1903
1904     /* Send to server */
1905     retval = TEMP_FAILURE_RETRY(write(sock_fd, buf, ptr));
1906     if (retval < sizeof(buf))
1907     {
1908         /* Write error */
1909         SEC_SVR_ERR("Error on write(): %d", retval);
1910         retval = SECURITY_SERVER_ERROR_SEND_FAILED;
1911         goto error;
1912     }
1913     retval = SECURITY_SERVER_SUCCESS;
1914
1915 error:
1916     return retval;
1917 }
1918
1919 /* Receive request header */
1920 int recv_hdr(int client_sockfd, basic_header *basic_hdr)
1921 {
1922     int retval;
1923
1924     /* Check poll */
1925     retval = check_socket_poll(client_sockfd, POLLIN, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
1926     if (retval == SECURITY_SERVER_ERROR_POLL)
1927     {
1928         SEC_SVR_ERR("%s", "poll() error");
1929         return SECURITY_SERVER_ERROR_SOCKET;
1930     }
1931     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
1932     {
1933         SEC_SVR_ERR("%s", "poll() timeout");
1934         return SECURITY_SERVER_ERROR_TIMEOUT;
1935     }
1936
1937     /* Receive request header first */
1938     retval = TEMP_FAILURE_RETRY(read(client_sockfd, basic_hdr, sizeof(basic_header)));
1939     if (retval < sizeof(basic_header))
1940     {
1941         SEC_SVR_ERR("read failed. closing socket %d", retval);
1942         return SECURITY_SERVER_ERROR_RECV_FAILED;
1943     }
1944
1945     /* Validate header */
1946     retval = validate_header(*basic_hdr);
1947     return retval;
1948 }
1949
1950
1951 /* Receive check privilege request packet body */
1952 int recv_check_privilege_request(int sockfd, unsigned char *requested_cookie, int *requested_privilege)
1953 {
1954     int retval;
1955     retval = TEMP_FAILURE_RETRY(read(sockfd, requested_cookie, SECURITY_SERVER_COOKIE_LEN));
1956     if (retval < SECURITY_SERVER_COOKIE_LEN)
1957     {
1958         SEC_SVR_ERR("Received cookie size is too small: %d", retval);
1959         return SECURITY_SERVER_ERROR_RECV_FAILED;
1960     }
1961
1962     retval = TEMP_FAILURE_RETRY(read(sockfd, requested_privilege, sizeof(int)));
1963     if (retval < sizeof(int))
1964     {
1965         SEC_SVR_ERR("privilege size is too small: %d", retval);
1966         return SECURITY_SERVER_ERROR_RECV_FAILED;
1967     }
1968     return SECURITY_SERVER_SUCCESS;
1969 }
1970
1971 /* Receive check privilege request packet body (new mode)*/
1972 int recv_check_privilege_new_request(int sockfd,
1973                                      unsigned char *requested_cookie,
1974                                      char *object_label,
1975                                      char *access_rights)
1976 {
1977     int retval;
1978     int olen, alen;
1979
1980     retval = TEMP_FAILURE_RETRY(read(sockfd, requested_cookie, SECURITY_SERVER_COOKIE_LEN));
1981     if (retval < SECURITY_SERVER_COOKIE_LEN)
1982     {
1983         SEC_SVR_ERR("Received cookie size is too small: %d", retval);
1984         return SECURITY_SERVER_ERROR_RECV_FAILED;
1985     }
1986
1987     retval = TEMP_FAILURE_RETRY(read(sockfd, &olen, sizeof(int)));
1988     if (retval < sizeof(int) || olen < 0 || olen > MAX_OBJECT_LABEL_LEN)
1989     {
1990         SEC_SVR_ERR("error reading object_label len: %d", retval);
1991         return SECURITY_SERVER_ERROR_RECV_FAILED;
1992     }
1993
1994     retval = TEMP_FAILURE_RETRY(read(sockfd, &alen, sizeof(int)));
1995     if (retval < sizeof(int) || alen < 0 || alen > MAX_MODE_STR_LEN)
1996     {
1997         SEC_SVR_ERR("error reading access_rights len: %d", retval);
1998         return SECURITY_SERVER_ERROR_RECV_FAILED;
1999     }
2000
2001     retval = TEMP_FAILURE_RETRY(read(sockfd, object_label, olen));
2002     if (retval < olen)
2003     {
2004         SEC_SVR_ERR("error reading object_label: %d", retval);
2005         return SECURITY_SERVER_ERROR_RECV_FAILED;
2006     }
2007     object_label[olen] = '\0';
2008
2009     retval = TEMP_FAILURE_RETRY(read(sockfd, access_rights, alen));
2010     if (retval < alen)
2011     {
2012         SEC_SVR_ERR("error reading access_rights: %d", retval);
2013         return SECURITY_SERVER_ERROR_RECV_FAILED;
2014     }
2015     access_rights[alen] = '\0';
2016
2017     return SECURITY_SERVER_SUCCESS;
2018 }
2019
2020 /* Receive pid request packet body */
2021 int recv_pid_request(int sockfd, unsigned char *requested_cookie)
2022 {
2023     int retval;
2024     retval = TEMP_FAILURE_RETRY(read(sockfd, requested_cookie, SECURITY_SERVER_COOKIE_LEN));
2025     if (retval < SECURITY_SERVER_COOKIE_LEN)
2026     {
2027         SEC_SVR_ERR("Received cookie size is too small: %d", retval);
2028         return SECURITY_SERVER_ERROR_RECV_FAILED;
2029     }
2030     return SECURITY_SERVER_SUCCESS;
2031 }
2032
2033 /* receiving cookie from package */
2034 int recv_smack_request(int sockfd, unsigned char *requested_cookie)
2035 {
2036     int retval;
2037     retval = TEMP_FAILURE_RETRY(read(sockfd, requested_cookie, SECURITY_SERVER_COOKIE_LEN));
2038     if (retval < SECURITY_SERVER_COOKIE_LEN)
2039     {
2040         SEC_SVR_ERR("Received cookie size is too small: %d", retval);
2041         return SECURITY_SERVER_ERROR_RECV_FAILED;
2042     }
2043     return SECURITY_SERVER_SUCCESS;
2044 }
2045
2046 int recv_pid_privilege_request(int sockfd, int datasize, int *pid, char **object, char **access_rights)
2047 {
2048     int retval;
2049     char *buff = NULL;
2050     int object_size = 0;
2051     int access_rights_size = 0;
2052
2053     buff = (char*)malloc(datasize);
2054     if (buff == NULL)
2055         return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
2056
2057     //receive all data to buffer
2058     retval = TEMP_FAILURE_RETRY(read(sockfd, buff, datasize));
2059     if (retval < datasize) {
2060         SEC_SVR_ERR("Received data size is too small: %d / %d", retval, datasize);
2061         retval = SECURITY_SERVER_ERROR_RECV_FAILED;
2062         goto error;
2063     }
2064
2065     //getPID
2066     memcpy(pid, buff, sizeof(int));
2067
2068     //get object
2069     while (buff[sizeof(int) + object_size] != '\0') {
2070         object_size++;
2071
2072         if (object_size > datasize) {
2073             SEC_SVR_ERR("%s", "Wrong object_size");
2074             retval = SECURITY_SERVER_ERROR_UNKNOWN;
2075             goto error;
2076         }
2077     }
2078     object_size++; //for '\0' at end
2079
2080     *object = (char*)malloc(object_size);
2081     memcpy(*object, buff + sizeof(int), object_size);
2082
2083     //get access_rights
2084     access_rights_size = datasize - object_size - sizeof(int);
2085     *access_rights = (char*)malloc(access_rights_size);
2086     memcpy(*access_rights, buff + sizeof(int) + object_size, access_rights_size);
2087
2088     SEC_SVR_DBG("%s %d", "Received PID:", *pid);
2089     SEC_SVR_DBG("%s %s", "Received object:", *object);
2090     SEC_SVR_DBG("%s %s", "Received privileges:", *access_rights);
2091
2092     retval = SECURITY_SERVER_SUCCESS;
2093
2094 error:
2095     if (buff != NULL)
2096         free(buff);
2097
2098     return retval;
2099 }
2100
2101 /* Receive pid request packet body */
2102 /* Table argv and content will be freed by function caller */
2103 int recv_launch_tool_request(int sockfd, int argc, char *argv[])
2104 {
2105     int retval, i, argv_len;
2106
2107     argv[0] = malloc(strlen(SECURITY_SERVER_DEBUG_TOOL_PATH) + 1);
2108     strncpy(argv[0], SECURITY_SERVER_DEBUG_TOOL_PATH, (strlen(SECURITY_SERVER_DEBUG_TOOL_PATH) + 1));
2109
2110     for (i = 1; i < argc; i++)
2111     {
2112         retval = TEMP_FAILURE_RETRY(read(sockfd, &argv_len, sizeof(int)));
2113         if (retval < sizeof(int))
2114         {
2115             SEC_SVR_ERR("Error: argv length recieve failed: %d", retval);
2116             return SECURITY_SERVER_ERROR_RECV_FAILED;
2117         }
2118
2119         if (argv_len <= 0 || argv_len >= INT_MAX)
2120         {
2121             SEC_SVR_ERR("Error: argv length out of boundaries");
2122             return SECURITY_SERVER_ERROR_RECV_FAILED;
2123         }
2124
2125         argv[i] = malloc(argv_len + 1);
2126         if (argv[i] == NULL)
2127         {
2128             SEC_SVR_ERR("Error: malloc() failed: %d", retval);
2129             return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
2130         }
2131
2132         memset(argv[i], 0x00, argv_len + 1);
2133         retval = TEMP_FAILURE_RETRY(read(sockfd, argv[i], argv_len));
2134         if (retval < argv_len)
2135         {
2136             SEC_SVR_ERR("Error: argv recieve failed: %d", retval);
2137             return SECURITY_SERVER_ERROR_RECV_FAILED;
2138         }
2139     }
2140
2141     return SECURITY_SERVER_SUCCESS;
2142 }
2143
2144 int recv_generic_response(int sockfd, response_header *hdr)
2145 {
2146     int retval;
2147
2148     /* Check poll */
2149     retval = check_socket_poll(sockfd, POLLIN, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
2150     if (retval == SECURITY_SERVER_ERROR_POLL)
2151     {
2152         SEC_SVR_ERR("%s", "Client: poll() error");
2153         return SECURITY_SERVER_ERROR_RECV_FAILED;
2154     }
2155     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
2156     {
2157         SEC_SVR_ERR("%s", "Client: poll() timeout");
2158         return SECURITY_SERVER_ERROR_RECV_FAILED;
2159     }
2160
2161     /* Receive response */
2162     retval = TEMP_FAILURE_RETRY(read(sockfd, hdr, sizeof(response_header)));
2163     if (retval < sizeof(response_header))
2164     {
2165         /* Error on socket */
2166         SEC_SVR_ERR("Client: Receive failed %d", retval);
2167         return SECURITY_SERVER_ERROR_RECV_FAILED;
2168     }
2169
2170     if (hdr->return_code != SECURITY_SERVER_RETURN_CODE_SUCCESS)
2171     {
2172         /* Return codes
2173          *   SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_REQUEST
2174          *   SECURITY_SERVER_MSG_TYPE_CHECK_PRIVILEGE_RESPONSE
2175          * are not errors but warnings
2176          */
2177         SEC_SVR_WRN("Client: return code is not success: %d", hdr->return_code);
2178         return return_code_to_error_code(hdr->return_code);
2179     }
2180     return SECURITY_SERVER_SUCCESS;
2181 }
2182
2183 int recv_get_gid_response(int sockfd, response_header *hdr, int *gid)
2184 {
2185     int retval;
2186
2187     retval = recv_generic_response(sockfd, hdr);
2188     if (retval != SECURITY_SERVER_SUCCESS)
2189         return return_code_to_error_code(hdr->return_code);
2190
2191     retval = TEMP_FAILURE_RETRY(read(sockfd, gid, sizeof(int)));
2192     if (retval < sizeof(int))
2193     {
2194         /* Error on socket */
2195         SEC_SVR_ERR("Receive failed %d", retval);
2196         return SECURITY_SERVER_ERROR_RECV_FAILED;
2197     }
2198     return SECURITY_SERVER_SUCCESS;
2199 }
2200
2201 int recv_get_object_name(int sockfd, response_header *hdr, char *object, int max_object_size)
2202 {
2203     int retval;
2204     char *local_obj_name = NULL;
2205
2206     /* Check poll */
2207     retval = check_socket_poll(sockfd, POLLIN, SECURITY_SERVER_SOCKET_TIMEOUT_MILISECOND);
2208     if (retval == SECURITY_SERVER_ERROR_POLL)
2209     {
2210         SEC_SVR_ERR("%s", "poll() error");
2211         return SECURITY_SERVER_ERROR_RECV_FAILED;
2212     }
2213     if (retval == SECURITY_SERVER_ERROR_TIMEOUT)
2214     {
2215         SEC_SVR_ERR("%s", "poll() timeout");
2216         return SECURITY_SERVER_ERROR_RECV_FAILED;
2217     }
2218
2219     /* Read response */
2220     retval = TEMP_FAILURE_RETRY(read(sockfd, hdr, sizeof(response_header)));
2221     if (retval < sizeof(response_header))
2222     {
2223         /* Error on socket */
2224         SEC_SVR_ERR("cannot recv respons: %d", retval);
2225         return SECURITY_SERVER_ERROR_RECV_FAILED;
2226     }
2227
2228     if (hdr->return_code == SECURITY_SERVER_RETURN_CODE_SUCCESS)
2229     {
2230         if (max_object_size < hdr->basic_hdr.msg_len)
2231         {
2232             SEC_SVR_ERR("Object name is too small need %d bytes, but %d bytes", hdr->basic_hdr.msg_len, max_object_size);
2233             return SECURITY_SERVER_ERROR_BUFFER_TOO_SMALL;
2234         }
2235         if (hdr->basic_hdr.msg_len > SECURITY_SERVER_MAX_OBJ_NAME)
2236         {
2237             SEC_SVR_ERR("Received object name is too big. %d", hdr->basic_hdr.msg_len);
2238             return SECURITY_SERVER_ERROR_BAD_RESPONSE;
2239         }
2240
2241         local_obj_name = malloc(hdr->basic_hdr.msg_len + 1);
2242         if (local_obj_name == NULL)
2243         {
2244             SEC_SVR_ERR("%s", "Out of memory error");
2245             return SECURITY_SERVER_ERROR_OUT_OF_MEMORY;
2246         }
2247
2248         retval = TEMP_FAILURE_RETRY(read(sockfd, local_obj_name, hdr->basic_hdr.msg_len));
2249         if (retval < (hdr->basic_hdr.msg_len))
2250         {
2251             /* Error on socket */
2252             SEC_SVR_ERR("read() failed: %d", retval);
2253             if (local_obj_name != NULL)
2254                 free(local_obj_name);
2255             return SECURITY_SERVER_ERROR_RECV_FAILED;
2256         }
2257         memcpy(object, local_obj_name, hdr->basic_hdr.msg_len);
2258         object[hdr->basic_hdr.msg_len] = 0;
2259         retval = SECURITY_SERVER_SUCCESS;
2260     }
2261     else
2262     {
2263         SEC_SVR_ERR("Error received. return code: %d", hdr->return_code);
2264         retval = return_code_to_error_code(hdr->return_code);
2265         return retval;
2266     }
2267
2268     if (local_obj_name != NULL)
2269         free(local_obj_name);
2270     return SECURITY_SERVER_SUCCESS;
2271 }
2272
2273 int recv_cookie(int sockfd, response_header *hdr, char *cookie)
2274 {
2275     int retval;
2276
2277     retval = recv_generic_response(sockfd, hdr);
2278     if (retval != SECURITY_SERVER_SUCCESS)
2279         return return_code_to_error_code(hdr->return_code);
2280
2281     retval = TEMP_FAILURE_RETRY(read(sockfd, cookie, SECURITY_SERVER_COOKIE_LEN));
2282     if (retval < SECURITY_SERVER_COOKIE_LEN)
2283     {
2284         /* Error on socket */
2285         SEC_SVR_ERR("read() failed: %d", retval);
2286         return SECURITY_SERVER_ERROR_RECV_FAILED;
2287     }
2288     return SECURITY_SERVER_SUCCESS;
2289 }
2290
2291 int recv_privilege_check_response(int sockfd, response_header *hdr)
2292 {
2293     int retval;
2294
2295     retval = recv_generic_response(sockfd, hdr);
2296     if (hdr->return_code != SECURITY_SERVER_RETURN_CODE_ACCESS_GRANTED &&
2297         hdr->return_code != SECURITY_SERVER_RETURN_CODE_ACCESS_DENIED)
2298     {
2299         SEC_SVR_ERR("response error: %d", hdr->return_code);
2300         return return_code_to_error_code(hdr->return_code);
2301     }
2302     return SECURITY_SERVER_SUCCESS;
2303 }
2304
2305 int recv_privilege_check_new_response(int sockfd, response_header *hdr)
2306 {
2307     int retval;
2308
2309     retval = recv_generic_response(sockfd, hdr);
2310     if (hdr->return_code != SECURITY_SERVER_RETURN_CODE_ACCESS_GRANTED &&
2311         hdr->return_code != SECURITY_SERVER_RETURN_CODE_ACCESS_DENIED)
2312     {
2313         SEC_SVR_ERR("response error: %d", hdr->return_code);
2314         return return_code_to_error_code(hdr->return_code);
2315     }
2316     return SECURITY_SERVER_SUCCESS;
2317 }
2318
2319 int recv_smack_response(int sockfd, response_header *hdr, char *label)
2320 {
2321     int retval;
2322
2323     retval = recv_generic_response(sockfd, hdr);
2324     if (retval != SECURITY_SERVER_SUCCESS)
2325         return return_code_to_error_code(hdr->return_code);
2326
2327     retval = TEMP_FAILURE_RETRY(read(sockfd, label, SMACK_LABEL_LEN + 1));
2328     if (retval < sizeof(int))
2329     {
2330         /* Error on socket */
2331         SEC_SVR_ERR("Client: Receive failed %d", retval);
2332         return SECURITY_SERVER_ERROR_RECV_FAILED;
2333     }
2334     return SECURITY_SERVER_SUCCESS;
2335 }
2336
2337 int recv_pid_privilege_response(int sockfd, response_header *hdr)
2338 {
2339     int retval;
2340
2341     retval = recv_generic_response(sockfd, hdr);
2342
2343     if (retval != SECURITY_SERVER_SUCCESS)
2344         return return_code_to_error_code(hdr->return_code);
2345
2346     return SECURITY_SERVER_SUCCESS;
2347 }
2348
2349 int recv_pid_response(int sockfd, response_header *hdr, int *pid)
2350 {
2351     int retval;
2352
2353     retval = recv_generic_response(sockfd, hdr);
2354     if (retval != SECURITY_SERVER_SUCCESS)
2355         return return_code_to_error_code(hdr->return_code);
2356
2357     retval = TEMP_FAILURE_RETRY(read(sockfd, pid, sizeof(int)));
2358     if (retval < sizeof(int))
2359     {
2360         /* Error on socket */
2361         SEC_SVR_ERR("Client: Receive failed %d", retval);
2362         return SECURITY_SERVER_ERROR_RECV_FAILED;
2363     }
2364     return SECURITY_SERVER_SUCCESS;
2365 }
2366
2367 int recv_pwd_response(int sockfd, response_header *hdr,
2368                       unsigned int *current_attempts,
2369                       unsigned int *max_attempts,
2370                       unsigned int *valid_secs)
2371 {
2372     int retval;
2373     *current_attempts = 0;
2374     *max_attempts = 0;
2375     *valid_secs = 0;
2376
2377     retval = recv_generic_response(sockfd, hdr);
2378
2379     switch (retval)
2380     {
2381         case SECURITY_SERVER_ERROR_PASSWORD_EXIST:
2382         case SECURITY_SERVER_ERROR_NO_PASSWORD:
2383         case SECURITY_SERVER_ERROR_PASSWORD_MISMATCH:
2384         case SECURITY_SERVER_ERROR_PASSWORD_RETRY_TIMER:
2385         case SECURITY_SERVER_ERROR_PASSWORD_MAX_ATTEMPTS_EXCEEDED:
2386         case SECURITY_SERVER_ERROR_PASSWORD_EXPIRED:
2387         case SECURITY_SERVER_ERROR_PASSWORD_REUSED:
2388         case SECURITY_SERVER_SUCCESS:
2389             break;
2390         default:
2391             return return_code_to_error_code(hdr->return_code);
2392     }
2393
2394     retval = TEMP_FAILURE_RETRY(read(sockfd, current_attempts, sizeof(unsigned int)));
2395     if (retval < sizeof(unsigned int))
2396     {
2397         /* Error on socket */
2398         SEC_SVR_ERR("Client: Receive failed %d", retval);
2399         return SECURITY_SERVER_ERROR_RECV_FAILED;
2400     }
2401     retval = TEMP_FAILURE_RETRY(read(sockfd, max_attempts, sizeof(unsigned int)));
2402     if (retval < sizeof(unsigned int))
2403     {
2404         /* Error on socket */
2405         SEC_SVR_ERR("Client: Receive failed %d", retval);
2406         return SECURITY_SERVER_ERROR_RECV_FAILED;
2407     }
2408     retval = TEMP_FAILURE_RETRY(read(sockfd, valid_secs, sizeof(unsigned int)));
2409     if (retval < sizeof(unsigned int))
2410     {
2411         /* Error on socket */
2412         SEC_SVR_ERR("Client: Receive failed %d", retval);
2413         return SECURITY_SERVER_ERROR_RECV_FAILED;
2414     }
2415
2416     //if come here there were no errors
2417     return SECURITY_SERVER_SUCCESS;
2418 }
2419
2420 /* Authenticate client application *
2421  * Currently it only gets peer's credential information only *
2422  * If we need, we can extend in the futer */
2423 int authenticate_client_application(int sockfd, int *pid, int *uid)
2424 {
2425     struct ucred cr;
2426     unsigned int cl = sizeof(cr);
2427
2428     /* get PID of socket peer */
2429     if (getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &cr, &cl) != 0)
2430     {
2431         SEC_SVR_DBG("%s", "getsockopt failed");
2432         return SECURITY_SERVER_ERROR_SOCKET;
2433     }
2434     *pid = cr.pid;
2435     *uid = cr.uid;
2436     return SECURITY_SERVER_SUCCESS;
2437 }
2438
2439 /* Authenticate the application is middleware daemon
2440  * The middleware must run as root (or middleware user) and the cmd line must be
2441  * pre listed for authentication to succeed */
2442 int authenticate_client_middleware(int sockfd, int *pid)
2443 {
2444     int uid;
2445     return authenticate_client_application(sockfd, pid, &uid);
2446 #if 0
2447     int retval = SECURITY_SERVER_SUCCESS;
2448     struct ucred cr;
2449     unsigned int cl = sizeof(cr);
2450     char *exe = NULL;
2451     struct passwd pw, *ppw;
2452     size_t buf_size;
2453     char *buf;
2454     static uid_t middleware_uid = 0;
2455
2456     *pid = 0;
2457
2458     /* get PID of socket peer */
2459     if (getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &cr, &cl) != 0)
2460     {
2461         retval = SECURITY_SERVER_ERROR_SOCKET;
2462         SEC_SVR_ERR("%s", "Error on getsockopt");
2463         goto error;
2464     }
2465
2466     if (!middleware_uid)
2467     {
2468         buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
2469         if (buf_size == -1)
2470             buf_size = 1024;
2471
2472         buf = malloc(buf_size);
2473
2474         /* This test isn't essential, skip it in case of error */
2475         if (buf) {
2476             if (getpwnam_r(SECURITY_SERVER_MIDDLEWARE_USER, &pw, buf, buf_size, &ppw) == 0 && ppw)
2477                 middleware_uid = pw.pw_uid;
2478
2479             free(buf);
2480         }
2481     }
2482
2483     /* Middleware services need to run as root or middleware/app user */
2484     if (cr.uid != 0 && cr.uid != middleware_uid)
2485     {
2486         retval = SECURITY_SERVER_ERROR_AUTHENTICATION_FAILED;
2487         SEC_SVR_ERR("Non root process has called API: %d", cr.uid);
2488         goto error;
2489     }
2490
2491     /* Read command line of the PID from proc fs */
2492     exe = read_exe_path_from_proc(cr.pid);
2493     if (exe == NULL)
2494     {
2495         /* It's weired. no file in proc file system, */
2496         retval = SECURITY_SERVER_ERROR_FILE_OPERATION;
2497         SEC_SVR_ERR("Error on opening /proc/%d/exe", cr.pid);
2498         goto error;
2499     }
2500
2501     *pid = cr.pid;
2502
2503 error:
2504     if (exe != NULL)
2505         free(exe);
2506
2507     return retval;
2508 #endif
2509 }
2510
2511 /* Get app PID from socked and read its privilege (GID) list
2512  * from /proc/<PID>/status.
2513  *
2514  * param 1: socket descriptor
2515  * param 2: pointer for hold returned array
2516  *
2517  * ret: size of array or -1 in case of error
2518  *
2519  * Notice that user must free space allocated in this function and
2520  * returned by second parameter (int * privileges)
2521  * */
2522 int get_client_gid_list(int sockfd, int **privileges)
2523 {
2524     int ret;
2525     //for read socket options
2526     struct ucred socopt;
2527     unsigned int socoptSize = sizeof(socopt);
2528     //buffer for store /proc/<PID>/status filepath
2529     const int PATHSIZE = 24;
2530     char path[PATHSIZE];
2531     //file pointer
2532     FILE *fp = NULL;
2533     //buffer for filelines
2534     const int LINESIZE = 256;
2535     char fileLine[LINESIZE];
2536     //for parsing file
2537     char delim[] = ": ";
2538     char *token = NULL;
2539
2540
2541     //clear pointer
2542     *privileges = NULL;
2543
2544     //read socket options
2545     ret = getsockopt(sockfd, SOL_SOCKET, SO_PEERCRED, &socopt, &socoptSize);
2546     if (ret != 0)
2547     {
2548         SEC_SVR_ERR("%s", "Error on getsockopt");
2549         return -1;
2550     }
2551
2552     //now we have PID in sockopt.pid
2553     bzero(path, PATHSIZE);
2554     snprintf(path, PATHSIZE, "/proc/%d/status", socopt.pid);
2555
2556     fp = fopen(path, "r");
2557     if (fp == NULL)
2558     {
2559         SEC_SVR_ERR("%s", "Error on fopen");
2560         return -1;
2561     }
2562
2563     bzero(fileLine, LINESIZE);
2564
2565     //search for line beginning with "Groups:"
2566     while (strncmp(fileLine, "Groups:", 7) != 0)
2567     {
2568         if (NULL == fgets(fileLine, LINESIZE, fp))
2569         {
2570             SEC_SVR_ERR("%s", "Error on fgets");
2571             fclose(fp);
2572             return -1;
2573         }
2574     }
2575
2576     fclose(fp);
2577
2578     //now we have "Groups:" line in fileLine[]
2579     ret = 0;
2580     strtok(fileLine, delim);
2581     while (token = strtok(NULL, delim))
2582     {
2583         //add found GID
2584         if (*privileges == NULL)
2585         {
2586             //first GID on list
2587             *privileges = (int*)malloc(sizeof(int) * 1);
2588             if (*privileges == NULL)
2589             {
2590                 SEC_SVR_ERR("%s", "Error on malloc");
2591                 return -1;
2592             }
2593             (*privileges)[0] = atoi(token);
2594         }
2595         else
2596         {
2597             *privileges = realloc(*privileges, sizeof(int) * (ret + 1));
2598             (*privileges)[ret] = atoi(token);
2599         }
2600
2601         ret++;
2602     }
2603
2604     //check if we found any GIDs for process
2605     if (*privileges == NULL)
2606     {
2607         SEC_SVR_DBG("%s %d", "No GIDs found for PID:", socopt.pid);
2608     }
2609     else
2610     {
2611         SEC_SVR_DBG("%s %d", "Number of GIDs found:", ret);
2612     }
2613
2614     return ret;
2615 }
2616
2617 int free_argv(char **argv, int argc)
2618 {
2619     int i;
2620     if (argv == NULL)
2621     {
2622         SEC_SVR_ERR("%s", "Cannot free NULL pointer");
2623         return SECURITY_SERVER_ERROR_INPUT_PARAM;
2624     }
2625     for (i = 0; i < argc; i++)
2626     {
2627         if (argv[i] != NULL)
2628             free(argv[i]);
2629     }
2630     free(argv);
2631     return SECURITY_SERVER_SUCCESS;
2632 }
2633