moved ubi_ modules back into the ubiqx directory.
[samba.git] / source3 / pipeutil.c
1
2 /* 
3    Unix SMB/Netbios implementation.
4    Version 1.9.
5    Pipe SMB utility routines
6    Copyright (C) Andrew Tridgell 1992-1997,
7    Copyright (C) Luke Kenneth Casson Leighton 1996-1997.
8    Copyright (C) Paul Ashton  1997.
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24 /*
25    This file handles reply_ calls on named pipes that the server
26    makes to handle specific protocols
27 */
28
29
30 #include "includes.h"
31 #include "trans2.h"
32
33 extern int DEBUGLEVEL;
34
35 /* this function is due to be replaced */
36 void initrpcreply(char *inbuf, char *q)
37 {
38         uint32 callid;
39
40         SCVAL(q, 0, 5); q++; /* RPC version 5 */
41         SCVAL(q, 0, 0); q++; /* minor version 0 */
42         SCVAL(q, 0, 2); q++; /* RPC response packet */
43         SCVAL(q, 0, 3); q++; /* first frag + last frag */
44         RSIVAL(q, 0, 0x10000000); q += 4; /* packed data representation */
45         RSSVAL(q, 0, 0); q += 2; /* fragment length, fill in later */
46         SSVAL(q, 0, 0); q += 2; /* authentication length */
47         callid = RIVAL(inbuf, 12);
48         RSIVAL(q, 0, callid); q += 4; /* call identifier - match incoming RPC */
49         SIVAL(q, 0, 0x18); q += 4; /* allocation hint (no idea) */
50         SSVAL(q, 0, 0); q += 2; /* presentation context identifier */
51         SCVAL(q, 0, 0); q++; /* cancel count */
52         SCVAL(q, 0, 0); q++; /* reserved */
53 }
54
55 /* this function is due to be replaced */
56 void endrpcreply(char *inbuf, char *q, int datalen, int rtnval, int *rlen)
57 {
58         SSVAL(q, 8, datalen + 4);
59         SIVAL(q,0x10,datalen+4-0x18); /* allocation hint */
60         SIVAL(q, datalen, rtnval);
61         *rlen = datalen + 4;
62         { int fd; fd = open("/tmp/rpc", O_RDWR); write(fd, q, datalen + 4); }
63 }
64
65 /* Group and User RID username mapping function */
66 BOOL name_to_rid(char *user_name, uint32 *u_rid, uint32 *g_rid)
67 {
68     struct passwd *pw = Get_Pwnam(user_name, False);
69
70         if (u_rid == NULL || g_rid == NULL || user_name == NULL)
71         {
72                 return False;
73         }
74
75     if (!pw)
76         {
77       DEBUG(1,("Username %s is invalid on this system\n", user_name));
78       return False;
79     }
80
81         if (user_in_list(user_name, lp_domain_guest_users()))
82         {
83                 *u_rid = DOMAIN_USER_RID_GUEST;
84         }
85         else if (user_in_list(user_name, lp_domain_admin_users()))
86         {
87                 *u_rid = DOMAIN_USER_RID_ADMIN;
88         }
89         else
90         {
91                 /* turn the unix UID into a Domain RID.  this is what the posix
92                    sub-system does (adds 1000 to the uid) */
93                 *u_rid = (uint32)(pw->pw_uid + 1000);
94         }
95
96         /* absolutely no idea what to do about the unix GID to Domain RID mapping */
97         *g_rid = (uint32)(pw->pw_gid + 1000);
98
99         return True;
100 }
101
102
103 /* BIG NOTE: this function only does SIDS where the identauth is not >= 2^32 */
104 char *dom_sid_to_string(DOM_SID *sid)
105 {
106   static pstring sidstr;
107   char subauth[16];
108   int i;
109   uint32 ia = (sid->id_auth[5]) +
110               (sid->id_auth[4] << 8 ) +
111               (sid->id_auth[3] << 16) +
112               (sid->id_auth[2] << 24);
113
114   sprintf(sidstr, "S-%d-%d", sid->sid_rev_num, ia);
115
116   for (i = 0; i < sid->num_auths; i++)
117   {
118     sprintf(subauth, "-%d", sid->sub_auths[i]);
119     strcat(sidstr, subauth);
120   }
121
122   DEBUG(5,("dom_sid_to_string returning %s\n", sidstr));
123   return sidstr;
124 }
125
126 int make_dom_sids(char *sids_str, DOM_SID *sids, int max_sids)
127 {
128         char *ptr;
129         pstring s2;
130         int count;
131
132         DEBUG(4,("make_dom_sids: %s\n", sids_str));
133
134         if (sids_str == NULL || *sids_str == 0) return 0;
135
136         for (count = 0, ptr = sids_str; next_token(&ptr, s2, NULL) && count < max_sids; count++) 
137         {
138                 make_dom_sid(&sids[count], s2);
139         }
140
141         return count;
142 }
143
144 /* array lookup of well-known RID aliases.  the purpose of these escapes me.. */
145 /* XXXX this structure should not have the well-known RID groups added to it,
146    i.e the DOMAIN_GROUP_RID_ADMIN/USER/GUEST.  */
147 static struct
148 {
149         uint32 rid;
150         char   *rid_name;
151
152 } rid_lookups[] = 
153 {
154         { DOMAIN_ALIAS_RID_ADMINS       , "admins" },
155         { DOMAIN_ALIAS_RID_USERS        , "users" },
156         { DOMAIN_ALIAS_RID_GUESTS       , "guests" },
157         { DOMAIN_ALIAS_RID_POWER_USERS  , "power_users" },
158
159         { DOMAIN_ALIAS_RID_ACCOUNT_OPS  , "account_ops" },
160         { DOMAIN_ALIAS_RID_SYSTEM_OPS   , "system_ops" },
161         { DOMAIN_ALIAS_RID_PRINT_OPS    , "print_ops" },
162         { DOMAIN_ALIAS_RID_BACKUP_OPS   , "backup_ops" },
163         { DOMAIN_ALIAS_RID_REPLICATOR   , "replicator" },
164         { 0                             , NULL }
165 };
166
167 int make_dom_gids(char *gids_str, DOM_GID *gids)
168 {
169         char *ptr;
170         pstring s2;
171         int count;
172
173         DEBUG(4,("make_dom_gids: %s\n", gids_str));
174
175         if (gids_str == NULL || *gids_str == 0) return 0;
176
177         for (count = 0, ptr = gids_str; next_token(&ptr, s2, NULL) && count < LSA_MAX_GROUPS; count++) 
178         {
179                 /* the entries are of the form GID/ATTR, ATTR being optional.*/
180                 char *attr;
181                 uint32 rid = 0;
182                 int i;
183
184                 attr = strchr(s2,'/');
185                 if (attr) *attr++ = 0;
186                 if (!attr || !*attr) attr = "7"; /* default value for attribute is 7 */
187
188                 /* look up the RID string and see if we can turn it into a rid number */
189                 for (i = 0; rid_lookups[i].rid_name != NULL; i++)
190                 {
191                         if (strequal(rid_lookups[i].rid_name, s2))
192                         {
193                                 rid = rid_lookups[i].rid;
194                                 break;
195                         }
196                 }
197
198                 if (rid == 0) rid = atoi(s2);
199
200                 if (rid == 0)
201                 {
202                         DEBUG(1,("make_dom_gids: unknown well-known alias RID %s/%s\n",
203                                   s2, attr));
204                         count--;
205                 }
206                 else
207                 {
208                         gids[count].g_rid = rid;
209                         gids[count].attr  = atoi(attr);
210
211                         DEBUG(5,("group id: %d attr: %d\n",
212                                   gids[count].g_rid,
213                                   gids[count].attr));
214                 }
215         }
216
217         return count;
218 }
219
220 int create_rpc_request(uint32 call_id, uint8 op_num, char *q, int data_len)
221 {
222         RPC_HDR_RR hdr;
223
224         make_rpc_hdr_rr(&hdr, RPC_REQUEST, call_id, data_len, op_num);
225         return smb_io_rpc_hdr_rr(False, &hdr, q, q, 4, 0) - q;
226 }
227
228 int create_rpc_reply(uint32 call_id, char *q, int data_len)
229 {
230         RPC_HDR_RR hdr;
231
232         make_rpc_hdr_rr(&hdr, RPC_RESPONSE, call_id, data_len, 0);
233         return smb_io_rpc_hdr_rr(False, &hdr, q, q, 4, 0) - q;
234 }
235