nmbd/nmbd_synclists.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    NBT netbios routines and daemon - version 2
00004    Copyright (C) Andrew Tridgell 1994-1998
00005    Copyright (C) Luke Kenneth Casson Leighton 1994-1998
00006    Copyright (C) Jeremy Allison 1994-1998
00007    
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012    
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017    
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021    
00022 */
00023 
00024 /* this file handles asynchronous browse synchronisation requests. The
00025    requests are done by forking and putting the result in a file in the
00026    locks directory. We do it this way because we don't want nmbd to be
00027    blocked waiting for some server to respond on a TCP connection. This
00028    also allows us to have more than 1 sync going at once (tridge) */
00029 
00030 #include "includes.h"
00031 
00032 extern fstring local_machine;
00033 
00034 struct sync_record {
00035         struct sync_record *next, *prev;
00036         unstring workgroup;
00037         unstring server;
00038         pstring fname;
00039         struct in_addr ip;
00040         pid_t pid;
00041 };
00042 
00043 /* a linked list of current sync connections */
00044 static struct sync_record *syncs;
00045 
00046 static XFILE *fp;
00047 
00048 /*******************************************************************
00049   This is the NetServerEnum callback.
00050   Note sname and comment are in UNIX codepage format.
00051   ******************************************************************/
00052 
00053 static void callback(const char *sname, uint32 stype, 
00054                      const char *comment, void *state)
00055 {
00056         x_fprintf(fp,"\"%s\" %08X \"%s\"\n", sname, stype, comment);
00057 }
00058 
00059 /*******************************************************************
00060   Synchronise browse lists with another browse server.
00061   Log in on the remote server's SMB port to their IPC$ service,
00062   do a NetServerEnum and record the results in fname
00063 ******************************************************************/
00064 
00065 static void sync_child(char *name, int nm_type, 
00066                        char *workgroup,
00067                        struct in_addr ip, BOOL local, BOOL servers,
00068                        char *fname)
00069 {
00070         fstring unix_workgroup;
00071         struct cli_state *cli;
00072         uint32 local_type = local ? SV_TYPE_LOCAL_LIST_ONLY : 0;
00073         struct nmb_name called, calling;
00074         NTSTATUS status;
00075 
00076         /* W2K DMB's return empty browse lists on port 445. Use 139.
00077          * Patch from Andy Levine andyl@epicrealm.com.
00078          */
00079 
00080         cli = cli_initialise();
00081         if (!cli) {
00082                 return;
00083         }
00084 
00085         if (!cli_set_port(cli, 139)) {
00086                 return;
00087         }
00088 
00089         status = cli_connect(cli, name, &ip);
00090         if (!NT_STATUS_IS_OK(status)) {
00091                 return;
00092         }
00093 
00094         make_nmb_name(&calling, local_machine, 0x0);
00095         make_nmb_name(&called , name, nm_type);
00096 
00097         if (!cli_session_request(cli, &calling, &called)) {
00098                 cli_shutdown(cli);
00099                 return;
00100         }
00101 
00102         if (!cli_negprot(cli)) {
00103                 cli_shutdown(cli);
00104                 return;
00105         }
00106 
00107         if (!NT_STATUS_IS_OK(cli_session_setup(cli, "", "", 1, "", 0,
00108                                                workgroup))) {
00109                 cli_shutdown(cli);
00110                 return;
00111         }
00112 
00113         if (!cli_send_tconX(cli, "IPC$", "IPC", "", 1)) {
00114                 cli_shutdown(cli);
00115                 return;
00116         }
00117 
00118         /* All the cli_XX functions take UNIX character set. */
00119         fstrcpy(unix_workgroup, cli->server_domain ? cli->server_domain : workgroup);
00120 
00121         /* Fetch a workgroup list. */
00122         cli_NetServerEnum(cli, unix_workgroup,
00123                           local_type|SV_TYPE_DOMAIN_ENUM, 
00124                           callback, NULL);
00125         
00126         /* Now fetch a server list. */
00127         if (servers) {
00128                 fstrcpy(unix_workgroup, workgroup);
00129                 cli_NetServerEnum(cli, unix_workgroup, 
00130                                   local?SV_TYPE_LOCAL_LIST_ONLY:SV_TYPE_ALL,
00131                                   callback, NULL);
00132         }
00133         
00134         cli_shutdown(cli);
00135 }
00136 
00137 /*******************************************************************
00138   initialise a browse sync with another browse server.  Log in on the
00139   remote server's SMB port to their IPC$ service, do a NetServerEnum
00140   and record the results
00141 ******************************************************************/
00142 
00143 void sync_browse_lists(struct work_record *work,
00144                        char *name, int nm_type, 
00145                        struct in_addr ip, BOOL local, BOOL servers)
00146 {
00147         struct sync_record *s;
00148         static int counter;
00149 
00150         START_PROFILE(sync_browse_lists);
00151         /* Check we're not trying to sync with ourselves. This can
00152            happen if we are a domain *and* a local master browser. */
00153         if (ismyip(ip)) {
00154 done:
00155                 END_PROFILE(sync_browse_lists);
00156                 return;
00157         }
00158 
00159         s = SMB_MALLOC_P(struct sync_record);
00160         if (!s) goto done;
00161 
00162         ZERO_STRUCTP(s);
00163         
00164         unstrcpy(s->workgroup, work->work_group);
00165         unstrcpy(s->server, name);
00166         s->ip = ip;
00167 
00168         slprintf(s->fname, sizeof(pstring)-1,
00169                  "%s/sync.%d", lp_lockdir(), counter++);
00170         all_string_sub(s->fname,"//", "/", 0);
00171         
00172         DLIST_ADD(syncs, s);
00173 
00174         /* the parent forks and returns, leaving the child to do the
00175            actual sync and call END_PROFILE*/
00176         CatchChild();
00177         if ((s->pid = sys_fork())) return;
00178 
00179         BlockSignals( False, SIGTERM );
00180 
00181         DEBUG(2,("Initiating browse sync for %s to %s(%s)\n",
00182                  work->work_group, name, inet_ntoa(ip)));
00183 
00184         fp = x_fopen(s->fname,O_WRONLY|O_CREAT|O_TRUNC, 0644);
00185         if (!fp) {
00186                 END_PROFILE(sync_browse_lists);
00187                 _exit(1);       
00188         }
00189 
00190         sync_child(name, nm_type, work->work_group, ip, local, servers,
00191                    s->fname);
00192 
00193         x_fclose(fp);
00194         END_PROFILE(sync_browse_lists);
00195         _exit(0);
00196 }
00197 
00198 /**********************************************************************
00199  Handle one line from a completed sync file.
00200  **********************************************************************/
00201 
00202 static void complete_one(struct sync_record *s, 
00203                          char *sname, uint32 stype, char *comment)
00204 {
00205         struct work_record *work;
00206         struct server_record *servrec;
00207 
00208         stype &= ~SV_TYPE_LOCAL_LIST_ONLY;
00209 
00210         if (stype & SV_TYPE_DOMAIN_ENUM) {
00211                 /* See if we can find the workgroup on this subnet. */
00212                 if((work=find_workgroup_on_subnet(unicast_subnet, sname))) {
00213                         /* We already know about this workgroup -
00214                            update the ttl. */
00215                         update_workgroup_ttl(work,lp_max_ttl());
00216                 } else {
00217                         /* Create the workgroup on the subnet. */
00218                         work = create_workgroup_on_subnet(unicast_subnet, 
00219                                                           sname, lp_max_ttl());
00220                         if (work) {
00221                                 /* remember who the master is */
00222                                 unstrcpy(work->local_master_browser_name, comment);
00223                         }
00224                 }
00225                 return;
00226         } 
00227 
00228         work = find_workgroup_on_subnet(unicast_subnet, s->workgroup);
00229         if (!work) {
00230                 DEBUG(3,("workgroup %s doesn't exist on unicast subnet?\n",
00231                          s->workgroup));
00232                 return;
00233         }
00234 
00235         if ((servrec = find_server_in_workgroup( work, sname))) {
00236                 /* Check that this is not a locally known
00237                    server - if so ignore the entry. */
00238                 if(!(servrec->serv.type & SV_TYPE_LOCAL_LIST_ONLY)) {
00239                         /* We already know about this server - update
00240                            the ttl. */
00241                         update_server_ttl(servrec, lp_max_ttl());
00242                         /* Update the type. */
00243                         servrec->serv.type = stype;
00244                 }
00245                 return;
00246         } 
00247 
00248         /* Create the server in the workgroup. */ 
00249         create_server_on_workgroup(work, sname,stype, lp_max_ttl(), comment);
00250 }
00251                 
00252 /**********************************************************************
00253  Read the completed sync info.
00254 **********************************************************************/
00255 
00256 static void complete_sync(struct sync_record *s)
00257 {
00258         XFILE *f;
00259         unstring server, type_str;
00260         unsigned type;
00261         pstring comment;
00262         pstring line;
00263         const char *ptr;
00264         int count=0;
00265 
00266         f = x_fopen(s->fname,O_RDONLY, 0);
00267 
00268         if (!f)
00269                 return;
00270         
00271         while (!x_feof(f)) {
00272                 
00273                 if (!fgets_slash(line,sizeof(pstring),f))
00274                         continue;
00275                 
00276                 ptr = line;
00277 
00278                 if (!next_token(&ptr,server,NULL,sizeof(server)) ||
00279                     !next_token(&ptr,type_str,NULL, sizeof(type_str)) ||
00280                     !next_token(&ptr,comment,NULL, sizeof(comment))) {
00281                         continue;
00282                 }
00283 
00284                 sscanf(type_str, "%X", &type);
00285 
00286                 complete_one(s, server, type, comment);
00287 
00288                 count++;
00289         }
00290 
00291         x_fclose(f);
00292 
00293         unlink(s->fname);
00294 
00295         DEBUG(2,("sync with %s(%s) for workgroup %s completed (%d records)\n",
00296                  s->server, inet_ntoa(s->ip), s->workgroup, count));
00297 }
00298 
00299 /**********************************************************************
00300  Check for completion of any of the child processes.
00301 **********************************************************************/
00302 
00303 void sync_check_completion(void)
00304 {
00305         struct sync_record *s, *next;
00306 
00307         for (s=syncs;s;s=next) {
00308                 next = s->next;
00309                 if (!process_exists_by_pid(s->pid)) {
00310                         /* it has completed - grab the info */
00311                         complete_sync(s);
00312                         DLIST_REMOVE(syncs, s);
00313                         ZERO_STRUCTP(s);
00314                         SAFE_FREE(s);
00315                 }
00316         }
00317 }

Sambaに対してSat Aug 29 21:23:08 2009に生成されました。  doxygen 1.4.7