| 1 | /* | 
|---|
| 2 | Unix SMB/CIFS implementation. | 
|---|
| 3 | client dgram calls | 
|---|
| 4 | Copyright (C) Andrew Tridgell 1994-1998 | 
|---|
| 5 | Copyright (C) Richard Sharpe 2001 | 
|---|
| 6 | Copyright (C) John Terpstra 2001 | 
|---|
| 7 |  | 
|---|
| 8 | This program is free software; you can redistribute it and/or modify | 
|---|
| 9 | it under the terms of the GNU General Public License as published by | 
|---|
| 10 | the Free Software Foundation; either version 2 of the License, or | 
|---|
| 11 | (at your option) any later version. | 
|---|
| 12 |  | 
|---|
| 13 | This program is distributed in the hope that it will be useful, | 
|---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 | GNU General Public License for more details. | 
|---|
| 17 |  | 
|---|
| 18 | You should have received a copy of the GNU General Public License | 
|---|
| 19 | along with this program; if not, write to the Free Software | 
|---|
| 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | #include "includes.h" | 
|---|
| 24 |  | 
|---|
| 25 | /* | 
|---|
| 26 | * cli_send_mailslot, send a mailslot for client code ... | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | BOOL cli_send_mailslot(BOOL unique, const char *mailslot, | 
|---|
| 30 | uint16 priority, | 
|---|
| 31 | char *buf, int len, | 
|---|
| 32 | const char *srcname, int src_type, | 
|---|
| 33 | const char *dstname, int dest_type, | 
|---|
| 34 | struct in_addr dest_ip) | 
|---|
| 35 | { | 
|---|
| 36 | struct packet_struct p; | 
|---|
| 37 | struct dgram_packet *dgram = &p.packet.dgram; | 
|---|
| 38 | char *ptr, *p2; | 
|---|
| 39 | char tmp[4]; | 
|---|
| 40 | pid_t nmbd_pid; | 
|---|
| 41 |  | 
|---|
| 42 | if ((nmbd_pid = pidfile_pid("nmbd")) == 0) { | 
|---|
| 43 | DEBUG(3, ("No nmbd found\n")); | 
|---|
| 44 | return False; | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | if (!message_init()) | 
|---|
| 48 | return False; | 
|---|
| 49 |  | 
|---|
| 50 | memset((char *)&p, '\0', sizeof(p)); | 
|---|
| 51 |  | 
|---|
| 52 | /* | 
|---|
| 53 | * Next, build the DGRAM ... | 
|---|
| 54 | */ | 
|---|
| 55 |  | 
|---|
| 56 | /* DIRECT GROUP or UNIQUE datagram. */ | 
|---|
| 57 | dgram->header.msg_type = unique ? 0x10 : 0x11; | 
|---|
| 58 | dgram->header.flags.node_type = M_NODE; | 
|---|
| 59 | dgram->header.flags.first = True; | 
|---|
| 60 | dgram->header.flags.more = False; | 
|---|
| 61 | dgram->header.dgm_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) + | 
|---|
| 62 | ((unsigned)sys_getpid()%(unsigned)100); | 
|---|
| 63 | /* source ip is filled by nmbd */ | 
|---|
| 64 | dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */ | 
|---|
| 65 | dgram->header.packet_offset = 0; | 
|---|
| 66 |  | 
|---|
| 67 | make_nmb_name(&dgram->source_name,srcname,src_type); | 
|---|
| 68 | make_nmb_name(&dgram->dest_name,dstname,dest_type); | 
|---|
| 69 |  | 
|---|
| 70 | ptr = &dgram->data[0]; | 
|---|
| 71 |  | 
|---|
| 72 | /* Setup the smb part. */ | 
|---|
| 73 | ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */ | 
|---|
| 74 | memcpy(tmp,ptr,4); | 
|---|
| 75 |  | 
|---|
| 76 | if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) { | 
|---|
| 77 | DEBUG(0, ("cli_send_mailslot: Cannot write beyond end of packet\n")); | 
|---|
| 78 | return False; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | set_message(ptr,17,strlen(mailslot) + 1 + len,True); | 
|---|
| 82 | memcpy(ptr,tmp,4); | 
|---|
| 83 |  | 
|---|
| 84 | SCVAL(ptr,smb_com,SMBtrans); | 
|---|
| 85 | SSVAL(ptr,smb_vwv1,len); | 
|---|
| 86 | SSVAL(ptr,smb_vwv11,len); | 
|---|
| 87 | SSVAL(ptr,smb_vwv12,70 + strlen(mailslot)); | 
|---|
| 88 | SSVAL(ptr,smb_vwv13,3); | 
|---|
| 89 | SSVAL(ptr,smb_vwv14,1); | 
|---|
| 90 | SSVAL(ptr,smb_vwv15,priority); | 
|---|
| 91 | SSVAL(ptr,smb_vwv16,2); | 
|---|
| 92 | p2 = smb_buf(ptr); | 
|---|
| 93 | fstrcpy(p2,mailslot); | 
|---|
| 94 | p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2); | 
|---|
| 95 | if (!p2) { | 
|---|
| 96 | return False; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | memcpy(p2,buf,len); | 
|---|
| 100 | p2 += len; | 
|---|
| 101 |  | 
|---|
| 102 | dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */ | 
|---|
| 103 |  | 
|---|
| 104 | p.packet_type = DGRAM_PACKET; | 
|---|
| 105 | p.ip = dest_ip; | 
|---|
| 106 | p.timestamp = time(NULL); | 
|---|
| 107 |  | 
|---|
| 108 | DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ", | 
|---|
| 109 | mailslot, nmb_namestr(&dgram->source_name))); | 
|---|
| 110 | DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), | 
|---|
| 111 | inet_ntoa(dest_ip))); | 
|---|
| 112 |  | 
|---|
| 113 | return NT_STATUS_IS_OK(message_send_pid(pid_to_procid(nmbd_pid), | 
|---|
| 114 | MSG_SEND_PACKET, &p, sizeof(p), | 
|---|
| 115 | False)); | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | /* | 
|---|
| 119 | * cli_get_response: Get a response ... | 
|---|
| 120 | */ | 
|---|
| 121 | BOOL cli_get_response(const char *mailslot, char *buf, int bufsiz) | 
|---|
| 122 | { | 
|---|
| 123 | struct packet_struct *p; | 
|---|
| 124 |  | 
|---|
| 125 | p = receive_unexpected(DGRAM_PACKET, 0, mailslot); | 
|---|
| 126 |  | 
|---|
| 127 | if (p == NULL) | 
|---|
| 128 | return False; | 
|---|
| 129 |  | 
|---|
| 130 | memcpy(buf, &p->packet.dgram.data[92], | 
|---|
| 131 | MIN(bufsiz, p->packet.dgram.datasize-92)); | 
|---|
| 132 |  | 
|---|
| 133 | return True; | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | /* | 
|---|
| 137 | * cli_get_backup_list: Send a get backup list request ... | 
|---|
| 138 | */ | 
|---|
| 139 |  | 
|---|
| 140 | static char cli_backup_list[1024]; | 
|---|
| 141 |  | 
|---|
| 142 | int cli_get_backup_list(const char *myname, const char *send_to_name) | 
|---|
| 143 | { | 
|---|
| 144 | pstring outbuf; | 
|---|
| 145 | char *p; | 
|---|
| 146 | struct in_addr sendto_ip; | 
|---|
| 147 |  | 
|---|
| 148 | if (!resolve_name(send_to_name, &sendto_ip, 0x1d)) { | 
|---|
| 149 |  | 
|---|
| 150 | DEBUG(0, ("Could not resolve name: %s<1D>\n", send_to_name)); | 
|---|
| 151 | return False; | 
|---|
| 152 |  | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | memset(cli_backup_list, '\0', sizeof(cli_backup_list)); | 
|---|
| 156 | memset(outbuf, '\0', sizeof(outbuf)); | 
|---|
| 157 |  | 
|---|
| 158 | p = outbuf; | 
|---|
| 159 |  | 
|---|
| 160 | SCVAL(p, 0, ANN_GetBackupListReq); | 
|---|
| 161 | p++; | 
|---|
| 162 |  | 
|---|
| 163 | SCVAL(p, 0, 1); /* Count pointer ... */ | 
|---|
| 164 | p++; | 
|---|
| 165 |  | 
|---|
| 166 | SIVAL(p, 0, 1); /* The sender's token ... */ | 
|---|
| 167 | p += 4; | 
|---|
| 168 |  | 
|---|
| 169 | cli_send_mailslot(True, "\\MAILSLOT\\BROWSE", 1, outbuf, | 
|---|
| 170 | PTR_DIFF(p, outbuf), myname, 0, send_to_name, | 
|---|
| 171 | 0x1d, sendto_ip); | 
|---|
| 172 |  | 
|---|
| 173 | /* We should check the error and return if we got one */ | 
|---|
| 174 |  | 
|---|
| 175 | /* Now, get the response ... */ | 
|---|
| 176 |  | 
|---|
| 177 | cli_get_response("\\MAILSLOT\\BROWSE", | 
|---|
| 178 | cli_backup_list, sizeof(cli_backup_list)); | 
|---|
| 179 |  | 
|---|
| 180 | return True; | 
|---|
| 181 |  | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | /* | 
|---|
| 185 | * cli_get_backup_server: Get the backup list and retrieve a server from it | 
|---|
| 186 | */ | 
|---|
| 187 |  | 
|---|
| 188 | int cli_get_backup_server(char *my_name, char *target, char *servername, int namesize) | 
|---|
| 189 | { | 
|---|
| 190 |  | 
|---|
| 191 | /* Get the backup list first. We could pull this from the cache later */ | 
|---|
| 192 |  | 
|---|
| 193 | cli_get_backup_list(my_name, target);  /* FIXME: Check the response */ | 
|---|
| 194 |  | 
|---|
| 195 | if (!cli_backup_list[0]) { /* Empty list ... try again */ | 
|---|
| 196 |  | 
|---|
| 197 | cli_get_backup_list(my_name, target); | 
|---|
| 198 |  | 
|---|
| 199 | } | 
|---|
| 200 |  | 
|---|
| 201 | strncpy(servername, cli_backup_list, MIN(16, namesize)); | 
|---|
| 202 |  | 
|---|
| 203 | return True; | 
|---|
| 204 |  | 
|---|
| 205 | } | 
|---|